Skip to content

Commit

Permalink
Merge pull request #1 from eminetto/add-otel-collector
Browse files Browse the repository at this point in the history
feat: add services to docker-compose
  • Loading branch information
eminetto authored Apr 5, 2023
2 parents 88f3446 + 4305c91 commit 5273fe2
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
auth/go.work
feedbacks/go.work
votes/go.work
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ You need to use the token generated by the ```Auth``` service:
curl -X "POST" "http://localhost:8082/v1/feedback" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'Authorization:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImVtaW5ldHRvQGVtYWlsLmNvbSIsImV4cCI6MTY4MDY1Mzg3MSwiaWF0IjoxNjgwNjUwMjQxLCJuYmYiOjE2ODA2NTAyNDF9.wLCwGjmazo3mdvPWpaGrjENnrI7J16wNVghRY4v4I1E' \
-H 'Authorization:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImVtaW5ldHRvQGVtYWlsLmNvbSIsImV4cCI6MTY4MDcwNzI3OSwiaWF0IjoxNjgwNzAzNjQ5LCJuYmYiOjE2ODA3MDM2NDl9.M9RSuq_oGsTCz-2m0EgUmK3mvjqg0ujhF59WVRnMAZ8' \
-d $'{
"title": "Feedback test",
"body": "Feedback body"
Expand Down
20 changes: 8 additions & 12 deletions auth/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
FROM golang:1.19.2-alpine AS builder
#RUN apk add build-base
FROM golang:1.20-alpine AS builder

WORKDIR /app/
WORKDIR /src

# Restore dependencies
COPY * /app/
COPY auth/ .

RUN ls -la /
RUN go mod download
RUN go mod tidy

# Build executable
RUN go build -o /app/auth ./
RUN go build -o /src/auth ./

FROM scratch

WORKDIR /app/

COPY --from=builder /app/auth ./
WORKDIR /src
COPY --from=builder /src/auth ./
EXPOSE 8081
CMD ["/app/auth"]
CMD ["/src/auth"]
11 changes: 8 additions & 3 deletions auth/go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
module auth
module github.com/eminetto/api-o11y/auth

go 1.12
go 1.20

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/eminetto/api-o11y v0.0.1 // indirect
github.com/go-chi/chi/v5 v5.0.8
github.com/go-sql-driver/mysql v1.7.0
github.com/google/uuid v1.3.0
github.com/gorilla/context v1.1.1
github.com/stretchr/testify v1.4.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
2 changes: 0 additions & 2 deletions auth/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/eminetto/api-o11y v0.0.1 h1:fSi83tPiKJFpsC8+aseb6gHJz528bebYlVgMj54P3gU=
github.com/eminetto/api-o11y v0.0.1/go.mod h1:rwcGFE85b639qzTaL13QJR+n5XAmVdKBub6cbzQxsjI=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
Expand Down
1 change: 1 addition & 0 deletions auth/go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
14 changes: 2 additions & 12 deletions auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,8 @@ import (
"github.com/gorilla/context"
)

// @todo get this variables from env or config
const (
DB_USER = "auth_user"
DB_PASSWORD = "auth_pwd"
DB_HOST = "localhost"
DB_DATABASE = "auth_db"
DB_PORT = "3306"
PORT = "8081"
)

func main() {
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_DATABASE)
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_DATABASE"))
db, err := sql.Open("mysql", dataSourceName)
if err != nil {
log.Panic(err.Error())
Expand All @@ -48,7 +38,7 @@ func main() {
srv := &http.Server{
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Addr: ":" + PORT,
Addr: ":" + os.Getenv("PORT"),
Handler: context.ClearHandler(http.DefaultServeMux),
ErrorLog: logger,
}
Expand Down
92 changes: 49 additions & 43 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ x-default-logging: &logging
options:
max-size: "5m"
max-file: "2"
networks:
default:
name: api-o11y
driver: bridge
services:
mysql-auth:
image: mariadb:10.5.8
Expand All @@ -18,7 +22,6 @@ services:
ports:
- "3306:3306"
container_name: auth_mysql
network_mode: "bridge"
mysql-feedbacks:
image: mariadb:10.5.8
command: --default-authentication-plugin=mysql_native_password --init-file /data/application/init.sql
Expand All @@ -32,7 +35,6 @@ services:
ports:
- "3307:3306"
container_name: feedbacks_mysql
network_mode: "bridge"
mysql-votes:
image: mariadb:10.5.8
command: --default-authentication-plugin=mysql_native_password --init-file /data/application/init.sql
Expand All @@ -46,24 +48,59 @@ services:
ports:
- "3308:3306"
container_name: votes_mysql
network_mode: "bridge"
auth:
image: auth
build:
context: ./
dockerfile: ./auth/Dockerfile
# environment:
# MYSQL_ROOT_PASSWORD: votes_user
# MYSQL_DATABASE: votes_db
# MYSQL_USER: votes_user
# MYSQL_PASSWORD: votes_pwd
environment:
DB_USER: "auth_user"
DB_PASSWORD: "auth_pwd"
DB_HOST: "auth_mysql"
DB_DATABASE: "auth_db"
DB_PORT: "3306"
PORT: "8081"
ports:
- "8081:8081"
container_name: auth
network_mode: "bridge"



depends_on:
- mysql-auth
feedbacks:
image: feedbacks
build:
context: ./
dockerfile: ./feedbacks/Dockerfile
environment:
DB_USER: "feedbacks_user"
DB_PASSWORD: "feedbacks_pwd"
DB_HOST: "feedbacks_mysql"
DB_DATABASE: "feedbacks_db"
DB_PORT: "3307"
PORT: "8082"
AUTH_URL: "http://auth:8081"
ports:
- "8082:8082"
container_name: feedbacks
depends_on:
- mysql-feedbacks
votes:
image: votes
build:
context: ./
dockerfile: ./votes/Dockerfile
environment:
DB_USER: "votes_user"
DB_PASSWORD: "votes_pwd"
DB_HOST: "votes_mysql"
DB_DATABASE: "votes_db"
DB_PORT: "3308"
PORT: "8083"
AUTH_URL: "http://auth:8081"
ports:
- "8083:8083"
container_name: votes
depends_on:
- mysql-votes

# ********************
# Telemetry Components
Expand All @@ -72,7 +109,6 @@ services:
jaeger:
image: jaegertracing/all-in-one
container_name: jaeger
network_mode: "bridge"
command:
- "--memory.max-traces"
- "10000"
Expand All @@ -97,7 +133,6 @@ services:
grafana:
image: grafana/grafana:9.1.0
container_name: grafana
network_mode: "bridge"
deploy:
resources:
limits:
Expand All @@ -113,7 +148,6 @@ services:
otelcol:
image: otel/opentelemetry-collector-contrib:0.74.0
container_name: otel-col
network_mode: "bridge"
deploy:
resources:
limits:
Expand All @@ -136,7 +170,6 @@ services:
prometheus:
image: quay.io/prometheus/prometheus:v2.34.0
container_name: prometheus
network_mode: "bridge"
command:
- --web.console.templates=/etc/prometheus/consoles
- --web.console.libraries=/etc/prometheus/console_libraries
Expand All @@ -156,30 +189,3 @@ services:
- "${PROMETHEUS_SERVICE_PORT}:${PROMETHEUS_SERVICE_PORT}"
logging: *logging


# grafana:
# image: grafana/grafana
# ports:
# - "3000:3000"
# container_name: clean-architecture-go-v2-grafana
# network_mode: "bridge"
# depends_on:
# - prometheus
# - prometheus-pushgateway
# prometheus:
# image: prom/prometheus
# ports:
# - 9090:9090
# command:
# - --config.file=/etc/prometheus/prometheus.yml
# volumes:
# - ./ops/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
# container_name: clean-architecture-go-v2-prometheus
# network_mode: "bridge"
# prometheus-pushgateway:
# image: prom/pushgateway
# container_name: clean-architecture-go-v2-pushgateway
# expose:
# - 9091
# ports:
# - "9091:9091"
18 changes: 16 additions & 2 deletions feedbacks/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
FROM golang:1.20-alpine AS builder

WORKDIR /src

# Restore dependencies
COPY feedbacks/ .

RUN go mod tidy

# Build executable
RUN go build -o /src/feedbacks ./


FROM scratch
ADD bin/feedbacks /
WORKDIR /src
COPY --from=builder /src/feedbacks ./
EXPOSE 8082
CMD ["/feedbacks"]
CMD ["/src/feedbacks"]
14 changes: 2 additions & 12 deletions feedbacks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,8 @@ import (
"time"
)

// @todo get this variables from env or config
const (
DB_USER = "feedbacks_user"
DB_PASSWORD = "feedbacks_pwd"
DB_HOST = "localhost"
DB_DATABASE = "feedbacks_db"
DB_PORT = "3307"
PORT = "8082"
)

func main() {
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_DATABASE)
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_DATABASE"))
db, err := sql.Open("mysql", dataSourceName)
if err != nil {
log.Panic(err.Error())
Expand All @@ -48,7 +38,7 @@ func main() {
srv := &http.Server{
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Addr: ":" + PORT,
Addr: ":" + os.Getenv("PORT"),
Handler: http.DefaultServeMux,
ErrorLog: logger,
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/middleware/is_authenticated.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"encoding/json"
"errors"
"net/http"
"os"
"strconv"
"strings"
)

//@TODO get address from environment variables
const URL = "http://localhost:8081/v1/validate-token"

func IsAuthenticated(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
errorMessage := "Erro na autenticação"
Expand All @@ -25,7 +23,7 @@ func IsAuthenticated(next http.Handler) http.Handler {
"token": "` + tokenString + `"
}`

req, err := http.Post(URL, "text/plain", strings.NewReader(payload))
req, err := http.Post(os.Getenv("AUTH_URL")+"/v1/validate-token", "text/plain", strings.NewReader(payload))
if err != nil {
respondWithError(rw, http.StatusUnauthorized, err.Error(), errorMessage)
return
Expand Down
17 changes: 15 additions & 2 deletions votes/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
FROM golang:1.20-alpine AS builder

WORKDIR /src

# Restore dependencies
COPY votes/ .

RUN go mod tidy

# Build executable
RUN go build -o /src/votes ./

FROM scratch
ADD bin/votes /
WORKDIR /src
COPY --from=builder /src/votes ./
EXPOSE 8083
CMD ["/votes"]
CMD ["/src/votes"]

0 comments on commit 5273fe2

Please sign in to comment.