/[sudobot]/trunk/src/api/Server.ts
ViewVC logotype

Annotation of /trunk/src/api/Server.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 393 - (hide annotations)
Mon Jul 29 17:29:59 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2684 byte(s)
style: add license comments (#77)

* style: add license commits

* fix: shebang errors
1 rakin 393 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20 rakin 323 import DiscordClient from "../client/Client";
21     import Service from "../utils/structures/Service";
22 rakin 324 import express, { Express, Router as ExpressRouter } from 'express';
23 rakin 323 import Router from "./Router";
24     import path from "path";
25 rakin 347 import rateLimit from 'express-rate-limit';
26 rakin 367 import cors from 'cors';
27 rakin 323
28     export interface ServerOptions {
29     port?: number;
30     }
31    
32     export default class Server extends Service {
33     port: number;
34     express: Express;
35     router: Router;
36    
37     constructor(client: DiscordClient, { port = 4000 }: ServerOptions = {}) {
38     super(client);
39     this.port = port;
40     this.express = express();
41     this.router = new Router(client, this, { routesDir: path.resolve(__dirname, 'routes') });
42     }
43    
44     async boot() {
45 rakin 324 type methods = 'get' | 'post' | 'put' | 'patch' | 'delete';
46     const expressRouter = ExpressRouter();
47    
48 rakin 367 expressRouter.use(cors());
49    
50 rakin 347 expressRouter.use(rateLimit({
51     windowMs: 1 * 60 * 1000,
52     max: 50,
53     standardHeaders: true,
54     legacyHeaders: true,
55     message: { code: 429, error: 'Too many requests at a time. Please try again later.' },
56     }));
57    
58 rakin 348 expressRouter.use(express.json());
59     expressRouter.use(express.urlencoded({ extended: true }));
60    
61 rakin 323 await this.router.loadRoutes();
62 rakin 324
63 rakin 348 for (const route of this.router.routes) {
64 rakin 354 // console.log(route.callback[1], route.callback[0].middleware()[route.callback[1]]);
65 rakin 348 expressRouter[route.method.toLowerCase() as methods](route.path, ...(route.callback[0].middleware()[route.callback[1]] as any[] ?? []), ...route.middlewareList as any[], await route.getCallbackFunction());
66 rakin 324 }
67    
68     this.express.use(expressRouter);
69 rakin 323 }
70    
71     async run() {
72     await this.boot();
73    
74     this.express.listen(this.port, () => {
75     console.log(`API Server listening at port ${this.port}`);
76     });
77     }
78     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26