start backend work
This commit is contained in:
2
projects/backend/.dockerignore
Normal file
2
projects/backend/.dockerignore
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
dist
|
42
projects/backend/.gitignore
vendored
Normal file
42
projects/backend/.gitignore
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# next.js
|
||||
/.next/
|
||||
/out/
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
*.pem
|
||||
|
||||
# debug
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# local env files
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
# vercel
|
||||
.vercel
|
||||
|
||||
**/*.trace
|
||||
**/*.zip
|
||||
**/*.tar.gz
|
||||
**/*.tgz
|
||||
**/*.log
|
||||
package-lock.json
|
||||
**/*.bun
|
19
projects/backend/Dockerfile
Normal file
19
projects/backend/Dockerfile
Normal file
@ -0,0 +1,19 @@
|
||||
FROM imbios/bun-node AS base
|
||||
|
||||
# Install dependencies
|
||||
FROM base AS depends
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN bun install --frozen-lockfile
|
||||
|
||||
# Run the app
|
||||
FROM base AS runner
|
||||
WORKDIR /app
|
||||
|
||||
ENV NODE_ENV production
|
||||
|
||||
COPY --from=depends /app/node_modules ./node_modules
|
||||
COPY --from=depends /app/package.json* /app/bun.lockb* ./
|
||||
COPY --from=depends /app/projects/backend ./projects/backend
|
||||
|
||||
CMD ["bun", "run", "--filter", "backend", "start"]
|
9
projects/backend/README.md
Normal file
9
projects/backend/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# Backend
|
||||
|
||||
## Development
|
||||
To start the development server run:
|
||||
```bash
|
||||
bun run dev
|
||||
```
|
||||
|
||||
Open http://localhost:3000/ with your browser to see the result.
|
21
projects/backend/package.json
Normal file
21
projects/backend/package.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "backend",
|
||||
"version": "1.0.0",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"dev": "bun run --watch src/index.ts",
|
||||
"start": "bun run src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@elysiajs/cors": "^1.1.1",
|
||||
"@ssr/common": "workspace:common",
|
||||
"@tqman/nice-logger": "^1.0.1",
|
||||
"elysia": "latest",
|
||||
"elysia-autoroutes": "^0.5.0",
|
||||
"elysia-decorators": "^1.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bun-types": "latest"
|
||||
},
|
||||
"module": "src/index.js"
|
||||
}
|
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);
|
12
projects/backend/tsconfig.json
Normal file
12
projects/backend/tsconfig.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2021",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "node",
|
||||
"types": ["bun-types"],
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user