diff --git a/Dockerfile b/Dockerfile index 9e5abd4..a22e20c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,8 +14,10 @@ RUN npm run tsc && \ rm -rf node_modules && \ npm install --production && \ npm install cross-env tsconfig-paths --save-dev && \ + npm install pm2 --global && \ npm cache clean -f -CMD [ "npm", "run", "start:prod" ] +CMD pm2 start index.js --node-args="-r tsconfig-paths/register" -i 0 --no-daemon +# CMD [ "npm", "run", "start:prod" ] EXPOSE 9000 diff --git a/TODOLIST.md b/TODOLIST.md index c8f1a11..ca2065e 100644 --- a/TODOLIST.md +++ b/TODOLIST.md @@ -10,4 +10,5 @@ - [ ] 配置文件写入初始化用户账号密码 - [ ] 接入AuthBox - [ ] 缓存整理 -- [ ] Redis 接入 \ No newline at end of file +- [ ] Redis 接入 + - [x] 支持Session \ No newline at end of file diff --git a/config/app.default.yaml b/config/app.default.yaml index 7946bfa..8ff3ff5 100644 --- a/config/app.default.yaml +++ b/config/app.default.yaml @@ -1,5 +1,9 @@ server: port: 9000 +redis: + host: 127.0.0.1 + port: 6379 + url: db: type: mongo host: 0.0.0.0 diff --git a/docker/app.yaml b/docker/app.yaml index 86bad22..73dad1c 100644 --- a/docker/app.yaml +++ b/docker/app.yaml @@ -1,4 +1,6 @@ db: host: "db" +redis: + host: "cache" path: tmp: /tmp \ No newline at end of file diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 83516fe..8782e58 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -5,6 +5,11 @@ db: container_name: storebox_db restart: always +cache: + image: redis + container_name: storebox_cache + restart: always + svr: build: .. ports: @@ -16,5 +21,6 @@ svr: - ./app.yaml:/usr/src/app/config/app.yaml:ro links: - db + - cache container_name: storebox_svr restart: always \ No newline at end of file diff --git a/package.json b/package.json index 3e8d5a1..70e0fb3 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "@nestjs/testing": "^4.5.1", "@types/basic-auth": "^1.1.2", "@types/bunyan": "^1.8.4", + "@types/connect-redis": "0.0.7", "@types/cors": "^2.8.3", "@types/express": "^4.0.39", "@types/express-session": "^1.15.6", @@ -43,6 +44,7 @@ "@types/mocha-steps": "^1.1.0", "@types/mongoose": "^4.7.26", "@types/multer": "^1.3.6", + "@types/node": "^9.3.0", "@types/path-exists": "^3.0.0", "@types/reflect-metadata": "0.0.5", "@types/should": "^11.2.0", @@ -68,11 +70,11 @@ "@nestjs/common": "^4.5.1", "@nestjs/core": "^4.5.1", "@nestjs/swagger": "^1.1.3", - "@types/node": "^9.3.0", "basic-auth": "^2.0.0", "body-parser": "^1.18.2", "bunyan": "^1.8.12", "class-validator": "^0.7.3", + "connect-redis": "^3.3.3", "cookie-parser": "^1.4.3", "cors": "^2.8.4", "express": "^4.16.2", diff --git a/src/express.ts b/src/express.ts index 1d55582..8e59413 100644 --- a/src/express.ts +++ b/src/express.ts @@ -1,9 +1,15 @@ import * as express from "express"; -import * as session from "express-session"; +import session = require("express-session"); import * as bodyParser from "body-parser"; import * as cookieParser from "cookie-parser"; +import connectRedis = require("connect-redis"); +import { config } from "@utils/config"; import helmet = require("helmet"); + import { error } from "./modules/common/middlewares/logger.middleware"; +import { isTest } from "./modules/common/helper/env"; + +const RedisStore = connectRedis(session); let server: express.Express; @@ -21,12 +27,19 @@ export const initExpress = () => { mServer.use(bodyParser.json()); mServer.use(bodyParser.urlencoded()); mServer.use(cookieParser("storebox")); - mServer.use(session({ + const sessionOpts = { + store: undefined, secret: "storebox", resave: false, saveUninitialized: true, cookie: { secure: false, maxAge: 7200 * 1000 } - })); + }; + if (!isTest) { + sessionOpts.store = new RedisStore({ + url: config.redis.url + }); + } + mServer.use(session(sessionOpts)); mServer.use(error); server = mServer; diff --git a/src/index.ts b/src/index.ts index 54dac33..8b085f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,7 @@ import { isDevelopment } from "./modules/common/helper/env"; const bootstrap = async () => { const server = initExpress(); - const app = await NestFactory.create(ApplicationModule, server); + const app = await NestFactory.create(ApplicationModule, server, { }); app.useGlobalPipes(new ValidationPipe()); if (isDevelopment) { diff --git a/src/utils/config.d.ts b/src/utils/config.d.ts index fd629e6..2556f66 100644 --- a/src/utils/config.d.ts +++ b/src/utils/config.d.ts @@ -19,8 +19,13 @@ interface paths { backup: string; } +interface redis { + url: string; +} + export interface ConfigObj { + redis: redis; server: server; db: db; paths: paths; -} \ No newline at end of file +} diff --git a/src/utils/config.ts b/src/utils/config.ts index f4562f2..5f8f5b3 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -25,6 +25,15 @@ for (const item of Object.keys(configModule.paths)) { } } +configModule.redis.url = configModule.redis.url || + `redis://${configModule.redis.host}:${configModule.redis.port}`; +if ( + configModule.redis.url.indexOf("redis://") !== 0 && + configModule.redis.url.indexOf("//") !== 0 +) { + configModule.redis.url = `redis://${configModule.redis.url}`; +} + export const config = configModule.getConfig() as ConfigObj; import { systemLogger } from "../modules/common/helper/log";