commit 91a12cc98e28404c37b2e20cf6ce2396b3469829 Author: gangwen.lv Date: Mon Jul 17 21:47:42 2023 +0800 init diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f71d1da --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM node:16-alpine + +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \ + apk --update add tzdata && \ + cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone && \ + apk del tzdata && \ + rm -rf /var/cache/apk/* diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b11f79a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,43 @@ +version: "3.5" + +services: + fellow: + image: node:16-alpine-cn + build: . + hostname: fellow + container_name: fellow + network_mode: host + privileged: true + restart: always + environment: + - MOCK_HOME_DIR=/fellow + volumes: + - /home/xiaoyh/node/schoolfellow:/fellow + - /home/xiaoyh/node/schoolfellow/logs:/home/node/logs + - ./package/fellow.json:/fellow/package.json:ro + - ./patch/gzip.js:/fellow/app/middleware/gzip.js + working_dir: /fellow + # command: /bin/sh -c "while true; do echo hello world; sleep 1;done" + command: npm run start-docker + ports: + - "7001:7001" + + foundation: + image: node:16-alpine-cn + build: . + hostname: foundation + container_name: foundation + network_mode: host + privileged: true + restart: always + environment: + - MOCK_HOME_DIR=/foundation + volumes: + - /home/xiaoyh/node/jjh-server:/foundation + - /home/xiaoyh/node/jjh-server/logs:/home/node/logs + - ./package/foundation.json:/foundation/package.json:ro + - ./patch/gzip.js:/foundation/app/middleware/gzip.js + working_dir: /foundation + command: npm run start-docker + ports: + - "7002:7002" \ No newline at end of file diff --git a/package/fellow.json b/package/fellow.json new file mode 100644 index 0000000..ed37592 --- /dev/null +++ b/package/fellow.json @@ -0,0 +1,56 @@ +{ + "name": "schoolefellow-server", + "version": "1.0.0", + "description": "后台管理系统api", + "private": true, + "egg": { + "declarations": true + }, + "dependencies": { + "egg": "^2.15.1", + "egg-bcrypt": "^1.1.0", + "egg-cors": "^2.2.3", + "egg-scripts": "^2.11.0", + "egg-sequelize": "^6.0.0", + "egg-validate-plus": "^1.1.6", + "jsonwebtoken": "^8.5.1", + "mysql2": "^2.2.5", + "svg-captcha": "^1.4.0" + }, + "devDependencies": { + "autod": "^3.0.1", + "autod-egg": "^1.1.0", + "egg-bin": "^4.11.0", + "egg-ci": "^1.11.0", + "egg-mock": "^3.21.0", + "eslint": "^5.13.0", + "eslint-config-egg": "^7.1.0", + "sequelize-cli": "^6.2.0" + }, + "engines": { + "node": ">=10.0.0" + }, + "scripts": { + "start": "egg-scripts start --daemon --title=egg-server-cms-api --ignore-stderr", + "start-docker": "egg-scripts start --title=egg-server-cms-api --ignore-stderr", + "stop": "egg-scripts stop --title=egg-server-cms-api", + "dev": "egg-bin dev", + "debug": "egg-bin debug", + "test": "npm run lint -- --fix && npm run test-local", + "test-local": "egg-bin test", + "cov": "egg-bin cov", + "lint": "eslint .", + "ci": "npm run lint && npm run cov", + "autod": "autod" + }, + "ci": { + "version": "10" + }, + "repository": { + "type": "git", + "url": "" + }, + "author": "gaoxiangan", + "license": "MIT" +} + diff --git a/package/foundation.json b/package/foundation.json new file mode 100644 index 0000000..b954636 --- /dev/null +++ b/package/foundation.json @@ -0,0 +1,55 @@ +{ + "name": "schoolefellow-server", + "version": "1.0.0", + "description": "后台管理系统api", + "private": true, + "egg": { + "declarations": true + }, + "dependencies": { + "egg": "^2.15.1", + "egg-bcrypt": "^1.1.0", + "egg-cors": "^2.2.3", + "egg-scripts": "^2.11.0", + "egg-sequelize": "^6.0.0", + "egg-validate-plus": "^1.1.6", + "jsonwebtoken": "^8.5.1", + "mysql2": "^2.2.5", + "svg-captcha": "^1.4.0" + }, + "devDependencies": { + "autod": "^3.0.1", + "autod-egg": "^1.1.0", + "egg-bin": "^4.11.0", + "egg-ci": "^1.11.0", + "egg-mock": "^3.21.0", + "eslint": "^5.13.0", + "eslint-config-egg": "^7.1.0", + "sequelize-cli": "^6.2.0" + }, + "engines": { + "node": ">=10.0.0" + }, + "scripts": { + "start": "egg-scripts start --daemon --title=egg-server-cms-api --ignore-stderr", + "start-docker": "egg-scripts start --title=egg-server-cms-api --ignore-stderr", + "stop": "egg-scripts stop --title=egg-server-cms-api", + "dev": "egg-bin dev", + "debug": "egg-bin debug", + "test": "npm run lint -- --fix && npm run test-local", + "test-local": "egg-bin test", + "cov": "egg-bin cov", + "lint": "eslint .", + "ci": "npm run lint && npm run cov", + "autod": "autod" + }, + "ci": { + "version": "10" + }, + "repository": { + "type": "git", + "url": "" + }, + "author": "gaoxiangan", + "license": "MIT" +} diff --git a/patch/gzip.js b/patch/gzip.js new file mode 100644 index 0000000..863d858 --- /dev/null +++ b/patch/gzip.js @@ -0,0 +1,35 @@ +const isJSON = require("koa-is-json"); +const zlib = require("zlib"); + +module.exports = (options) => { + return async function gzip(ctx, next) { + // 限制域名访问 added by steven @2023-07-11 + const ipCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; + const host = ctx.request.host.indexOf(":") + ? ctx.request.host.split(":")[0] + : ctx.request.host; + if (ipCheck.test(host)) { + ctx.status = 200; + ctx.body = `访问禁止只允许域名方式访问`; + // ctx.throw(401, "只允许域名访问"); + return; + } + ///// + await next(); + + // 后续中间件执行完成后将响应体转换成 gzip + let body = ctx.body; + if (!body) return; + + // 支持 options.threshold + if (options.threshold && ctx.length < options.threshold) return; + + if (isJSON(body)) body = JSON.stringify(body); + + // 设置 gzip body,修正响应头 + const stream = zlib.createGzip(); + stream.end(body); + ctx.body = stream; + ctx.set("Content-Encoding", "gzip"); + }; +};