-
Notifications
You must be signed in to change notification settings - Fork 0
/
express-entry.ts
124 lines (107 loc) · 4.03 KB
/
express-entry.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
import hostname from '@small-tech/cross-platform-hostname'
import https from '@small-tech/https'
import compression from 'compression'
import cookieParser from 'cookie-parser'
import express from 'express'
import fs from 'node:fs'
import os from 'node:os'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { renderPage } from 'vike/server'
import 'dotenv/config'
import 'isomorphic-unfetch'
import connectDatabase, { client, edgeql } from './core/db/client'
import {
errorHandlingMiddleware,
DomainContextProvider,
DomainDataCollector,
} from './core'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const isProduction = process.env.NODE_ENV === "production"
const root = __dirname
export { root }
connectDatabase().catch(console.error)
startServer()
async function startServer() {
const router = express.Router()
const app = express();
const domainCollector = await DomainDataCollector.init()
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(compression())
if (isProduction) {
const sirv = (await import('sirv')).default
app.use(sirv(`${root}/dist/client`))
} else {
// Instantiate Vite's development server and integrate its middleware to our server.
// ⚠️ We should instantiate it *only* in development. (It isn't needed in production
// and would unnecessarily bloat our server in production.)
const certificateDirectory = join(os.homedir(), '.small-tech.org', 'auto-encrypt-localhost')
const cert = fs.readFileSync(join(certificateDirectory, 'localhost.pem'), 'utf-8')
const key = fs.readFileSync(join(certificateDirectory, 'localhost-key.pem'), 'utf-8')
const vite = await import("vite");
const viteDevMiddleware = (
await vite.createServer({
root,
server: {
middlewareMode: true,
https: {
cert,
key
}
},
})
).middlewares;
app.use(viteDevMiddleware);
}
/**
* Domain registration
*
* Todo: Move all proxy and domain registration logic to specific service/util
*/
let proxies = {}
const domainContext: { [key: string]: any } = {}
const contextParams = { app, router, client, edgeql, proxies }
for (const name of domainCollector.getNames()) {
const {
router: routeMiddleware,
[name]: currentContext
}: { [key: string]: any } = await DomainContextProvider.provide(
domainCollector.getDomainByName(name),
contextParams,
domainContext
)
if (routeMiddleware) app.use(routeMiddleware)
Object.assign(domainContext, { [name]: currentContext })
}
/**
* Handling errors
*/
app.use(errorHandlingMiddleware)
/**
* Vike route
*
* @link {@see https://vike.dev}
**/
app.all("*", async (req, res, next) => {
const pageContextInit = {
domainContext,
urlOriginal: req.originalUrl
}
const pageContext = await renderPage(pageContextInit)
const { httpResponse } = pageContext
if (httpResponse === null) return next()
const { body, statusCode, headers, earlyHints } = httpResponse
if (res.writeEarlyHints) res.writeEarlyHints({ link: earlyHints.map((e) => e.earlyHintLink) })
headers.forEach(([name, value]) => res.setHeader(name, value))
res.status(statusCode)
// For HTTP streams use httpResponse.pipe() instead, see https://vike.dev/stream
res.send(body)
});
const server = isProduction ? https.createServer({ domains: [hostname] }, app) : https.createServer(app)
server.listen(process.env.PORT ? parseInt(process.env.PORT) : 3000, () => {
console.log(`Server running at https://${isProduction ? hostname : 'localhost'}`)
})
}