More restructuring but now the pages/ doesn't quite work the way I want. Need to find a way to unify the api and controllers together.

main
Zed A. Shaw 9 months ago
parent 7548d1bbde
commit a43d47fc01
  1. 17
      commands/app.js
  2. 1
      pages/header.html
  3. 2
      pages/todo_alpine.html
  4. 3
      pages/todo_alpine/index.html

@ -19,18 +19,15 @@ let CONFIGURED = false;
// nunjucks does it's own reload so let it do that // nunjucks does it's own reload so let it do that
nunjucks.configure("pages", { watch: true }); nunjucks.configure("pages", { watch: true });
const load_mode = async (mod) => { const load_mod = async (app, mod) => {
const control_mod = await import(`../${mod}?update=${Date.now()}`); const control_mod = await import(`../${mod}?update=${Date.now()}`);
const pf = path.parse(mod); const pf = path.parse(mod);
const url = `/${pf.name}`; const url = `/${pf.name}`;
console.log("URL IS", url);
for(let verb of ["get", "post", "put", "delete"]) { for(let verb of ["get", "post", "put", "delete"]) {
const handler = control_mod[verb]; const handler = control_mod[verb];
if(handler !== undefined) { if(handler !== undefined) {
console.log("ADDING VERB", verb);
app[verb](url, async (req, rep) => { app[verb](url, async (req, rep) => {
try { try {
await handler(req, rep); await handler(req, rep);
@ -49,7 +46,7 @@ const configure = async (fastify, opts) => {
// forces reload of the modules // forces reload of the modules
for(let mod of fg.sync("{api,controllers}/*.js")) { for(let mod of fg.sync("{api,controllers}/*.js")) {
load_mode(mod); await load_mod(app, mod);
} }
app.register(Formbody); app.register(Formbody);
@ -57,19 +54,17 @@ const configure = async (fastify, opts) => {
app.get("/*", (req, rep) => { app.get("/*", (req, rep) => {
const target = path.resolve(path.join("pages", req.url)); const target = path.resolve(path.join("pages", req.url));
const index = path.resolve(path.join("pages", req.url, "index.html")); const index = path.resolve(path.join("pages", req.url, "index.html"));
console.log("TARGET", target, "INDEX", index);
if(fs.existsSync(index)) { if(fs.existsSync(index)) {
const fname = path.join(req.url.slice(1), "index.html"); const result = nunjucks.render(index);
console.log("RENDERING", fname);
const result = nunjucks.render(fname);
rep.code(200).type("text/html").send(result); rep.code(200).type("text/html").send(result);
} else if(fs.existsSync(target)) { } else if(fs.existsSync(target)) {
const fname = req.url.slice(1); const fname = req.url.slice(1);
const result = nunjucks.render(fname); const result = nunjucks.render(fname);
rep.code(200).type("text/html").send(result); rep.code(200).type("text/html").send(result);
} else { } else {
rep.code(404); console.error("404 NOT FOUND", target, "NO INDEX", index);
rep.code(404).type("text/html").send("Not Found");
} }
}); });
@ -94,7 +89,7 @@ const reload = () => {
export const main = async (arg, opts) => { export const main = async (arg, opts) => {
try { try {
chokidar.watch(["lib","commands","controllers","static","migrations","tests"]) chokidar.watch(["lib","api", "pages", "commands","controllers","static","migrations","tests"])
.on("add", path => reload()) .on("add", path => reload())
.on("change", path => reload()) .on("change", path => reload())
.on("unlink", path => reload()) .on("unlink", path => reload())

@ -3,6 +3,7 @@
<html> <html>
<head> <head>
<script src="/static/axios.js"></script> <script src="/static/axios.js"></script>
<script src="/static/alpine.js"></script>
<title>Bandolier2</title> <title>Bandolier2</title>
<style> <style>

@ -1,8 +1,6 @@
{% include "./header.html" %} {% include "./header.html" %}
<h1>Your TODOs</h1> <h1>Your TODOs</h1>
<script src="/alpine.js"></script>
<script> <script>
const new_task = async (todos, task) => { const new_task = async (todos, task) => {
const result = await axios({ const result = await axios({

@ -1,8 +1,5 @@
{% include "../header.html" %} {% include "../header.html" %}
<h1>Your TODOs</h1> <h1>Your TODOs</h1>
<script src="/alpine.js"></script>
<script> <script>
const new_task = async (todos, task) => { const new_task = async (todos, task) => {
const result = await axios({ const result = await axios({

Loading…
Cancel
Save