This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ams-backend.ts
77 lines (64 loc) · 2.23 KB
/
ams-backend.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
import cors from 'cors'
import dayjs from 'dayjs'
import express from 'express'
import fs from 'fs'
import morgan from 'morgan'
import path from 'path'
import 'dayjs/locale/ja'
import * as accesslogsRoutes from './app/routes/accessLogsRoutes'
import * as inRoomUsersRoutes from './app/routes/inRoomUsersRoutes'
import * as readerInputRoutes from './app/routes/readerInputRoutes'
import * as sseRoutes from './app/routes/sseRoutes'
import { amsOptions } from './config'
import * as packageJson from './package.json'
function prepareMorgan () {
// morganの:dateはUTCなので、日本時間を表示するものを作る
morgan.token('localdate', () => dayjs().locale('ja').format())
// ログを吐くフォルダを作る
try {
fs.mkdirSync(amsOptions.logPath)
} catch (err: any) {
if (err.code === 'EEXIST') {
// EEXISTの場合は既にフォルダがあるので無視する
} else {
// それ以外の場合はエラーを出力して死ぬ
console.error('Could not create folder:', err)
process.exit(1)
}
}
// ログファイルのストリームを開く
const wStream = fs.createWriteStream(
path.join(amsOptions.logPath, '/access.log'),
{
flags: 'a'
}
)
const formatStr = ':remote-addr (fwd :req[x-forwarded-for]) [:localdate] ":method :url" :status :res[content-length] ":referrer" ":user-agent"'
return morgan(formatStr, {
stream: wStream
})
}
const app: express.Express = express()
// logging
app.use(prepareMorgan())
// parse requests of content-type - application/json
app.use(express.json())
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }))
// CORS
app.use(cors())
// `/`, `/v1`, `/v1/`のいずれかに"完全に"マッチした場合に
// このサーバーのメタ情報を返すパス
app.get(/^\/$|^\/v1\/?$/, (_req, res) => {
res.json({
message: 'This is backend server.',
version: packageJson.version
})
})
// set middlewares
app.use('/v1', accesslogsRoutes.router, inRoomUsersRoutes.router, readerInputRoutes.router, sseRoutes.router)
// set port, listen for requests
const PORT = amsOptions.port
app.listen(PORT, () => {
console.log(`[*] Server is running on port ${PORT}.`)
})