1
1
import { ValidateError } from '@tsoa/runtime' ;
2
2
import express , { ErrorRequestHandler } from 'express' ;
3
- import { existsSync } from 'fs' ;
3
+ import { existsSync , readFileSync } from 'fs' ;
4
4
import http from 'http' ;
5
5
import path from 'path' ;
6
6
import * as Auth from './auth' ;
7
7
import * as Election from './election' ;
8
8
import * as ManagedGameServers from './managedGameServers' ;
9
- import * as Presets from './presets' ;
10
9
import * as Match from './match' ;
11
10
import { checkAndNormalizeLogAddress } from './match' ;
12
11
import * as MatchMap from './matchMap' ;
13
12
import * as MatchService from './matchService' ;
13
+ import * as Presets from './presets' ;
14
14
import { RegisterRoutes } from './routes' ;
15
15
import * as Storage from './storage' ;
16
16
import * as WebSocket from './webSocket' ;
@@ -28,18 +28,31 @@ export const TMT_LOG_ADDRESS: string | null = (() => {
28
28
return addr ;
29
29
} ) ( ) ;
30
30
31
- const STATIC_PATH = ( ( ) => {
32
- if ( existsSync ( path . join ( __dirname , '../../frontend/dist' ) ) ) {
33
- return path . join ( __dirname , '../../frontend/dist' ) ;
31
+ const APP_DIR = ( ( ) => {
32
+ if ( __dirname . endsWith ( path . join ( '/backend/dist/backend/src' ) ) ) {
33
+ // in production: __dirname = /app/backend/dist/backend/src
34
+ return path . join ( __dirname , '../../../..' ) ;
34
35
}
35
- if ( existsSync ( path . join ( __dirname , '../../../../frontend/dist' ) ) ) {
36
- return path . join ( __dirname , '../../../../frontend/dist' ) ;
36
+ if ( __dirname . endsWith ( path . join ( '/backend/src' ) ) ) {
37
+ // in development: __dirname = /app/backend/src
38
+ return path . join ( __dirname , '../..' ) ;
37
39
}
38
- throw 'Could not determine static path' ;
40
+ console . error ( `__dirname is ${ __dirname } ` ) ;
41
+ throw 'Could not determine APP_DIR' ;
39
42
} ) ( ) ;
40
43
44
+ const FRONTEND_DIR = path . join ( APP_DIR , '/frontend/dist' ) ;
45
+
41
46
export const PORT = process . env [ 'TMT_PORT' ] || 8080 ;
42
- export const VERSION = process . env [ 'COMMIT_SHA' ] || null ;
47
+ export const VERSION = process . env [ 'TMT_VERSION' ] || null ;
48
+ export const COMMIT_SHA = process . env [ 'TMT_COMMIT_SHA' ] || null ;
49
+ export const IMAGE_BUILD_TIMESTAMP = ( ( ) => {
50
+ const file = path . join ( APP_DIR , '.TMT_IMAGE_BUILD_TIMESTAMP' ) ;
51
+ if ( existsSync ( file ) ) {
52
+ return readFileSync ( file ) . toString ( ) . trim ( ) ;
53
+ }
54
+ return null ;
55
+ } ) ( ) ;
43
56
44
57
const app = express ( ) ;
45
58
const httpServer = http . createServer ( app ) ;
@@ -103,11 +116,14 @@ app.get('/api', (req, res) => {
103
116
res . sendFile ( 'swagger.json' , { root : '.' } ) ;
104
117
} ) ;
105
118
106
- app . get ( '*' , express . static ( STATIC_PATH ) ) ;
107
- app . get ( '*' , ( req , res ) => res . sendFile ( path . join ( STATIC_PATH , 'index.html' ) ) ) ;
119
+ app . get ( '*' , express . static ( FRONTEND_DIR ) ) ;
120
+ app . get ( '*' , ( req , res ) => res . sendFile ( path . join ( FRONTEND_DIR , 'index.html' ) ) ) ;
108
121
109
122
const main = async ( ) => {
110
- console . info ( `Start TMT (version ${ VERSION ? VERSION : 'unknown' } )` ) ;
123
+ console . info (
124
+ `Start TMT (version ${ VERSION ?? 'unknown' } , commit ${ COMMIT_SHA ?? 'unknown' } , build timestamp ${ IMAGE_BUILD_TIMESTAMP ?? 'unknown' } )`
125
+ ) ;
126
+ console . info ( `App dir: ${ APP_DIR } , frontend dir: ${ FRONTEND_DIR } ` ) ;
111
127
await Storage . setup ( ) ;
112
128
await Auth . setup ( ) ;
113
129
await WebSocket . setup ( httpServer ) ;
0 commit comments