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.

131 lines
4.1 KiB

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