Skip to content

Commit 5e72fca

Browse files
authored
Merge pull request #74 from baoduy/develop
Improvement
2 parents b9e69de + 1a95fb0 commit 5e72fca

File tree

6 files changed

+75
-49
lines changed

6 files changed

+75
-49
lines changed

README.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The PRD really sttarted kit for **ReactJs** with hot module replacement (HMR) fo
3333

3434
Based on the best practises of the Single-Page-Application development with NodeJS. I added the nice font-end component [Material-Dashboard-React](https://github.com/creativetimofficial/material-dashboard-react) and Redux store integration.
3535

36-
Beside of that I also developped some useful compoment as Message box, Notification to make the Started-kit to be a **Production ready** SPA.
36+
Beside of that I also developped some useful compoment as Message box, Notification to make the Started-kit to be a **Production ready** SPA kit.
3737

3838
## Support Features
3939

@@ -61,15 +61,13 @@ async function getDataFromServer() {
6161
}
6262
```
6363

64-
### Installation
64+
## Installation
6565

6666
1. Clone/download repo
6767
2. `npm install`
6868
3. Replace the CodeCov.io token to your one in the `package.json` file.
6969
4. Run `npm start` to run the project.
7070

71-
---
72-
7371
## Usage
7472

7573
### Development
@@ -92,12 +90,12 @@ async function getDataFromServer() {
9290

9391
- Analysis served @ `http://localhost:8888`
9492

95-
---
96-
9793
## New Compoments
9894

9995
1. **Message Box and Notification**: Allow to show Info, Confirm, Success and Error message and notification. Refer to the MessageBox in Views folder so sample that using Redux store to manage the state.
10096

97+
## Environment Supports
98+
10199
### Docker Support
102100

103101
1. Build Image `docker build`.
@@ -106,36 +104,42 @@ async function getDataFromServer() {
106104
3. Push inage to Docker hub `docker push [YOUR_ID]/react-materialui-started-kit:latest`
107105
Example `docker push baoduy2412/react-materialui-started-kit:latest`.
108106

109-
The application will running port 3000 in Docker.
107+
The application will running port 80 and 443 in Docker.
110108
The image can be found in Docker hub [here](https://hub.docker.com/r/baoduy2412/react-materialui-started-kit/).
111109

112-
**If you are using Docker the `Docker` folder in this project can be deleted without any impact.**
113-
114-
---
110+
However if you are not using Docker just simply remove the submodule **sm-react-docker-nginx** There is no impact to the application.
115111

116112
### IIS Support
117113

118-
The `Web.config` file had been added for IIS hosting purpose. When build the application this file will be copied to dist folder automatically and make the package ready for IIS.
114+
The `Web.config` file in **sm-react-iis** had been added for IIS hosting purpose. This file should be copy along with all files in dist folder when hosting in IIS.
119115

120-
However if you are not hosting this app in IIS just simply delete this file.or leave if there. There is no impact to the application.
116+
However if you are not hosting this app in IIS just simply remove the submodule **sm-react-iis** There is no impact to the application.
121117

122118
### Azure Service Fabric Support
123119

124-
All stuffs in `service-fabric` folder are using for **[Azure Service Fabric](https://azure.microsoft.com/en-us/services/service-fabric/)** hosting purpose.
125-
The project inside this folder will copy all files in dist folder and host as a static side in Azure Service Fabric.
126-
I'm using .Net Core 2.0 to make the project is flexible enough to host on any platforms.
120+
All stuffs in `sm-react-service-fabric` folder are using for **[Azure Service Fabric](https://azure.microsoft.com/en-us/services/service-fabric/)** hosting purpose.
121+
122+
The C# project inside this folder will copy all files from dist folder to wwwroot folder and host them as a static side in Azure Service Fabric.
123+
124+
Currently, I'm using .Net Core 2.1 to make the project is flexible enough to host on any platforms.
127125

128126
When build the Service Fabric application it will copy all files in `dist` folder to `wwwroot` folder. So ensure you run the `npm build` before deploy the Service Fabric app.
129127

130-
Defiantly, If you are not using **Azure Service Fabric**. This folder shall be deleted.
128+
However if you are not using Service Fabric just simply remove the submodule **sm-react-service-fabric** There is no impact to the application.
129+
130+
### Node Js Hosting
131+
132+
There is a sub module contains the Express.js configuration to host the application in the Node Js environment.
133+
The application will running port 3000 and 3001 in NodeJs and the port is configurable in the Js file.
134+
135+
If you are not using Node Js hosting just simply remove the submodule **sm-react-node-express** There is no impact to the application.
136+
But, please note that the `npm run start-prod` is using this submodule to hosting the dist folder as a static side. Please be considered before removing this module.
131137

132138
### GZIP and SSL
133139

134140
The **GZIP** and **SSL** had been applied for all hosting environments above.
135141

136-
---
137-
138-
### All commands
142+
## All commands
139143

140144
| Command | Description |
141145
| ------------------------ | ------------------------------------------------------------------------ |

configs/jest

Submodule jest updated from 9b59a48 to 5195b1c

src/commons/commonFuncs.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
1+
/*eslint no-console: ["off", { allow: ["warn", "error"] }] */
2+
13
import linq from 'linq';
24
import uuidv4 from 'uuid/v4';
35
//correct URL for Reserved proxy
46
//Get the millisecond of current time.
57

8+
export function GetBaseUrl() {
9+
const key = 'BASE_URL';
10+
let base = window.sessionStorage.getItem(key);
11+
12+
if (base === undefined || base === null) {
13+
base = document.getElementsByTagName('base')[0].getAttribute('href');
14+
window.sessionStorage.setItem(key, base);
15+
16+
console.log(`base URL is ${base}`);
17+
}
18+
19+
return base;
20+
}
21+
622
export function newGuid() {
723
return uuidv4();
824
}
9-
25+
/**
26+
*getImgSrc for both normal hosting and Reverse proxy
27+
*
28+
* @export
29+
* @param {string} url the relative image url
30+
* @returns real url
31+
*/
1032
export function getImgSrc(url) {
1133
if (typeof url !== 'string') return url;
12-
const base = window._base;
13-
14-
if (!base || base === '/') return url;
15-
if (url.indexOf(base) >= 0) return url;
34+
const base = GetBaseUrl();
1635

17-
return `${base}/${url}`;
36+
return !base || base === '/' || url.indexOf(base) >= 0
37+
? url
38+
: `${base}/${url}`;
1839
}
40+
1941
/**
2042
*Merge second array to first array if existed then update.
2143
*

src/index.jsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
/*eslint no-console: ["off", { allow: ["warn", "error"] }] */
21
import React from 'react';
32
import ReactDOM from 'react-dom';
43
import { createBrowserHistory } from 'history';
54
import { Router, Route, Switch, BrowserRouter } from 'react-router-dom';
65
import Provider from 'react-redux-thunk-store';
76
import ExceptionHandler from './layouts/ExceptionHandler';
7+
8+
import { GetBaseUrl } from './commons/commonFuncs';
89
//Style-sheets
910
import './assets/less/material-dashboard-react.less';
1011

@@ -15,14 +16,12 @@ import indexRoutes from 'routes/index.jsx';
1516
const isPrd = process.env.NODE_ENV === 'production';
1617

1718
//Update for Reserved proxy
18-
window._base = document.getElementsByTagName('base')[0].getAttribute('href');
19-
console.log(`base URL ${window._base}`);
20-
21-
const hist = createBrowserHistory({ basename: window._base });
19+
const base = GetBaseUrl();
20+
const hist = createBrowserHistory({ basename: base });
2221

2322
const createRouter = () => {
2423
return (
25-
<BrowserRouter basename={window._base || '/'}>
24+
<BrowserRouter basename={base || '/'}>
2625
<Router history={hist}>
2726
<Switch>
2827
{indexRoutes.map((prop, key) => {

tests/commons/commonFuncs.test.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
1-
import { getImgSrc, newGuid } from "../../src/commons/commonFuncs";
1+
import { getImgSrc, newGuid } from '../../src/commons/commonFuncs';
22

3-
test("2 GUID must be difference", () => {
3+
window.sessionStorage.setItem('BASE_URL', '/ReactJs');
4+
5+
test('2 GUID must be difference', () => {
46
const g1 = newGuid();
57
const g2 = newGuid();
68

79
expect(g1).not.toBe(g2);
810
});
911

10-
test("getImgSrc should add basename to url", () => {
11-
window._base = "/ReactJs";
12-
expect(getImgSrc("/img/a.png")).toContain(window._base);
12+
test('getImgSrc should add basename to url', () => {
13+
expect(getImgSrc('/img/a.png')).toContain('/ReactJs');
1314
});
1415

15-
test("getImgSrc should not add basename to url", () => {
16-
window._base = "/ReactJs";
17-
expect(getImgSrc("/ReactJs/img/a.png")).toBe("/ReactJs/img/a.png");
16+
test('getImgSrc should not add basename to url', () => {
17+
expect(getImgSrc('/ReactJs/img/a.png')).toBe('/ReactJs/img/a.png');
1818
});
1919

20-
test("getImgSrc should return original object", () => {
21-
window._base = "/ReactJs";
20+
test('getImgSrc should return original object', () => {
2221
const url = { a: 1 };
2322
expect(getImgSrc(url)).toBe(url);
2423
});
2524

26-
test("getImgSrc should return original string", () => {
27-
window._base = "/";
28-
const url = "123";
25+
test('getImgSrc should return original string', () => {
26+
const url = '123';
27+
window.sessionStorage.setItem('BASE_URL', '/');
28+
2929
expect(getImgSrc(url)).toBe(url);
3030
});
3131

32-
test("getImgSrc should do nothing", () => {
33-
window._base = undefined;
34-
const url = "123";
32+
test('getImgSrc should do nothing', () => {
33+
const url = '123';
34+
window.sessionStorage.setItem('BASE_URL', '');
35+
3536
expect(getImgSrc(url)).toBe(url);
3637
});

0 commit comments

Comments
 (0)