-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathindex.ts
More file actions
59 lines (51 loc) · 1.64 KB
/
Copy pathindex.ts
File metadata and controls
59 lines (51 loc) · 1.64 KB
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
import { Hono } from "hono";
import {
type ScheduledController,
type ExecutionContext,
} from "@cloudflare/workers-types";
import { timingSafeEqual } from "node:crypto";
import { cors } from "hono/cors";
import { env } from "./env";
import { doBackup } from "./functions";
interface Env {}
// api stuff
const api = new Hono();
// middleware
api.use("/backup/*", cors()).use("/backup/*", async (c, next) => {
// grab the bearer token from the request headers
const authToken = c.req.header("Authorization") || "";
const encoder = new TextEncoder();
const expected = `Bearer ${env.BACKUPS_SECRET_KEY}`;
if (
authToken.length !== expected.length ||
!timingSafeEqual(encoder.encode(authToken), encoder.encode(expected))
) {
return c.json({ error: "Unauthorized" }, 401);
}
await next();
});
api.get("/health", (c) => {
return c.json({ status: "ok" }, 200);
});
api.post("/backup", async (c) => {
await doBackup();
return c.json({ status: "Backup completed successfully" }, 200);
});
// cron stuff
/**
* Notes for the future: no specific job schedule is specified here as in the future,
* the ideal would be to have an endpoint that takes in a schedule and an instance (of hackkit, clubkit, etc) and allow the endpoint to use the cloudflare API to
* modify the builld itself and assign jobs that way.
* Basically you would have a db table that would hold the instance ID, and the schedule and another one for the unique cron entires and then tie them together.
*/
const cron = async (
_controller: ScheduledController,
_env: Env,
ctx: ExecutionContext,
) => {
ctx.waitUntil(doBackup());
};
export default {
fetch: api.fetch,
scheduled: cron,
};