This commit is contained in:
2023-09-22 21:13:33 +09:00
parent 295d39327b
commit 438ee85ed2
24 changed files with 539 additions and 40 deletions
+23
View File
@@ -0,0 +1,23 @@
{
"name": "@sammo/gateway",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "tsc --build"
},
"author": "",
"type": "module",
"license": "MIT",
"dependencies": {
"@sammo/api_def": "workspace:^",
"@sammo/server_util": "workspace:^",
"@sammo/util": "workspace:^",
"@strpc/express": "workspace:^",
"dotenv": "^16.3.1",
"mongoose": "^7.4.3"
},
"devDependencies": {
"@types/node": "^20.6.3"
}
}
+25
View File
@@ -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;
}
}
View File
+22
View File
@@ -0,0 +1,22 @@
import './dotenv.js';
import { connect, Mongoose } from "mongoose";
import { unwrap } from '@sammo/util';
const dbConfig = {
host: unwrap(process.env.GATEWAY_DB_HOST),
port: Number(unwrap(process.env.GATEWAY_DB_PORT)),
user: unwrap(process.env.GATEWAY_DB_USER),
password: unwrap(process.env.GATEWAY_DB_PASSWORD),
database: unwrap(process.env.GATEWAY_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;
+3
View File
@@ -0,0 +1,3 @@
import { resolve } from "node:path";
export const rootPath = resolve(resolve(), '../..');
+47
View File
@@ -0,0 +1,47 @@
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']),
apiRootPath: process.env['API_ROOT_PATH'] ?? '/api/gateway',
} as const;
}
export function ownConfig() {
if(_ownConfig !== undefined){
return _ownConfig;
}
if(!init){
initDotEnv();
}
_ownConfig = generateConfig();
return _ownConfig;
}
+7
View File
@@ -0,0 +1,7 @@
import 'dotenv/config';
import { unwrap } from "@sammo/util";
export const gatewayConfig = {
sessionSecret: unwrap(process.env.GATEWAY_SESSION_SECRET),
port: Number(unwrap(process.env.GATEWAY_PORT)),
};
+34
View File
@@ -0,0 +1,34 @@
import './dotenv.js'
import 'reflect-metadata';
import gatewayDB from './connectDB.js';
import express, { type Request, type Response } from "express"
import session from "express-session";
import { buildAPISystem } from '@strpc/express/generator';
//import { sammoGatewayAPI } from './api/index.js';
import { unwrap } from '@sammo/util';;
import { gatewayConfig } from './gatewayConfig.js';
gatewayDB.then(async (gatewayDB) => {
// 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(sammoGatewayAPI));
// start express server
app.listen(gatewayConfig.port)
console.log(`Gateway server has started on port ${gatewayConfig.port}`)
}).catch(error => console.log(error));
export default {};
@@ -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);
+21
View File
@@ -0,0 +1,21 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
},
"references": [
{
"path": "../../@strpc/express"
},
{
"path": "../util"
},
{
"path": "../crypto"
},
{
"path": "../server_util"
},
]
}