mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
Add logging options
This commit is contained in:
parent
aa1ffe5b9b
commit
76b1b8c3b4
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
|
|
||||||
logs
|
logs/*
|
||||||
_.log
|
_.log
|
||||||
npm-debug.log_
|
npm-debug.log_
|
||||||
yarn-debug.log*
|
yarn-debug.log*
|
||||||
|
|
|
||||||
39
index.ts
39
index.ts
|
|
@ -1,4 +1,5 @@
|
||||||
import { getConfig } from "@config";
|
import { getConfig } from "@config";
|
||||||
|
import { appendFile } from "fs/promises";
|
||||||
import "reflect-metadata";
|
import "reflect-metadata";
|
||||||
import { AppDataSource } from "~database/datasource";
|
import { AppDataSource } from "~database/datasource";
|
||||||
|
|
||||||
|
|
@ -10,6 +11,12 @@ const router = new Bun.FileSystemRouter({
|
||||||
console.log("[+] Starting FediProject...");
|
console.log("[+] Starting FediProject...");
|
||||||
|
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
|
const requests_log = Bun.file(process.cwd() + "/logs/requests.log");
|
||||||
|
|
||||||
|
if (!(await requests_log.exists())) {
|
||||||
|
console.log("[+] requests.log does not exist, creating it...");
|
||||||
|
await Bun.write(process.cwd() + "/logs/requests.log", "");
|
||||||
|
}
|
||||||
|
|
||||||
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
|
if (!AppDataSource.isInitialized) await AppDataSource.initialize();
|
||||||
|
|
||||||
|
|
@ -17,6 +24,38 @@ Bun.serve({
|
||||||
port: config.http.port,
|
port: config.http.port,
|
||||||
hostname: config.http.base_url || "0.0.0.0", // defaults to "0.0.0.0"
|
hostname: config.http.base_url || "0.0.0.0", // defaults to "0.0.0.0"
|
||||||
async fetch(req) {
|
async fetch(req) {
|
||||||
|
if (config.logging.log_requests_verbose) {
|
||||||
|
await appendFile(
|
||||||
|
`${process.cwd()}/logs/requests.log`,
|
||||||
|
`[${new Date().toISOString()}] ${req.method} ${
|
||||||
|
req.url
|
||||||
|
}\n\tHeaders:\n`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Add headers
|
||||||
|
|
||||||
|
const headers = req.headers.entries();
|
||||||
|
|
||||||
|
for (const [key, value] of headers) {
|
||||||
|
await appendFile(
|
||||||
|
`${process.cwd()}/logs/requests.log`,
|
||||||
|
`\t\t${key}: ${value}\n`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await req.clone().text();
|
||||||
|
|
||||||
|
await appendFile(
|
||||||
|
`${process.cwd()}/logs/requests.log`,
|
||||||
|
`\tBody:\n\t${body}\n`
|
||||||
|
);
|
||||||
|
} else if (config.logging.log_requests) {
|
||||||
|
await appendFile(
|
||||||
|
process.cwd() + "/logs/requests.log",
|
||||||
|
`[${new Date().toISOString()}] ${req.method} ${req.url}\n`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const matchedRoute = router.match(req);
|
const matchedRoute = router.match(req);
|
||||||
|
|
||||||
if (matchedRoute) {
|
if (matchedRoute) {
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,20 @@ export interface ConfigType {
|
||||||
activitypub: {
|
activitypub: {
|
||||||
use_tombstones: boolean;
|
use_tombstones: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
filters: {
|
||||||
|
note_filters: string[];
|
||||||
|
username_filters: string[];
|
||||||
|
displayname_filters: string[];
|
||||||
|
bio_filters: string[];
|
||||||
|
emoji_filters: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
logging: {
|
||||||
|
log_requests: boolean;
|
||||||
|
log_requests_verbose: boolean;
|
||||||
|
log_filters: boolean;
|
||||||
|
};
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -115,6 +129,18 @@ export const configDefaults: ConfigType = {
|
||||||
activitypub: {
|
activitypub: {
|
||||||
use_tombstones: true,
|
use_tombstones: true,
|
||||||
},
|
},
|
||||||
|
filters: {
|
||||||
|
note_filters: [],
|
||||||
|
username_filters: [],
|
||||||
|
displayname_filters: [],
|
||||||
|
bio_filters: [],
|
||||||
|
emoji_filters: [],
|
||||||
|
},
|
||||||
|
logging: {
|
||||||
|
log_requests: false,
|
||||||
|
log_requests_verbose: false,
|
||||||
|
log_filters: true,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getConfig = () => {
|
export const getConfig = () => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue