From e3b857153265432392d872dec75eb100f89f32bd Mon Sep 17 00:00:00 2001 From: Ahmed Radwan Date: Wed, 10 Apr 2024 00:55:35 +0200 Subject: [PATCH] refactor env and makefile --- .env | 60 +++++++++++++++++++++++++++++++++++++++------- Dockerfile | 2 +- Makefile | 38 +++++++++++++++++++---------- docker-compose.yml | 28 +++++++--------------- go.mod | 8 +++---- go.sum | 12 +++++----- 6 files changed, 97 insertions(+), 51 deletions(-) diff --git a/.env b/.env index d4b30d2..f567320 100644 --- a/.env +++ b/.env @@ -1,32 +1,76 @@ +# ------------------------- +# Environment Configuration +# ------------------------- ENVIRONMENT=development + +# ---------------- +# Database Config +# ---------------- DB_DRIVER=pgx -DB_SOURCE=postgresql://root:secret@localhost:5432/eenergy?sslmode=disable +DB_USER=root +DB_PASSWORD=secret +DB_NAME=eenergy +DB_PORT=5432 +DB_HOST=localhost +DB_SSL_MODE=disable +DB_SOURCE="postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=${DB_SSL_MODE}" MIGRATIONS_URL=file://db/migrations -HTTP_SERVER_ADDRESS=0.0.0.0:8080 -GRPC_SERVER_ADDRESS=0.0.0.0:9091 + +# -------------------- +# Server Configuration +# -------------------- +SERVER_IP=0.0.0.0 +HTTP_SERVER_PORT=8080 +GRPC_SERVER_PORT=9091 +HTTP_SERVER_ADDRESS="${SERVER_IP}:${HTTP_SERVER_PORT}" +GRPC_SERVER_ADDRESS="${SERVER_IP}:${GRPC_SERVER_PORT}" + +# ---------------------- +# Security Configuration +# ---------------------- TOKEN_SYMMETRIC_KEY=B51FE30989F143F6F07D2CB828495D69 ACCESS_TOKEN_DURATION=15m REFRESH_TOKEN_DURATION=24h -REDIS_ADDRESS=0.0.0.0:6379 + +# --------------- +# Redis Config +# --------------- +REDIS_HOST=redis +REDIS_PORT=6379 +REDIS_ADDRESS="${REDIS_HOST}:${REDIS_PORT}" + +# --------------- +# Email Configuration +# --------------- EMAIL_SENDER_NAME=eenergy EMAIL_SENDER_ADDRESS=ahmedradwan9966@gmail.com EMAIL_SENDER_PASSWORD= + +# ------------------------- +# Certificates Path Config +# ------------------------- SERVER_CRT_PATH=dev-certs/server.crt SERVER_KEY_PATH=dev-certs/server.key CA_CRT_PATH=dev-certs/ca.pem -# OpenTelemetry Collector +# ------------------------------- +# OpenTelemetry Collector Config +# ------------------------------- OTEL_COLLECTOR_HOST=otel-collector OTEL_COLLECTOR_PORT_GRPC=4317 OTEL_COLLECTOR_PORT_HTTP=4318 OTEL_EXPORTER_OTLP_ENDPOINT=http://${OTEL_COLLECTOR_HOST}:${OTEL_COLLECTOR_PORT_GRPC} -PUBLIC_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:8080/otlp-http/v1/traces +OTEL_SERVICE_NAME=eenergy-api -# Jaeger +# ----------- +# Jaeger Config +# ----------- JAEGER_SERVICE_PORT=16686 JAEGER_SERVICE_HOST=jaeger -# Prometheus +# --------------- +# Prometheus Config +# --------------- PROMETHEUS_SERVICE_PORT=9090 PROMETHEUS_SERVICE_HOST=prometheus PROMETHEUS_ADDR=${PROMETHEUS_SERVICE_HOST}:${PROMETHEUS_SERVICE_PORT} diff --git a/Dockerfile b/Dockerfile index 4b5ef98..f193e9d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Build stage -FROM golang:1.21-alpine AS builder +FROM golang:1.21.9-alpine AS builder WORKDIR /app COPY go.mod ./ COPY go.sum ./ diff --git a/Makefile b/Makefile index 5f28031..f69dbd3 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,26 @@ - -DB_SOURCE = "postgresql://root:secret@localhost:5432/eenergy?sslmode=disable" -MIGRATIONS_PATH = db/migrations +# Variables +DB_NAME=eenergy +DB_USER=root +DB_PASS=secret +DB_HOST=localhost +DB_PORT=5432 +DB_SOURCE=postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=disable +MIGRATIONS_PATH=db/migrations CONFIG_PATH=dev-certs/ +DOCKER_CONTAINER_NAME=postgres +# Setup init: mkdir -p ${CONFIG_PATH} +# Database Operations createdb: - docker exec -it postgres15 createdb --username=root --owner=root eenergy + docker exec -it ${DOCKER_CONTAINER_NAME} createdb --username=${DB_USER} --owner=${DB_USER} ${DB_NAME} dropdb: - docker exec -it postgres15 dropdb eenergy - -mock: - mockgen -package mockdb -destination db/mock/store.go github.com/aradwann/eenergy/db/store Store - mockgen -package mockmail -destination mail/mock/sender.go github.com/aradwann/eenergy/mail EmailSender - mockgen -package mockwk -destination worker/mock/distributor.go github.com/aradwann/eenergy/worker TaskDistributor - mockgen -package mockwk -destination worker/mock/processor.go github.com/aradwann/eenergy/worker TaskProcessor + docker exec -it ${DOCKER_CONTAINER_NAME} dropdb ${DB_NAME} +# Migration migrateup: go run db/scripts/migrate.go @@ -33,15 +36,25 @@ migratedown1: createmigration: migrate create -ext sql -dir $(MIGRATIONS_PATH) -seq "$(filter-out $@,$(MAKECMDGOALS))" +# Mocks +mock: + mockgen -package mockdb -destination db/mock/store.go github.com/aradwann/eenergy/db/store Store + mockgen -package mockmail -destination mail/mock/sender.go github.com/aradwann/eenergy/mail EmailSender + mockgen -package mockwk -destination worker/mock/distributor.go github.com/aradwann/eenergy/worker TaskDistributor + mockgen -package mockwk -destination worker/mock/processor.go github.com/aradwann/eenergy/worker TaskProcessor + +# Testing test: go test -short -v -cover ./... testci: go test -short -race -covermode atomic -coverprofile=covprofile $$(go list ./... | grep -v /pb$$) +# Run Server server: go run main.go +# Protocol Buffers protoc: rm -f pb/*.go rm -f doc/swagger/*.swagger.json @@ -51,6 +64,7 @@ protoc: --openapiv2_out=doc/swagger --openapiv2_opt=allow_merge=true,merge_file_name=eenergy \ proto/*.proto +# gRPC Client evans: evans --host localhost --port 9091 -r repl @@ -76,5 +90,5 @@ gen-cert: mv *.pem *.csr *.crt *.srl *.key ${CONFIG_PATH} -.PHONEY: createdb dropdb migrateup migrateup1 migratedown migratedown1 test server protoc evans gen-cert init +.PHONY: createdb dropdb migrateup migrateup1 migratedown migratedown1 test server protoc evans gen-cert init diff --git a/docker-compose.yml b/docker-compose.yml index 37b4d12..d7df116 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,14 +1,13 @@ services: postgres: - container_name: postgres image: postgis/postgis:16-3.4-alpine environment: - - POSTGRES_DB=eenergy - - POSTGRES_USER=root - - POSTGRES_PASSWORD=secret + - POSTGRES_DB=${DB_NAME} + - POSTGRES_USER=${DB_USER} + - POSTGRES_PASSWORD=${DB_PASSWORD} ports: - - "5432:5432" + - "${DB_PORT}:${DB_PORT}" volumes: - data-volume:/var/lib/postgresql/data healthcheck: @@ -18,7 +17,6 @@ services: retries: 5 pgadmin4: - container_name: pgadmin4 image: dpage/pgadmin4 environment: - PGADMIN_DEFAULT_EMAIL=user@domain.com @@ -27,10 +25,9 @@ services: - "80:80" redis: - container_name: redis image: redis:7-alpine ports: - - "6379:6379" + - "${REDIS_PORT}:${REDIS_PORT}" healthcheck: test: ["CMD", "redis-cli", "ping"] interval: 10s @@ -38,20 +35,18 @@ services: retries: 5 api: - container_name: api build: context: . dockerfile: Dockerfile ports: - - 8080:8080 - - 9091:9091 + - ${HTTP_SERVER_PORT}:${HTTP_SERVER_PORT} + - ${GRPC_SERVER_PORT}:${GRPC_SERVER_PORT} environment: - DB_SOURCE=postgresql://root:secret@postgres:5432/eenergy?sslmode=disable - - REDIS_ADDRESS=redis:6379 - OTEL_EXPORTER_OTLP_ENDPOINT - OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE - OTEL_RESOURCE_ATTRIBUTES - - OTEL_SERVICE_NAME=eenergy + - OTEL_SERVICE_NAME depends_on: postgres: # Wait for postgres to start @@ -63,7 +58,6 @@ services: # logs containers loki: - container_name: loki image: grafana/loki:latest ports: - "3100:3100" @@ -74,7 +68,6 @@ services: - ./observability/loki/rules:/loki/rules/fake alertmanager: - container_name: alertmanager image: prom/alertmanager:latest volumes: - ./alertmanager.yml:/etc/alertmanager/alertmanager.yml @@ -87,7 +80,6 @@ services: - prometheus promtail: - container_name: promtail image: grafana/promtail:latest volumes: - ./promtail-config.yml:/etc/promtail/promtail.yml @@ -98,7 +90,6 @@ services: - loki grafana: - container_name: grafana image: grafana/grafana:latest ports: - "3000:3000" @@ -109,7 +100,6 @@ services: # metrics containers prometheus: - container_name: prometheus image: prom/prometheus:latest volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml @@ -119,7 +109,6 @@ services: # traces containes # Jaeger jaeger: - container_name: jaeger image: jaegertracing/all-in-one:latest ports: - "${JAEGER_SERVICE_PORT}" # Jaeger UI @@ -128,7 +117,6 @@ services: - COLLECTOR_OTLP_ENABLED=true otel-collector: - container_name: otel-collector image: otel/opentelemetry-collector-contrib volumes: - ./otel-collector-config.yaml:/etc/otelcol-contrib/config.yaml diff --git a/go.mod b/go.mod index 636bc79..8253c83 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/aradwann/eenergy -go 1.21.8 +go 1.21.9 require ( github.com/aead/chacha20poly1305 v0.0.0-20201124145622-1a5aba2a8b29 @@ -23,7 +23,7 @@ require ( golang.org/x/crypto v0.22.0 google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda - google.golang.org/grpc v1.63.0 + google.golang.org/grpc v1.63.2 google.golang.org/protobuf v1.33.0 ) @@ -50,7 +50,7 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.25.0 // indirect go.opentelemetry.io/otel/metric v1.25.0 // indirect go.opentelemetry.io/otel/trace v1.25.0 // indirect - go.opentelemetry.io/proto/otlp v1.1.0 // indirect + go.opentelemetry.io/proto/otlp v1.2.0 // indirect go.uber.org/atomic v1.11.0 // indirect golang.org/x/net v0.24.0 // indirect golang.org/x/sync v0.7.0 // indirect @@ -80,7 +80,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.6.0 // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 // indirect + golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 // indirect golang.org/x/sys v0.19.0 // indirect golang.org/x/text v0.14.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/go.sum b/go.sum index 74a4320..e832fe7 100644 --- a/go.sum +++ b/go.sum @@ -265,8 +265,8 @@ go.opentelemetry.io/otel/sdk/metric v1.25.0 h1:7CiHOy08LbrxMAp4vWpbiPcklunUshVpA go.opentelemetry.io/otel/sdk/metric v1.25.0/go.mod h1:LzwoKptdbBBdYfvtGCzGwk6GWMA3aUzBOwtQpR6Nz7o= go.opentelemetry.io/otel/trace v1.25.0 h1:tqukZGLwQYRIFtSQM2u2+yfMVTgGVeqRLPUYx1Dq6RM= go.opentelemetry.io/otel/trace v1.25.0/go.mod h1:hCCs70XM/ljO+BeQkyFnbK28SBIJ/Emuha+ccrCRT7I= -go.opentelemetry.io/proto/otlp v1.1.0 h1:2Di21piLrCqJ3U3eXGCTPHE9R8Nh+0uglSnOyxikMeI= -go.opentelemetry.io/proto/otlp v1.1.0/go.mod h1:GpBHCBWiqvVLDqmHZsoMM3C5ySeKTC7ej/RNTae6MdY= +go.opentelemetry.io/proto/otlp v1.2.0 h1:pVeZGk7nXDC9O2hncA6nHldxEjm6LByfA2aN8IOkz94= +go.opentelemetry.io/proto/otlp v1.2.0/go.mod h1:gGpR8txAl5M03pDhMC79G6SdqNV26naRm/KDsgaHD8A= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= @@ -302,8 +302,8 @@ golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDf golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ= golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= -golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 h1:985EYyeCOxTpcgOTJpflJUwOeEz0CQOdPt73OzpE9F8= -golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= +golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 h1:ESSUROHIBHg7USnszlcdmjBEwdMj9VUvU+OPk4yl2mc= +golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= @@ -396,8 +396,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda h1: google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda/go.mod h1:AHcE/gZH76Bk/ROZhQphlRoWo5xKDEtz3eVEO1LfA8c= google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda h1:LI5DOvAxUPMv/50agcLLoo+AdWc1irS9Rzz4vPuD1V4= google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= -google.golang.org/grpc v1.63.0 h1:WjKe+dnvABXyPJMD7KDNLxtoGk5tgk+YFWN6cBWjZE8= -google.golang.org/grpc v1.63.0/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= +google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= +google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=