mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-07 03:14:35 -06:00
Setup API config POST route mechanisms
This commit is contained in:
@@ -20,6 +20,10 @@ export async function fetchConfiguration(
|
|||||||
return getParsed ? parsed : json;
|
return getParsed ? parsed : json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function setConfiguration(config: any): Promise<'OK'> {
|
||||||
|
return redis.set('config', JSON.stringify(config));
|
||||||
|
}
|
||||||
|
|
||||||
export function getKey(identifier: string, now: Date) {
|
export function getKey(identifier: string, now: Date) {
|
||||||
return format(now, 'yyyy-MM-dd') + ':' + identifier;
|
return format(now, 'yyyy-MM-dd') + ':' + identifier;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +1,40 @@
|
|||||||
import type {NextApiRequest, NextApiResponse} from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import {env} from '@/env/server.mjs';
|
import { env } from '@/env/server.mjs';
|
||||||
import {fetchConfiguration} from "@/db";
|
import { fetchConfiguration, setConfiguration } from '@/db';
|
||||||
import {Configuration} from "@/timing";
|
import { Configuration, ConfigurationSchema } from '@/timing';
|
||||||
|
|
||||||
type StatusData = { status: ResponseStatus };
|
type StatusData = { status: ResponseStatus };
|
||||||
|
|
||||||
type ResponseStatus =
|
type ResponseStatus = 'unauthorized' | 'invalid' | 'failed' | 'success';
|
||||||
| 'unauthorized'
|
|
||||||
| 'failed'
|
|
||||||
| 'success';
|
|
||||||
|
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse<StatusData | Configuration>
|
res: NextApiResponse<StatusData | Configuration>
|
||||||
) {
|
) {
|
||||||
if (req.query.key != env.API_KEY) {
|
if (req.query.key != env.API_KEY) {
|
||||||
// auth failed
|
// auth failed
|
||||||
res.status(401).json({status: 'unauthorized'});
|
res.status(401).json({ status: 'unauthorized' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method == "GET") {
|
if (req.method == 'GET') {
|
||||||
try {
|
try {
|
||||||
const configuration = await fetchConfiguration();
|
const configuration = await fetchConfiguration();
|
||||||
res.status(200).json(configuration);
|
res.status(200).json(configuration);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
res.status(500).json({ status: 'failed' });
|
||||||
res.status(500).json({status: "failed"});
|
}
|
||||||
}
|
} else if (req.method == 'POST') {
|
||||||
} else if (req.method == "POST") {
|
const json = JSON.parse(req.body);
|
||||||
res.status(200).json({status: 'success'});
|
const parsed = ConfigurationSchema.safeParse(json);
|
||||||
}
|
if (parsed.success) {
|
||||||
|
try {
|
||||||
|
await setConfiguration(json);
|
||||||
|
res.status(200).json({ status: 'success' });
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
res.status(500).json({ status: 'failed' });
|
||||||
|
}
|
||||||
|
} else res.status(400).json({ status: 'invalid' });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user