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.

101 lines
3.0 KiB

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. // Page routes
  61. app.use("/story/", require("./routes/story"))
  62. app.use("/story/by-category/", require("./routes/story/by-category"))
  63. app.use("/story/by-tag/", require("./routes/story/by-tag"))
  64. app.use("/story/tag-list/", require("./routes/story/tag-list"))
  65. app.use("/story/:id(S[0-9a-z]{15})/", require("./routes/story-content"))
  66. app.use("/logs/", require("./routes/logs"))
  67. 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"))
  68. // 2018-03-10_035344747_Miner'sRespite
  69. // Entry point
  70. app.get("/", function(req, res) {
  71. res.redirect("/story/")
  72. })
  73. // Render common templates
  74. require("./marko/page/story/list.marko").render()
  75. require("./marko/page/logs/list.marko").render()
  76. require("./marko/page/story-content/view.marko").render({story: {chapters: []}})
  77. // Start server
  78. app.listen(config.port, function() {
  79. console.log("Server started: http://localhost:" + config.port + "/")
  80. if (process.send) {
  81. setTimeout(() => process.send("online"), 500)
  82. }
  83. })
  84. // Handle shutdown signals (Docker needs this to shutdown quickly)
  85. process.on('SIGINT', () => process.exit(0))
  86. process.on('SIGTERM', () => process.exit(0))