diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2bc9958 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +FROM alpine:latest + +ENV APP_VERSION=1.0.0 \ + APP_HOME=/opt \ + APP_NAME=overflow_server_app + +ENV APP_CONFIG_PATH="${APP_HOME}/config" \ + APP_LOGS_PATH="${APP_HOME}/logs" \ + PATH="$PATH:${APP_HOME}/bin" + +ADD dist/${APP_NAME} ${APP_HOME}/bin/ +ADD _docker/bin/*.sh ${APP_HOME}/bin/ + +RUN apk add --no-cache curl \ + && chmod +x ${APP_HOME}/bin/*.sh \ + && mkdir -p ${APP_LOGS_PATH} + +ENV TINI_VERSION='0.15.0' \ + TINI_SHA='4007655082f573603c02bc1d2137443c8e153af047ffd088d02ccc01e6f06170' + +# Use tini as subreaper in Docker container to adopt zombie processes +RUN curl -fsSL https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini-static-amd64 -o /bin/tini \ + && chmod +x /bin/tini \ + && echo "$TINI_SHA /bin/tini" | sha256sum -c - + +VOLUME ${APP_CONFIG_PATH} +VOLUME ${APP_LOGS_PATH} + +EXPOSE 80 443 + +ENTRYPOINT ["/bin/tini", "--"] +CMD ["docker-entrypoint.sh"] + + +# docker build -f Dockerfile -t docker.loafle.net/overflow/overflow_service_websocket:1.0.0-alpine . +# docker run -d -p 18081:80 -t docker.loafle.net/overflow/overflow_service_websocket:1.0.0-alpine diff --git a/_docker/bin/docker-entrypoint.sh b/_docker/bin/docker-entrypoint.sh new file mode 100644 index 0000000..11e783b --- /dev/null +++ b/_docker/bin/docker-entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -e + +/opt/bin/overflow_server_app -config=/opt/config/ + +exec "$@" \ No newline at end of file diff --git a/_docker/config/config.json b/_docker/config/config.json new file mode 100644 index 0000000..1657414 --- /dev/null +++ b/_docker/config/config.json @@ -0,0 +1,45 @@ +{ + "server": { + "addr": ":80", + "tls": false + }, + "auth": { + "signingKey": "tWB0lUXiCwX4U3qsJZcZ10mKvEH793RHkTJDbDuZVshQTk4uNB6ck59UQ96lhsRi4XNUiEnlIbP8XYQMPabeNtERX3iyHeDcwocgUVAor1nkAajYeq1gNyJszGpMhEOT" + }, + "grpc": { + "addr": "192.168.1.50:50006", + "tls": false, + "pool": { + "MaxIdle": 1, + "MaxCapacity": 3, + "increaseCapacity": 10 + } + }, + "logging": { + "level": "debug", + "development": true, + "disableCaller": true, + "disableStacktrace": true, + "sampling": { + "initial": 100, + "thereafter": 100 + }, + "encoding": "console", + "encoderConfig": { + "messageKey": "message", + "levelKey": "level", + "timeKey": "time", + "nameKey": "name", + "callerKey": "caller", + "stacktraceKey": "stacktrace", + "lineEnding": "\n", + "levelEncoder": "color", + "timeEncoder": "ISO8601", + "durationEncoder": "string", + "callerEncoder": "full", + "nameEncoder": "full" + }, + "outputPaths": ["/opt/logs/log"], + "errorOutputPaths": ["stderr"] + } +} \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..29a49d4 --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +rm ./dist/* +CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o ./dist/overflow_server_app \ No newline at end of file diff --git a/config.json b/config.json index 83a2ad8..b016136 100644 --- a/config.json +++ b/config.json @@ -7,7 +7,7 @@ "signingKey": "tWB0lUXiCwX4U3qsJZcZ10mKvEH793RHkTJDbDuZVshQTk4uNB6ck59UQ96lhsRi4XNUiEnlIbP8XYQMPabeNtERX3iyHeDcwocgUVAor1nkAajYeq1gNyJszGpMhEOT" }, "grpc": { - "addr": "127.0.0.1:50006", + "addr": "192.168.1.50:50006", "tls": false, "pool": { "MaxIdle": 1, diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9c045d4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,33 @@ +version: "3" + +services: + overflow_server_app: + restart: always + image: docker.loafle.net/overflow/overflow_server_app:1.0.0-SNAPSHOT + container_name: overflow_server_app + volumes: + - /service/overflow_server_app/data/opt/config:/opt/config + - /service/overflow_server_app/data/opt/logs:/opt/logs + # - /home/crusader/Temp/docker/overflow_gateway_web/config:/opt/config + # - /home/crusader/Temp/docker/overflow_gateway_web/logs:/opt/logs + ports: + - "19080:80" + - "19543:443" + +# postgresql: +# restart: always +# image: postgres:9.6-alpine +# container_name: overFlow-dao-postgres +# environment: +# - POSTGRES_DB=overflow +# - POSTGRES_USER=overflow +# - POSTGRES_PASSWORD=qwer5795 +# # - POSTGRES_INITDB_ARGS="--data-checksums" +# ports: +# - "5432:5432" + +# docker-compose up -d +# docker-compose stop +# docker-compose rm +# or +# docker-compose -f ./docker-compose.yml up -d \ No newline at end of file diff --git a/main.go b/main.go index 6ac8a57..93a7c1a 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "flag" "log" "go.uber.org/zap" @@ -20,7 +21,11 @@ import ( var logger *zap.Logger func main() { - loadConfig() + configPath := flag.String("config", ".", "The path of config file") + + flag.Parse() + + loadConfig(*configPath) ctx := newContext() defer logger.Sync() @@ -35,12 +40,12 @@ func main() { s.Route("POST", "/account/reset_password", member.ResetPassword) s.Route("GET", "/account/check_email", member.CheckEmail) - fasthttp.ListenAndServe(":19080", c.Handler(s.Handler)) + fasthttp.ListenAndServe(config.GetString("server.addr"), c.Handler(s.Handler)) } -func loadConfig() { +func loadConfig(path string) { config.SetConfigName("config") - config.AddConfigPath(".") + config.AddConfigPath(path) err := config.ReadInConfig() if nil != err { log.Fatalf("config error: %v", err)