Add ?current=1 support for /api/config

This commit is contained in:
2023-06-25 16:40:12 +08:00
parent 01d364a1ec
commit 627b38d85d
3 changed files with 20 additions and 10 deletions

View File

@@ -1,6 +1,8 @@
import { Handlers } from "$fresh/server.ts";
import { ConfigType, load_settings, save_settings } from "../../config.ts";
import { get_cfg_path, get_task_manager } from "../../server.ts";
import { parse_bool } from "../../server/parse_form.ts";
import { return_json } from "../../server/utils.ts";
const UNSAFE_TYPE: (keyof ConfigType)[] = [
"base",
@@ -14,12 +16,16 @@ const UNSAFE_TYPE: (keyof ConfigType)[] = [
const UNSAFE_TYPE2 = UNSAFE_TYPE as string[];
export const handler: Handlers = {
async GET(_req, _ctx) {
async GET(req, _ctx) {
const u = new URL(req.url);
const current = await parse_bool(u.searchParams.get("current"), false);
if (current) {
const t = get_task_manager();
return return_json(t.cfg.to_json());
}
const path = get_cfg_path();
const cfg = await load_settings(path);
return new Response(JSON.stringify(cfg.to_json()), {
headers: { "Content-Type": "application/json" },
});
return return_json(cfg.to_json());
},
async POST(req, _ctx) {
const content_type = req.headers.get("Content-Type");
@@ -39,9 +45,7 @@ export const handler: Handlers = {
}
});
await save_settings(path, cfg, m.force_aborts);
return new Response(JSON.stringify({ is_unsafe }), {
headers: { "Content-Type": "application/json" },
});
return return_json({ is_unsafe });
} else {
return new Response("Bad Request", { status: 400 });
}

View File

@@ -1,10 +1,9 @@
import { Handlers } from "$fresh/server.ts";
import { return_json } from "../../server/utils.ts";
export const handler: Handlers = {
GET(_req, _ctx) {
const data = { id: Deno.env.get("DENO_DEPLOYMENT_ID") };
return new Response(JSON.stringify(data), {
headers: { "Content-Type": "application/json" },
});
return return_json(data);
},
};

View File

@@ -33,3 +33,10 @@ export function return_error<T = unknown>(
export function return_data<T = unknown>(data: T, status = 200) {
return gen_response<T>({ ok: true, status: 0, data }, status);
}
export function return_json<T = unknown>(data: T, status = 200) {
return new Response(JSON.stringify(data), {
status,
headers: { "Content-Type": "application/json" },
});
}