-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
85 lines (75 loc) · 2.94 KB
/
index.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
import express from 'express'
import * as path from 'path'
import * as os from 'os'
let app
const CUSTOM_HEADERS = {
"Content-Security-Policy": "script-src 'self'; style-src 'self' 'unsafe-inline'; font-src 'self';"
}
export async function start ({port, configDir, domain}) {
configDir = configDir || path.join(os.homedir(), '.ctznry')
app = express()
const staticDir = (path) => (
express.static(path, {
setHeaders: (res) => {
for (let k in CUSTOM_HEADERS) {
res.setHeader(k, CUSTOM_HEADERS[k])
}
}
})
)
const staticFile = (path, opts) => (req, res) => {
for (let k in CUSTOM_HEADERS) {
res.setHeader(k, CUSTOM_HEADERS[k])
}
res.sendFile(path, {root: process.cwd(), cacheControl: opts?.cacheControl})
}
app.get('/', staticFile('./static/index.html'))
app.get('/manifest.json', staticFile('./static/manifest.json'))
// for the dev server, just serve the SW template
app.get('/service-worker.js', staticFile('./static/service-worker-template.js', {cacheControl: false}))
app.use('/img', staticDir('static/img'))
app.use('/css', staticDir('static/css'))
app.get('/js/:filename([^\.]+).build.js', (req, res) => {
// for the dev server, just serve the non-built assets
res.sendFile(`static/js/${req.params.filename}.js`, {root: process.cwd()})
})
app.use('/js', staticDir('static/js'))
app.use('/vendor', staticDir('static/vendor'))
app.use('/webfonts', staticDir('static/webfonts'))
app.get('/signup', staticFile('static/index.html'))
app.get('/forgot-password', staticFile('static/index.html'))
app.get('/inbox', staticFile('static/index.html'))
app.get('/notifications', staticFile('static/index.html'))
app.get('/communities', staticFile('static/index.html'))
app.get('/account', staticFile('static/index.html'))
app.get('/profile', staticFile('static/index.html'))
app.get('/search', staticFile('static/index.html'))
app.get('/:username([^\/]{3,})/ctzn.network/page/:key', staticFile('static/index.html'))
app.get('/:username([^\/]{3,})/ctzn.network/post/:key', staticFile('static/index.html'))
app.get('/:username([^\/]{3,})/ctzn.network/comment/:key', staticFile('static/index.html'))
app.get('/:username([^\/]{3,})/:subview', staticFile('static/index.html'))
app.get('/:username([^\/]{3,})', staticFile('static/index.html'))
app.use((req, res) => {
res.status(404).send('404 Page not found')
})
const server = await new Promise((resolve, reject) => {
let s = app.listen(port, async () => {
console.log(`CTZNRY server listening at http://localhost:${port}`)
resolve()
})
})
// process.on('SIGINT', close)
// process.on('SIGTERM', close)
// async function close () {
// console.log('Shutting down, this may take a moment...')
// await db.cleanup()
// server.close()
// }
return {
server,
close: async () => {
console.log('Shutting down, this may take a moment...')
server.close()
}
}
}