The frontend/UI server, written in JS using the MarkoJS library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

107 lines
3.2 KiB

// Setup global environment
require("marko/node-require").install()
require("marko/express")
require("es6-promise").polyfill()
// Load server config
const config = require("./config")
// Express depedencies
const express = require("express")
const proxy = require("express-http-proxy")
const lasso = require("lasso")
const lassoMiddleware = require("lasso/middleware")
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
}
})
// Apply middleware
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-z\,\']{2,})/", require("./routes/logs-content"))
app.use("/data/", require("./routes/data"))
app.use("/data/characters/", require("./routes/data/characters"))
// Entry point
app.get("/", function(req, res) {
res.redirect("/story/")
})
// Render common templates
require("./marko/page/story/list.marko").render()
require("./marko/page/logs/list.marko").render()
require("./marko/page/story-content/view.marko").render({story: {chapters: []}})
// Start server
app.listen(config.port, function() {
console.log("Server started: http://localhost:" + config.port + "/")
if (process.send) {
setTimeout(() => process.send("online"), 500)
}
})
// Handle shutdown signals (Docker needs this to shutdown quickly)
process.on('SIGINT', () => process.exit(0))
process.on('SIGTERM', () => process.exit(0))