-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroot-controller.ts
127 lines (117 loc) · 3.91 KB
/
root-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* THis is the module page of the root-controller.
*
* @module internal/controllers
*/
import { Request, Response } from 'express';
import log4js, { Logger } from 'log4js';
import BaseController, { BaseControllerOptions } from './base-controller';
import Policy from './policy';
import { parseRequestPagination } from '../helpers/pagination';
import BannerService from '../service/banner-service';
import ServerSettingsStore from '../server-settings/server-settings-store';
import { ServerStatusResponse } from './response/server-status-response';
export default class RootController extends BaseController {
/**
* Reference to the logger instance.
*/
private logger: Logger = log4js.getLogger('RootController');
/**
* Creates a new root controller instance.
* @param options - The options passed to the base controller.
*/
public constructor(options: BaseControllerOptions) {
super(options);
this.logger.level = process.env.LOG_LEVEL;
}
/**
* @inheritDoc
*/
public getPolicy(): Policy {
return {
'/ping': {
GET: {
policy: async () => Promise.resolve(true),
handler: this.ping.bind(this),
restrictions: { availableDuringMaintenance: true },
},
},
'/open/banners': {
GET: {
policy: async () => true,
handler: this.returnAllBanners.bind(this),
},
},
};
}
/**
* GET /open/banners
* @summary Returns all existing banners
* @operationId getAllOpenBanners
* @tags banners - Operations of banner controller
* @param {integer} take.query - How many banners the endpoint should return
* @param {integer} skip.query - How many banners should be skipped (for pagination)
* @return {PaginatedBannerResponse} 200 - All existing banners
* @return {string} 400 - Validation error
* @return {string} 500 - Internal server error
*/
public async returnAllBanners(req: Request, res: Response): Promise<void> {
this.logger.trace('Get all banners by', req.ip);
let take;
let skip;
try {
const pagination = parseRequestPagination(req);
take = pagination.take;
skip = pagination.skip;
} catch (e) {
res.status(400).send(e.message);
return;
}
// handle request
try {
res.json(await BannerService.getBanners({}, { take, skip }));
} catch (error) {
this.logger.error('Could not return all banners:', error);
res.status(500).json('Internal server error.');
}
}
/**
* GET /ping
* @summary Get the current status of the backend
* @operationId ping
* @tags root - Operations of the root controller
* @return {ServerStatusResponse} 200 - Success
* @return {string} 500 - Internal server error
*/
public async ping(req: Request, res: Response): Promise<void> {
this.logger.trace('Ping by', req.ip);
try {
const store = ServerSettingsStore.getInstance();
const maintenanceMode = await store.getSettingFromDatabase('maintenanceMode');
res.status(200).json({
maintenanceMode,
} as ServerStatusResponse);
} catch (e) {
res.status(500).json('Internal server error.');
}
}
}