Skip to content

Commit bcad193

Browse files
committed
chore: enable base path in URL for mb3server
1 parent 8e41a50 commit bcad193

File tree

10 files changed

+46
-25
lines changed

10 files changed

+46
-25
lines changed

Dockerfile-server

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ COPY api/schemas ./swagger-ui/schemas
1515
COPY entrypoint-mb3server.sh ./entrypoint-mb3server.sh
1616

1717
EXPOSE 8080
18-
ENTRYPOINT sh entrypoint-mb3server.sh "${MB3_API_URL}"
18+
ENTRYPOINT sh entrypoint-mb3server.sh "${MB3_API_URL}" "${MB3_API_BASE_URL}"

api/openapi.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ openapi: 3.0.3
22
info:
33
title: "MassBank3 API"
44
version: "3.0"
5-
# servers:
6-
# - url: "http://localhost:8081"
75
paths:
86
/records:
97
parameters:

compose/docker-compose.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ services:
4848
SIMILARITY_SERVICE_COSINE_PORT: 8080
4949
EXPORT_SERVICE_HOST: export-service
5050
EXPORT_SERVICE_PORT: 8080
51-
MB3_API_URL: ${MB3_API_URL}
51+
MB3_API_URL: http://${MB3_API_HOST}:${MB3_API_PORT}
52+
MB3_API_BASE_URL: ${MB3_API_BASE_URL}
5253

5354
similarity-service:
5455
# platform: linux/amd64
@@ -92,7 +93,7 @@ services:
9293
dockerfile: Dockerfile-frontend
9394
environment:
9495
MB3_API_URL: "${MB3_API_URL}"
95-
MB3_API_URL_INTERNAL: "http://${MB3_SERVER_HOST}:${MB3_SERVER_PORT}"
96+
MB3_API_URL_INTERNAL: "http://${MB3_SERVER_HOST}:${MB3_SERVER_PORT}${MB3_API_BASE_URL}"
9697
MB3_FRONTEND_URL: "http://${MB3_FRONTEND_HOST}:${MB3_FRONTEND_PORT}"
9798
MB3_FRONTEND_BASE_URL: "${MB3_FRONTEND_BASE_URL}"
9899
MB3_FRONTEND_VERSION: "${MB3_FRONTEND_VERSION}"

compose/env.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ MB3_API_PORT=8081
3434
# The host where the API is running
3535
MB3_API_HOST=localhost
3636

37+
# Base URL for the API
38+
MB3_API_BASE_URL=/MassBank-api
39+
3740
# The port used internally by the server
3841
MB3_SERVER_PORT=8080
3942

@@ -136,4 +139,4 @@ DISTRIBUTOR_URL=""
136139
# ---------------------------
137140

138141
# The URL of the API server (from external)
139-
MB3_API_URL=http://${MB3_API_HOST}:${MB3_API_PORT}
142+
MB3_API_URL=http://${MB3_API_HOST}:${MB3_API_PORT}${MB3_API_BASE_URL}

entrypoint-mb3server.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/bash
22

3-
echo "servers:\n - url: \"" >> swagger-ui/openapi.yaml
4-
echo $1 >> swagger-ui/openapi.yaml
3+
echo -n "servers:\n - url: \"" >> swagger-ui/openapi.yaml
4+
echo -n $1 >> swagger-ui/openapi.yaml
5+
echo -n $2 >> swagger-ui/openapi.yaml
56
echo "\"" >> swagger-ui/openapi.yaml
67

78
CGO_ENABLED=0

main.go

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type ServerConfig struct {
2222
database.DBConfig
2323
ServerPort uint
2424
ApiUrl string
25+
BaseUrl string
2526
}
2627

2728
const (
@@ -37,6 +38,8 @@ const (
3738
mbDataDirectoryDefault = ""
3839
mbDbInitDefault = "true"
3940
serverPortDefault = "8080"
41+
apiUrlDefault = "localhost:8081"
42+
baseUrlDefault = "/MassBank-api"
4043
)
4144

4245
var toolConfig *ToolConfig = nil
@@ -87,7 +90,13 @@ func GetServerConfig() *ServerConfig {
8790
flag.UintVar(&serverConfig.ServerPort, "server_port", serverConfig.ServerPort, "Listen on this port. Overwrites environment variable SERVER_PORT")
8891
flag.Parse()
8992

90-
serverConfig.ApiUrl = getEnv("MB3_API_URL", "")
93+
serverConfig.ApiUrl = getEnv("MB3_API_URL", apiUrlDefault)
94+
serverConfig.BaseUrl = getEnv("MB3_API_BASE_URL", baseUrlDefault)
95+
96+
lastChar := serverConfig.BaseUrl[len(serverConfig.BaseUrl)-1:]
97+
if lastChar == "/" {
98+
serverConfig.BaseUrl = serverConfig.BaseUrl[:len(serverConfig.BaseUrl)-1]
99+
}
91100

92101
return serverConfig
93102
}

web-frontend/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
echo "MB3_API_URL=$1" > .env
44
echo "MB3_FRONTEND_URL=$2" >> .env
5-
echo "MB3_BASE_URL=$3" >> .env
5+
echo "MB3_FRONTEND_BASE_URL=$3" >> .env
66
echo "MB3_VERSION=$4" >> .env
77
echo "EXPORT_SERVICE_URL=$5" >> .env
88
echo "GOOGLE_SEARCH_CONSOLE_KEY=$6" >> .env

web-frontend/server.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@ const port = 3000;
1414
const host = '0.0.0.0';
1515

1616
const isProduction = process.env.NODE_ENV === 'production';
17-
const backendUrl = process.env.MB3_API_URL ?? 'http://localhost:8081';
17+
const backendUrl =
18+
process.env.MB3_API_URL && process.env.MB3_API_URL.trim().length > 0
19+
? process.env.MB3_API_URL.replace(/\/$/, '')
20+
: 'http://localhost:8081/MassBank-api';
1821
const frontendUrl = process.env.MB3_FRONTEND_URL ?? 'http://localhost:8080';
19-
const baseUrl = process.env.MB3_BASE_URL ?? '/MassBank3/';
22+
const frontendBaseUrl = process.env.MB3_FRONTEND_BASE_URL ?? '/MassBank3/';
2023
const exportServiceUrl =
2124
process.env.EXPORT_SERVICE_URL ?? 'http://localhost:8083';
2225
const version = process.env.MB3_VERSION ?? '0.4.0 (beta)';
2326
const googleSearchConsoleKey = process.env.GOOGLE_SEARCH_CONSOLE_KEY ?? '';
2427
const backendUrlInternal =
25-
process.env.MB3_API_URL_INTERNAL ?? 'http://mb3server:8080';
28+
process.env.MB3_API_URL_INTERNAL &&
29+
process.env.MB3_API_URL_INTERNAL.trim().length > 0
30+
? process.env.MB3_API_URL_INTERNAL.replace(/\/$/, '')
31+
: 'http://mb3server:8080/MassBank-api';
2632
const exportServiceUrlInternal =
2733
process.env.EXPORT_SERVICE_URL_INTERNAL ?? 'http://export-service:8080';
2834
const distributorText =
@@ -38,7 +44,7 @@ console.log('\n');
3844
console.log('isProduction', process.env.NODE_ENV === 'production');
3945
console.log('port', port);
4046
console.log('host', host);
41-
console.log('baseUrl', baseUrl);
47+
console.log('baseUrl', frontendBaseUrl);
4248
console.log('frontendUrl', frontendUrl);
4349
console.log('version', version);
4450
console.log('backendUrl', backendUrl);
@@ -67,7 +73,7 @@ if (!isProduction) {
6773
const compression = (await import('compression')).default;
6874
const sirv = (await import('sirv')).default;
6975
app.use(compression());
70-
app.use(baseUrl, sirv('./dist/client', { extensions: [] }));
76+
app.use(frontendBaseUrl, sirv('./dist/client', { extensions: [] }));
7177
}
7278

7379
const buildRecordMetadata = async (_accession: string) => {
@@ -131,10 +137,10 @@ async function getLastmodDate() {
131137

132138
// Create router for base URL
133139
const baseRouter = express.Router();
134-
app.use(baseUrl, baseRouter);
140+
app.use(frontendBaseUrl, baseRouter);
135141

136142
const nRecords = 40000;
137-
const prefixUrl = frontendUrl + baseUrl;
143+
const prefixUrl = frontendUrl + frontendBaseUrl;
138144

139145
// serve sitemap index for search engines
140146
baseRouter.get('/robots.txt', async (req: Request, res: Response) => {
@@ -259,7 +265,7 @@ baseRouter.use(/(.*)/, async (req: Request, res: Response) => {
259265
let render;
260266
if (!isProduction) {
261267
// Always read fresh template in development
262-
const url = req.originalUrl.replace(baseUrl, '');
268+
const url = req.originalUrl.replace(frontendBaseUrl, '');
263269
template = await fs.readFile('./index.html', 'utf-8');
264270
template = await vite.transformIndexHtml(url, template);
265271
render = (await vite.ssrLoadModule('./src/ServerApp.tsx')).render;
@@ -274,7 +280,7 @@ baseRouter.use(/(.*)/, async (req: Request, res: Response) => {
274280

275281
const path = req.originalUrl.split('?')[0];
276282
const props = {
277-
baseUrl,
283+
baseUrl: frontendBaseUrl,
278284
backendUrl,
279285
frontendUrl,
280286
exportServiceUrl,
@@ -304,7 +310,7 @@ baseRouter.use(/(.*)/, async (req: Request, res: Response) => {
304310
: googleSearchConsoleMeta;
305311
}
306312

307-
const pageRoute = path.replace(baseUrl, '');
313+
const pageRoute = path.replace(frontendBaseUrl, '');
308314
if (
309315
(pageRoute === 'recordDisplay' || pageRoute === 'RecordDisplay') &&
310316
req.query.id
@@ -345,5 +351,5 @@ app.listen(port, host, (err) => {
345351
console.error(err);
346352
return;
347353
}
348-
console.log(`Server started at http://${host}:${port}`);
354+
console.log(`Server started at http://${host}:${port}${frontendBaseUrl}`);
349355
});

web-frontend/src/context/properties/properties.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const emptyState: PropertiesContextProps = {
99
version: '',
1010
distributorText: '',
1111
distributorUrl: '',
12+
homepageIntroText: '',
1213
};
1314

1415
const propertiesContext = createContext<PropertiesContextProps>(emptyState);

0 commit comments

Comments
 (0)