diff --git a/Dockerfile b/Dockerfile index 88a3df5..51f8274 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,7 @@ -FROM node:14-alpine +FROM node:lts-alpine +COPY svr /app/svr +WORKDIR /app/svr +RUN npm install --registry=https://registry.npmmirror.com && npm run build:pro -WORKDIR /home/tlrtcfile -ADD . . - -RUN npm conf set registry https://registry.npm.taobao.org; \ - npm install -g pm2; \ - cd /home/tlrtcfile/svr; \ - npm install; - -EXPOSE 9092 8444 - -CMD ["/bin/sh", "docker-entrypoint.sh"] +ENTRYPOINT ["node"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..03a2713 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3' +services: + api: + env_file: + - local.env + build: . + ports: + - 9092:9092 + links: + - socket + command: localapi + socket: + env_file: + - local.env + build: . + ports: + - 8444:8444 + command: localsocket diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh deleted file mode 100644 index ec008d5..0000000 --- a/docker-entrypoint.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -cd /home/tlrtcfile/svr - -npm i - -pm2 start npm --name=tlapi -- run lapi - -pm2 start npm --name=tlsocket -- run lsocket - -npm run build:pro - -sleep 2 - -pm2 logs - - - diff --git a/local.env b/local.env new file mode 100644 index 0000000..c719131 --- /dev/null +++ b/local.env @@ -0,0 +1,6 @@ +ENV_MODE=local +WEBRTC_STUN_HOST=stun:stun.xten.com +WEBRTC_TURN_HOST=turn:global.turn.twilio.com:3478?transport=udp +WEBRTC_TURN_USERNAME=dc2d2894d5a9023620c467b0e71cfa6a35457e6679785ed6ae9856fe5bdfa269 +WEBRTC_TURN_CREDENTIAL=tE2DajzSJwnsSbc123 +WS_HOST=ws://127.0.0.1:8444 \ No newline at end of file diff --git a/svr/conf/env_config.js b/svr/conf/env_config.js new file mode 100644 index 0000000..d0dcfba --- /dev/null +++ b/svr/conf/env_config.js @@ -0,0 +1,23 @@ +const inject_env_config=(conf)=>{ + Object.keys(process.env).filter(key=>/^(WS(S)?_|API_|WEBRTC_).+/.test(key)).map(key=>{ + let data=process.env[key] + if(key.endsWith('_PORT')){ + data=parseInt(data) + } + let curr=conf; + const paths=key.split('_').map(p=>p.toLowerCase()) + const last=paths.pop() + for (const path of paths){ + curr=curr[path] + } + if(curr){ + console.log(`config ${paths.join('.')}.${last} to ${data}`); + curr[last]=data + } + + }) + return conf +} +module.exports = { + inject_env_config +} diff --git a/svr/localapi.js b/svr/localapi.js index 0a656dc..f6fb4cd 100644 --- a/svr/localapi.js +++ b/svr/localapi.js @@ -1,5 +1,6 @@ const express = require("express"); -const conf = require("./conf/cfg.json"); +const { inject_env_config } = require("./conf/env_config"); +const conf = inject_env_config(require("./conf/cfg.json")); const fileApiRouters = require("./src/controller/router")(); const db = require("./src/tables/db"); //db const utils = require("./src/utils/utils"); diff --git a/svr/localsocket.js b/svr/localsocket.js index a5af49e..5b35a20 100644 --- a/svr/localsocket.js +++ b/svr/localsocket.js @@ -1,7 +1,8 @@ const http = require('http'); // http const socketIO = require('socket.io'); //socket const db = require("./src/tables/db"); //db -const conf = require("./conf/cfg.json"); //conf +const { inject_env_config } = require("./conf/env_config"); +const conf = inject_env_config(require("./conf/cfg.json")); //conf const socket = require("./src/socket/index") //socket handler const utils = require("./src/utils/utils");