1 |
rakinar2 |
577 |
/** |
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 |
|
|
import { readdir } from "fs/promises"; |
21 |
|
|
import path from "path"; |
22 |
|
|
import DiscordClient from "../client/Client"; |
23 |
|
|
import Service from "../utils/structures/Service"; |
24 |
|
|
import Controller from "./Controller"; |
25 |
|
|
import Route from "./Route"; |
26 |
|
|
import Server from "./Server"; |
27 |
|
|
|
28 |
|
|
export interface RouterOptions { |
29 |
|
|
routesDir: string; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
export default class Router extends Service { |
33 |
|
|
routesDir: string; |
34 |
|
|
controllerObjects: Array <Controller> = []; |
35 |
|
|
routes: Route[] = []; |
36 |
|
|
|
37 |
|
|
constructor(client: DiscordClient, protected server: Server, { routesDir }: RouterOptions) { |
38 |
|
|
super(client); |
39 |
|
|
this.routesDir = routesDir; |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
async loadRoutes() { |
43 |
|
|
const files = await readdir(this.routesDir); |
44 |
|
|
|
45 |
|
|
for await (const file of files) { |
46 |
|
|
if (!file.endsWith('.ts') && !file.endsWith('.js')) { |
47 |
|
|
continue; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
console.log(`Loading routes from "${file}"...`); |
51 |
|
|
|
52 |
|
|
await import(path.join(this.routesDir, file)); |
53 |
|
|
} |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
static insert(method: string, path: string, callback: [typeof Controller, string]) { |
57 |
|
|
let controller = DiscordClient.client.server.router.controllerObjects.find(c => c instanceof callback[0]); |
58 |
|
|
|
59 |
|
|
if (!controller) { |
60 |
|
|
controller = new callback[0](DiscordClient.client); |
61 |
|
|
DiscordClient.client.server.router.controllerObjects.push(controller); |
62 |
|
|
console.log("Pushed a controller"); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
const route = new Route(method, path, [controller, callback[1]]); |
66 |
|
|
DiscordClient.client.server.router.routes.push(route); |
67 |
|
|
return route; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
static get(path: string, callback: [typeof Controller, string]) { |
71 |
|
|
return Router.insert("GET", path, callback); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
static post(path: string, callback: [typeof Controller, string]) { |
75 |
|
|
return Router.insert("POST", path, callback); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
static put(path: string, callback: [typeof Controller, string]) { |
79 |
|
|
return Router.insert("PUT", path, callback); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
static patch(path: string, callback: [typeof Controller, string]) { |
83 |
|
|
return Router.insert("PATCH", path, callback); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
static delete(path: string, callback: [typeof Controller, string]) { |
87 |
|
|
return Router.insert("DELETE", path, callback); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
static resource(path: string, controller: typeof Controller, { get = true, post = true, put = true, patch = true, del = true }: { get?: boolean, post?: boolean, put?: boolean, patch?: boolean, del?: boolean } = {}) { |
91 |
|
|
return { |
92 |
|
|
get: get ? Router.get(path, [controller, 'index']) : undefined, |
93 |
|
|
post: post ? Router.post(path, [controller, 'create']) : undefined, |
94 |
|
|
put: put ? Router.put(path, [controller, 'update']) : undefined, |
95 |
|
|
patch: patch ? Router.patch(path, [controller, 'update']) : undefined, |
96 |
|
|
delete: del ? Router.delete(path, [controller, 'delete']) : undefined, |
97 |
|
|
}; |
98 |
|
|
} |
99 |
|
|
} |