/[sudobot]/branches/2.x/src/api/Router.ts
ViewVC logotype

Annotation of /branches/2.x/src/api/Router.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 2554 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 import express, { NextFunction, Request, RequestHandler, Response, Router } from "express";
2     import { lstatSync, readdir } from "fs";
3     import path from "path";
4     import DiscordClient from "../client/Client";
5     import auth from "./Auth";
6     import apiRoute from "./routes";
7    
8     const router = Router();
9    
10     router.use(express.json());
11     router.use(express.urlencoded({
12     extended: false
13     }));
14     router.use(auth);
15     router.use((req: Request, res: Response, next: NextFunction) => {
16     res.setHeader('Access-Control-Allow-Origin', DiscordClient.client.config.props.global.cp_host);
17     res.setHeader('Access-Control-Allow-Methods', '*');
18     res.setHeader('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Access-Control-Allow-Origin, Access-Control-Allow-Methods, Content-Type, Content-Length');
19     next();
20     });
21    
22     export interface Route {
23     path: string;
24     middleware: RequestHandler[];
25     get?: (req: Request, res: Response) => any,
26     post?: (req: Request, res: Response) => any,
27     put?: (req: Request, res: Response) => any,
28     patch?: (req: Request, res: Response) => any,
29     delete?: (req: Request, res: Response) => any,
30     all?: (req: Request, res: Response) => any,
31     };
32    
33     export function loadRoutes(dir: string = __dirname, router: Router) {
34     readdir(dir, async (err, data) => {
35     if (err) {
36     console.log(err);
37     return;
38     }
39    
40     for await (const file of data) {
41     if (file === '.' || file === '..')
42     continue;
43    
44     let filePath = await path.resolve(dir, file);
45    
46     if (lstatSync(filePath).isDirectory())
47     continue;
48    
49     let route = <Route> (await import(filePath)).default;
50     let middleware = route.middleware ?? [];
51    
52     if (route.get) {
53     router.get(route.path, ...middleware, route.get);
54     }
55    
56     if (route.post) {
57     router.post(route.path, ...middleware, route.post);
58     }
59    
60     if (route.put) {
61     router.put(route.path, ...middleware, route.put);
62     }
63    
64     if (route.patch) {
65     router.patch(route.path, ...middleware, route.patch);
66     }
67    
68     if (route.delete) {
69     router.delete(route.path, ...middleware, route.delete);
70     }
71    
72     if (route.all) {
73     router.all(route.path, ...middleware, route.all);
74     }
75     }
76     });
77     }
78    
79     loadRoutes(path.resolve(__dirname, 'routes'), router);
80    
81     export default router;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26