From 199938f2b2c32cd98e6516938362e315fcc7bbd4 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Fri, 18 Aug 2023 15:47:10 +0000 Subject: [PATCH] gateway --- server/api/api.ts | 5 ++++ server/apiStructure/sammoAPI.ts | 6 +++++ server/dotenv.d.ts | 29 +++++++++++++++----- server/gateway.ts | 37 ++++++++++++++++++++++++++ server/gatewayConfig.ts | 7 +++++ server/index.ts | 14 +++------- server/schema/gateway/LoginToken.ts | 2 +- server/schema/gateway/ServerConfig.ts | 1 + server/schema/gateway/ServerVersion.ts | 2 +- server/schema/gateway/User.ts | 2 +- server/schema/gateway/UserLog.ts | 2 +- server/serverConfig.ts | 9 +++++++ 12 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 server/api/api.ts create mode 100644 server/apiStructure/sammoAPI.ts create mode 100644 server/gateway.ts create mode 100644 server/gatewayConfig.ts create mode 100644 server/serverConfig.ts diff --git a/server/api/api.ts b/server/api/api.ts new file mode 100644 index 0000000..aa88013 --- /dev/null +++ b/server/api/api.ts @@ -0,0 +1,5 @@ +import type { structure } from "../apiStructure/sammoAPI.js"; +import type { APINamespaceType } from "./defs.js"; + +export const sammoAPI = { +} satisfies APINamespaceType; \ No newline at end of file diff --git a/server/apiStructure/sammoAPI.ts b/server/apiStructure/sammoAPI.ts new file mode 100644 index 0000000..e29b7e4 --- /dev/null +++ b/server/apiStructure/sammoAPI.ts @@ -0,0 +1,6 @@ +import { type DefAPINamespace, GET, POST } from "./defs.js"; + +/** @internal */ +export const structure = { + +} satisfies DefAPINamespace; \ No newline at end of file diff --git a/server/dotenv.d.ts b/server/dotenv.d.ts index 5a28cec..99f4201 100644 --- a/server/dotenv.d.ts +++ b/server/dotenv.d.ts @@ -1,13 +1,28 @@ declare namespace NodeJS { interface ProcessEnv { NODE_ENV: 'development' | 'production'; - GAME_DB_HOST: string; - GAME_DB_DATABASE: string; - GAME_DB_PORT: string; - GAME_DB_USER: string; - GAME_DB_PASSWORD: string; - SERVER_PORT: string; //숫자 - SESSION_SECRET: string; //길게 + + //Docker 환경이라면 GAME_DB 또는 GATEWAY_DB 둘중에 하나만 설정되어 있을 것이다. + GAME_DB_HOST?: string; + GAME_DB_DATABASE?: string; + GAME_DB_PORT?: string; + GAME_DB_USER?: string; + GAME_DB_PASSWORD?: string; + + GATEWAY_DB_HOST?: string; + GATEWAY_DB_DATABASE?: string; + GATEWAY_DB_PORT?: string; + GATEWAY_DB_USER?: string; + GATEWAY_DB_PASSWORD?: string; + + //Gateway RPC용 + GATEWAY_HOST?: string; + GATEWAY_PORT?: string; + //gateway.ts용 + GATEWAY_SESSION_SECRET?: string; + + SERVER_PORT?: string; //숫자 + SESSION_SECRET?: string; //길게 API_ROOT_PATH?: string; VITE_API_ROOT_PATH?: string; diff --git a/server/gateway.ts b/server/gateway.ts new file mode 100644 index 0000000..362473b --- /dev/null +++ b/server/gateway.ts @@ -0,0 +1,37 @@ +import 'dotenv/config'; +import 'reflect-metadata'; +import express, { type Request, type Response } from "express" +import session from "express-session"; +import path from 'node:path'; +import { LiteHashDRBG } from './util/LiteHashDRBG.js'; +import { simpleSerialize } from './util/simpleSerialize.js'; +import { SammoRootAPI } from './clientAPI/sammoRootAPI.js'; +import { buildAPISystem } from './api/generator.js'; +import { sammoRootAPI } from './api/root.js'; +import { unwrap } from './util/unwrap.js'; +import { gatewayConfig } from './gatewayConfig.js'; + + + + +(async () => { + // create express app + const app = express() + app.use(express.json()); + app.use(session({ + secret: unwrap(gatewayConfig.sessionSecret), + resave: false, + saveUninitialized: false, + })) + //app.set('etag', false); + + app.use('/gateway_api', buildAPISystem(sammoRootAPI)); + + // start express server + app.listen(gatewayConfig.port) + + console.log(`Gateway server has started on port ${gatewayConfig.port}`) + +})().catch(error => console.log(error)); + +export default {}; \ No newline at end of file diff --git a/server/gatewayConfig.ts b/server/gatewayConfig.ts new file mode 100644 index 0000000..492a4ed --- /dev/null +++ b/server/gatewayConfig.ts @@ -0,0 +1,7 @@ +import 'dotenv/config'; +import { unwrap } from './util/unwrap.js'; + +export const gatewayConfig = { + sessionSecret: unwrap(process.env.GATEWAY_SESSION_SECRET), + port: Number(unwrap(process.env.GATEWAY_PORT)), +}; \ No newline at end of file diff --git a/server/index.ts b/server/index.ts index b78a3b5..42280af 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,20 +1,14 @@ -import 'dotenv/config'; + import 'reflect-metadata'; import express, { type Request, type Response } from "express" import session from "express-session"; import path from 'node:path'; import { LiteHashDRBG } from './util/LiteHashDRBG.js'; import { simpleSerialize } from './util/simpleSerialize.js'; -import { SammoRootAPI } from './clientAPI/sammoRootAPI.js'; import { buildAPISystem } from './api/generator.js'; -import { sammoRootAPI } from './api/root.js'; import { unwrap } from './util/unwrap.js'; - -const ownConfig = { - sessionSecret: process.env.SESSION_SECRET, - port: Number(process.env.SERVER_PORT), -}; - +import { sammoAPI } from './api/api.js'; +import { ownConfig } from './serverConfig.js'; (async () => { // create express app @@ -27,7 +21,7 @@ const ownConfig = { })) //app.set('etag', false); - app.use('/rootAPI', buildAPISystem(sammoRootAPI)); + app.use('/api', buildAPISystem(sammoAPI)); // start express server app.listen(ownConfig.port) diff --git a/server/schema/gateway/LoginToken.ts b/server/schema/gateway/LoginToken.ts index b3bb8be..2e07add 100644 --- a/server/schema/gateway/LoginToken.ts +++ b/server/schema/gateway/LoginToken.ts @@ -15,7 +15,7 @@ export const LoginToken = new Schema({ ip: { type: String, required: true }, regDate: { type: Date, required: true }, validUntil: { type: Date, required: true }, -}, { autoIndex: false }) +}, { autoIndex: false, autoCreate: false, }) .index({ userID: 1, token: 1 }) .index({ validUntil: 1 }, { expireAfterSeconds: 1 }) ; diff --git a/server/schema/gateway/ServerConfig.ts b/server/schema/gateway/ServerConfig.ts index 3db36d7..00b4236 100644 --- a/server/schema/gateway/ServerConfig.ts +++ b/server/schema/gateway/ServerConfig.ts @@ -12,6 +12,7 @@ export const ServerConfig = new Schema({ allowRegister: { type: Boolean, required: true }, }, { autoIndex: false, + autoCreate: false, capped: { max: 1, } diff --git a/server/schema/gateway/ServerVersion.ts b/server/schema/gateway/ServerVersion.ts index 739fbd8..ac34dd7 100644 --- a/server/schema/gateway/ServerVersion.ts +++ b/server/schema/gateway/ServerVersion.ts @@ -12,7 +12,7 @@ export const ServerVersion = new Schema({ branch: { type: String, required: true }, version: { type: String, required: true }, updateDate: { type: Date, required: true }, -}, { autoIndex: false }) +}, { autoIndex: false, autoCreate: false, }) .index({ serverKey: 1 }, { unique: true }) ; diff --git a/server/schema/gateway/User.ts b/server/schema/gateway/User.ts index 45ac50a..3a3aed1 100644 --- a/server/schema/gateway/User.ts +++ b/server/schema/gateway/User.ts @@ -80,7 +80,7 @@ export const User = new Schema({ regDate: { type: Date, required: true }, deleteAfter: { type: Date, required: false }, -}, { autoIndex: false }) +}, { autoIndex: false, autoCreate: false }) .index({ id: 1 }, { unique: true }) .index({ email: 1 }, { unique: true }) .index({ oauthID: 1 }, { unique: true, sparse: true }) diff --git a/server/schema/gateway/UserLog.ts b/server/schema/gateway/UserLog.ts index 2b019e6..2742802 100644 --- a/server/schema/gateway/UserLog.ts +++ b/server/schema/gateway/UserLog.ts @@ -22,7 +22,7 @@ export const UserLog = new Schema({ logDate: { type: Date, required: true }, logType: { type: String, required: true, enum: LogTypeList }, action: { type: Object, required: true }, -}, { autoIndex: false }) +}, { autoIndex: false, autoCreate: false, }) .index({ userID: 1, logDate: 1 }) .index({ logDate: 1 }, { expireAfterSeconds: 60 * 60 * 24 * 365 * 3 }) ; diff --git a/server/serverConfig.ts b/server/serverConfig.ts new file mode 100644 index 0000000..6806321 --- /dev/null +++ b/server/serverConfig.ts @@ -0,0 +1,9 @@ +import 'dotenv/config'; +import { unwrap } from './util/unwrap.js'; + +export const ownConfig = { + sessionSecret: unwrap(process.env.SESSION_SECRET), + port: Number(unwrap(process.env.SERVER_PORT)), + gatewayHost: unwrap(process.env.GATEWAY_HOST), + gatewayPort: Number(unwrap(process.env.GATEWAY_PORT)), +};