forked from ispyhumanfly/flight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight.ts
executable file
·147 lines (122 loc) · 4.67 KB
/
flight.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!ts-node
import Koa from 'koa'
import Router from '@koa/router'
import fg from 'fast-glob'
import path, { resolve } from 'path'
import cors from '@koa/cors'
import bodyParser from 'koa-bodyparser'
import logger from 'koa-logger'
import compress from 'koa-compress'
import ratelimit from 'koa-ratelimit'
import cluster from 'cluster'
import os from 'os'
import { exec } from 'child_process'
import serve from 'koa-static'
import cache from 'koa-redis-cache' // Import the middleware
import session from 'koa-session'
import RedisStore from 'koa-redis'
import Redis from 'ioredis'
// import send from 'koa-send'
// import historyFallback from 'koa-connect-history-api-fallback'
const argv = require('yargs/yargs')(process.argv.slice(2)).argv
if (!argv.app_home) {
argv.app_home = '.'
}
const appHomePath = path.resolve(argv.app_home)
process.chdir(appHomePath)
console.log(appHomePath)
if (!argv.mode) {
argv.mode = 'development'
}
console.log = console.log.bind(null, 'Flight:')
const redis = new Redis({
host: process.env.FLIGHT_REDIS_HOST || 'localhost',
port: Number(process.env.FLIGHT_REDIS_PORT) || 6379
})
if (cluster.isPrimary) {
const numCPUs = os.cpus().length
for (let i = 0; i < numCPUs; i++) {
cluster.fork()
}
cluster.on('exit', () => {
cluster.fork()
})
} else {
const app = new Koa()
app.use(logger())
app.keys = ['//input example secret key//']
const SESSION_CONFIG = {
// Session configuration options
// For example, to change the cookie name:
key: 'flightApp', // default
maxAge: 86400000, // cookie's expire time, 24 hours in milliseconds
// autoCommit: true, /** (boolean) automatically commit headers (default true) */
// overwrite: true, /** (boolean) can overwrite or not (default true) */
// httpOnly: true, /** (boolean) httpOnly or not (default true) */
// signed: true, /** (boolean) signed or not (default true) */
// rolling: false, /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */
// renew: false, /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/
// secure: false, /** (boolean) secure cookie*/
sameSite: true /** (string) session cookie sameSite options (default null, don't set it) */,
path: '/' /** (string) session cookie path */,
store: RedisStore({
client: redis
})
}
app.use(session(SESSION_CONFIG, app))
const router = new Router()
app.use(cors()).use(bodyParser())
const backEndFiles = fg.sync('**/*.backend.ts')
backEndFiles.forEach((file) => {
const serverRoutes = require(path.resolve(file))
console.log('Found backend file: ' + path.resolve(file))
if (serverRoutes && serverRoutes.default) {
router.use(serverRoutes.default)
}
})
app.use(router.routes()).use(router.allowedMethods())
if (argv.mode === 'production') {
exec('npx vite build', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`)
return
}
console.log(`stdout: ${stdout}`)
console.error(`stderr: ${stderr}`)
})
app.use(compress())
app.use(
ratelimit({
driver: 'redis',
db: redis,
duration: 60000,
errorMessage: 'Sometimes You Just Have to Slow Down.',
id: (ctx) => ctx.ip,
headers: {
remaining: 'Rate-Limit-Remaining',
reset: 'Rate-Limit-Reset',
total: 'Rate-Limit-Total'
},
max: 100,
disableHeader: false
})
)
app.use(cache({ expire: 30 /* Cache time in seconds */ }))
app.use(serve(process.env.FLIGHT_DIST_PATH || '../dist'))
console.log('App served out of dist/ and available on port 3000')
}
app.listen(3000, () => {
console.log(`Server worker ${process.pid} started, All backend services are running on port 3000`)
})
if (argv.mode === 'development') {
exec('npx vite', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`)
return
}
console.log(`stdout: ${stdout}`)
console.error(`stderr: ${stderr}`)
})
console.log(`Vite development server with hot module reload ${process.pid} started on 3001`)
}
}