|
|
// Setup global environment
require("marko/node-require").install() require("es6-promise").polyfill()
// Load server config
const config = require("./config")
// Load utilities
const build = require("./builder")
// Express depedencies
const express = require("express") const proxy = require("express-http-proxy") const lasso = require("lasso") const lassoMiddleware = require("lasso/middleware") const markoExpress = require("marko/express") const bodyParser = require("body-parser")
// Load middleware
const passport = require("./middleware/passport") const session = require("./middleware/session")
// Setup express
const app = express()
// Configure lasso
const isProduction = process.env.NODE_ENV === "production" lasso.configure({ plugins: [ "lasso-marko", "lasso-less", ], outputDir: "./.static", // Place all generated JS/CSS/etc. files into the "static" dir
bundlingEnabled: isProduction, // Only enable bundling in production
minify: isProduction, // Only minify JS and CSS code in production
fingerprintsEnabled: isProduction, // Only add fingerprints to URLs in production
require: { builtins: { fs: require.resolve("empty-module"), },
transforms: isProduction ? [ { transform: "lasso-babel-transform", config: { extensions: [".js", ".es6"], // Enabled file extensions. Default: [".js", ".es6"]
babelOptions: { presets: [ "es2015" ] } } } ] : null } })
// Set trusted proxy
if (config.trustedProxy != null) { app.set('trust proxy', config.trustedProxy) }
// Apply middleware
app.use(markoExpress()) app.use(session) app.use(passport.initialize()) app.use(passport.session()) app.use(require("./middleware/locals")) app.use(bodyParser.json({limit: "1mb"})) app.use(bodyParser.text({limit: "256kb"}))
// Static assets
app.use("/assets", express.static(__dirname + "/assets")) app.use(lassoMiddleware.serveStatic())
// Authentication
app.use("/auth", require("./routes/auth"))
// API Proxy
app.use("/graphql", require("./routes/graphql")) app.use("/playground", proxy(config.playgroundEndpoint))
// Page routes
app.use("/story/", require("./routes/story")) app.use("/story/by-category/", require("./routes/story/by-category")) app.use("/story/by-tag/", require("./routes/story/by-tag")) app.use("/story/by-month/", require("./routes/story/by-month")) app.use("/story/by-author/", require("./routes/story/by-author")) app.use("/story/tag-list/", require("./routes/story/tag-list")) app.use("/story/:id(S[0-9a-z]{15})/", require("./routes/story-content")) app.use("/logs/", require("./routes/logs")) app.use("/logs/:id([0-9]{4}-[0-1][0-9]-[0-3][0-9]_[0-9]{9}_[A-Za-z0-9\,\']{2,})/", require("./routes/logs-content")) app.use("/logs/:id(L[0-9]{0,7})/", require("./routes/logs-content")) app.use("/data/", require("./routes/data")) app.use("/data/characters/", require("./routes/data/characters")) app.use("/data/channels/", require("./routes/data/channels"))
// Entry point
app.get("/", function(req, res) { res.redirect("/story/") })
async function main() { // Build templates
if (isProduction) { await build("./marko/page/story/list.marko") await build("./marko/page/story/tag-list.marko", {tags: []}) await build("./marko/page/logs/list.marko") await build("./marko/page/data/channels.marko", {channels: [], user: {}}) await build("./marko/page/data/characters.marko", {characters: [], user: {}}) await build("./marko/page/story-content/view.marko", {story: {chapters: []}}) await build("./marko/page/logs-content/view.marko", {log: {posts: [], channel: {}}}) }
// Start server
app.listen(config.port, function() { console.log("Server started: http://localhost:" + config.port + "/")
if (process.send) { setTimeout(() => process.send("online"), 100) } }) }
// Handle shutdown signals (Docker needs this to shutdown quickly)
process.on('SIGINT', () => process.exit(0)) process.on('SIGTERM', () => process.exit(0))
main().catch(err => { console.error(err) process.exit(1) })
|