Browse Source

server: Added sequentual template build at startup.

1.0 1.0.1
Gisle Aune 6 years ago
parent
commit
dd3c8642cb
  1. 3
      Dockerfile
  2. 53
      build.js
  3. 15
      builder.js
  4. 2
      package.json
  5. 43
      server.js

3
Dockerfile

@ -26,6 +26,9 @@ RUN rm -rf .static
RUN rm -rf .cache RUN rm -rf .cache
RUN rm -rf .git RUN rm -rf .git
# Build templates (when that works)
# RUN node build.js
## 2. Regroup ## 2. Regroup
FROM node:10-alpine FROM node:10-alpine
RUN apk add --no-cache ca-certificates RUN apk add --no-cache ca-certificates

53
build.js

@ -0,0 +1,53 @@
const build = require("./builder")
// Setup global environment
require("marko/node-require").install()
require("es6-promise").polyfill()
// Configure lasso
require("lasso").configure({
plugins: [
"lasso-marko",
"lasso-less",
],
outputDir: "./.static",
bundlingEnabled: true,
minify: true,
fingerprintsEnabled: true,
require: {
builtins: {
fs: require.resolve("empty-module"),
},
transforms: [
{
transform: "lasso-babel-transform",
config: {
extensions: [".js", ".es6"], // Enabled file extensions. Default: [".js", ".es6"]
babelOptions: {
presets: [ "es2015" ]
}
}
}
]
}
})
// Render templates
async function buildAll() {
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: {}}})
}
buildAll().then(() => {
console.log("Done!")
}).catch(err => {
console.error(err)
process.exit(1)
})

15
builder.js

@ -0,0 +1,15 @@
module.exports = function build(path, data) {
console.log("Building", path)
return new Promise((resolve, reject) => {
const render = require(path).render(data)
render.once("error", err => reject)
const interval = setInterval(() => {
if (render._remaining === 0) {
clearInterval(interval)
resolve()
}
}, 500)
})
}

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "@gisle/rpdata-frontend", "name": "@gisle/rpdata-frontend",
"version": "1.0.0",
"version": "1.0.1",
"description": "RPData (www.aiterp.net) frontend/proxy", "description": "RPData (www.aiterp.net) frontend/proxy",
"main": "./index.js", "main": "./index.js",
"scripts": { "scripts": {

43
server.js

@ -1,16 +1,19 @@
// Setup global environment // Setup global environment
require("marko/node-require").install() require("marko/node-require").install()
require("marko/express")
require("es6-promise").polyfill() require("es6-promise").polyfill()
// Load server config // Load server config
const config = require("./config") const config = require("./config")
// Load utilities
const build = require("./builder")
// Express depedencies // Express depedencies
const express = require("express") const express = require("express")
const proxy = require("express-http-proxy") const proxy = require("express-http-proxy")
const lasso = require("lasso") const lasso = require("lasso")
const lassoMiddleware = require("lasso/middleware") const lassoMiddleware = require("lasso/middleware")
const markoExpress = require("marko/express")
const bodyParser = require("body-parser") const bodyParser = require("body-parser")
// Load middleware // Load middleware
@ -30,7 +33,7 @@ lasso.configure({
outputDir: "./.static", // Place all generated JS/CSS/etc. files into the "static" dir outputDir: "./.static", // Place all generated JS/CSS/etc. files into the "static" dir
bundlingEnabled: isProduction, // Only enable bundling in production bundlingEnabled: isProduction, // Only enable bundling in production
minify: isProduction, // Only minify JS and CSS code in production minify: isProduction, // Only minify JS and CSS code in production
fingerprintsEnabled: isProduction, // Only add fingerprints to URLs in production
fingerprintsEnabled: true, // Only add fingerprints to URLs in production
require: { require: {
builtins: { builtins: {
@ -52,6 +55,7 @@ lasso.configure({
}) })
// Apply middleware // Apply middleware
app.use(markoExpress())
app.use(session) app.use(session)
app.use(passport.initialize()) app.use(passport.initialize())
app.use(passport.session()) app.use(passport.session())
@ -90,24 +94,33 @@ app.get("/", function(req, res) {
res.redirect("/story/") res.redirect("/story/")
}) })
// Render common templates
require("./marko/page/story/list.marko").render()
require("./marko/page/story/tag-list.marko").render({tags: []})
require("./marko/page/logs/list.marko").render()
require("./marko/page/data/channels.marko").render({channels: [], user: {}})
require("./marko/page/data/characters.marko").render({characters: [], user: {}})
require("./marko/page/story-content/view.marko").render({story: {chapters: []}})
require("./marko/page/logs-content/view.marko").render({log: {posts: [], channel: {}}})
// Start server
app.listen(config.port, function() {
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 + "/") console.log("Server started: http://localhost:" + config.port + "/")
if (process.send) { if (process.send) {
setTimeout(() => process.send("online"), 500)
setTimeout(() => process.send("online"), 100)
} }
})
})
}
// Handle shutdown signals (Docker needs this to shutdown quickly) // Handle shutdown signals (Docker needs this to shutdown quickly)
process.on('SIGINT', () => process.exit(0)) process.on('SIGINT', () => process.exit(0))
process.on('SIGTERM', () => process.exit(0)) process.on('SIGTERM', () => process.exit(0))
main().catch(err => {
console.error(err)
process.exit(1)
})
Loading…
Cancel
Save