initial commit
This commit is contained in:
5
packages/server/nodemon.json
Normal file
5
packages/server/nodemon.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"watch": ["src"],
|
||||
"ext": "ts",
|
||||
"exec": "concurrently \"npx tsc --watch\" \"ts-node src/index.ts\""
|
||||
}
|
||||
36
packages/server/package.json
Normal file
36
packages/server/package.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"name": "egret-app-server",
|
||||
"version": "0.1.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "tsc && tsc-alias",
|
||||
"serve": "node ./build/index.js",
|
||||
"start": "nodemon"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"compression": "^1.7.5",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.21.2",
|
||||
"express-oas-generator": "^1.0.48",
|
||||
"nocache": "^4.0.0",
|
||||
"sequelize": "^6.37.7",
|
||||
"sqlite3": "^5.1.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/compression": "^1.7.5",
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/swagger-ui-express": "^4.1.8",
|
||||
"nodemon": "^3.1.7",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsc-alias": "^1.8.10",
|
||||
"tsconfig-paths": "^4.2.0"
|
||||
},
|
||||
"resolutions": {
|
||||
"@types/express": "^4.17.13"
|
||||
}
|
||||
}
|
||||
12
packages/server/src/api/index.ts
Normal file
12
packages/server/src/api/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Express } from "express";
|
||||
|
||||
export const createApis = (app: Express) => {
|
||||
app.get("/api/test", async (req, res) => {
|
||||
res.status(200).json({
|
||||
message: "success",
|
||||
data: null,
|
||||
});
|
||||
});
|
||||
|
||||
/** add apis here */
|
||||
};
|
||||
20
packages/server/src/database/index.ts
Normal file
20
packages/server/src/database/index.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import path from "path";
|
||||
import { Sequelize } from "sequelize";
|
||||
|
||||
export const sequelize = new Sequelize({
|
||||
dialect: "sqlite",
|
||||
storage: path.resolve(__dirname, process.env.DB_PATH || "./data.sqlite"),
|
||||
});
|
||||
|
||||
async function initializeDatabase() {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
/**
|
||||
* add database table here
|
||||
*/
|
||||
} catch (error) {
|
||||
console.error("connect db error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
initializeDatabase();
|
||||
55
packages/server/src/index.ts
Normal file
55
packages/server/src/index.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import dotenv from "dotenv";
|
||||
import express from "express";
|
||||
import cors from "cors";
|
||||
import compression from "compression";
|
||||
import nocache from "nocache";
|
||||
import path from "path";
|
||||
import expressOasGenerator, {
|
||||
OpenAPIV3,
|
||||
SPEC_OUTPUT_FILE_BEHAVIOR,
|
||||
} from "express-oas-generator";
|
||||
|
||||
dotenv.config({ path: path.resolve(__dirname, "../../../.env") });
|
||||
|
||||
import "./database";
|
||||
import { createApis } from "./api";
|
||||
|
||||
const port = process.env.PORT || 3005;
|
||||
|
||||
const app = express();
|
||||
app.set("etag", false);
|
||||
app.use(nocache());
|
||||
app.use(cors());
|
||||
app.use(express.json({ limit: "100mb" }));
|
||||
app.use(compression());
|
||||
|
||||
app.use(express.static(path.resolve(__dirname, "client")));
|
||||
|
||||
createApis(app);
|
||||
|
||||
expressOasGenerator.handleResponses(app, {
|
||||
specOutputFileBehavior: SPEC_OUTPUT_FILE_BEHAVIOR.RECREATE,
|
||||
swaggerDocumentOptions: {},
|
||||
predefinedSpec: (spec: OpenAPIV3.Document) => {
|
||||
if (spec.info) {
|
||||
spec.info.description = "Egret App Server";
|
||||
}
|
||||
if (spec.paths) {
|
||||
delete spec.paths["/v3/api-docs"];
|
||||
}
|
||||
return spec;
|
||||
},
|
||||
});
|
||||
|
||||
expressOasGenerator.handleRequests();
|
||||
|
||||
app.get("/v3/api-docs", async (req, res) => {
|
||||
expressOasGenerator.getSpecV3((err, doc) => {
|
||||
res.status(200).json(doc);
|
||||
});
|
||||
});
|
||||
|
||||
const host = "0.0.0.0";
|
||||
app.listen(Number(port), host, () => {
|
||||
console.log(`[server]: Server is running at http://${host}:${port}`);
|
||||
});
|
||||
20
packages/server/tsconfig.json
Normal file
20
packages/server/tsconfig.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"lib": ["esnext"],
|
||||
"module": "node16",
|
||||
"moduleResolution": "node16",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["src/*"]
|
||||
},
|
||||
"outDir": "./build"
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules", "build"],
|
||||
"ts-node": {
|
||||
"require": ["tsconfig-paths/register"],
|
||||
"files": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user