diff options
author | HampusM <hampus@hampusmat.com> | 2021-05-24 10:49:41 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-05-24 10:49:41 +0200 |
commit | 518bd58c5aba1a6eaa645074f3ae2d780c45f6dc (patch) | |
tree | a99acd3b434422e019bc76064ad0798c8472f07d /src/app.js | |
parent | 21e52ba2fa323e8aebf291882083c1eca9f6a5af (diff) |
Restructured the whole project & added Nodemon and Concurrently
Diffstat (limited to 'src/app.js')
-rw-r--r-- | src/app.js | 101 |
1 files changed, 95 insertions, 6 deletions
@@ -1,7 +1,96 @@ -import { createApp } from "vue/dist/vue.esm-bundler"; -import App from "./App.vue"; -import router from "./router"; +const express = require("express"); +const api = require("./api/v1"); +const yaml = require('js-yaml'); +const fs = require('fs'); +const { exit } = require("process"); -createApp(App) - .use(router) - .mount("#app");
\ No newline at end of file +const settings = yaml.load(fs.readFileSync(__dirname + "/../settings.yml", 'utf8')); +const settings_keys = Object.keys(settings); + +const mandatory_settings = [ "host", "port", "title", "about", "base_dir" ]; + +const mandatory_not_included = settings_keys.filter(x => !mandatory_settings.includes(x)); +const settings_not_included = mandatory_settings.filter(x => !settings_keys.includes(x)); + +if(settings_not_included.length !== 0) { + console.log(`Error: settings.yml is missing ${(mandatory_not_included.length > 1) ? "keys" : "key"}:`); + console.log(settings_not_included.join(", ")); + exit(1); +} +if(mandatory_not_included.length !== 0) { + console.log(`Error: settings.yml includes ${(mandatory_not_included.length > 1) ? "pointless keys" : "a pointless key"}:`); + console.log(mandatory_not_included.join(", ")); + exit(1); +} + +const dist_dir = __dirname + "/../dist"; + +const app = express(); + +app.get(/.*\.(css|js|ico)$/, (req, res, next) => +{ + fs.access(`${dist_dir}${req.path}`, err => + { + if(err) { + next(); + return; + } + res.sendFile(`${req.path}`, { root: dist_dir }); + }); +}); + +app.use("/api/v1", (req, res, next) => +{ + req.settings = settings; + next(); +}, api); + +app.get("/", (req, res) => +{ + res.sendFile(`app.html`, { root: dist_dir }); +}); + +const repo_router = express.Router(); + +app.use("/:repo([a-zA-Z0-9-_]+)", (req, res, next) => +{ + console.log("AAAA"); + fs.readdir(settings["base_dir"], (err, dir_content) => + { + if(err) { + res.status(404).send("404: Not found"); + return; + } + + dir_content = dir_content.filter(repo => repo.endsWith(".git")); + if(!dir_content.includes(req.params.repo + ".git")) { + res.status(404).send("404: Not found"); + return; + } + else { + next(); + } + }); +}, repo_router); + +repo_router.get(/$|log$|refs$|tree$/, (req, res) => +{ + res.sendFile(`app.html`, { root: dist_dir }); +}); + +repo_router.get(/\/log\/[a-fA-F0-9]{40}$/, (req, res) => +{ + res.sendFile(`app.html`, { root: dist_dir }); +}) + +repo_router.use((req, res) => +{ + res.status(404).send("404: Not found eeee"); +}); + +app.use((req, res) => +{ + res.status(404).send("404: Not found"); +}) + +app.listen(settings["port"], settings["host"], () => console.log(`App is running on ${settings["host"]}:${settings["port"]}`));
\ No newline at end of file |