-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
104 lines (78 loc) · 2.61 KB
/
app.ts
File metadata and controls
104 lines (78 loc) · 2.61 KB
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
import express, { Request, Response, NextFunction } from 'express'
import cookieParser from 'cookie-parser'
import session from 'express-session'
import createMemoryStore from 'memorystore'
import FileStore from 'session-file-store'
import swaggerUi from 'swagger-ui-express'
import YAML from 'yamljs'
import path from 'path'
import flash from 'connect-flash'
import { passport, setOauthStrategies } from './config/passport'
import { apiRouter } from './routes/index'
import { createServer } from 'http'
import { Server } from 'socket.io'
import { WEBSOCKET_PORT, setWebsockets } from './chat'
import sharedsession from 'express-socket.io-session'
// import path from 'path'
// const path = require('path')
// const config = require('./config/config')
import { logger } from './config/logger'
import * as morgan from './config/morgan'
import httpStatus from 'http-status'
import * as error from './middlewares/error'
import { ApiError } from './utils/error'
import cors from 'cors'
import { chatRouter } from './chat'
export const app = express()
export const httpServer = createServer(app)
const swaggerSpec = YAML.load(path.join(__dirname, './swagger.yaml'))
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec))
// app.use(express.static(__dirname + "/front/dist"));
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))
// 라우터 설정
app.use(morgan.successHandler)
app.use(morgan.errorHandler)
const corsOption = {
origin: [process.env.APP_CLIENT_HOST!],
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE'],
}
app.use(express.json())
app.use(cors(corsOption))
app.use(cookieParser())
const checkPeriod = 24 * 60 * 60 * 1000
// const MemoryStore = createMemoryStore(session)
// const memoryStore = new MemoryStore({ checkPeriod })
const sessionMiddleWare = session({
secret: '!@E@E$#T4twerwf@#%ew^&rrrr',
// store: memoryStore,
store: new (FileStore(session))({
path: path.join(__dirname, 'sessions'),
}),
resave: false,
saveUninitialized: false,
cookie: { maxAge: checkPeriod },
name: 'SESSIONID',
})
app.use(sessionMiddleWare)
app.use(passport.initialize())
app.use(passport.session())
setOauthStrategies(app)
app.use(flash())
app.use('/chats', chatRouter)
app.use('/api', apiRouter)
app.use((req: Request, res: Response, next: NextFunction) => {
next(new ApiError(httpStatus.NOT_FOUND, 'Not found'))
})
app.use(error.errorConverter)
app.use(error.errorHandler)
const io = new Server(httpServer, {
cors: {
origin: [process.env.APP_CLIENT_HOST!],
credentials: true,
methods: ['GET', 'POST'],
},
})
io.engine.use(sessionMiddleWare)
setWebsockets(io)