monorepo 버전 준비
## @strpc 기존 RPC를 package화 ### @strpc/express express의 middleware + router 결함 ## @sammo 게임 전체 - server, client - gateway_server, gateway_client
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "@sammo/server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc --build"
|
||||
},
|
||||
"author": "",
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@esfx/async-readerwriterlock": "^1.0.0",
|
||||
"@sammo/api_def": "workspace:^",
|
||||
"@sammo/game_logic": "workspace:^",
|
||||
"@sammo/server_util": "workspace:^",
|
||||
"@sammo/util": "workspace:^",
|
||||
"@strpc/express": "workspace:^",
|
||||
"date-fns": "^2.30.0",
|
||||
"dotenv": "^16.3.1",
|
||||
"eventemitter3": "^5.0.1",
|
||||
"express": "^4.18.2",
|
||||
"express-session": "^1.17.3",
|
||||
"ky": "^1.0.1",
|
||||
"lodash-es": "^4.17.21",
|
||||
"mongoose": "^7.4.3",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"tslib": "^2.6.2",
|
||||
"zod": "^3.22.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.17",
|
||||
"@types/express-session": "^1.17.7",
|
||||
"@sammo/gateway_server": "workspace:^",
|
||||
"@types/node": "^20.6.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { StateIncrementer } from '@sammo/server_util';
|
||||
import SchemaSequence from './schema/SchemaSequence.js';
|
||||
import { InvalidArgument } from '@sammo/util';
|
||||
|
||||
export function MongoSequenceFactory(collectionName: string): StateIncrementer{
|
||||
|
||||
return async (increase: number) => {
|
||||
if(increase <= 0){
|
||||
throw new InvalidArgument('increase must be > 0');
|
||||
}
|
||||
increase = Math.ceil(increase);
|
||||
|
||||
const result = await SchemaSequence.findOneAndUpdate({
|
||||
collectionName,
|
||||
}, {
|
||||
$inc: {
|
||||
nextSeq: increase,
|
||||
},
|
||||
}, {
|
||||
upsert: true,
|
||||
new: true,
|
||||
});
|
||||
return result.nextSeq;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import type { structure } from "@sammo/api_def";
|
||||
import type { APINamespaceType } from "@strpc/express";
|
||||
|
||||
export const sammoAPI = {
|
||||
} satisfies APINamespaceType<typeof structure>;
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'dotenv/config';
|
||||
import { connect, Mongoose } from "mongoose";
|
||||
import { unwrap } from '@sammo/util';
|
||||
|
||||
const dbConfig = {
|
||||
host: unwrap(process.env.GAME_DB_HOST),
|
||||
port: Number(unwrap(process.env.GAME_DB_PORT)),
|
||||
user: unwrap(process.env.GAME_DB_USER),
|
||||
password: unwrap(process.env.GAME_DB_PASSWORD),
|
||||
database: unwrap(process.env.GAME_DB_DATABASE),
|
||||
}
|
||||
|
||||
const db: Promise<Mongoose> = (async () => {
|
||||
return await connect(`mongodb://${dbConfig.host}:${dbConfig.port}/${dbConfig.database}`, {
|
||||
auth:{
|
||||
username: dbConfig.user,
|
||||
password: dbConfig.password,
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
export default db;
|
||||
@@ -0,0 +1,3 @@
|
||||
import { resolve } from "node:path";
|
||||
|
||||
export const rootPath = resolve(resolve(), '../..');
|
||||
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
declare namespace NodeJS {
|
||||
interface ProcessEnv {
|
||||
NODE_ENV: 'development' | 'production';
|
||||
|
||||
//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;
|
||||
|
||||
PRESHARED_SECURE_TOKEN_SECRET?: string; //길게
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import dotenv from 'dotenv';
|
||||
import { rootPath } from './constPath.js';
|
||||
import { unwrap } from '@sammo/util';
|
||||
|
||||
let init = false;
|
||||
const dirPath = rootPath
|
||||
console.log(rootPath);
|
||||
|
||||
function initDotEnv() {
|
||||
if (process.env['NODE_ENV'] == 'production') {
|
||||
dotenv.config({ path: `${dirPath}/.env.production.local` });
|
||||
dotenv.config({ path: `${dirPath}/.env.local` });
|
||||
dotenv.config({ path: `${dirPath}/.env.production` });
|
||||
}
|
||||
else {
|
||||
dotenv.config({ path: `${dirPath}/.env.development.local` });
|
||||
dotenv.config({ path: `${dirPath}/.env.local` });
|
||||
dotenv.config({ path: `${dirPath}/.env.development` });
|
||||
}
|
||||
dotenv.config();
|
||||
init = true;
|
||||
}
|
||||
|
||||
if (!init) {
|
||||
initDotEnv();
|
||||
}
|
||||
|
||||
let _ownConfig: ReturnType<typeof generateConfig>|undefined = undefined;
|
||||
|
||||
function generateConfig() {
|
||||
return {
|
||||
port: parseInt(process.env['SERVER_PORT'] ?? "3001"),
|
||||
sessionSecret: unwrap(process.env['SESSION_SECRET']),
|
||||
gatewayHost: unwrap(process.env.GATEWAY_HOST),
|
||||
gatewayPort: Number(unwrap(process.env.GATEWAY_PORT)),
|
||||
apiRootPath: process.env['API_ROOT_PATH'] ?? '/api',
|
||||
} as const;
|
||||
}
|
||||
|
||||
export function serverConfig() {
|
||||
if(_ownConfig !== undefined){
|
||||
return _ownConfig;
|
||||
}
|
||||
if(!init){
|
||||
initDotEnv();
|
||||
}
|
||||
_ownConfig = generateConfig();
|
||||
return _ownConfig;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import './dotenv.js'
|
||||
import 'reflect-metadata';
|
||||
import connectDB from './connectDB.js';
|
||||
import express, { type Request, type Response } from "express"
|
||||
import session from "express-session";
|
||||
import { buildAPISystem } from '@strpc/express/generator';
|
||||
import { sammoAPI } from './api/index.js';
|
||||
import { unwrap } from '@sammo/util';;
|
||||
import { serverConfig } from './serverConfig.js';
|
||||
|
||||
connectDB.then(async (db) => {
|
||||
// create express app
|
||||
const app = express()
|
||||
app.use(express.json());
|
||||
app.use(session({
|
||||
secret: unwrap(serverConfig.sessionSecret),
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
}))
|
||||
//app.set('etag', false);
|
||||
|
||||
app.use('/api', buildAPISystem(sammoAPI));
|
||||
|
||||
// start express server
|
||||
app.listen(serverConfig.port)
|
||||
|
||||
console.log(`Express server has started on port ${serverConfig.port}`)
|
||||
|
||||
}).catch(error => console.log(error));
|
||||
|
||||
export default {};
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { SessionCtx } from "@sammo/server_util";
|
||||
import type { LoginCtx } from "./ReqLogin.js";
|
||||
import type { ProcDecoratorGenerator } from "@strpc/express/proc_decorator";
|
||||
|
||||
export type GameLoginCtx = {
|
||||
generalID: number;
|
||||
generalName: string;
|
||||
gameLoginDate: Date;
|
||||
}
|
||||
|
||||
export function ReqGameLogin<Q extends LoginCtx & SessionCtx>(): ProcDecoratorGenerator<GameLoginCtx, Q> {
|
||||
return async (ctx) => {
|
||||
//NOTE: 게임 서버별로 gameLoginCtx를 따로 가져야 하는가?
|
||||
let gameLoginCtx = ctx.session.getItem<GameLoginCtx>(`gameLoginCtx`);
|
||||
if (gameLoginCtx) {
|
||||
if (gameLoginCtx.gameLoginDate >= ctx.loginDate) {
|
||||
return [{
|
||||
result: true,
|
||||
}, {
|
||||
...gameLoginCtx,
|
||||
...ctx,
|
||||
}];
|
||||
}
|
||||
gameLoginCtx = undefined;
|
||||
}
|
||||
|
||||
//TODO: DB에서 generalID를 가져오는 로직
|
||||
return [{
|
||||
result: false,
|
||||
type: 'Required GameLogin',
|
||||
info: 'NotYetImplemented'
|
||||
}, {
|
||||
...ctx
|
||||
}];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import type { SessionCtx } from "@sammo/server_util";
|
||||
import type { ProcDecorator } from "@strpc/express/proc_decorator";
|
||||
import type { ServerActionType } from "@sammo/gateway_server/exports"
|
||||
|
||||
export type LoginCtx = {
|
||||
userID: number;
|
||||
userName: string;
|
||||
userLevel: number;
|
||||
allowServerAction?: Set<ServerActionType>;
|
||||
loginDate: Date;
|
||||
}
|
||||
export const loginCtxSessionKey = 'loginCtx';
|
||||
|
||||
export function ReqLogin<Q extends SessionCtx>(): ProcDecorator<LoginCtx & Q, Q> {
|
||||
return (ctx) => {
|
||||
const loginCtx = ctx.session.getItem<LoginCtx>(loginCtxSessionKey);
|
||||
if(!loginCtx){
|
||||
return [{
|
||||
result: false,
|
||||
type: 'Required Login',
|
||||
info: 'ReqLogin'
|
||||
}, ctx];
|
||||
}
|
||||
|
||||
return [{
|
||||
result: true,
|
||||
},{
|
||||
...loginCtx,
|
||||
...ctx,
|
||||
}];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Schema, model, Types } from 'mongoose';
|
||||
|
||||
interface ISchemaSequence {
|
||||
_id: Types.ObjectId;
|
||||
|
||||
collectionName: string;
|
||||
nextSeq: number;
|
||||
}
|
||||
|
||||
export const SchemaSequence = new Schema<ISchemaSequence>({
|
||||
collectionName: { type: String, required: true },
|
||||
nextSeq: {
|
||||
type: Number, required: true,
|
||||
get: (v: number) => Math.ceil(v),
|
||||
set: (v: number) => Math.ceil(v),
|
||||
default: 0,
|
||||
},
|
||||
|
||||
}, { autoIndex: true, autoCreate: true })
|
||||
.index({ collectionName: 1 }, { unique: true })
|
||||
;
|
||||
|
||||
export default model<ISchemaSequence>('SchemaSequence', SchemaSequence);
|
||||
@@ -0,0 +1,9 @@
|
||||
import 'dotenv/config';
|
||||
import { unwrap } from '@sammo/util';
|
||||
|
||||
export const serverConfig = {
|
||||
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)),
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist",
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"references": [
|
||||
{
|
||||
"path": "../../@strpc/express"
|
||||
},
|
||||
{
|
||||
"path": "../util"
|
||||
},
|
||||
{
|
||||
"path": "../crypto"
|
||||
},
|
||||
{
|
||||
"path": "../server_util"
|
||||
},
|
||||
{
|
||||
"path": "../api_def"
|
||||
},
|
||||
{
|
||||
"path": "../gateway_server"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user