Skip to content

Commit

Permalink
Merge pull request #34 from go-park-mail-ru/PRI-68
Browse files Browse the repository at this point in the history
Pri 68
  • Loading branch information
vovangy authored May 8, 2024
2 parents d032db4 + b302ff1 commit 2155e94
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 4 deletions.
23 changes: 23 additions & 0 deletions build/advert.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:1.21.0-alpine AS builder

COPY . /github.com/go-park-mail-ru/2024_1_TeaStealers/
WORKDIR /github.com/go-park-mail-ru/2024_1_TeaStealers/

RUN go mod download
RUN go clean --modcache
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/auth/main.go


FROM scratch AS runner

WORKDIR /docker-cian-advert/

COPY --from=builder /github.com/go-park-mail-ru/2024_1_TeaStealers/.bin .
COPY --from=builder /github.com/go-park-mail-ru/2024_1_TeaStealers/config config/
COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /

ENV TZ="Europe/Moscow"
ENV ZONEINFO=/zoneinfo.zip
EXPOSE 80 443

ENTRYPOINT ["./.bin"]
23 changes: 23 additions & 0 deletions build/auth.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:1.21.0-alpine AS builder

COPY . /github.com/go-park-mail-ru/2024_1_TeaStealers/
WORKDIR /github.com/go-park-mail-ru/2024_1_TeaStealers/

RUN go mod download
RUN go clean --modcache
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/auth/main.go


FROM scratch AS runner

WORKDIR /docker-cian-auth/

COPY --from=builder /github.com/go-park-mail-ru/2024_1_TeaStealers/.bin .
COPY --from=builder /github.com/go-park-mail-ru/2024_1_TeaStealers/config config/
COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /

ENV TZ="Europe/Moscow"
ENV ZONEINFO=/zoneinfo.zip
EXPOSE 80 443

ENTRYPOINT ["./.bin"]
23 changes: 23 additions & 0 deletions build/user.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:1.21.0-alpine AS builder

COPY . /github.com/go-park-mail-ru/2024_1_TeaStealers/
WORKDIR /github.com/go-park-mail-ru/2024_1_TeaStealers/

RUN go mod download
RUN go clean --modcache
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o ./.bin ./cmd/auth/main.go


FROM scratch AS runner

WORKDIR /docker-cian-user/

COPY --from=builder /github.com/go-park-mail-ru/2024_1_TeaStealers/.bin .
COPY --from=builder /github.com/go-park-mail-ru/2024_1_TeaStealers/config config/
COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /

ENV TZ="Europe/Moscow"
ENV ZONEINFO=/zoneinfo.zip
EXPOSE 80 443

ENTRYPOINT ["./.bin"]
2 changes: 1 addition & 1 deletion cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func main() {
defer grcpConnUsers.Close()

grcpConnAdverts, err := grpc.Dial(
fmt.Sprintf("%s:%d", cfg.GRPC.AdvertsContainerIP, cfg.GRPC.AdvertPort),
fmt.Sprintf("%s:%d", cfg.GRPC.AdvertContainerIP, cfg.GRPC.AdvertPort),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
Expand Down
51 changes: 51 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ services:
- ./.env
ports:
- ${DB_PORT}:5432
networks:
- tean-network

main:
depends_on:
- db
Expand All @@ -28,6 +31,54 @@ services:
- ./.env
volumes:
- "${BASE_DIR}:/docker-cian/images"
networks:
- tean-network

tean-auth:
env_file:
- ./.env
container_name: ${GRPC_AUTH_CONTAINER_IP}
build:
context: .
dockerfile: ./build/auth.Dockerfile
restart: always
expose:
- "${GRPC_AUTH_PORT}"
depends_on:
- db
networks:
- tean-network

tean-user:
env_file:
- ./.env
container_name: ${GRPC_USER_CONTAINER_IP}
build:
context: .
dockerfile: ./build/user.Dockerfile
restart: always
expose:
- "${GRPC_USER_PORT}"
depends_on:
- db
networks:
- tean-network

tean-advert:
env_file:
- ./.env
container_name: ${GRPC_ADVERT_CONTAINER_IP}
build:
context: .
dockerfile: ./build/advert.Dockerfile
restart: always
expose:
- "${GRPC_ADVERT_PORT}"
depends_on:
- db
networks:
- tean-network


volumes:
postgresdb-data:
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ type GRPC struct {
UserPort int `env:"GRPC_USER_PORT" env-default:"8082"`
AdvertPort int `env:"GRPC_ADVERT_PORT" env-default:"8083"`

AuthContainerIP string `env:"GRPC_AUTH_CONTAINER_IP" env-default:"localhost"`
UsersContainerIP string `env:"GRPC_USER_CONTAINER_IP" env-default:"localhost"`
AdvertsContainerIP string `env:"GRPC_ADVERT_CONTAINER_IP" env-default:"localhost"`
AuthContainerIP string `env:"GRPC_AUTH_CONTAINER_IP" env-default:"localhost"`
UsersContainerIP string `env:"GRPC_USER_CONTAINER_IP" env-default:"localhost"`
AdvertContainerIP string `env:"GRPC_ADVERT_CONTAINER_IP" env-default:"localhost"`
}

func MustLoad() *Config {
Expand Down
16 changes: 16 additions & 0 deletions internal/pkg/metrics/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package middleware

import (
"2024_1_TeaStealers/internal/pkg/metrics"
"2024_1_TeaStealers/internal/pkg/utils"
"context"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
"net/http"
"time"
)

Expand Down Expand Up @@ -72,3 +74,17 @@ func (m *GrpcMiddleware) ServerMetricsInterceptor(ctx context.Context,
m.AddDurationToHistogram(info.FullMethod, "", tm)
return h, err
}

func (m *GrpcMiddleware) ServerMetricsMiddleware(next http.Handler, urlTruncCount int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

start := time.Now()
next.ServeHTTP(w, r)
tm := time.Since(start)
methodName, err := utils.TruncSlash(r.URL.String(), urlTruncCount)
if err == nil {
m.IncreaseHits(methodName, "")
m.AddDurationToHistogram(methodName, "", tm)
}
})
}
28 changes: 28 additions & 0 deletions internal/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)

Expand Down Expand Up @@ -83,3 +86,28 @@ func StringToTime(layout, value string) (time.Time, error) {
}
return t, nil
}

func TruncSlash(methodName string, count int) (string, error) {
if count < 0 {
return "", errors.New("count must be non-negative")
}

slashes := strings.Count(methodName, `/`)
if slashes < count {
return "", fmt.Errorf("methodName contains %d slashes, but count is %d", slashes, count)
}

// Split the methodName string into a slice of strings using `/` as the separator
parts := strings.Split(methodName, `?`)

parts = strings.Split(parts[0], `/`)

// parts = parts[:len(parts)-count-1]

// Join the remaining elements of the slice back into a string using `/` as the separator
newMethodName := strings.Join(parts, `/`)
trSlash := `/`
newMethodName = newMethodName + trSlash

return newMethodName, nil
}

0 comments on commit 2155e94

Please sign in to comment.