Skip to content

Commit

Permalink
🔧 Fix Craziest Routing RegEx
Browse files Browse the repository at this point in the history
  • Loading branch information
el-ang committed Sep 4, 2022
1 parent 3eb5b5c commit 706a921
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const { route } = require("./routes/indo");

console.time("Execution time");
require("dotenv").config();
let merdeka, proj, indo;
const
express = require("express"),
routes = require("./utils/routes"),
port = process.env.PORT ??= 1945,
app = express(),
pkg = require("./package.json"),
Expand Down Expand Up @@ -55,7 +58,8 @@ if(deploy.vercel){
module.exports = app
.set("json spaces", 1)
.set("view engine", "ejs")
.use((req, res, next)=>{
.use("/indo(nesia)?:all(.json)?", require("./routes/indo"))
.use(express.static("public"), (req, res, next)=>{
let
now = new Date();
date = [{year: "numeric"}, {month: "2-digit"}, {day: "numeric"}]
Expand All @@ -65,14 +69,16 @@ module.exports = app
})).join("-"),
time = now.toLocaleTimeString("en-GB", {timeZone: "Asia/Jakarta"});

indo = require("./utils/algo");
merdeka = ((now = new Date(`${date}T${time}.000Z`)).getDate() == 17) && (now.getMonth() == 7);
if(!proj?.fetched || (Math.abs(new Date(proj.fetched) - new Date()) >= 108e5)) fetch.proj();
indo = require("./utils/algo"); next();
})
.use("/favicon.ico", (req, res)=>res.sendFile(`${__dirname}${deploy.vercel? "/.vercel/output": ""}/public/img/${merdeka? "17": "logo"}.svg`))
.use("/indo(nesia)?:json(\.json)?", require("./routes/indo"))
.use("/", express.static("public"), (req, res)=>res.status(200).render(".", {merdeka, proj, page: {}}))
.use("*", (req, res)=>res.redirect("/lost"));
if(!proj?.fetched || (Math.abs(new Date(proj.fetched) - new Date()) >= 108e5)) fetch.proj().then(next);
else next();
}, express.Router()
.all("/", (req, res)=>res.status(200).render(".", {merdeka, proj, page: {}}))
.all("/favicon.ico", (req, res)=>res.status(200).sendFile(`${__dirname}${deploy.vercel? "/.vercel/output": ""}/public/img/${merdeka? "17": "logo"}.svg`))
)
.use((err, req, res, next)=>res.status(500).send(err))
.use((req, res)=>res.sendStatus(404));

app.listen(port, ()=>console.log(`GeoID | Listening on port: ${port}`));
console.timeEnd("Execution time");
35 changes: 24 additions & 11 deletions routes/indo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
const indo = require("../utils/algo");
module.exports = require("express").Router()
/* Default Route */
.use((req, res, next)=>res.status(200))
/* Public Routes */
.get("/:prov(\d{2}):rgnct(\d{2})?:dist(\d{2})?:vlg(\d{4})?.json", (req, res)=>{
const {prov, rgnct, dist, vlg} = req.params;
console.log(req.params);
res.send(200).json(req.params).end();
})
/* Restricted Admin-Auth Only Routes */
let indo;
const router = require("express").Router({mergeParams: !0})
/* Default Route */
.use((req, res, next)=>{
indo = require("../utils/algo");
if(req.params.all) res.status(200).json(indo);
else next();
});

/* Public Routes */
router.route("/:prov(\\d{2}):rgnct(\\d{2})?:dist(\\d{2})?:vlg(\\d{4})?.json").get((req, res)=>{
const {prov, rgnct, dist, vlg} = req.params;
res.status(200).json(req.params);
});

router.route("/:q\\D.json").get((req, res)=>{
const {q} = req.params;
res.status(200).json(req.params);
});

/* Restricted Admin-Auth Only Routes */

/* router.use((req, res, next)=>{console.log("Router Closed"); next();}); */
module.exports = router;

1 comment on commit 706a921

@vercel
Copy link

@vercel vercel bot commented on 706a921 Sep 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

geo-id – ./

geo-id-el-ang.vercel.app
geo-id.vercel.app
geo-id-git-main-el-ang.vercel.app

Please sign in to comment.