Add appState

appState contains variables that indicate whether an event is
currently ongoing, and whether registration is currently allowed.
This commit is contained in:
Phillip Kühne 2020-10-03 23:41:45 +02:00
parent 80682ab883
commit 9b31131d5b
4 changed files with 88 additions and 19 deletions

View File

@ -0,0 +1,14 @@
import AppState from "../models/appState.model";
import fs from "fs";
const appState = new AppState();
if (fs.existsSync("/tmp/.kqueue_eventlock")) {
appState.currentlyInEvent=true;
} else {
appState.currentlyInEvent=false;
}
appState.registrationEnabled=false;
export default appState;

View File

@ -1,4 +1,7 @@
import { Controller, Get, Res, Post, Delete, Patch, Req } from "routing-controllers";
import { Request, Response } from "express";
import { Controller, Get, Res, Post, Delete, Patch, Req, Authorized, CurrentUser } from "routing-controllers";
import appState from "../containers/appState.container";
import DataStoredInToken from "../interfaces/dataStoredInToken.interface";
@Controller("/queue")
@ -8,25 +11,33 @@ export class QueueController {
* Fetch entry Queue content
*/
@Get()
getQueue(@Req() req: any, @Res() res: any) {
getQueue(@Req() req: Request, @Res() res: Response) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ placeholder: "//TODO fetch" }));
res.send(JSON.stringify({ placeholder: "//TODO fetch" }));
return res;
}
/*
* Add entry to Queue
*/
@Post()
addEntry(@Req() req: any, @Res() res: any) {
addEntry(@Req() req: Request, @Res() res: Response) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ placeholder: "//TODO add" }));
if (appState.registrationEnabled) {
res.send(JSON.stringify({ placeholder: "//TODO add" }));
} else {
res.status(403)
res.send("Entry submission is currently not allowed.")
}
return res;
}
/*
*
*/
@Delete()
clearQueue(@Req() req: any, @Res() res: any) {
@Authorized()
clearQueue(@Req() req: Request, @Res() res: Response) {
return "//TODO clear";
}
@ -34,21 +45,32 @@ export class QueueController {
*
*/
@Get("/:entry:id")
getEntry(@Req() req: any, @Res() res: any) {
getEntry(@Req() req: Request, @Res() res: Response) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ placeholder: "//TODO get" }));
res.send(JSON.stringify({ placeholder: "//TODO get" }));
return res;
}
/*
*
*/
@Patch("/:entry_id")
editEntry(@Req() req: any, @Res() res: any) {
return "//TODO edit"
editEntry(@Req() req: Request, @Res() res: Response, @CurrentUser() user?: DataStoredInToken) {
if (user) {
return "You're "+user._id;
} else {
/*TODO: Require song-specific auth to modify queue entry */
return "You are not logged in."
}
}
@Delete("/:entry_id")
deleteEntry(@Req() req: any, @Res() res: any) {
return "//TODO delete"
deleteEntry(@Req() req: Request, @Res() res: Response, @CurrentUser() user?: DataStoredInToken) {
if (user) {
return "You're " + user._id;
} else {
/*TODO: Require song-specific auth to delete queue entry */
return "You are not logged in."
}
}
}

View File

@ -1,25 +1,52 @@
import { Controller, Get, Param, QueryParam } from "routing-controllers";
import { Response } from "express";
import { Authorized, Controller, Get, Param, QueryParam, Res } from "routing-controllers";
import appState from "../containers/appState.container";
import fs from "fs";
@Controller("/rpc")
@Authorized()
export class RpcController {
@Get("/start_event")
doStartEvent() {
return "//TODO start_event"
/* TODO: Wipe state (queue and song playbacks) when starting new event. Maybe also refresh Song database automatically? */
appState.currentlyInEvent = true;
fs.openSync("/tmp/.kqueue_eventlock", 'w')
return 200;
}
@Get("/end_event")
doEndEvent() {
return "//TODO end_event"
appState.currentlyInEvent = false;
fs.unlinkSync("/tmp/.kqueue_eventlock");
return 200;
}
@Get("/enable_registration")
doEnableRegistration() {
return "//TODO enable_registration"
doEnableRegistration(@Res() res: Response) {
if (appState.currentlyInEvent) {
appState.registrationEnabled = true;
return 200;
} else {
res.status(403).send("No event currently active")
return res;
}
}
@Get("/disable_registration")
doDisableRegistration() {
return "//TODO disable_registration"
doDisableRegistration(@Res() res: Response) {
if (appState.currentlyInEvent) {
appState.registrationEnabled = false;
return 200;
} else {
res.status(403).send("No event currently active")
return res;
}
}
@Get("/get_state")
doGetState() {
return appState;
}
@Get("/get_playstats")

View File

@ -0,0 +1,6 @@
class AppState {
registrationEnabled: boolean;
currentlyInEvent: boolean;
}
export default AppState;