start backend work
This commit is contained in:
10
projects/backend/src/common/app-utils.ts
Normal file
10
projects/backend/src/common/app-utils.ts
Normal 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");
|
||||
}
|
13
projects/backend/src/controller/app.ts
Normal file
13
projects/backend/src/controller/app.ts
Normal 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(),
|
||||
};
|
||||
}
|
||||
}
|
53
projects/backend/src/index.ts
Normal file
53
projects/backend/src/index.ts
Normal 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);
|
Reference in New Issue
Block a user