Skip to content

Commit

Permalink
feat(gateway): add gateway service;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 12, 2021
1 parent 5063b2e commit 2b1debe
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 33 deletions.
55 changes: 29 additions & 26 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,6 @@ services:
networks:
- laf_shared_network

storage-service:
image: node:16-alpine
user: root
working_dir: /app
environment:
DB_URI : mongodb://root:password123@mongo:27017/laf-fs?authSource=admin&replicaSet=laf&writeConcern=majority
LOG_LEVEL : debug
command: npx nodemon
volumes:
- ./packages/storage-service:/app
- ./packages/database-proxy:/app/node_modules/database-proxy:ro
- ./packages/database-ql:/app/node_modules/database-ql:ro
- ./packages/database-ql:/app/node_modules/database-proxy/node_modules/database-ql:ro
ports:
- "9001"
depends_on:
- mongo
tmpfs:
- /tmp
restart: always
networks:
- laf_shared_network

system_server:
image: node:16-alpine
user: root
Expand Down Expand Up @@ -76,22 +53,48 @@ services:
networks:
- laf_shared_network

system_client:
image: nginx:latest
gateway:
image: openresty/openresty:latest
depends_on:
- mongo
- system_server
environment:
DEPLOY_DOMAIN: "*.local-dev.host" # `*.local-dev.host` always resolved to 127.0.0.1, used to local development
SYS_CLIENT_HOST: console.local-dev.host
volumes:
- ./packages/gateway/scripts:/scripts
- ./packages/system-client/dist:/app
- ./packages/system-client/nginx.conf:/etc/nginx/templates/default.conf.template
- ./packages/gateway/app.conf:/etc/nginx/templates/app.conf.template
- ./packages/gateway/system.conf:/etc/nginx/templates/system.conf.template
command: "sh /scripts/start.sh"
ports:
- 8080:80
networks:
- laf_shared_network

# storage-service:
# image: node:16-alpine
# user: root
# working_dir: /app
# environment:
# DB_URI : mongodb://root:password123@mongo:27017/laf-fs?authSource=admin&replicaSet=laf&writeConcern=majority
# LOG_LEVEL : debug
# command: npx nodemon
# volumes:
# - ./packages/storage-service:/app
# - ./packages/database-proxy:/app/node_modules/database-proxy:ro
# - ./packages/database-ql:/app/node_modules/database-ql:ro
# - ./packages/database-ql:/app/node_modules/database-proxy/node_modules/database-ql:ro
# ports:
# - "9001"
# depends_on:
# - mongo
# tmpfs:
# - /tmp
# restart: always
# networks:
# - laf_shared_network

volumes:
db-data:
grifs-data:
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
"docs:deploy": "npm run docs:build && sh ./scripts/deploy-docs.sh",
"docker-build-system-server": "cross-env PACKAGE=system-server node ./scripts/docker-build.js",
"docker-build-app-service": "cross-env PACKAGE=app-service node ./scripts/docker-build.js",
"docker-build-system-client": "cross-env PACKAGE=system-client node ./scripts/docker-build.js",
"docker-build": "npm run docker-build-system-server && npm run docker-build-app-service && npm run docker-build-system-client",
"docker-build-gateway": "cross-env PACKAGE=gateway node ./scripts/docker-build.js",
"docker-build": "npm run docker-build-system-server && npm run docker-build-app-service && npm run docker-build-gateway",
"docker-push-app-service": "cross-env PACKAGE=app-service node ./scripts/docker-push.js",
"docker-push-system-server": "cross-env PACKAGE=system-server node ./scripts/docker-push.js",
"docker-push-system-client": "cross-env PACKAGE=system-client node ./scripts/docker-push.js",
"docker-push": "npm run docker-push-app-service && npm run docker-push-system-server && npm run docker-push-system-client",
"docker-push-gateway": "cross-env PACKAGE=gateway node ./scripts/docker-push.js",
"docker-push": "npm run docker-push-app-service && npm run docker-push-system-server && npm run docker-push-gateway",
"docker-release-app-service": "cross-env PACKAGE=app-service node ./scripts/docker-release.js",
"docker-release-system-server": "cross-env PACKAGE=system-server node ./scripts/docker-release.js",
"docker-release-system-client": "cross-env PACKAGE=system-client node ./scripts/docker-release.js",
"docker-release": "npm run docker-release-app-service && npm run docker-release-system-server && npm run docker-release-system-client",
"docker-release-gateway": "cross-env PACKAGE=gateway node ./scripts/docker-release.js",
"docker-release": "npm run docker-release-app-service && npm run docker-release-system-server && npm run docker-release-gateway",
"docker-bpr": "npm run docker-build && npm run docker-push && npm run docker-release",
"clean:build": "rm -rf ./packages/*/dist",
"stats": "find ./packages -name '*.ts' | xargs cat | grep -v ^$| wc -l"
Expand Down
17 changes: 17 additions & 0 deletions packages/gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM openresty/openresty:latest
RUN apt-get update && apt-get install -y wget

# ENV DOCKERIZE_VERSION v0.6.1
# RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
# && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
# && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

WORKDIR /app
EXPOSE 80
ADD ./system.conf /etc/nginx/templates/system.conf.template
ADD ./app.conf /etc/nginx/templates/app.conf.template
ADD ./scripts /scripts

ADD ./dist /app/

CMD [ "sh", "/scripts/start.sh" ]
66 changes: 66 additions & 0 deletions packages/gateway/app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

# for app service
server {
listen 80;
server_name ${DEPLOY_DOMAIN};
client_max_body_size 0;

location / {

# Allow CORS
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
add_header Access-Control-Allow-Headers *;
add_header Access-Control-Expose-Headers *;
add_header Access-Control-Max-Age 3600;

if ($request_method = 'OPTIONS') {
return 204;
}

# Resolve app service
resolver 127.0.0.11;
if ($host ~* "(\w{8}(-\w{4}){3}-\w{12})\.(.+)$") {
set $appid $1;
set $service_id app_$appid;
}

proxy_pass http://$service_id:8000;
add_header appid $appid;
proxy_read_timeout 600s;
proxy_set_header Host $host;
proxy_http_version 1.1;

# Upgrade websocket
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";


# Logging
log_by_lua_block {
ngx.log(ngx.ERR, ngx.var.appid, ',', ngx.var.service_id, ',', ngx.var.request_uri, ',')
}
}

location /deploy/incoming {
# Allow CORS
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,PATCH,OPTIONS;
add_header Access-Control-Allow-Headers *;
add_header Access-Control-Expose-Headers *;
add_header Access-Control-Max-Age 3600;

if ($request_method = 'OPTIONS') {
return 204;
}

if ($host ~* "(\w{8}(-\w{4}){3}-\w{12})\.(.+)$") {
set $appid $1;
proxy_pass http://system_server:9000/apps/$appid/deploy/incoming;
add_header appid $appid;
}

proxy_send_timeout 600s;
proxy_read_timeout 600s;
}
}
10 changes: 10 additions & 0 deletions packages/gateway/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "gateway",
"version": "0.6.12",
"description": "",
"main": "index.js",
"private": true,
"scripts": {
},
"author": "Maslow(wangfugen@126.com)"
}
32 changes: 32 additions & 0 deletions packages/gateway/scripts/auto_envsubst.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

set -e

ME=$(basename $0)

auto_envsubst() {
local template_dir="${NGINX_ENVSUBST_TEMPLATE_DIR:-/etc/nginx/templates}"
local suffix="${NGINX_ENVSUBST_TEMPLATE_SUFFIX:-.template}"
local output_dir="${NGINX_ENVSUBST_OUTPUT_DIR:-/etc/nginx/conf.d}"

local template defined_envs relative_path output_path subdir
defined_envs=$(printf '${%s} ' $(env | cut -d= -f1))
[ -d "$template_dir" ] || return 0
if [ ! -w "$output_dir" ]; then
echo "$ME: ERROR: $template_dir exists, but $output_dir is not writable"
return 0
fi
find "$template_dir" -follow -type f -name "*$suffix" -print | while read -r template; do
relative_path="${template#$template_dir/}"
output_path="$output_dir/${relative_path%$suffix}"
subdir=$(dirname "$relative_path")
# create a subdirectory where the template file exists
mkdir -p "$output_dir/$subdir"
echo "$ME: Running envsubst on $template to $output_path"
envsubst "$defined_envs" < "$template" > "$output_path"
done
}

auto_envsubst

exit 0
5 changes: 5 additions & 0 deletions packages/gateway/scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/sh

sh /scripts/auto_envsubst.sh

/usr/bin/openresty -g "daemon off;"
18 changes: 18 additions & 0 deletions packages/gateway/system.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# for system client & server
server {
listen 80;
server_name localhost ${SYS_CLIENT_HOST};
client_max_body_size 0;

root /app;
location / {
index index.html index.htm;
}

location /sys-api/ {
proxy_pass http://system_server:9000/;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
}
}
2 changes: 1 addition & 1 deletion scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const child_process = require('child_process')
const images = {
'system-server': 'lafyun/system-server',
'app-service': 'lafyun/app-service',
'system-client': 'lafyun/system-client'
'gateway': 'lafyun/gateway'
}

function getPackageVersion(packagePath) {
Expand Down

0 comments on commit 2b1debe

Please sign in to comment.