From 7f951cd42de9e9e9486c3841cb0669144f4fc663 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Mon, 13 Oct 2025 19:32:31 +0200 Subject: [PATCH] Migrate to K8S stack and fixes before big refactoring --- .app.config | 16 --- .env.dist | 15 --- .env.local | 11 -- .gitignore | 10 +- Dockerfile.builder | 3 +- Dockerfile.target | 8 +- Makefile | 10 +- README.md | 27 +++- bin/entrypoint.sh | 23 ---- bin/wait-for-it.sh | 165 ------------------------- deploy/.env.dist | 13 ++ deploy/certs/.gitkeep | 0 deploy/image-build.sh | 27 ++-- deploy/image-push.sh | 6 +- postfile.txt | 4 - src/cmd/migrate/main.go | 11 +- src/cmd/server/main.go | 12 +- src/go.mod | 6 +- src/go.sum | 8 +- src/internal/app/app.go | 6 +- src/internal/app/plugins.go | 86 +++---------- src/internal/server/config.go | 43 +++---- src/internal/server/config_handler.go | 9 -- src/internal/server/health_handler.go | 22 ++++ src/internal/server/login_handler.go | 2 +- src/internal/server/middleware.go | 2 +- src/internal/server/router.go | 2 - src/internal/server/server.go | 51 ++------ src/internal/server/traefik_handler.go | 31 ----- src/internal/service/auth.go | 2 +- src/internal/service/jwt.go | 2 +- 31 files changed, 163 insertions(+), 470 deletions(-) delete mode 100644 .app.config delete mode 100644 .env.dist delete mode 100644 .env.local delete mode 100755 bin/wait-for-it.sh create mode 100644 deploy/.env.dist create mode 100644 deploy/certs/.gitkeep delete mode 100644 postfile.txt delete mode 100644 src/internal/server/config_handler.go delete mode 100644 src/internal/server/traefik_handler.go diff --git a/.app.config b/.app.config deleted file mode 100644 index d3f058c..0000000 --- a/.app.config +++ /dev/null @@ -1,16 +0,0 @@ -{ - "ID": "identity", - "Name": "identity", - "Address": "__IP__", - "Tags": ["identity-svc", "identity", "https", "service"], - "Port": 443, - "Connect": { - "Native": true - }, - "Check": { - "TCP": "__IP__:443", - "Interval": "5s", - "Timeout": "1s", - "DeregisterCriticalServiceAfter": "10s" - } -} \ No newline at end of file diff --git a/.env.dist b/.env.dist deleted file mode 100644 index c78c02f..0000000 --- a/.env.dist +++ /dev/null @@ -1,15 +0,0 @@ -SERVER_ADDR=:443 - -APP_NAME=identity-svc -APP_DOMAIN=identity.service.ego.io -REGISTRY_USE_DOMAIN_OVER_IP=false -APP_PATH_PREFIX=/identity -APP_KV_NAMESPACE=dev.egommerce/service/identity-svc - -LOGGER_ADDR=api-logger:24224 -REGISTRY_ADDR=api-registry:8501 -DATABASE_URL=postgres://postgres:12345678@postgres-db:5432/egommerce -CACHE_ADDR=api-cache:6379 -CACHE_PASSWORD=12345678 -MONGODB_URL=mongodb://mongodb:12345678@mongo-db:27017 -# EVENTBUS_URL=amqp://guest:guest@api-eventbus:5672 \ No newline at end of file diff --git a/.env.local b/.env.local deleted file mode 100644 index 9c533e8..0000000 --- a/.env.local +++ /dev/null @@ -1,11 +0,0 @@ -SERVER_ADDR=:80 - -APP_NAME=identity-svc -APP_DOMAIN=auth.egosport -APP_PATH_PREFIX= -APP_KV_NAMESPACE=egosport/identity-svc - -LOGGER_ADDR=logger.egosport:24224 -DATABASE_URL=postgres://egosport:12345678@db.egosport:5432/egosport -CACHE_ADDR=cache.egosport:6379 -CACHE_PASSWORD=12345678 diff --git a/.gitignore b/.gitignore index 4c1faf5..d4b204a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,10 @@ -.env -.env.* -!.env.dist +deploy/.env +!deploy/.env.dist +deploy/.env.* + +deploy/server +!deploy/certs/.gitkeep +deploy/certs/* .vscode/ __debug_bin \ No newline at end of file diff --git a/Dockerfile.builder b/Dockerfile.builder index 5d968b3..fbdd422 100644 --- a/Dockerfile.builder +++ b/Dockerfile.builder @@ -11,6 +11,5 @@ COPY src ./ RUN export CGO_ENABLED=0 ; export GOOS=linux ; export GOARCH=amd64 && \ go build -ldflags="-w -s" -o "$BIN_OUTPUT/server" $GO_SERVER && \ - go build -ldflags="-w -s" -o "$BIN_OUTPUT/migrate" $GO_MIGRATE && \ - go build -ldflags="-w -s" -o "$BIN_OUTPUT/health" $GO_HEALTH + go build -ldflags="-w -s" -o "$BIN_OUTPUT/migrate" $GO_MIGRATE \ No newline at end of file diff --git a/Dockerfile.target b/Dockerfile.target index 1980273..d188784 100644 --- a/Dockerfile.target +++ b/Dockerfile.target @@ -20,10 +20,10 @@ LABEL dev.egommerce.image.build_time=${BUILD_TIME} WORKDIR / COPY --from=builder $BIN_OUTPUT /app COPY --from=builder /go/bin/migrate /bin/migrate -COPY --from=builder /go/bin/health /bin/health -COPY .env.docker /.env -COPY ./.app.config / +COPY deploy/.env.docker /.env COPY ./bin /bin +# COPY deploy/certs /certs + RUN chmod 755 /bin/entrypoint.sh /bin/migrate.sh RUN apk add curl @@ -33,4 +33,4 @@ EXPOSE 443 ENTRYPOINT ["entrypoint.sh"] CMD ["sh", "-c", "/app"] -HEALTHCHECK --interval=5s --timeout=1s --retries=20 CMD health >/dev/null || exit 1 +# HEALTHCHECK --interval=5s --timeout=1s --retries=20 CMD health >/dev/null || exit 1 diff --git a/Makefile b/Makefile index 86e36c3..6691faa 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ DEPLOY_DIR := ./deploy SRC_DIR := ./src -## DEPLOY PART build-image-dev: - sh ${DEPLOY_DIR}/image-build.sh dev @@ -14,9 +13,8 @@ push-image-dev: push-image-prod: - sh ${DEPLOY_DIR}/image-push.sh -# (GOLANG) APP PART -app-run: - - make -C ${SRC_DIR} run +build-local: + - go build -C ${SRC_DIR} -o ../deploy/server cmd/server/main.go -app-build: - - make -C ${SRC_DIR} build +run-local: + - cd deploy/ && ./server diff --git a/README.md b/README.md index 011101a..caf79a3 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,27 @@ Authentication & Authorization service -Budowanie obrazu: -$ sh deploy/image-build.sh [docker_tag] +Prepare local dev environment -Opublikowanie obrazu: -$ sh deploy/image-push.sh [docker_tag] +1. Add following entry to the /etc/hosts file + ```127.0.0.1 egommerce.local``` +2. Copy or link .env.local to the .env file +3. Link the cert and key files from stack (./deploy/certs/identity-svc/) to the local ./deploy/certs dir: + ```ln -s PATH_TO_THE_STACK_DIR/deploy/certs/identity-svc/identity-svc.crt deploy/certs/identity-svc.crt``` + ```ln -s PATH_TO_THE_STACK_DIR/deploy/certs/identity-svc/identity-svc.key deploy/certs/identity-svc.key``` +4. Build server app binary: + ```make build-local``` +5. Run server app: + ```make run-local``` + +Build prod image: +$ make build-image-prod + +Push prod image: +$ make push-image-prod + +Build dev image: +$ make build-image-dev + +Push dev image: +$ make push-image-dev diff --git a/bin/entrypoint.sh b/bin/entrypoint.sh index 7201635..15a6a8a 100755 --- a/bin/entrypoint.sh +++ b/bin/entrypoint.sh @@ -1,27 +1,4 @@ #!/usr/bin/env sh -set +e - -waitForService() -{ - wait-for-it.sh $1 -t 2 1>/dev/null 2>&1 - status=$? - while [ $status != 0 ] - do - echo "[x] wating for $1..." - sleep 1 - wait-for-it.sh $1 -t 2 1>/dev/null 2>&1 - status=$? - done -} - -update-resolv # provided by stack - better approach - single copy -update-ca-certificates - -waitForService "api-registry:8501" -waitForService "api-logger:24224" -waitForService "db-postgres:5432" - -register-service # run migrations migrate.sh diff --git a/bin/wait-for-it.sh b/bin/wait-for-it.sh deleted file mode 100755 index fff13c9..0000000 --- a/bin/wait-for-it.sh +++ /dev/null @@ -1,165 +0,0 @@ -#!/usr/bin/env sh -# Use this script to test if a given TCP host/port are available - -set -e - -cmdname=$(basename "$0") - -echoerr() { - if [ "$QUIET" -ne 1 ]; then - printf "%s\n" "$*" 1>&2; - fi -} - -usage() -{ - exitcode="$1" - cat << USAGE >&2 -Usage: - $cmdname host:port [-s] [-t timeout] [-- command args] - -h HOST | --host=HOST Host or IP under test - -p PORT | --port=PORT TCP port under test - Alternatively, you specify the host and port as host:port - -s | --strict Only execute subcommand if the test succeeds - -q | --quiet Don't output any status messages - -t TIMEOUT | --timeout=TIMEOUT - Timeout in seconds, zero for no timeout - -- COMMAND ARGS Execute command with args after the test finishes -USAGE - exit "$exitcode" -} - -wait_for() -{ - if [ "$TIMEOUT" -gt 0 ]; then - echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT" - else - echoerr "$cmdname: waiting for $HOST:$PORT without a timeout" - fi - start_ts=$(date +%s) - while true - do - nc -z "$HOST" "$PORT" >/dev/null 2>&1 - result=$? - if [ $result -eq 0 ]; then - end_ts=$(date +%s) - echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds" - break - fi - sleep 1 - done - return $result -} - -wait_for_wrapper() -{ - # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 - if [ "$QUIET" -eq 1 ]; then - timeout "$TIMEOUT" "$0" -q -child "$HOST":"$PORT" -t "$TIMEOUT" & - else - timeout "$TIMEOUT" "$0" --child "$HOST":"$PORT" -t "$TIMEOUT" & - fi - PID=$! - trap 'kill -INT -$PID' INT - wait $PID - RESULT=$? - if [ $RESULT -ne 0 ]; then - echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT" - fi - return $RESULT -} - -TIMEOUT=15 -STRICT=0 -CHILD=0 -QUIET=0 -# process arguments -while [ $# -gt 0 ] -do - case "$1" in - *:* ) - HOST=$(printf "%s\n" "$1"| cut -d : -f 1) - PORT=$(printf "%s\n" "$1"| cut -d : -f 2) - shift 1 - ;; - --child) - CHILD=1 - shift 1 - ;; - -q | --quiet) - QUIET=1 - shift 1 - ;; - -s | --strict) - STRICT=1 - shift 1 - ;; - -h) - HOST="$2" - if [ "$HOST" = "" ]; then break; fi - shift 2 - ;; - --host=*) - HOST=$(printf "%s" "$1" | cut -d = -f 2) - shift 1 - ;; - -p) - PORT="$2" - if [ "$PORT" = "" ]; then break; fi - shift 2 - ;; - --port=*) - PORT="${1#*=}" - shift 1 - ;; - -t) - TIMEOUT="$2" - if [ "$TIMEOUT" = "" ]; then break; fi - shift 2 - ;; - --timeout=*) - TIMEOUT="${1#*=}" - shift 1 - ;; - --) - shift - break - ;; - --help) - usage 0 - ;; - *) - echoerr "Unknown argument: $1" - usage 1 - ;; - esac -done - -if [ "$HOST" = "" -o "$PORT" = "" ]; then - echoerr "Error: you need to provide a host and port to test." - usage 2 -fi - -if [ $CHILD -gt 0 ]; then - wait_for - RESULT=$? - exit $RESULT -else - if [ "$TIMEOUT" -gt 0 ]; then - wait_for_wrapper - RESULT=$? - else - wait_for - RESULT=$? - fi -fi - -if [ "$*" != "" ]; then - if [ $RESULT -ne 0 -a $STRICT -eq 1 ]; then - echoerr "$cmdname: strict mode, refusing to execute subprocess" - exit $RESULT - fi - exec "$@" -else - exit $RESULT -fi diff --git a/deploy/.env.dist b/deploy/.env.dist new file mode 100644 index 0000000..32a81dc --- /dev/null +++ b/deploy/.env.dist @@ -0,0 +1,13 @@ +SERVER_ADDR=:443 + +APP_NAME=identity-svc +APP_DOMAIN=identity.service.ego.io +APP_PATH_PREFIX=/identity + +API_LOGGER_ADDR=api-logger:24224 +API_DATABASE_URL=postgres://postgres:12345678@db-postgres:5432/egommerce +API_CACHE_ADDR=api-cache:6379 +API_CACHE_USERNAME=default +API_CACHE_PASSWORD=12345678 +API_MONGODB_URL=mongodb://mongodb:12345678@mongo-db:27017 +# EVENTBUS_URL=amqp://guest:guest@api-eventbus:5672 \ No newline at end of file diff --git a/deploy/certs/.gitkeep b/deploy/certs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/deploy/image-build.sh b/deploy/image-build.sh index 5c021c8..0668f75 100755 --- a/deploy/image-build.sh +++ b/deploy/image-build.sh @@ -1,11 +1,11 @@ #!/bin/sh # RUN IN REPO ROOT DIR !! -export IMAGE_PREFIX="git.pbiernat.io/egommerce/identity" +export IMAGE_PREFIX="git.ego.freeddns.org/egommerce/identity" export BUILDER_IMAGE="egommerce-builder:identity" export BUILD_TIME=$(date +"%Y%m%d%H%M%S") export SERVER_IMAGE="$IMAGE_PREFIX-svc" -export DOCKER_BUILDKIT=1 +# export DOCKER_BUILDKIT=1 TARGET=${1:-latest} @@ -16,14 +16,25 @@ docker build --rm -t $BUILDER_IMAGE -f Dockerfile.builder . if [ $TARGET = "latest" ] then - # SERVER - docker build --build-arg SVC_NAME=identity-service --build-arg SVC_VER="1.0" --build-arg BIN_OUTPUT=/go/bin/server \ - --build-arg BUILDER_IMAGE=$BUILDER_IMAGE --build-arg BUILD_TIME --rm --cache-from $SERVER_IMAGE:$TARGET -t $SERVER_IMAGE:$TARGET \ + # PROD + docker build \ + --build-arg SVC_NAME=identity-service \ + --build-arg SVC_VER="1.0" \ + --build-arg BIN_OUTPUT=/go/bin/server \ + --build-arg BUILDER_IMAGE=$BUILDER_IMAGE \ + --build-arg BUILD_TIME \ + --rm --cache-from $SERVER_IMAGE:$TARGET \ + -t $SERVER_IMAGE:$TARGET \ -f Dockerfile.target . >/dev/null 2>&1 && echo "Successfully tagged $SERVER_IMAGE:$TARGET" else - # SERVER - docker build --build-arg SVC_NAME=identity-service --build-arg SVC_VER="dev" --build-arg BIN_OUTPUT=/go/bin/server \ - --build-arg BUILDER_IMAGE=$BUILDER_IMAGE --build-arg BUILD_TIME --rm --no-cache -t $SERVER_IMAGE:$TARGET \ + # DEV + docker build \ + --build-arg SVC_NAME=identity-service \ + --build-arg SVC_VER="dev" \ + --build-arg BIN_OUTPUT=/go/bin/server \ + --build-arg BUILDER_IMAGE=$BUILDER_IMAGE \ + --build-arg BUILD_TIME \ + --rm --no-cache -t $SERVER_IMAGE:$TARGET \ -f Dockerfile.target . >/dev/null 2>&1 && echo "Successfully tagged $SERVER_IMAGE:$TARGET" fi diff --git a/deploy/image-push.sh b/deploy/image-push.sh index 6543fb4..617395a 100755 --- a/deploy/image-push.sh +++ b/deploy/image-push.sh @@ -1,16 +1,14 @@ #!/bin/sh # RUN IN REPO ROOT DIR !! -export IMAGE_PREFIX="git.pbiernat.io/egommerce/identity" +export IMAGE_PREFIX="git.ego.freeddns.org/egommerce/identity" export SERVER_IMAGE="$IMAGE_PREFIX-svc" -# export WORKER_IMAGE="$IMAGE_PREFIX-worker" TARGET=${1:-latest} -echo $DOCKER_PASSWORD | docker login git.pbiernat.io -u $DOCKER_USERNAME --password-stdin +echo $DOCKER_PASSWORD | docker login git.ego.freeddns.org -u $DOCKER_USERNAME --password-stdin docker push "$SERVER_IMAGE:$TARGET" -# docker push "$WORKER_IMAGE:$TARGET" # Restart container curl -X POST http://127.0.0.1:9001/api/webhooks/64ea5d78-ae21-474c-ad4d-1d98f6b83acb diff --git a/postfile.txt b/postfile.txt deleted file mode 100644 index 9fa06d2..0000000 --- a/postfile.txt +++ /dev/null @@ -1,4 +0,0 @@ -{ - "product_id": 1, - "quantity": 1 -} \ No newline at end of file diff --git a/src/cmd/migrate/main.go b/src/cmd/migrate/main.go index b5cf4eb..e6d13e3 100644 --- a/src/cmd/migrate/main.go +++ b/src/cmd/migrate/main.go @@ -9,17 +9,14 @@ import ( "github.com/go-pg/migrations/v8" "github.com/go-pg/pg/v10" - "git.pbiernat.io/egommerce/go-api-pkg/fluentd" + "git.ego.freeddns.org/egommerce/go-api-pkg/fluentd" - baseCnf "git.pbiernat.io/egommerce/go-api-pkg/config" - cnf "git.pbiernat.io/egommerce/identity-service/internal/server" + baseCnf "git.ego.freeddns.org/egommerce/go-api-pkg/config" + cnf "git.ego.freeddns.org/egommerce/identity-service/internal/server" ) const ( - defAppName = "identity-service-migrations" defMigrationsTableName = "identity.migrations" - defLoggerAddr = "api-logger:24224" - // defKVNmspc = "dev.egommerce/service/identity-migration" ) const usageText = `This program runs command on the db. Supported commands are: @@ -43,7 +40,7 @@ func main() { flag.Parse() if baseCnf.ErrLoadingEnvs != nil { - log.Panicln("Error loading .env file", baseCnf.ErrLoadingEnvs) + log.Panicln(baseCnf.ErrLoadingEnvs) } c := cnf.NewConfig("migrator") diff --git a/src/cmd/server/main.go b/src/cmd/server/main.go index 55fc5ac..9592f4e 100644 --- a/src/cmd/server/main.go +++ b/src/cmd/server/main.go @@ -5,18 +5,18 @@ import ( "log" "os" - cnf "git.pbiernat.io/egommerce/go-api-pkg/config" + cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config" - "git.pbiernat.io/egommerce/identity-service/internal/app" - "git.pbiernat.io/egommerce/identity-service/internal/server" + "git.ego.freeddns.org/egommerce/identity-service/internal/app" + "git.ego.freeddns.org/egommerce/identity-service/internal/server" ) func main() { if cnf.ErrLoadingEnvs != nil { - log.Panicln("Error loading .env file", cnf.ErrLoadingEnvs) + log.Panicln(cnf.ErrLoadingEnvs) } - c := server.NewConfig("identity") + c := server.NewConfig("identity-svc") cArr := c.GetArray() doer := server.New(c) @@ -24,8 +24,6 @@ func main() { a.RegisterPlugin(app.LoggerPlugin(cArr)) a.RegisterPlugin(app.CachePlugin(cArr)) a.RegisterPlugin(app.DatabasePlugin(cArr)) - // a.RegisterPlugin(app.EventbusPlugin(cArr)) - // a.RegisterPlugin(app.RegistryPlugin(cArr)) while := make(chan struct{}) err := a.Start(while) diff --git a/src/go.mod b/src/go.mod index b171829..899bd38 100644 --- a/src/go.mod +++ b/src/go.mod @@ -1,10 +1,10 @@ -module git.pbiernat.io/egommerce/identity-service +module git.ego.freeddns.org/egommerce/identity-service go 1.18 require ( - git.pbiernat.io/egommerce/api-entities v0.2.3 - git.pbiernat.io/egommerce/go-api-pkg v0.3.24 + git.ego.freeddns.org/egommerce/api-entities v0.3.0 + git.ego.freeddns.org/egommerce/go-api-pkg v0.4.6 github.com/go-pg/migrations/v8 v8.1.0 github.com/go-pg/pg/v10 v10.11.1 github.com/go-redis/redis/v8 v8.11.5 diff --git a/src/go.sum b/src/go.sum index 14121b5..54ddba8 100644 --- a/src/go.sum +++ b/src/go.sum @@ -5,10 +5,10 @@ cloud.google.com/go/compute v1.23.0/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdi cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY= cloud.google.com/go/iam v1.1.1 h1:lW7fzj15aVIXYHREOqjRBV9PsH0Z6u8Y46a1YGvQP4Y= cloud.google.com/go/iam v1.1.1/go.mod h1:A5avdyVL2tCppe4unb0951eI9jreack+RJ0/d+KUZOU= -git.pbiernat.io/egommerce/api-entities v0.2.3 h1:mR6EYfZkAzh4teydb7KXDBWoxwVW3qasnmmH5J3mnas= -git.pbiernat.io/egommerce/api-entities v0.2.3/go.mod h1:INXAG5x4+i+vNwg1NpfPHiDW8nY1kn1K7pgLOtX+/I0= -git.pbiernat.io/egommerce/go-api-pkg v0.3.24 h1:if6xsFOStckWdGm7kcgKWOhOGOe6iIBOOr+3DjgX5tM= -git.pbiernat.io/egommerce/go-api-pkg v0.3.24/go.mod h1:XIy2mmvRNIzQmYIUAcDZafhRPxTQFS2HDmsK7ZQ6980= +git.ego.freeddns.org/egommerce/api-entities v0.3.0 h1:IhJNOfze8/D8Hgy8Mr9hoFEwrg45xeFSnVRUnUrC5xc= +git.ego.freeddns.org/egommerce/api-entities v0.3.0/go.mod h1:IqynARw+06GOm4eZGZuepmbi7bUxWBnOB4jd5cI7jf8= +git.ego.freeddns.org/egommerce/go-api-pkg v0.4.6 h1:1iZW+vkbv7fQusv/pMjtIM1QvJ+QQr3nyvuuajgHc80= +git.ego.freeddns.org/egommerce/go-api-pkg v0.4.6/go.mod h1:5Ft8LCd0UXp5hHpvXRBCv9mCGikogFhL7LP2qit12JM= github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU= github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs= github.com/Azure/go-autorest/autorest v0.11.28 h1:ndAExarwr5Y+GaHE6VCaY1kyS/HwwGGyuimVhWsHOEM= diff --git a/src/internal/app/app.go b/src/internal/app/app.go index 26225c9..957fed1 100644 --- a/src/internal/app/app.go +++ b/src/internal/app/app.go @@ -1,6 +1,7 @@ package app import ( + "fmt" "log" "os" "os/signal" @@ -36,14 +37,15 @@ func (a *App) Start(while chan struct{}) error { sigint := make(chan os.Signal, 1) signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) <-sigint + fmt.Println("Received signal:", sigint) a.Shutdown() close(while) }() - run := a.createRunFile("./app.run") // FIXME path... - defer a.removeRunFile(run) + runFile := a.createRunFile("./app.run") // FIXME path... + defer a.removeRunFile(runFile) err := a.doer.Start() if err != nil { diff --git a/src/internal/app/plugins.go b/src/internal/app/plugins.go index 1a66260..9d86b67 100644 --- a/src/internal/app/plugins.go +++ b/src/internal/app/plugins.go @@ -3,14 +3,11 @@ package app import ( "log" "os" - "strconv" + "time" + "git.ego.freeddns.org/egommerce/go-api-pkg/fluentd" + db "git.ego.freeddns.org/egommerce/identity-service/pkg/database" redis "github.com/go-redis/redis/v8" - - "git.pbiernat.io/egommerce/go-api-pkg/consul" - "git.pbiernat.io/egommerce/go-api-pkg/fluentd" - - db "git.pbiernat.io/egommerce/identity-service/pkg/database" ) type ( @@ -22,13 +19,16 @@ type ( ) func CachePlugin(cArr map[string]string) Plugin { + // fmt.Println(cArr["cacheAddr"], cArr["cacheUsername"], cArr["cachePassword"]) return Plugin{ name: "cache", - fn: func() any { + fn: func() any { // FIXME: return type return redis.NewClient(&redis.Options{ - Addr: cArr["cacheAddr"], - Password: cArr["cachePassword"], - DB: 0, + Addr: cArr["cacheAddr"], + Username: cArr["cacheUsername"], + Password: cArr["cachePassword"], + DB: 0, + DialTimeout: 100 * time.Millisecond, }) }, } @@ -37,12 +37,12 @@ func CachePlugin(cArr map[string]string) Plugin { func DatabasePlugin(cArr map[string]string) Plugin { return Plugin{ name: "database", - fn: func() any { - dbConn, err := db.Connect(cArr["dbURL"]) - if err != nil { - log.Fatalf("Failed to connect to the Database: %s. Err: %v\n", cArr["dbURL"], err) - os.Exit(1) // TODO: retry in background... - } + fn: func() any { // FIXME: return type + dbConn, _ := db.Connect(cArr["dbURL"]) + // if err != nil { + // log.Fatalf("Failed to connect to the Database: %s. Err: %v\n", cArr["dbURL"], err) + // os.Exit(1) + // } return dbConn }, @@ -52,7 +52,7 @@ func DatabasePlugin(cArr map[string]string) Plugin { // func EventbusPlugin(cArr map[string]string) Plugin { // return Plugin{ // name: "eventbus", -// fn: func() any { +// fn: func() any { // FIXME: return type // conn, err := amqp.Dial(cArr["eventBusURL"]) // if err != nil { // log.Fatalf("Failed to connect to the EventBus: %s. Err: %v\n", cArr["eventBusURL"], err) @@ -73,66 +73,20 @@ func DatabasePlugin(cArr map[string]string) Plugin { func LoggerPlugin(cArr map[string]string) Plugin { return Plugin{ name: "logger", - fn: func() any { + fn: func() any { // FIXME: return type logHost, logPort, err := fluentd.ParseAddr(cArr["loggerAddr"]) if err != nil { log.Fatalf("Failed to parse FluentD address: %s. Err: %v", cArr["loggerAddr"], err) - os.Exit(1) // TODO: retry in background... + os.Exit(1) } logger, err := fluentd.NewLogger(cArr["appFullname"], logHost, logPort) if err != nil { log.Fatalf("Failed to connect to the FluentD on %s:%d. Err: %v", logHost, logPort, err) - os.Exit(1) // TODO: retry in background... + os.Exit(1) } return logger }, } } - -func RegistryPlugin(cArr map[string]string) Plugin { - return Plugin{ - name: "registry", - fn: func() any { - port, _ := strconv.Atoi(cArr["netAddr"][1:]) // FIXME: can be IP:PORT or :PORT - // log.Printf("Consul retrieved port: %v", port) - registry, err := consul.NewService(cArr["registryAddr"], cArr["id"], cArr["name"], cArr["registryDomainOverIP"], cArr["ip"], cArr["domain"], cArr["pathPrefix"], port) - if err != nil { - log.Fatalf("Failed to connect to the Consul on: %s. Err: %v", cArr["registryAddr"], err) - os.Exit(1) // TODO: retry in background... - } - - err = registry.Register() - if err != nil { - log.Fatalf("Failed to register in the Consul service. Err: %v", err) - os.Exit(1) // TODO: retry in background... - } - - registry.RegisterHealthChecks() - // a.registerKVUpdater() // FIXME run as goroutine - - return registry - - // svc, _ := registry.Connect() - // tlsCnf := svc.ServerTLSConfig() - // s.Base.App.Server().TLSConfig = tlsCnf - // fmt.Println("Podmiana configa TLS") - // defer svc.Close() - - // go func() { // Consul KV updater - // ticker := time.NewTicker(time.Second * 15) - // for range ticker.C { - // fetchKVConfig(s) // FIXME: duplicated in worker - // } - // }() - - // go func() { // Server metadata cache updater - // ticker := time.NewTicker(time.Second * 5) - // for range ticker.C { - // s.cacheMetadata() - // } - // }() - }, - } -} diff --git a/src/internal/server/config.go b/src/internal/server/config.go index ddf453e..93f1b21 100644 --- a/src/internal/server/config.go +++ b/src/internal/server/config.go @@ -6,13 +6,14 @@ import ( "os" "time" - cnf "git.pbiernat.io/egommerce/go-api-pkg/config" + cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config" ) const ( defName = "identity-svc" defDomain = "identity-svc" - defCacheAddr = "egommerce.local:6379" + defCacheAddr = "api-cache:6379" + defCacheUsername = "default" defCachePassword = "12345678" defDbURL = "postgres://postgres:12345678@db-postgres:5432/egommerce" defEventBusURL = "amqp://guest:guest@api-eventbus:5672" @@ -21,18 +22,16 @@ const ( defNetAddr = ":443" defMongoDbURL = "mongodb://mongodb:12345678@mongo-db:27017" defPathPrefix = "/identity" - defRegistryAddr = "api-registry:8501" defEbEventsExchange = "api-events" defEbEventsQueue = "identity-svc" ) type Config struct { - ID string - Name string - Domain string - NetAddr string - RegistryDomainOverIP string - PathPrefix string + ID string + Name string + Domain string + NetAddr string + PathPrefix string IdleTimeout time.Duration // miliseconds ReadTimeout time.Duration // miliseconds @@ -41,15 +40,12 @@ type Config struct { LoggerAddr string `json:"logger_addr"` DbURL string `json:"db_url"` CacheAddr string `json:"cache_addr"` + CacheUsername string `json:"cache_username"` CachePassword string `json:"cache_password"` MongoDbUrl string `json:"mongodb_url"` EventBusURL string `json:"eventbus_url"` EventBusExchange string `json:"eventbus_exchange"` EventBusQueue string `json:"eventbus_queue"` - KVNamespace string - RegistryAddr string - - // Fields with JSON mappings are available through Consul KV storage } func NewConfig(name string) *Config { @@ -59,17 +55,14 @@ func NewConfig(name string) *Config { c.Name = name c.Domain = cnf.GetEnv("APP_DOMAIN", defDomain) c.NetAddr = cnf.GetEnv("SERVER_ADDR", defNetAddr) - c.RegistryDomainOverIP = cnf.GetEnv("REGISTRY_USE_DOMAIN_OVER_IP", "false") - c.PathPrefix = cnf.GetEnv("APP_PATH_PREFIX", defPathPrefix) - - c.CacheAddr = cnf.GetEnv("CACHE_ADDR", defCacheAddr) - c.CachePassword = cnf.GetEnv("CACHE_PASSWORD", defCachePassword) - c.DbURL = cnf.GetEnv("DATABASE_URL", defDbURL) + c.CacheAddr = cnf.GetEnv("API_CACHE_ADDR", defCacheAddr) + c.CacheUsername = cnf.GetEnv("API_CACHE_USERNAME", defCacheUsername) + c.CachePassword = cnf.GetEnv("API_CACHE_PASSWORD", defCachePassword) + c.DbURL = cnf.GetEnv("API_DATABASE_URL", defDbURL) c.EventBusExchange = defEbEventsExchange - c.EventBusURL = cnf.GetEnv("EVENTBUS_URL", defEventBusURL) - c.KVNamespace = cnf.GetEnv("APP_KV_NAMESPACE", defKVNmspc) - c.LoggerAddr = cnf.GetEnv("LOGGER_ADDR", defLoggerAddr) - c.RegistryAddr = cnf.GetEnv("REGISTRY_ADDR", defRegistryAddr) + c.EventBusURL = cnf.GetEnv("API_EVENTBUS_URL", defEventBusURL) + c.LoggerAddr = cnf.GetEnv("API_LOGGER_ADDR", defLoggerAddr) + c.PathPrefix = cnf.GetEnv("APP_PATH_PREFIX", defPathPrefix) return c } @@ -96,16 +89,14 @@ func (c *Config) GetArray() map[string]string { // FIXME fix types etc arr["domain"] = c.Domain arr["ip"] = c.GetIP() arr["netAddr"] = c.NetAddr - arr["registryDomainOverIP"] = c.RegistryDomainOverIP arr["pathPrefix"] = c.PathPrefix arr["cacheAddr"] = c.CacheAddr + arr["cacheUsername"] = c.CacheUsername arr["cachePassword"] = c.CachePassword arr["dbURL"] = c.DbURL arr["eventBusExchange"] = c.EventBusExchange arr["eventBusURL"] = c.EventBusURL - arr["kvNamespace"] = c.KVNamespace arr["loggerAddr"] = c.LoggerAddr - arr["registryAddr"] = c.RegistryAddr return arr } diff --git a/src/internal/server/config_handler.go b/src/internal/server/config_handler.go deleted file mode 100644 index 26c3ac1..0000000 --- a/src/internal/server/config_handler.go +++ /dev/null @@ -1,9 +0,0 @@ -package server - -import ( - "github.com/gofiber/fiber/v2" -) - -func (s *Server) ConfigHandler(c *fiber.Ctx) error { - return c.JSON(s.Config) -} diff --git a/src/internal/server/health_handler.go b/src/internal/server/health_handler.go index 3181e8e..a5685c7 100644 --- a/src/internal/server/health_handler.go +++ b/src/internal/server/health_handler.go @@ -1,6 +1,9 @@ package server import ( + "context" + "net/http" + "github.com/gofiber/fiber/v2" ) @@ -9,6 +12,25 @@ type HealthResponse struct { } func (s *Server) HealthHandler(c *fiber.Ctx) error { + // Only 404 indicate service as non-healthy + err := s.GetDatabase().Ping(context.Background()) + if err != nil { + // fmt.Println("db unavailable", err) + return c.SendStatus(http.StatusNotFound) + } + + err = s.GetLogger().Ping() + if err != nil { + // fmt.Println("logger unavailable", err) + return c.SendStatus(http.StatusNotFound) + } + + err = s.GetCache().Ping(context.Background()).Err() + if err != nil { + // fmt.Println("cache unavailable", err) + return c.SendStatus(http.StatusNotFound) + } + return c.JSON(&HealthResponse{ Status: "OK", }) diff --git a/src/internal/server/login_handler.go b/src/internal/server/login_handler.go index b1be135..0ad1fe9 100644 --- a/src/internal/server/login_handler.go +++ b/src/internal/server/login_handler.go @@ -1,7 +1,7 @@ package server import ( - "git.pbiernat.io/egommerce/identity-service/internal/service" + "git.ego.freeddns.org/egommerce/identity-service/internal/service" "github.com/gofiber/fiber/v2" ) diff --git a/src/internal/server/middleware.go b/src/internal/server/middleware.go index afa92ca..bf4281d 100644 --- a/src/internal/server/middleware.go +++ b/src/internal/server/middleware.go @@ -3,7 +3,7 @@ package server import ( "github.com/gofiber/fiber/v2" - "git.pbiernat.io/egommerce/go-api-pkg/fluentd" + "git.ego.freeddns.org/egommerce/go-api-pkg/fluentd" ) // "github.com/gofiber/fiber/v2" diff --git a/src/internal/server/router.go b/src/internal/server/router.go index 43ed137..2b860e6 100644 --- a/src/internal/server/router.go +++ b/src/internal/server/router.go @@ -18,10 +18,8 @@ func SetupRouter(s *Server) { s.Use(defaultCORS) s.Get("/health", s.HealthHandler) - s.Get("/config", s.ConfigHandler) api := s.Group("/api") v1 := api.Group("/v1") v1.Post("/login", s.LoginHandler) - v1.All("/traefik", s.TraefikHandler) } diff --git a/src/internal/server/server.go b/src/internal/server/server.go index 7d6550c..5b2d772 100644 --- a/src/internal/server/server.go +++ b/src/internal/server/server.go @@ -10,9 +10,8 @@ import ( "github.com/gofiber/fiber/v2" "github.com/jackc/pgx/v5/pgxpool" - "git.pbiernat.io/egommerce/api-entities/http" - "git.pbiernat.io/egommerce/go-api-pkg/consul" - "git.pbiernat.io/egommerce/go-api-pkg/fluentd" + "git.ego.freeddns.org/egommerce/api-entities/common/dto" + "git.ego.freeddns.org/egommerce/go-api-pkg/fluentd" ) type ( @@ -48,11 +47,11 @@ func (s *Server) Start() error { SetupRouter(s) // fmt.Printf("Starting server at: %s...\n", s.addr) - cer, err := tls.LoadX509KeyPair("certs/client.crt", "certs/client.key") + crt, err := tls.LoadX509KeyPair("certs/identity-svc.crt", "certs/identity-svc.key") if err != nil { log.Fatal(err) } - tlsCnf := &tls.Config{Certificates: []tls.Certificate{cer}} + tlsCnf := &tls.Config{Certificates: []tls.Certificate{crt}} ln, _ := net.Listen("tcp", s.addr) ln = tls.NewListener(ln, tlsCnf) @@ -61,15 +60,13 @@ func (s *Server) Start() error { } func (s *Server) RegisterHandler(name string, fn func() any) { - // fmt.Printf("Registering plugin( with handler): %s... OK\n", name) + // fmt.Printf("Registering plugin(with handler): %s... OK\n", name) s.handlers[name] = fn() } func (s *Server) OnShutdown() { - // s.GetLogger().Log("Server %s is going down...", s.ID) + s.GetLogger().Log("Server %s is going down...", s.ID) - // s.GetRegistry().Unregister() - // a.clearMetadataCache() // s.GetEventBus().Close() s.GetDatabase().Close() s.GetLogger().Log("Gone.") @@ -88,7 +85,7 @@ func (s *Server) GetRequestID(c *fiber.Ctx) (string, error) { } func (s *Server) Error(c *fiber.Ctx, code int, msg string) error { - return c.Status(code).JSON(http.ErrorResponse{Error: msg}) + return c.Status(code).JSON(dto.ErrorResponseDTO{Error: msg}) } // Plugin helper funcitons @@ -107,37 +104,3 @@ func (s *Server) GetDatabase() *pgxpool.Pool { // FIXME hardcoded index issue func (s *Server) GetLogger() *fluentd.Logger { return (s.handlers["logger"]).(*fluentd.Logger) } - -func (s *Server) GetRegistry() *consul.Service { - return (s.handlers["registry"]).(*consul.Service) -} - -// @CHECK: merge s.Config and s.Base.Config to display all config as one array/map -// func (s *Server) registerKVUpdater() { // @FIXME: merge duplication in server.go and worker.go -// go func() { -// ticker := time.NewTicker(time.Second * 10) -// for range ticker.C { -// config, _, err := s.Registry.KV().Get(s.cnf.KVNamespace, nil) -// if err != nil || config == nil { -// return -// } - -// kvCnf := bytes.NewBuffer(config.Value) -// decoder := json.NewDecoder(kvCnf) -// if err := decoder.Decode(&s.cnf); err != nil { -// return -// } -// } -// }() -// } - -// // func (s *Server) clearMetadataCache() { -// // ctx := context.Background() -// // key, address := s.getMetadataIPsKey(), s.cnf.Base.AppID - -// // s.Cache.LRem(ctx, key, 0, address) -// // } - -// // func (s *Server) getMetadataIPsKey() string { -// // return "internal__" + s.cnf.Name + "__ips" -// // } diff --git a/src/internal/server/traefik_handler.go b/src/internal/server/traefik_handler.go deleted file mode 100644 index de0fa74..0000000 --- a/src/internal/server/traefik_handler.go +++ /dev/null @@ -1,31 +0,0 @@ -package server - -import ( - "net/http" - - "git.pbiernat.io/egommerce/identity-service/internal/service" - "github.com/gofiber/fiber/v2" -) - -type TraefikAuthResponse struct { - Status string `json:"status,omitempty"` - Message string `json:"msg,omitempty"` -} - -func (s *Server) TraefikHandler(c *fiber.Ctx) error { - cookie := service.AuthService.Cookie("traefik", "dummy-traefik-token") - - c.Cookie(cookie) - s.GetLogger().Log("Traefik action set cookie. done.") - - c.Response().Header.Add("Server", "identity-service/traefik") - reqCookie := c.Request().Header.Cookie("basket_id") - s.GetLogger().Log("Request cookie: %s", reqCookie) - - return c. - Status(http.StatusOK). - JSON(&TraefikAuthResponse{Status: "OK"}) - // return c. - // Status(http.StatusUnauthorized). - // JSON(&TraefikAuthResponse{Message: "Access denied mf..."}) -} diff --git a/src/internal/service/auth.go b/src/internal/service/auth.go index 9fef02a..aa54c1a 100644 --- a/src/internal/service/auth.go +++ b/src/internal/service/auth.go @@ -4,7 +4,7 @@ import ( "errors" "strconv" - baseCnf "git.pbiernat.io/egommerce/go-api-pkg/config" + baseCnf "git.ego.freeddns.org/egommerce/go-api-pkg/config" "github.com/gofiber/fiber/v2" ) diff --git a/src/internal/service/jwt.go b/src/internal/service/jwt.go index a756fc9..5d2248b 100644 --- a/src/internal/service/jwt.go +++ b/src/internal/service/jwt.go @@ -5,7 +5,7 @@ import ( "strconv" "time" - baseCnf "git.pbiernat.io/egommerce/go-api-pkg/config" + baseCnf "git.ego.freeddns.org/egommerce/go-api-pkg/config" "github.com/golang-jwt/jwt" )