-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (81 loc) · 3.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
console.time("\x1b[1m\x1b[96mExecution time\x1b[22m\x1b[90m");
require("dotenv").config();
let merdeka, proj, indo;
const
express = require("express"),
port = process.env.PORT ??= 1945,
app = express(),
pkg = require("./package.json"),
fs = require("fs"),
deploy = {vercel: process.env.VERCEL_DEPLOY},
fetch = {
at: require("node-fetch"),
github: async (req)=>{
const res = await fetch.at((!req.startsWith("https://api.github.com/")? "https://api.github.com/": "")+req);
if(!res.ok) throw new Error(`${res.status}\n${res.statusText}`);
return res.json();
},
proj: ()=>fetch
.github(`repos/${pkg.author}/${pkg.name}`)
.then(repo=>proj = repo).catch(e=>proj = {
name: pkg.name,
full_name: pkg.author+"/"+pkg.name,
owner: {
login: pkg.author,
url: "https://api.github.com/users/"+pkg.author,
html_url: "https://github.com/"+pkg.author
},
html_url: `https://github.com/${pkg.author}/${pkg.name}`,
description: pkg.description
})
.then(()=>fetch
.github(proj.owner.url)
.then(owner=>proj.owner = owner, e=>{})
.then(()=>{
proj.owner.name ??= "El Roy Situmorang";
proj.owner.blog ??= proj.owner.html_url;
proj.fetched = new Date().toISOString();
app.locals.title = proj.name;
})
)
}
if(deploy.vercel){
console.log("Moving static files to Build Output");
try{
moveDir = (from, to)=>fs.readdirSync(from, {withFileTypes: !0}).forEach(f=>{
if(!fs.existsSync(to)) fs.mkdirSync(to, {recursive: true});
if(f.isDirectory()) moveDir(from+"/"+f.name, to+"/"+f.name);
else fs.rename(from+"/"+f.name, to+"/"+f.name, e=>{throw new Error(e)});
}); moveDir("./public", "./.vercel/output");
console.info("Success! Deployed to Vercel!");
}catch(e){console.error(`${e}\nFail! Unable to serve on Vercel!`)}
}
module.exports = app
.set("json spaces", 1)
.set("view engine", "ejs")
.use(express.static("public"), (req, res, next)=>{
let
now = new Date();
date = [{year: "numeric"}, {month: "2-digit"}, {day: "numeric"}]
.map(format=>now.toLocaleString("en-GB", {
...format,
timeZone: "Asia/Jakarta"
}))
.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().then(next);
else next();
})
.use("/indo(nesia)?", require("./routes/indo"))
.use("/\\0?((zona)|((time)?zone))", require("./routes/zone"))
.use("/\\0?((club)|(group))", require("./routes/group"))
.use("/ar((go)|(ch(ipel(ago)?)?))", require("./routes/argo"))
.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`))
.all("/indo(nesia)?.json", (req, res)=>res.status(200).json(indo))
.use((err, req, res, next)=>res.status(500).send(err))
.use((req, res)=>res.sendStatus(404));
app.listen(port, ()=>console.log(`\x1b[1m\x1b[38;2;255;0;0mGeo\x1b[38;2;255;255;255mID\x1b[0m \x1b[32m| \x1b[93mListening on port: \x1b[1m\x1b[95m${port}\x1b[0m`));
console.timeEnd("\x1b[1m\x1b[96mExecution time\x1b[22m\x1b[90m");