start backend work

This commit is contained in:
Lee
2024-10-08 15:32:02 +01:00
parent 04ce91b459
commit aa0a0c4c16
445 changed files with 367 additions and 11413 deletions

View File

@ -0,0 +1,10 @@
/**
* Gets the app version.
*/
export function getAppVersion() {
if (!process.env.APP_VERSION) {
const packageJson = require("../../package.json");
process.env.APP_VERSION = packageJson.version;
}
return process.env.APP_VERSION + "-" + (process.env.GIT_REV?.substring(0, 7) ?? "dev");
}

View File

@ -0,0 +1,13 @@
import { Controller, Get } from "elysia-decorators";
import { getAppVersion } from "../common/app-utils";
@Controller("/")
export default class AppController {
@Get()
public index() {
return {
app: "backend",
version: getAppVersion(),
};
}
}

View File

@ -0,0 +1,53 @@
import { Elysia } from "elysia";
import cors from "@elysiajs/cors";
import { decorators } from "elysia-decorators";
import { logger } from "@tqman/nice-logger";
import AppController from "./controller/app";
const app = new Elysia();
/**
* Custom error handler
*/
app.onError({ as: "global" }, ({ code, error }) => {
// Return default error for type validation
if (code === "VALIDATION") {
return error.all;
}
let status = "status" in error ? error.status : undefined;
return {
...((status && { statusCode: status }) || { status: code }),
...(error.message != code && { message: error.message }),
timestamp: new Date().toISOString(),
};
});
/**
* Enable CORS
*/
app.use(cors());
/**
* Request logger
*/
app.use(
logger({
mode: "combined",
})
);
/**
* Controllers
*/
app.use(
decorators({
controllers: [AppController],
})
);
app.onStart(() => {
console.log("Listening on port http://localhost:8080");
});
app.listen(8080);