Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
appujet committed Aug 31, 2023
1 parent 6aa2659 commit afd6055
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class DiscordDashboard {
this.app.use(express.urlencoded({ extended: true }));
this.app.use(this.errorHandler.bind(this));
this.app.use(cors({
origin: config.dashboard.website + ':' + config.dashboard.port,
origin: config.dashboard.host + ':' + config.dashboard.port,
credentials: true
}));
this.app.use(session({
Expand Down
1 change: 0 additions & 1 deletion src/api/middlewares/Middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Request, Response, NextFunction } from 'express';

export const IsAuth = (req: Request, res: Response, next: NextFunction) => {
console.log(`IsAuth: ${req.user}`);
if (req.user) {
next();
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/api/routes/MainRouter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { Router } from "express";
import AuthRouter from "./auth/AuthRouter";
import GuildsRouter from "./guilds/GuildsRouter";
import GuildRouter from "./guilds/GuildRouter";

export default class MainRouter {
public router: Router;
Expand All @@ -12,6 +13,7 @@ export default class MainRouter {
private initializeRoutes() {
this.router.use("/auth", AuthRouter);
this.router.use("/guilds", GuildsRouter);
this.router.use("/guild", GuildRouter);
}
}

5 changes: 3 additions & 2 deletions src/api/routes/auth/AuthRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router, Request, Response } from "express";
import passport from "passport";
import config from "../../../config";

class AuthRouter {
public router: Router;
Expand All @@ -13,13 +14,13 @@ class AuthRouter {
res.sendStatus(200);
});
this.router.get("/redirect", passport.authenticate("discord"), (req, res) => {
res.redirect("/dashboard");
res.redirect(`${config.dashboard.website}/dashboard`);
});
this.router.get("/me", this.getLogin);
}

private getHello(req: Request, res: Response) {
res.json({ message: "Hello World" });
res.json({ message: "lavamusic api v1 made by devblacky" });
}

private async getLogin(req: Request, res: Response) {
Expand Down
27 changes: 27 additions & 0 deletions src/api/routes/guilds/GuildRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Router, Request, Response } from "express";
import { IsAuth } from "../../middlewares/Middlewares";
import { getGuild, getGuildMembers } from "../../services/guilds/GuildServices";


class GuildRouter {
public router: Router;
constructor() {
this.router = Router();
this.initializeRoutes();
}
private initializeRoutes() {
this.router.get("/:id", IsAuth, this.getGuild);
this.router.get("/:id/members", IsAuth, this.getGuildsMembers);
}
private async getGuild(req: Request, res: Response) {
const guild = await getGuild(req.params.id);
return res.send(guild);
}
private async getGuildsMembers(req: Request, res: Response) {
const guild = await getGuildMembers(req.params.id);
return res.send(guild);
}
}

const guildRouter = new GuildRouter();
export default guildRouter.router;
4 changes: 0 additions & 4 deletions src/api/routes/guilds/GuildsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ class GuildsRouter {
private initializeRoutes() {
this.router.get("/", IsAuth, getGuildController);
}

private getHello(req: Request, res: Response) {
res.json({ message: "Hello World" });
}
}

const guildsRouter = new GuildsRouter();
Expand Down
12 changes: 12 additions & 0 deletions src/api/services/guilds/GuildServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ export async function getGuildChannels(guildId: string) {
return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}/channels`, `Bot ${config.token}`);
}

export async function getGuildRoles(guildId: string) {
return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}/roles`, `Bot ${config.token}`);
}

export async function getGuildMembers(guildId: string) {
return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}/members`, `Bot ${config.token}`);
}

export async function getGuildMember(guildId: string, userId: string) {
return await fetchRequest(`${DISCORD_API_URL}/guilds/${guildId}/members/${userId}`, `Bot ${config.token}`);
}

export async function fetchRequest(url: string, auth): Promise<any> {
const response = await fetch(url, {
method: "GET",
Expand Down
5 changes: 3 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export default {
searchEngine: process.env.SEARCH_ENGINE || (SearchEngine.YouTube as SearchEngine),
dashboard: {
port: parseInt(process.env.DASHBOARD_PORT) || 3001,
website: process.env.DASHBOARD_WEBSITE || 'http://localhost',
website: process.env.DASHBOARD_WEBSITE || 'http://localhost:3000',
host: process.env.DASHBOARD_HOST || 'http://localhost',
enable: parseBoolean(process.env.DASHBOARD_ENABLE) || true,
redirectUri: process.env.DASHBOARD_REDIRECT_URI || 'http://localhost:3000/api/auth/redirect',
redirectUri: process.env.DASHBOARD_REDIRECT_URI || 'http://localhost:3001/api/auth/redirect',
sessionSecret: "AMVCXSZXGCHGCTEAEWATRCHVKHGXHDTEWGCJGDZDS",
},
maxPlaylistSize: parseInt(process.env.MAX_PLAYLIST_SIZE) || 100,
Expand Down

0 comments on commit afd6055

Please sign in to comment.