-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.ts
74 lines (61 loc) · 1.94 KB
/
server.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
const path = require('path')
const express = require('express')
const morgan = require('morgan')
const glob = require('glob')
const PORT = 3000
const main = async () => {
const app = express()
// log middleware
// skipping /healthz because docker health checks it every second or so
app.use(
morgan('tiny', {
skip: (req) => req.url === '/healthz'
})
)
// * Same settings as in Watchtower
app.use(
express.json({
limit: '6MB',
verify: (req, _res, buf) => {
// adding the raw body to the reqest object so it can be used for
// signature verification(e.g.Stripe Webhooks)
req.rawBody = buf.toString()
}
})
)
app.use(express.urlencoded({ extended: true }))
app.disable('x-powered-by')
app.get('/healthz', (_req, res) => {
res.status(200).send('ok')
})
const functionsPath = path.join(process.cwd(), process.env.FUNCTIONS_RELATIVE_PATH)
const files = glob.sync('**/*.@(js|ts)', {
cwd: functionsPath,
ignore: [
'**/node_modules/**', // ignore node_modules directories
'**/_*/**', // ignore files inside directories that start with _
'**/_*' // ignore files that start with _
]
})
for (const file of files) {
const { default: handler } = await import(path.join(functionsPath, file))
// File path relative to the project root directory. Used for logging.
const relativePath = path.relative(process.env.NHOST_PROJECT_PATH, file)
if (handler) {
const route = `/${file}`.replace(/(\.ts|\.js)$/, '').replace(/\/index$/, '/')
try {
app.all(route, handler)
} catch (error) {
console.warn(`Unable to load file ${relativePath} as a Serverless Function`)
continue
}
console.log(`Loaded route ${route} from ${relativePath}`)
} else {
console.warn(`No default export at ${relativePath}`)
}
}
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
})
}
main()