-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
111 lines (103 loc) · 2.92 KB
/
server.js
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
const express = require('express')
const session = require('express-session')
const { uuid } = require('uuidv4')
const locationRoute = require('./src/location')
const authRoute = require('./src/auth')
const commandRoute = require('./src/command')
const ocrRoute = require('./src/ocr')
const chatRoute = require('./src/chat')
const chatroomRoute = require('./src/chatroom')
const abtestRoute = require('./src/abtest')
const todoListRoute = require('./src/todoList')
const imggen = require('./src/imagegen')
const js6c1 = require('./src/js6c1')
const js6c2 = require('./src/js6c2')
const exampleRoute = require('./src/examples')
const lambdas = require('./src/lambdas')
const assetExercise = require('./src/assetExercise')
const imageAnalysis = require('./src/imageAnalysis')
const logGen = require('./src/log')
const app = express()
app.set('trust proxy', true)
app.use(express.static('public'))
app.use(express.json({ limit: '50mb' }))
// DOCS about session
// resave:
// If your store sets an expiration date on stored sessions, then you likely need resave: true.
// saveUninitialized:
// Choosing false is useful for implementing login sessions,
// reducing server storage usage,
// or complying with laws that require permission before setting a cookie.
const FIVE_MINUTES = 1000 * 60 * 5
app.use(
session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: false,
cookie: { sameSite: 'none', secure: true, maxAge: FIVE_MINUTES }
})
)
app.use((req, res, next) => {
const sid = uuid()
const reqLogger = logGen(null, sid)
req.log = reqLogger.log
req.warn = reqLogger.warn
req.error = reqLogger.error
res.set('c0d3-debug-id', sid)
next()
})
app.use('/assetExercise', assetExercise)
app.use('/imageAnalysis', imageAnalysis)
app.use('/location', locationRoute)
app.use('/auth', authRoute)
app.use('/commands', commandRoute)
app.use('/js6c1', js6c1)
app.use('/js6c2', js6c2)
app.use('/ocr', ocrRoute)
app.use('/chat', chatRoute)
app.use('/chatroom', chatroomRoute)
app.use('/imggen', imggen)
app.use('/lambdas', lambdas)
app.use('/abtest', abtestRoute)
app.use('/todoList', todoListRoute)
app.use('/examples', exampleRoute)
const requests = []
app.get('/jsFile', (req, res) => {
res.json({
total: requests.length,
requests
})
})
app.get('/jsFile.js', (req, res) => {
const date = new Date()
requests.unshift({
method: req.method,
time: date.toTimeString()
})
res.set('Cache-Control', 'max-age=50')
res.set('Expires', '50')
res.send("console.log('hello zach')")
})
let server
const startServer = (port = 3020) => {
return new Promise((resolve, reject) => {
server = app.listen(port, function () {
resolve(server)
})
server.on('error', (err) => {
server.close(() => {
reject(err)
})
})
})
}
const stopServer = () => {
return new Promise((resolve, reject) => {
server.close(resolve)
})
}
module.exports = {
app,
startServer,
stopServer
}