feat: implementing CORS protection and logging
This commit is contained in:
3
packages/backend/src/env/index.ts
vendored
3
packages/backend/src/env/index.ts
vendored
@ -7,10 +7,11 @@ const envSchema = z.object({
|
|||||||
PORT: z
|
PORT: z
|
||||||
.string()
|
.string()
|
||||||
.refine(
|
.refine(
|
||||||
(port) => parseInt(port) > 0 && parseInt(port) < 65536,
|
(port) => parseInt(port, 10) > 0 && parseInt(port, 10) < 65536,
|
||||||
"Invalid port number",
|
"Invalid port number",
|
||||||
),
|
),
|
||||||
DATABASE_URL: z.string().min(10),
|
DATABASE_URL: z.string().min(10),
|
||||||
|
FRONTEND_URL: z.url(),
|
||||||
});
|
});
|
||||||
|
|
||||||
type Env = z.infer<typeof envSchema>;
|
type Env = z.infer<typeof envSchema>;
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
import { ENV } from "./env";
|
import { ENV } from "./env";
|
||||||
import { getPetitions } from "./routes/get-petitions";
|
import { getPetitions } from "./routes/get-petitions";
|
||||||
import { signPetition } from "./routes/sign-petition";
|
import { signPetition } from "./routes/sign-petition";
|
||||||
|
import { styleText } from "node:util";
|
||||||
|
|
||||||
const CORS_HEADERS = {
|
const CORS_HEADERS = {
|
||||||
"Access-Control-Allow-Origin": "*",
|
"Access-Control-Allow-Origin": ENV.FRONTEND_URL,
|
||||||
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
|
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
|
||||||
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
||||||
};
|
};
|
||||||
@ -26,14 +27,44 @@ const withCors = (fn: Handler): Handler => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getColors = (status: number): Parameters<typeof styleText>[0] => {
|
||||||
|
if (status >= 200 && status < 300) {
|
||||||
|
return ["bgGreen", "white"];
|
||||||
|
} else if (status >= 300 && status < 400) {
|
||||||
|
return ["bgYellow", "white"];
|
||||||
|
} else if (status >= 400 && status < 500) {
|
||||||
|
return ["bgRed", "white"];
|
||||||
|
} else if (status >= 500) {
|
||||||
|
return ["bgRedBright", "white"];
|
||||||
|
} else {
|
||||||
|
return ["bgBlack", "white"];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const withLogger = (fn: Handler): Handler => {
|
||||||
|
return async (req) => {
|
||||||
|
const res = await fn(req);
|
||||||
|
|
||||||
|
const code = res.status;
|
||||||
|
|
||||||
|
const styles = getColors(code);
|
||||||
|
const codeText = styleText(styles, `${code}`);
|
||||||
|
|
||||||
|
const msg = `${codeText}: ${req.method} ${req.url}`;
|
||||||
|
console.log(msg);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const server = Bun.serve({
|
const server = Bun.serve({
|
||||||
port: ENV.PORT,
|
port: ENV.PORT,
|
||||||
routes: {
|
routes: {
|
||||||
"/health": new Response("alive!"),
|
"/health": new Response("alive!"),
|
||||||
"/sign": {
|
"/sign": {
|
||||||
GET: withCors(getPetitions),
|
GET: withLogger(withCors(getPetitions)),
|
||||||
POST: withCors(signPetition),
|
POST: withLogger(withCors(signPetition)),
|
||||||
OPTIONS: allowCors,
|
OPTIONS: withLogger(allowCors),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user