Refactor project to transition from Rust backend to Node.js with Fastify; update Dockerfile and Docker configurations for new build process; enhance local development instructions in CONTRIBUTING.md; implement health checks in Docker Compose; update pnpm-lock.yaml with new dependencies for API and shared packages; revise README.md to reflect new stack and development setup.
Build, Test, and Push CFDM Docker Image / test (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / build-and-push (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / create-release (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / update-wiki (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / test (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / build-and-push (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / create-release (push) Has been cancelled
Build, Test, and Push CFDM Docker Image / update-wiki (push) Has been cancelled
This commit is contained in:
1 parent
0a0a1a92f1
commit
d11414666f
2065 files changed
+15782
-16699
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||
import fp from "fastify-plugin";
|
||||
import type { AppConfig } from "../config.js";
|
||||
import { AppError } from "../errors.js";
|
||||
|
||||
async function authPlugin(
|
||||
app: FastifyInstance,
|
||||
opts: { config: AppConfig },
|
||||
) {
|
||||
await app.register(import("@fastify/jwt"), {
|
||||
secret: opts.config.jwtSecret,
|
||||
});
|
||||
}
|
||||
|
||||
export async function requireAuth(request: FastifyRequest): Promise<void> {
|
||||
const authHeader = request.headers.authorization ?? "";
|
||||
const token = authHeader.startsWith("Bearer ")
|
||||
? authHeader.slice(7)
|
||||
: "";
|
||||
if (!token) throw AppError.unauthorized();
|
||||
try {
|
||||
await request.jwtVerify();
|
||||
} catch {
|
||||
throw AppError.unauthorized();
|
||||
}
|
||||
}
|
||||
|
||||
export default fp(authPlugin, { name: "auth" });
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import fp from "fastify-plugin";
|
||||
import { CloudflareClient } from "../lib/cf-client.js";
|
||||
import type { AppConfig } from "../config.js";
|
||||
|
||||
declare module "fastify" {
|
||||
interface FastifyInstance {
|
||||
cf: CloudflareClient;
|
||||
config: AppConfig;
|
||||
}
|
||||
}
|
||||
|
||||
async function cfClientPlugin(
|
||||
app: FastifyInstance,
|
||||
opts: { config: AppConfig },
|
||||
) {
|
||||
app.decorate("config", opts.config);
|
||||
app.decorate("cf", new CloudflareClient(opts.config.cloudflareApiToken));
|
||||
}
|
||||
|
||||
export default fp(cfClientPlugin, { name: "cf-client" });
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import fp from "fastify-plugin";
|
||||
|
||||
async function corsPlugin(app: FastifyInstance) {
|
||||
await app.register(import("@fastify/cors"), {
|
||||
origin: true,
|
||||
methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
|
||||
allowedHeaders: ["Content-Type", "Authorization"],
|
||||
});
|
||||
}
|
||||
|
||||
export default fp(corsPlugin, { name: "cors" });
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import fp from "fastify-plugin";
|
||||
import {
|
||||
createDb,
|
||||
createMemoryDb,
|
||||
healthCheck,
|
||||
runMigrations,
|
||||
type Db,
|
||||
type Sqlite,
|
||||
} from "@cfdm/db";
|
||||
import type { AppConfig } from "../config.js";
|
||||
|
||||
declare module "fastify" {
|
||||
interface FastifyInstance {
|
||||
db: Db;
|
||||
sqlite: Sqlite;
|
||||
}
|
||||
}
|
||||
|
||||
export interface DbPluginOptions {
|
||||
config?: AppConfig;
|
||||
memory?: boolean;
|
||||
}
|
||||
|
||||
async function dbPlugin(
|
||||
app: FastifyInstance,
|
||||
opts: DbPluginOptions,
|
||||
) {
|
||||
const { db, sqlite } = opts.memory
|
||||
? createMemoryDb()
|
||||
: createDb(opts.config!.databaseUrl);
|
||||
|
||||
runMigrations(sqlite);
|
||||
app.decorate("db", db);
|
||||
app.decorate("sqlite", sqlite);
|
||||
|
||||
app.addHook("onClose", async () => {
|
||||
sqlite.close();
|
||||
});
|
||||
}
|
||||
|
||||
export default fp(dbPlugin, { name: "db" });
|
||||
|
||||
export { healthCheck };
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import fp from "fastify-plugin";
|
||||
import { AppError, errorBody, toAppError } from "../errors.js";
|
||||
|
||||
async function errorHandlerPlugin(app: FastifyInstance) {
|
||||
app.setErrorHandler((err, _request, reply) => {
|
||||
if (reply.sent) return;
|
||||
|
||||
const appErr =
|
||||
err.statusCode === 401
|
||||
? AppError.unauthorized()
|
||||
: toAppError(err);
|
||||
|
||||
reply.status(appErr.statusCode).send(errorBody(appErr));
|
||||
});
|
||||
}
|
||||
|
||||
export default fp(errorHandlerPlugin, { name: "error-handler" });
|
||||
Reference in new issue
Block a user