-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Open-Webtoon-Reader/improvement/admin-routes
Merge admin routes to Main
- Loading branch information
Showing
4 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,6 @@ SSL_CERT_FILE="" | |
|
||
# API | ||
PREFIX="api/v1/" | ||
|
||
# Security | ||
ADMIN_KEY="admin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,66 @@ | ||
import {Body, Controller, Post} from "@nestjs/common"; | ||
import {ApiResponse, ApiTags} from "@nestjs/swagger"; | ||
import {Body, Controller, Delete, Get, HttpCode, Post, UseGuards} from "@nestjs/common"; | ||
import {ApiBearerAuth, ApiResponse, ApiTags} from "@nestjs/swagger"; | ||
import {DownloadManagerService} from "../webtoon/download-manager.service"; | ||
import {AddWebtoonToQueueDto} from "./models/dto/add-webtoon-to-queue.dto"; | ||
import {HttpStatusCode} from "axios"; | ||
import CachedWebtoonModel from "../webtoon/models/models/cached-webtoon.model"; | ||
import {AdminGuard} from "./guard/admin.guard"; | ||
|
||
|
||
@Controller("admin") | ||
@ApiTags("Admin") | ||
@UseGuards(AdminGuard) | ||
export class AdminController{ | ||
|
||
constructor( | ||
private readonly downloadManagerService: DownloadManagerService, | ||
){} | ||
|
||
@Post("queue") | ||
@ApiResponse({status: HttpStatusCode.Ok, description: "Adds a webtoon to the download queue"}) | ||
@ApiBearerAuth() | ||
@ApiResponse({status: HttpStatusCode.Created, description: "Adds a webtoon to the download queue"}) | ||
@ApiResponse({status: HttpStatusCode.TooEarly, description: "Cache not loaded."}) | ||
async addWebtoonToQueue(@Body() addWebtoonToQueueDto: AddWebtoonToQueueDto): Promise<void>{ | ||
return this.downloadManagerService.addWebtoonToQueue(addWebtoonToQueueDto.name, addWebtoonToQueueDto.language); | ||
} | ||
|
||
@Post("update/all") | ||
@ApiResponse({status: HttpStatusCode.Ok, description: "Updates all webtoons in the database"}) | ||
@ApiBearerAuth() | ||
@ApiResponse({status: HttpStatusCode.Created, description: "Updates all webtoons in the database"}) | ||
@ApiResponse({status: HttpStatusCode.TooEarly, description: "Cache not loaded."}) | ||
async updateAllWebtoons(): Promise<void>{ | ||
return this.downloadManagerService.updateAllWebtoons(); | ||
} | ||
|
||
@Get("current-download") | ||
@ApiBearerAuth() | ||
@ApiResponse({status: HttpStatusCode.Ok, description: "Returns the current download"}) | ||
@ApiResponse({status: HttpStatusCode.NotFound, description: "No download in progress"}) | ||
async getCurrentDownload(): Promise<CachedWebtoonModel>{ | ||
return this.downloadManagerService.getCurrentDownload(); | ||
} | ||
|
||
@Get("queue") | ||
@ApiBearerAuth() | ||
@ApiResponse({status: HttpStatusCode.Ok, description: "Returns the current download queue"}) | ||
@ApiResponse({status: HttpStatusCode.NotFound, description: "No download in progress"}) | ||
async getQueue(): Promise<CachedWebtoonModel[]>{ | ||
return this.downloadManagerService.getDownloadQueue(); | ||
} | ||
|
||
@Delete("current-download") | ||
@ApiBearerAuth() | ||
@HttpCode(HttpStatusCode.NoContent) | ||
@ApiResponse({status: HttpStatusCode.NoContent, description: "Cancels the current download"}) | ||
async cancelCurrentDownload(): Promise<void>{ | ||
return this.downloadManagerService.skipCurrentDownload(); | ||
} | ||
|
||
@Delete("queue") | ||
@ApiBearerAuth() | ||
@HttpCode(HttpStatusCode.NoContent) | ||
@ApiResponse({status: HttpStatusCode.NoContent, description: "Clears the download queue"}) | ||
async clearQueue(): Promise<void>{ | ||
return this.downloadManagerService.clearDownloadQueue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {CanActivate, ExecutionContext, Injectable, UnauthorizedException} from "@nestjs/common"; | ||
import {ConfigService} from "@nestjs/config"; | ||
|
||
@Injectable() | ||
export class AdminGuard implements CanActivate{ | ||
constructor( | ||
private readonly configService: ConfigService, | ||
){} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean>{ | ||
const adminKey = this.configService.get("ADMIN_KEY"); | ||
const request = context.switchToHttp().getRequest(); | ||
const token = request.headers.authorization?.split(" ")[1]; | ||
if(!token) | ||
throw new UnauthorizedException("No token provided"); | ||
if(token !== adminKey) | ||
throw new UnauthorizedException("Invalid token"); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters