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.

108 lines
3.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. // Setup global environment
  2. require("marko/node-require").install()
  3. require("marko/express")
  4. require("es6-promise").polyfill()
  5. // Load server config
  6. const config = require("./config")
  7. // Express depedencies
  8. const express = require("express")
  9. const proxy = require("express-http-proxy")
  10. const lasso = require("lasso")
  11. const lassoMiddleware = require("lasso/middleware")
  12. const bodyParser = require("body-parser")
  13. // Load middleware
  14. const passport = require("./middleware/passport")
  15. const session = require("./middleware/session")
  16. // Setup express
  17. const app = express()
  18. // Configure lasso
  19. const isProduction = process.env.NODE_ENV === "production"
  20. lasso.configure({
  21. plugins: [
  22. "lasso-marko",
  23. "lasso-less",
  24. ],
  25. outputDir: "./.static", // Place all generated JS/CSS/etc. files into the "static" dir
  26. bundlingEnabled: isProduction, // Only enable bundling in production
  27. minify: isProduction, // Only minify JS and CSS code in production
  28. fingerprintsEnabled: isProduction, // Only add fingerprints to URLs in production
  29. require: {
  30. builtins: {
  31. fs: require.resolve("empty-module"),
  32. },
  33. transforms: isProduction ? [
  34. {
  35. transform: "lasso-babel-transform",
  36. config: {
  37. extensions: [".js", ".es6"], // Enabled file extensions. Default: [".js", ".es6"]
  38. babelOptions: {
  39. presets: [ "es2015" ]
  40. }
  41. }
  42. }
  43. ] : null
  44. }
  45. })
  46. // Apply middleware
  47. app.use(session)
  48. app.use(passport.initialize())
  49. app.use(passport.session())
  50. app.use(require("./middleware/locals"))
  51. app.use(bodyParser.json({limit: "1mb"}))
  52. app.use(bodyParser.text({limit: "256kb"}))
  53. // Static assets
  54. app.use("/assets", express.static(__dirname + "/assets"))
  55. app.use(lassoMiddleware.serveStatic())
  56. // Authentication
  57. app.use("/auth", require("./routes/auth"))
  58. // API Proxy
  59. app.use("/graphql", require("./routes/graphql"))
  60. app.use("/playground", proxy(config.playgroundEndpoint))
  61. // Page routes
  62. app.use("/story/", require("./routes/story"))
  63. app.use("/story/by-category/", require("./routes/story/by-category"))
  64. app.use("/story/by-tag/", require("./routes/story/by-tag"))
  65. app.use("/story/by-month/", require("./routes/story/by-month"))
  66. app.use("/story/by-author/", require("./routes/story/by-author"))
  67. app.use("/story/tag-list/", require("./routes/story/tag-list"))
  68. app.use("/story/:id(S[0-9a-z]{15})/", require("./routes/story-content"))
  69. app.use("/logs/", require("./routes/logs"))
  70. 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"))
  71. app.use("/logs/:id(L[0-9]{0,7})/", require("./routes/logs-content"))
  72. app.use("/data/", require("./routes/data"))
  73. app.use("/data/characters/", require("./routes/data/characters"))
  74. app.use("/data/channels/", require("./routes/data/channels"))
  75. // Entry point
  76. app.get("/", function(req, res) {
  77. res.redirect("/story/")
  78. })
  79. // Render common templates
  80. require("./marko/page/story/list.marko").render()
  81. require("./marko/page/logs/list.marko").render()
  82. require("./marko/page/story-content/view.marko").render({story: {chapters: []}})
  83. // Start server
  84. app.listen(config.port, function() {
  85. console.log("Server started: http://localhost:" + config.port + "/")
  86. if (process.send) {
  87. setTimeout(() => process.send("online"), 500)
  88. }
  89. })
  90. // Handle shutdown signals (Docker needs this to shutdown quickly)
  91. process.on('SIGINT', () => process.exit(0))
  92. process.on('SIGTERM', () => process.exit(0))