3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-17 07:05:03 +00:00
This commit is contained in:
iamshoXy 2024-04-11 21:54:00 +02:00
commit 2e4d84571b
164 changed files with 22377 additions and 25685 deletions

View file

@ -3,15 +3,15 @@ import moment from "moment-timezone";
import { GuildArchives } from "../data/GuildArchives";
import { notFound } from "./responses";
export function initArchives(app: express.Express) {
export function initArchives(router: express.Router) {
const archives = new GuildArchives(null);
// Legacy redirect
app.get("/spam-logs/:id", (req: Request, res: Response) => {
router.get("/spam-logs/:id", (req: Request, res: Response) => {
res.redirect("/archives/" + req.params.id);
});
app.get("/archives/:id", async (req: Request, res: Response) => {
router.get("/archives/:id", async (req: Request, res: Response) => {
const archive = await archives.find(req.params.id);
if (!archive) return notFound(res);

View file

@ -51,8 +51,8 @@ function simpleDiscordAPIRequest(bearerToken, path): Promise<any> {
});
}
export function initAuth(app: express.Express) {
app.use(passport.initialize());
export function initAuth(router: express.Router) {
router.use(passport.initialize());
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user as IPassportApiUser));
@ -110,8 +110,8 @@ export function initAuth(app: express.Express) {
),
);
app.get("/auth/login", passport.authenticate("oauth2"));
app.get(
router.get("/auth/login", passport.authenticate("oauth2"));
router.get(
"/auth/oauth-callback",
passport.authenticate("oauth2", { failureRedirect: "/", session: false }),
(req: Request, res: Response) => {
@ -122,7 +122,7 @@ export function initAuth(app: express.Express) {
}
},
);
app.post("/auth/validate-key", async (req: Request, res: Response) => {
router.post("/auth/validate-key", async (req: Request, res: Response) => {
const key = req.body.key;
if (!key) {
return res.status(400).json({ error: "No key supplied" });
@ -135,14 +135,14 @@ export function initAuth(app: express.Express) {
res.json({ valid: true, userId });
});
app.post("/auth/logout", ...apiTokenAuthHandlers(), async (req: Request, res: Response) => {
router.post("/auth/logout", ...apiTokenAuthHandlers(), async (req: Request, res: Response) => {
await apiLogins.expireApiKey(req.user!.apiKey);
return ok(res);
});
// API route to refresh the given API token's expiry time
// The actual refreshing happens in the api-token passport strategy above, so we just return 200 OK here
app.post("/auth/refresh", ...apiTokenAuthHandlers(), (req, res) => {
router.post("/auth/refresh", ...apiTokenAuthHandlers(), (req, res) => {
return ok(res);
});
}

View file

@ -1,6 +1,7 @@
import express from "express";
import z from "zod";
import { guildPlugins } from "../plugins/availablePlugins";
import { guildPluginInfo } from "../plugins/pluginInfo";
import { indentLines } from "../utils";
import { notFound } from "./responses";
@ -96,30 +97,31 @@ function formatZodConfigSchema(schema: z.ZodTypeAny) {
return "unknown";
}
export function initDocs(app: express.Express) {
const docsPlugins = guildPlugins.filter((plugin) => plugin.showInDocs);
export function initDocs(router: express.Router) {
const docsPluginNames = Object.keys(guildPluginInfo).filter((k) => guildPluginInfo[k].showInDocs);
app.get("/docs/plugins", (req: express.Request, res: express.Response) => {
router.get("/docs/plugins", (req: express.Request, res: express.Response) => {
res.json(
docsPlugins.map((plugin) => {
const thinInfo = plugin.info ? { prettyName: plugin.info.prettyName, legacy: plugin.info.legacy ?? false } : {};
docsPluginNames.map((pluginName) => {
const info = guildPluginInfo[pluginName];
const thinInfo = info ? { prettyName: info.prettyName, legacy: info.legacy ?? false } : {};
return {
name: plugin.name,
name: pluginName,
info: thinInfo,
};
}),
);
});
app.get("/docs/plugins/:pluginName", (req: express.Request, res: express.Response) => {
// prettier-ignore
const plugin = docsPlugins.find(_plugin => _plugin.name === req.params.pluginName);
if (!plugin) {
router.get("/docs/plugins/:pluginName", (req: express.Request, res: express.Response) => {
const name = req.params.pluginName;
const baseInfo = guildPluginInfo[name];
if (!baseInfo) {
return notFound(res);
}
const name = plugin.name;
const info = { ...(plugin.info || {}) };
const plugin = guildPlugins.find((p) => p.name === name)!;
const info = { ...baseInfo };
delete info.configSchema;
const messageCommands = (plugin.messageCommands || []).map((cmd) => ({
@ -132,7 +134,7 @@ export function initDocs(app: express.Express) {
}));
const defaultOptions = plugin.defaultOptions || {};
const configSchema = plugin.info?.configSchema && formatZodConfigSchema(plugin.info.configSchema);
const configSchema = info.configSchema && formatZodConfigSchema(info.configSchema);
res.json({
name,

View file

@ -1,4 +1,4 @@
import { ApiPermissions } from "@shared/apiPermissions";
import { ApiPermissions } from "@zeppelinbot/shared";
import express, { Request, Response } from "express";
import { YAMLException } from "js-yaml";
import moment from "moment-timezone";

View file

@ -1,4 +1,4 @@
import { ApiPermissions } from "@shared/apiPermissions";
import { ApiPermissions } from "@zeppelinbot/shared";
import express, { Request, Response } from "express";
import moment from "moment-timezone";
import { z } from "zod";

View file

@ -3,12 +3,12 @@ import { apiTokenAuthHandlers } from "../auth";
import { initGuildsImportExportAPI } from "./importExport";
import { initGuildsMiscAPI } from "./misc";
export function initGuildsAPI(app: express.Express) {
export function initGuildsAPI(router: express.Router) {
const guildRouter = express.Router();
guildRouter.use(...apiTokenAuthHandlers());
initGuildsMiscAPI(guildRouter);
initGuildsImportExportAPI(guildRouter);
app.use("/guilds", guildRouter);
router.use("/guilds", guildRouter);
}

View file

@ -1,4 +1,4 @@
import { ApiPermissions } from "@shared/apiPermissions";
import { ApiPermissions } from "@zeppelinbot/shared";
import express, { Request, Response } from "express";
import { YAMLException } from "js-yaml";
import moment from "moment-timezone";

View file

@ -1,4 +1,4 @@
import { ApiPermissions, hasPermission, permissionArrToSet } from "@shared/apiPermissions";
import { ApiPermissions, hasPermission, permissionArrToSet } from "@zeppelinbot/shared";
import { Request, Response } from "express";
import { ApiPermissionAssignments } from "../data/ApiPermissionAssignments";
import { isStaff } from "../staff";

View file

@ -10,6 +10,8 @@ import { initGuildsAPI } from "./guilds/index";
import { clientError, error, notFound } from "./responses";
import { startBackgroundTasks } from "./tasks";
const apiPathPrefix = env.API_PATH_PREFIX || (env.NODE_ENV === "development" ? "/api" : "");
const app = express();
app.use(
@ -24,16 +26,20 @@ app.use(
);
app.use(multer().none());
const rootRouter = express.Router();
initAuth(app);
initGuildsAPI(app);
initArchives(app);
initDocs(app);
// Default route
app.get("/", (req, res) => {
rootRouter.get("/", (req, res) => {
res.json({ status: "cookies", with: "milk" });
});
app.use(apiPathPrefix, rootRouter);
// Error response
// eslint-disable-next-line @typescript-eslint/no-unused-vars
app.use((err, req, res, next) => {
@ -51,7 +57,7 @@ app.use((req, res, next) => {
return notFound(res);
});
const port = env.API_PORT;
const port = 3001;
app.listen(port, "0.0.0.0", () => console.log(`API server listening on port ${port}`)); // tslint:disable-line
startBackgroundTasks();