Skip to content

Commit 680476f

Browse files
committed
build(docker): 修改启动命令;优化 dockerfile;增加 docker-compose 启动安全性配置;
1 parent 31a93c5 commit 680476f

15 files changed

+157
-44
lines changed

.dockerignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
.env
33
ecosystem.config.js
4-
dist
4+
dist
5+
data

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ tmp
77

88
.env
99

10-
ecosystem.config.js
10+
ecosystem.config.js
11+
.DS_Store

Dockerfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ FROM node:14-alpine
22
EXPOSE 8080
33
WORKDIR /app
44
ENV LOG_LEVEL=debug
5-
ADD . /app
6-
RUN npm i --prod
5+
COPY . /app
6+
RUN mkdir /app/data || true
7+
RUN chown node:node /app/data
8+
RUN npm i
79
RUN npm run build
10+
USER node
811
CMD [ "npm", "run", "init-start" ]

docker-compose.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
version: '3'
1+
version: '3.8'
22
services:
33
server:
44
image: lessx/less-framework:latest
5+
user: node
56
environment:
67
SERVER_SALT: Rewrite_Your_Own_Secret_Salt_abcdefg1234567
78
DB: less_app
@@ -15,11 +16,18 @@ services:
1516
- app-data:/app/data
1617
ports:
1718
- "8080:8080"
19+
read_only: true
20+
cap_drop:
21+
- ALL
22+
tmpfs:
23+
- /tmp
24+
restart: always
1825
networks:
1926
- less
2027

2128
mongodb:
2229
image: mongo:latest
30+
user: mongodb
2331
environment:
2432
MONGO_INITDB_ROOT_USERNAME: less
2533
MONGO_INITDB_ROOT_PASSWORD: less123

package-lock.json

+41-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
"description": "less framework base on less-api",
55
"main": "./dist/index.js",
66
"scripts": {
7-
"start": "node --experimental-vm-modules ./dist/index.js",
7+
"start": "node ./dist/index.js",
88
"build": "npx tsc -p tsconfig.json",
99
"watch": "npx tsc -p tsconfig.json -w",
1010
"init": "node init/index.js",
1111
"prepublishOnly": "npm run build",
1212
"prepare": "husky install",
1313
"commit": "commit",
14-
"init-start": "npm run build && npm run init && npm run start",
14+
"trace-gc": "node --trace_gc --trace_gc_verbose ./dist/index.js",
15+
"init-start": "npm run init && npm run start",
1516
"docker-build": "docker build -t lessx/less-framework:latest .",
1617
"docker-push": "docker push lessx/less-framework:latest",
1718
"docker-bp": "npm run docker-build && npm run docker-push"
@@ -54,6 +55,7 @@
5455
"@types/node": "^14.14.37",
5556
"@types/uuid": "^8.3.0",
5657
"@types/validator": "^13.1.3",
58+
"heapdump": "^0.3.15",
5759
"husky": "^5.2.0",
5860
"typescript": "^4.2.3"
5961
}

src/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export default class Config {
6060

6161
// 临时文件目录
6262
static get TMP_PATH(): string {
63-
return process.env['TMP_PATH'] ?? path.join(process.cwd(), "tmp/uploads")
63+
const tmp_path = process.env['TMP_PATH'] ?? path.join(process.cwd(), "tmp")
64+
return tmp_path
6465
}
6566

6667
/**

tests/ab/db_read.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"action": "database.queryDocument",
33
"collectionName": "permissions",
4-
"limit": 10,
4+
"limit": 20,
55
"queryType": "WHERE"
66
}

tests/ab/empty.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"params": {
3-
"test": 1
4-
}
2+
"username": "less",
3+
"password": "less123"
54
}

tests/ab/less.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# 读数据
2-
ab -n 10000 -c 100 -p ./db_read.json -T application/json -H "Authorization: Bearer eyJ1aWQiOiI2MDU1YTJhYWYyODhhNzQyNjEyNjA1MmYiLCJ0eXBlIjoiYWRtaW4iLCJleHBpcmUiOjE2MTcxNTY1MjY0MDJ9.a2ef2628165b3071a1694151111ca921" http://localhost:8080/admin/entry
2+
ab -n 10000 -c 100 -p ./db_read.json -T application/json -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI2MDg1MzhkMjhlMWI0MTM2YjEyMzIyZjIiLCJ0eXBlIjoiYWRtaW4iLCJleHAiOjE2Mjc1NTEyNzksImlhdCI6MTYyNjk0NjQ3OX0.BFJPeJYb9-tifpKWKW2C0ipWSwy9pEFvrORqrIbNh9s" http://localhost:8080/admin/entry
33

44

55
# 云函数调用
66
ab -n 1000 -c 100 -p ./empty.json -T application/json -H "Authorization: Bearer eyJ1aWQiOiI2MDU1YTJhYWYyODhhNzQyNjEyNjA1MmYiLCJ0eXBlIjoiYWRtaW4iLCJleHBpcmUiOjE2MTcxNTY1MjY0MDJ9.a2ef2628165b3071a1694151111ca921" http://localhost:8080/admin//func/invoke/hello
7+
8+
9+
ab -n 1000 -c 100 -p ./empty.json -T application/json http://localhost:8000/prod-api/func/invoke/user-passwd-login

tests/faas/engine.test.js

+48-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,65 @@
11

22
const path = require('path')
33
const assert = require('assert')
4-
const { FunctionEngine } = require('../../dist/lib/faas/index')
5-
const { handler } = require('./func')
4+
const { FunctionEngine } = require('../../dist/lib/faas/engine')
5+
const ts = require('typescript')
66

77

8+
const source = `
9+
import * as crypto from 'crypto'
10+
11+
exports.main = async function (ctx: any) {
12+
console.log(ctx)
13+
console.log(crypto)
14+
return 'ok'
15+
}
16+
`
17+
18+
const source2 = `
19+
import * as crypto from 'crypto'
20+
21+
export async function main (ctx: any) {
22+
console.log(ctx)
23+
console.log(crypto)
24+
return 'ok'
25+
}
26+
`
27+
828
describe("FaaS Engine", () => {
929

1030
it("constructor", async () => {
1131
const engine = new FunctionEngine()
1232
assert(engine instanceof FunctionEngine)
1333
})
1434

15-
it("run code", async () => {
35+
it("run code 1", async () => {
36+
const engine = new FunctionEngine()
37+
38+
const result = ts.transpile(source2, {
39+
module: ts.ModuleKind.CommonJS,
40+
target: ts.ScriptTarget.ES2017
41+
})
42+
43+
const code = result
44+
const ret = await engine.run(code, {
45+
context: {}
46+
})
47+
assert.strictEqual(ret.data, 'ok')
48+
})
49+
50+
it("run code 2", async () => {
1651
const engine = new FunctionEngine()
1752

18-
const code = handler.toString()
19-
const ret = await engine.run(code, {})
20-
assert.strictEqual(ret, 'ok')
53+
const result = ts.transpile(source2, {
54+
module: ts.ModuleKind.CommonJS,
55+
target: ts.ScriptTarget.ES2017
56+
})
57+
58+
const code = result
59+
const ret = await engine.run(code, {
60+
context: {}
61+
})
62+
assert.strictEqual(ret.data, 'ok')
2163
})
2264

2365
})

tests/faas/func.js

-19
This file was deleted.

tests/faas/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const crypto = require("crypto");
4+
exports.main = async function (ctx) {
5+
console.log(ctx);
6+
console.log(crypto);
7+
return 'ok';
8+
};

tests/faas/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as crypto from 'crypto'
2+
3+
exports.main = async function (ctx: any) {
4+
console.log(ctx)
5+
console.log(crypto)
6+
return 'ok'
7+
}
8+
9+
10+
// tsc --lib es6 -t ES2017 -m commonjs --pretty index.ts

tests/faas/transpiler.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
const ts = require('typescript')
3+
4+
const source = `
5+
import * as crypto from 'crypto'
6+
7+
exports.main = async function (ctx: any) {
8+
console.log(ctx)
9+
console.log(crypto)
10+
return 'ok'
11+
}
12+
`
13+
14+
const result = ts.transpile(source, {
15+
module: ts.ModuleKind.CommonJS,
16+
target: ts.ScriptTarget.ES2017
17+
})
18+
19+
console.log(result)

0 commit comments

Comments
 (0)