chore: format
This commit is contained in:
parent
305bf8b571
commit
7a5a1a5e9d
1 changed files with 99 additions and 97 deletions
196
index.js
196
index.js
|
|
@ -10,14 +10,13 @@ const { Eta } = require("eta");
|
|||
const { match } = require("path-to-regexp");
|
||||
const bcrypt = require("bcrypt");
|
||||
const cheerio = require("cheerio");
|
||||
const webPush = require('web-push');
|
||||
const app = express();
|
||||
const _expressWs = require("express-ws")(app);
|
||||
const bodyParser = require("body-parser");
|
||||
const model = require("./db")
|
||||
const model = require("./db");
|
||||
const PORT = 3000;
|
||||
|
||||
const db = model.db
|
||||
const db = model.db;
|
||||
|
||||
let viewpath = path.join(__dirname, "views");
|
||||
let eta = new Eta({ views: viewpath, cache: false, autoEscape: true });
|
||||
|
|
@ -28,16 +27,6 @@ app.use(bodyParser.urlencoded({ extended: true }));
|
|||
app.use(express.static("public"));
|
||||
app.use(fileUpload());
|
||||
|
||||
const publicVapidKey = 'BCW4o4mLShjgONRK21jwTT44ri49JZfrIeJ-W089TVatopdDh8UNujEptonafxM2ttbUPm4wbZF39GvY3dZWAO0';
|
||||
const privateVapidKey = 'sXpmx2nRwvVWhGt7uBysZnmFStf9QjaafGd9Ereo4Gw';
|
||||
|
||||
// Configure web-push with your VAPID details
|
||||
webPush.setVapidDetails(
|
||||
'mailto:signups@sheepmail.net', // a mailto URL or URL
|
||||
publicVapidKey,
|
||||
privateVapidKey
|
||||
);
|
||||
|
||||
app.use(
|
||||
session({
|
||||
store: new SQLiteStore({ client: db, expired: { clear: true } }),
|
||||
|
|
@ -45,7 +34,7 @@ app.use(
|
|||
resave: false,
|
||||
saveUninitialized: true,
|
||||
cookie: { secure: false },
|
||||
})
|
||||
}),
|
||||
);
|
||||
|
||||
function bootstrapContext(db, structureId, routeId, initContext) {
|
||||
|
|
@ -70,19 +59,22 @@ function bootstrapContext(db, structureId, routeId, initContext) {
|
|||
console: {
|
||||
log: function (...content) {
|
||||
const s = content.reduce((acc, curr) => {
|
||||
console.log(curr)
|
||||
return acc + (acc ? ' ' : '') + `${util.inspect(curr, {
|
||||
showHidden: false,
|
||||
depth: null, // `null` lets you see the full depth of the object
|
||||
colors: false, // Setting this to true uses ANSI color codes
|
||||
compact: false
|
||||
})
|
||||
}`;
|
||||
}, '');
|
||||
console.log(curr);
|
||||
return (
|
||||
acc +
|
||||
(acc ? " " : "") +
|
||||
`${util.inspect(curr, {
|
||||
showHidden: false,
|
||||
depth: null, // `null` lets you see the full depth of the object
|
||||
colors: false, // Setting this to true uses ANSI color codes
|
||||
compact: false,
|
||||
})}`
|
||||
);
|
||||
}, "");
|
||||
|
||||
model.createLog(db, structureId, routeId, s)
|
||||
}
|
||||
}
|
||||
model.createLog(db, structureId, routeId, s);
|
||||
},
|
||||
},
|
||||
});
|
||||
for (let appDb of dbs) {
|
||||
let dbInstance = model.getDbInstance(appDb.id);
|
||||
|
|
@ -110,7 +102,7 @@ function bootstrapWebsocketHandler(route) {
|
|||
const context = bootstrapContext(db, route.structure_id, route.id, { app });
|
||||
let result = vm.runInContext(
|
||||
route.handler + `\n\napp.ws("${routeWithPrefix(route)}", handler)`,
|
||||
context
|
||||
context,
|
||||
);
|
||||
model.updateRoute(db, { ...route, error: null });
|
||||
return result;
|
||||
|
|
@ -153,7 +145,6 @@ buildRoutes();
|
|||
// | | _____| || |___ | | | |
|
||||
// |_______||_______||_______||___| |_|
|
||||
|
||||
|
||||
app.post("/register", async (req, res) => {
|
||||
try {
|
||||
const { username, password } = req.body;
|
||||
|
|
@ -178,7 +169,7 @@ app.post("/login", async (req, res) => {
|
|||
return res.send(
|
||||
eta.render("auth/login", {
|
||||
error: "are you sure you entered that right?",
|
||||
})
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -216,7 +207,7 @@ function bootstrapTemplateWithHTMXetc(
|
|||
blissClone,
|
||||
blissCopy,
|
||||
headInjection,
|
||||
htmxRequest = false
|
||||
htmxRequest = false,
|
||||
) {
|
||||
if (
|
||||
htmlString.toLowerCase().startsWith("<html>") ||
|
||||
|
|
@ -282,8 +273,8 @@ app.get("/workshop", (req, res) => {
|
|||
bootstrapTemplateWithHTMXetc(
|
||||
eta.render("workshop/index", {
|
||||
structures: model.getStructures(db),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -308,8 +299,8 @@ function sidebarStuff(db, structId) {
|
|||
app.get("/workshop/:structure_id", (req, res) => {
|
||||
return res.send(
|
||||
bootstrapTemplateWithHTMXetc(
|
||||
eta.render("workshop/editor", sidebarStuff(db, req.params.structure_id))
|
||||
)
|
||||
eta.render("workshop/editor", sidebarStuff(db, req.params.structure_id)),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -324,7 +315,7 @@ app.post("/workshop/:structure_id/clone", (req, res) => {
|
|||
1,
|
||||
// req.session.userId,
|
||||
routePrefix,
|
||||
req.body.clone_dbs
|
||||
req.body.clone_dbs,
|
||||
);
|
||||
buildRoutes();
|
||||
} catch (e) {
|
||||
|
|
@ -349,7 +340,7 @@ app.get("/workshop/:structure_id/db/:db_id", (req, res) => {
|
|||
const structdb = model.getDbForStructure(
|
||||
db,
|
||||
req.params.structure_id,
|
||||
req.params.db_id
|
||||
req.params.db_id,
|
||||
);
|
||||
if (!structdb) {
|
||||
return res.send("uh oh");
|
||||
|
|
@ -359,8 +350,8 @@ app.get("/workshop/:structure_id/db/:db_id", (req, res) => {
|
|||
eta.render("workshop/db_garden", {
|
||||
db: structdb,
|
||||
...sidebarStuff(db, req.params.structure_id),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -385,14 +376,14 @@ app.post("/workshop/:structure_id/route", (req, res) => {
|
|||
req.body.verb,
|
||||
p[0] == "/" ? p.substring(1) : "/" + p,
|
||||
req.params.structure_id,
|
||||
dummyHandler
|
||||
dummyHandler,
|
||||
);
|
||||
// todo optimize by only adding new, don't just rebuild all
|
||||
buildRoutes();
|
||||
return smartRedirect(
|
||||
req,
|
||||
res,
|
||||
`/workshop/${req.params.structure_id}/route/${route}`
|
||||
`/workshop/${req.params.structure_id}/route/${route}`,
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -419,14 +410,16 @@ app.put("/workshop/:structure_id/db/:db_id/library", (req, res) => {
|
|||
},
|
||||
});
|
||||
let evaledCode = vm.runInContext(appDb.library, context);
|
||||
let stdout = capturedOutput.map(v => {
|
||||
return util.inspect(v, {
|
||||
showHidden: false,
|
||||
depth: null, // `null` lets you see the full depth of the object
|
||||
colors: false, // Setting this to true uses ANSI color codes
|
||||
compact: false
|
||||
})
|
||||
}).join("\n");
|
||||
let stdout = capturedOutput
|
||||
.map((v) => {
|
||||
return util.inspect(v, {
|
||||
showHidden: false,
|
||||
depth: null, // `null` lets you see the full depth of the object
|
||||
colors: false, // Setting this to true uses ANSI color codes
|
||||
compact: false,
|
||||
});
|
||||
})
|
||||
.join("\n");
|
||||
return res.send(`${stdout}\n\n> ${evaledCode}`.trim());
|
||||
} catch (e) {
|
||||
return res.send(`${e}\n\n${e.stack}`);
|
||||
|
|
@ -443,16 +436,19 @@ app.post("/workshop/:structure_id/db/:db_id/repl", (req, res) => {
|
|||
module: { exports: null },
|
||||
sql: dbInstance,
|
||||
console: {
|
||||
log: (...args) => capturedOutput.push(
|
||||
args.map(v => {
|
||||
return util.inspect(v, {
|
||||
showHidden: false,
|
||||
depth: null, // `null` lets you see the full depth of the object
|
||||
colors: false, // Setting this to true uses ANSI color codes
|
||||
compact: false
|
||||
})
|
||||
}
|
||||
).join(" ")),
|
||||
log: (...args) =>
|
||||
capturedOutput.push(
|
||||
args
|
||||
.map((v) => {
|
||||
return util.inspect(v, {
|
||||
showHidden: false,
|
||||
depth: null, // `null` lets you see the full depth of the object
|
||||
colors: false, // Setting this to true uses ANSI color codes
|
||||
compact: false,
|
||||
});
|
||||
})
|
||||
.join(" "),
|
||||
),
|
||||
},
|
||||
});
|
||||
const libraryScript = new vm.Script(appDb.library);
|
||||
|
|
@ -475,13 +471,13 @@ app.post("/workshop/:structure_id/template", (req, res) => {
|
|||
req.params.structure_id,
|
||||
name,
|
||||
"<div>henlo <%= it.name %></div>",
|
||||
"it = { name: 'templates!' };"
|
||||
"it = { name: 'templates!' };",
|
||||
);
|
||||
|
||||
return smartRedirect(
|
||||
req,
|
||||
res,
|
||||
`/workshop/${req.params.structure_id}/template/${template}`
|
||||
`/workshop/${req.params.structure_id}/template/${template}`,
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -490,7 +486,11 @@ app.put("/workshop/:structure_id/template/:template_id", (req, res) => {
|
|||
let content = req.body.content;
|
||||
let test_object = req.body.test_object;
|
||||
|
||||
model.updateTemplate(db, { ...model.updateTemplate(db, id), content, test_object });
|
||||
model.updateTemplate(db, {
|
||||
...model.updateTemplate(db, id),
|
||||
content,
|
||||
test_object,
|
||||
});
|
||||
|
||||
return res.send("good");
|
||||
});
|
||||
|
|
@ -499,16 +499,17 @@ app.get("/workshop/:structure_id/template/:template_id", (req, res) => {
|
|||
const template = model.getTemplate(db, req.params.template_id);
|
||||
|
||||
console.log("wassup", {
|
||||
template: template,
|
||||
...sidebarStuff(db, req.params.structure_id),})
|
||||
template: template,
|
||||
...sidebarStuff(db, req.params.structure_id),
|
||||
});
|
||||
|
||||
return res.send(
|
||||
bootstrapTemplateWithHTMXetc(
|
||||
eta.render("workshop/template", {
|
||||
template: template,
|
||||
...sidebarStuff(db, req.params.structure_id),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -516,7 +517,7 @@ app.get("/workshop/:structure_id/template/:template_id/preview", (req, res) => {
|
|||
const template = model.getTemplate(db, req.params.template_id);
|
||||
const struct = model.getStructure(db, req.params.structure_id);
|
||||
const eta = model.getTemplater(req.params.structure_id);
|
||||
console.log(template)
|
||||
console.log(template);
|
||||
|
||||
const context = vm.createContext({ it: null });
|
||||
vm.runInContext(template.test_object, context);
|
||||
|
|
@ -532,14 +533,17 @@ app.get("/workshop/:structure_id/template/:template_id/preview", (req, res) => {
|
|||
null,
|
||||
null,
|
||||
struct.head_injection,
|
||||
false
|
||||
)
|
||||
false,
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
app.get("/workshop/:structure_id/route/:id", (req, res) => {
|
||||
const route = model.getRoute(db, req.params.id);
|
||||
let routePrefix = model.getStructure(db, req.params.structure_id).route_prefix;
|
||||
let routePrefix = model.getStructure(
|
||||
db,
|
||||
req.params.structure_id,
|
||||
).route_prefix;
|
||||
let previewUrl = routePrefix
|
||||
? path.join(routePrefix || "", route.path)
|
||||
: route.path;
|
||||
|
|
@ -551,22 +555,21 @@ app.get("/workshop/:structure_id/route/:id", (req, res) => {
|
|||
previewUrl:
|
||||
route["verb"] == "GET" ? previewUrl : "/workshop/do-something-here",
|
||||
...sidebarStuff(db, req.params.structure_id),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
app.get("/workshop/:structure_id/route/:route_id/logs", (req, res) => {
|
||||
const since = req.query.since
|
||||
let logs = []
|
||||
const since = req.query.since;
|
||||
let logs = [];
|
||||
if (since != undefined) {
|
||||
logs = model.getNewLogsByRoute(db, req.params.route_id, since)
|
||||
}
|
||||
else {
|
||||
logs = model.getLogsByRoute(db, req.params.route_id)
|
||||
logs = model.getNewLogsByRoute(db, req.params.route_id, since);
|
||||
} else {
|
||||
logs = model.getLogsByRoute(db, req.params.route_id);
|
||||
}
|
||||
|
||||
const lastId = model.getMostRecentLogIdByRoute(db, req.params.route_id)
|
||||
const lastId = model.getMostRecentLogIdByRoute(db, req.params.route_id);
|
||||
|
||||
return res.send(
|
||||
bootstrapTemplateWithHTMXetc(
|
||||
|
|
@ -575,12 +578,11 @@ app.get("/workshop/:structure_id/route/:route_id/logs", (req, res) => {
|
|||
structId: req.params.structure_id,
|
||||
routeId: req.params.route_id,
|
||||
since: lastId,
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
// POST route to handle file upload
|
||||
app.post("/workshop/:structure_id/files", (req, res) => {
|
||||
if (!req.files || Object.keys(req.files).length === 0) {
|
||||
|
|
@ -608,7 +610,7 @@ app.post("/workshop/:structure_id/files", (req, res) => {
|
|||
name,
|
||||
storedPath,
|
||||
mime_type,
|
||||
mime_subtype
|
||||
mime_subtype,
|
||||
);
|
||||
|
||||
let file = model.getFile(db, id);
|
||||
|
|
@ -640,8 +642,8 @@ app.get("/workshop/:structure_id/files", (req, res) => {
|
|||
eta.render("workshop/files", {
|
||||
files,
|
||||
...sidebarStuff(db, structure_id),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -650,8 +652,8 @@ app.get("/workshop/:structure_id/settings", (req, res) => {
|
|||
bootstrapTemplateWithHTMXetc(
|
||||
eta.render("workshop/settings", {
|
||||
...sidebarStuff(db, req.params.structure_id),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -682,8 +684,8 @@ app.get("/workshop/:structure_id/new_template_modal", (req, res) => {
|
|||
bootstrapTemplateWithHTMXetc(
|
||||
eta.render("workshop/new_template_modal", {
|
||||
structure: model.getStructure(db, req.params.structure_id),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -692,8 +694,8 @@ app.get("/workshop/:structure_id/new_route_modal", (req, res) => {
|
|||
bootstrapTemplateWithHTMXetc(
|
||||
eta.render("workshop/new_route_modal", {
|
||||
structure: model.getStructure(db, req.params.structure_id),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -702,8 +704,8 @@ app.get("/workshop/:structure_id/new_db_modal", (req, res) => {
|
|||
bootstrapTemplateWithHTMXetc(
|
||||
eta.render("workshop/new_db_modal", {
|
||||
structure: model.getStructure(db, req.params.structure_id),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -717,8 +719,8 @@ app.get("/workshop/:structure_id/clone_modal", (req, res) => {
|
|||
structure,
|
||||
dbs,
|
||||
defaultRoutePrefix: path.join(structure.route_prefix || "", "clone"),
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -771,13 +773,13 @@ app.all("*", (req, res) => {
|
|||
`/workshop/${route.structure_id}/clone_modal/`,
|
||||
verb == "GET" ? embedHTML(req.originalUrl) : null,
|
||||
structure.head_injection,
|
||||
req.headers["hx-request"]
|
||||
)
|
||||
req.headers["hx-request"],
|
||||
),
|
||||
);
|
||||
};
|
||||
return vm.runInContext(
|
||||
(route.handler += `\n\nhandler(req, res)`),
|
||||
context
|
||||
context,
|
||||
);
|
||||
} catch (e) {
|
||||
const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue