|
|
@ -1,6 +1,7 @@ |
|
|
|
import Fastify from "fastify"; |
|
|
|
import Fastify from "fastify"; |
|
|
|
import { restartable } from "@fastify/restartable"; |
|
|
|
import { restartable } from "@fastify/restartable"; |
|
|
|
import FastifyStatic from "@fastify/static"; |
|
|
|
import FastifyStatic from "@fastify/static"; |
|
|
|
|
|
|
|
import Formbody from "@fastify/formbody"; |
|
|
|
import path from "path"; |
|
|
|
import path from "path"; |
|
|
|
import fs from "fs"; |
|
|
|
import fs from "fs"; |
|
|
|
import chokidar from "chokidar"; |
|
|
|
import chokidar from "chokidar"; |
|
|
@ -20,22 +21,18 @@ nunjucks.configure("templates", { watch: true }); |
|
|
|
const configure = async (fastify, opts) => { |
|
|
|
const configure = async (fastify, opts) => { |
|
|
|
const app = fastify(opts); |
|
|
|
const app = fastify(opts); |
|
|
|
|
|
|
|
|
|
|
|
// MAGIC: this is how you trick the importer to reload modules
|
|
|
|
// forces reload of the modules
|
|
|
|
// that have changed. Since it uses a URL you can add a query
|
|
|
|
|
|
|
|
// to it, but that isn't parsed as the file. Using a date then
|
|
|
|
|
|
|
|
// tags the module as being "new" when it's still the same file
|
|
|
|
|
|
|
|
// TODO: maybe use fs timestamps instead?
|
|
|
|
|
|
|
|
// BUG: sometimes reload is too fast for vim and crashes
|
|
|
|
|
|
|
|
const control_mod = await import(`../controllers/todo.js?update=${new Date()}`); |
|
|
|
const control_mod = await import(`../controllers/todo.js?update=${new Date()}`); |
|
|
|
|
|
|
|
|
|
|
|
// this is a sample that uses the handler we dynamic load
|
|
|
|
// TODO: mappings are very simple for now
|
|
|
|
// to handle the /todo but not sure how to work the actual
|
|
|
|
|
|
|
|
// URL mappings for it. Also not sure about using classes
|
|
|
|
|
|
|
|
for(let [url, controller] of Object.entries(control_mod.default)) { |
|
|
|
for(let [url, controller] of Object.entries(control_mod.default)) { |
|
|
|
const handler = new controller(); |
|
|
|
const handler = new controller(); |
|
|
|
app.get(`/${url}`, async (req, rep) => { |
|
|
|
// this goes through all the allowed verbs
|
|
|
|
|
|
|
|
for(let verb of ["get", "post", "put", "delete"]) { |
|
|
|
|
|
|
|
if(verb in handler) { |
|
|
|
|
|
|
|
app[verb](`/${url}`, async (req, rep) => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
await handler.get(req, rep); |
|
|
|
await handler[verb](req, rep); |
|
|
|
} catch(error) { |
|
|
|
} catch(error) { |
|
|
|
console.error(error); |
|
|
|
console.error(error); |
|
|
|
console.error(error.stack); |
|
|
|
console.error(error.stack); |
|
|
@ -43,6 +40,10 @@ const configure = async (fastify, opts) => { |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.register(Formbody); |
|
|
|
|
|
|
|
|
|
|
|
app.register(FastifyStatic, { |
|
|
|
app.register(FastifyStatic, { |
|
|
|
root: path.join(path.resolve("."), 'static'), |
|
|
|
root: path.join(path.resolve("."), 'static'), |
|
|
@ -51,15 +52,6 @@ const configure = async (fastify, opts) => { |
|
|
|
index: "index.html" |
|
|
|
index: "index.html" |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// this is from fastify/restartable
|
|
|
|
|
|
|
|
app.addHook('onClose', async () => { |
|
|
|
|
|
|
|
if(!app.closingRestartable) { |
|
|
|
|
|
|
|
console.log('closing the app because of restart') |
|
|
|
|
|
|
|
} else{ |
|
|
|
|
|
|
|
console.log('closing the app because server is stopping') |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return app; |
|
|
|
return app; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|