-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (49 loc) · 1.55 KB
/
index.ts
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
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-var-requires */
//@ts-nocheck
import express from "express";
import { Request, Response } from "express";
import path from "path";
import fs from "fs";
import Logger from "./utils/logger";
import constants from "./constants";
import * as pkg from "./package.json";
const app = new express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use((req: Request, res: Response, next: unknown) => {
if (req.path === "/log") return next();
const query = req.query;
const body = req.body;
Logger.info(`[ ${new Date().toLocaleString()} ]`, {
path: req.path,
query, body,
useragent: req.get("User-Agent")
});
next();
});
// print error message in json format
app.get("/", async (req: Request, res: Response) => {
res.send({
success: true,
message: "Hi, this is a cabul API!",
container: "https://github.com/sinkaroid/cabul/tree/proxy",
version: `v${pkg.version}`,
endpoint: "/reddit?subreddit=<subreddit>",
});
});
// ! Route Imports
fs.readdir(path.join(__dirname, "routes"), (err, files) => {
const r: unknown[] = [];
files.forEach(file => {
if (file.endsWith("ts")) return;
const pathstring = path.join(__dirname, "routes", file);
const { handler, paths } = require(pathstring);
r.push({ handler, paths });
});
r.forEach(route => {
app.use(route.paths, route.handler);
});
});
app.listen(constants.port, () => Logger.info(`Listening on port ${constants.port}`));
module.exports = app;