Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b41064

Browse files
authored
feat: core path configurable (#119)
1 parent 171d934 commit 4b41064

File tree

6 files changed

+42
-12
lines changed

6 files changed

+42
-12
lines changed

packages/prime-core/src/server.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const createServer = async ({ port, connection }: ServerConfig) => {
5050

5151
if (config.sofaApi) {
5252
app.use(
53-
'/api',
53+
`${config.path}api`,
5454
sofa({
5555
schema: external.schema,
5656
ignore: ['Prime_Document'],
@@ -63,13 +63,15 @@ export const createServer = async ({ port, connection }: ServerConfig) => {
6363

6464
externalServer.applyMiddleware({
6565
app,
66+
path: `${config.path}graphql`,
6667
cors: {
6768
origin: true,
6869
},
6970
});
7071

7172
fields.forEach(
72-
field => field.ui && app.use(`/prime/field/${field.type}`, express.static(field.ui))
73+
field =>
74+
field.ui && app.use(`${config.pathClean}/prime/field/${field.type}`, express.static(field.ui))
7375
);
7476

7577
const apollo = new ApolloServer({
@@ -95,14 +97,14 @@ export const createServer = async ({ port, connection }: ServerConfig) => {
9597
apollo.installSubscriptionHandlers(server);
9698
apollo.applyMiddleware({
9799
app,
98-
path: '/prime/graphql',
100+
path: `${config.path}prime/graphql`,
99101
cors: {
100102
origin: true,
101103
},
102104
});
103105

104106
previewRoutes(app);
105-
serveUI(app, config);
107+
serveUI(app);
106108

107109
return server.listen(port, () => {
108110
log(`🚀 Server ready at http://localhost:${port}${apollo.graphqlPath}`);

packages/prime-core/src/utils/config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ export const config = rc('prime', {
1717
sofaApi: true,
1818
uiDir: defaultUiDir(),
1919
coreUrl: defaultCoreUrl(),
20+
path: defaultCorePath(),
2021
});
2122

23+
config.coreUrl = config.coreUrl.trim().replace(/\/+$/, '');
24+
config.path = `/${config.path.trim()}/`.replace(/\/+$/, '/').replace(/^\/+/, '/');
25+
config.pathClean = config.path.replace(/\/+$/, '');
26+
2227
function defaultUiDir() {
2328
try {
2429
const dir = require.resolve('@primecms/ui/package.json');
@@ -47,3 +52,15 @@ function defaultCoreUrl() {
4752

4853
return `http://localhost:${PORT || 4000}`;
4954
}
55+
56+
function defaultCorePath() {
57+
const { CORE_PATH } = process.env;
58+
if (CORE_PATH) {
59+
return CORE_PATH;
60+
}
61+
const matches = defaultCoreUrl().match(/[0-9a-zA-Z]\/(.*)$/);
62+
if (matches && matches[1]) {
63+
return matches[1];
64+
}
65+
return '/';
66+
}

packages/prime-core/src/utils/preview.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import express from 'express';
22
import { getRepository } from 'typeorm';
33
import { Document } from '../entities/Document';
44
import { Schema } from '../entities/Schema';
5+
import { config } from './config';
56
import { DocumentTransformer } from './DocumentTransformer';
67

78
export const previewRoutes = (app: express.Application) => {
8-
app.get('/prime/redirect', (req, res) => {
9+
app.get(`${config.pathClean}/prime/redirect`, (req, res) => {
910
const { id, url, accessToken, refreshToken } = req.query;
1011
if (id.length === 36) {
1112
const cookieConfig = {
@@ -22,7 +23,7 @@ export const previewRoutes = (app: express.Application) => {
2223
res.json({ success: false });
2324
});
2425

25-
app.get('/prime/preview', async (req, res) => {
26+
app.get(`${config.pathClean}/prime/preview`, async (req, res) => {
2627
const cookie = String(req.headers.cookie);
2728
const cookies = cookie.split(';').reduce((acc, item) => {
2829
const [key, value] = item

packages/prime-core/src/utils/serveUI.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
import express from 'express';
22
import fs from 'fs';
33
import path from 'path';
4+
import { config } from './config';
45
import { log } from './log';
56

6-
export const serveUI = (app: express.Application, config) => {
7+
export const serveUI = (app: express.Application) => {
78
const { uiDir } = config;
89

910
if (!uiDir) {
1011
log('no ui dir found %o', uiDir);
1112
}
1213

1314
app.use(
15+
config.pathClean,
1416
express.static(uiDir, {
1517
index: false,
1618
})
1719
);
1820

19-
app.get('*', (req, res, next) => {
20-
if (req.url.substr(0, 4) === '/api') {
21+
app.get(`${config.pathClean}*`, (req, res, next) => {
22+
if (req.url.substr(config.path.length, 4) === '/api') {
2123
return next();
2224
}
2325

@@ -26,7 +28,12 @@ export const serveUI = (app: express.Application, config) => {
2628
log(err);
2729
res.send('error');
2830
} else {
29-
res.send(data.toString().replace('"$PRIME_CONFIG$"', `'${JSON.stringify(config)}'`));
31+
res.send(
32+
data
33+
.toString()
34+
.replace('"$PRIME_CONFIG$"', `'${JSON.stringify(config)}'`)
35+
.replace(/\/static\//g, `${config.path}static/`)
36+
);
3037
}
3138
});
3239
});

packages/prime-ui/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { ResetPassword } from './routes/reset-password/ResetPassword';
1212
import { Schemas } from './routes/schemas/Schemas';
1313
import { Settings } from './routes/settings/Settings';
1414
import { Auth } from './stores/auth';
15-
import { ContentTypes } from './stores/contentTypes';
15+
import { Settings as CSettings } from './stores/settings';
1616

1717
const Private = observer(({ children }) => {
1818
if (Auth.isLoggedIn) {
@@ -38,7 +38,7 @@ export class App extends React.Component {
3838
}
3939

4040
return (
41-
<BrowserRouter>
41+
<BrowserRouter basename={CSettings.path}>
4242
<Route
4343
render={({ location }) => {
4444
let key = location.pathname;

packages/prime-ui/src/stores/settings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ const isProd = process.env.NODE_ENV === 'production';
88

99
let config = get(window, 'prime.config', '');
1010
let coreUrl = isProd ? '/' : 'http://localhost:4000';
11+
let corePath = '';
1112
const fields: any = [];
1213
let env: any = {};
1314

1415
try {
1516
config = JSON.parse(config);
1617
coreUrl = config.coreUrl || coreUrl;
18+
corePath = config.path || '';
1719
env = config.env || env;
1820
} catch (err) {
1921
if (isProd) {
@@ -65,6 +67,7 @@ export const Settings = types
6567
.model('Settings', {
6668
isProd,
6769
coreUrl,
70+
path: corePath,
6871
packages: types.array(PackageVersion),
6972
shouldReloadPlayground: false,
7073
env: types.frozen(),

0 commit comments

Comments
 (0)