Skip to content

Commit

Permalink
Merge pull request #35 from go-park-mail-ru/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
vr009 authored May 8, 2024
2 parents 760782d + dfbba11 commit 4d81d2d
Show file tree
Hide file tree
Showing 155 changed files with 8,914 additions and 2,911 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ migrate-lib:
go get -tags 'postgres' -u github.com/golang-migrate/migrate/v4/cmd/migrate/

create-migration:
migrate create -dir migrations -ext sql -seq $(TABLE_NAME)
migrate create -dir db/migrations -ext sql -seq $(TABLE_NAME)

migrate-up:
migrate -path migrations -database "postgres://$(DB_USER):$(DB_PASS)@localhost:$(DB_PORT)/$(DB_NAME)?sslmode=disable" up
migrate -path db/migrations -database "postgres://$(DB_USER):$(DB_PASS)@localhost:$(DB_PORT)/$(DB_NAME)?sslmode=disable" up

migrate-down:
migrate -path migrations -database "postgres://$(DB_USER):$(DB_PASS)@localhost:$(DB_PORT)/$(DB_NAME)?sslmode=disable" down
migrate -path db/migrations -database "postgres://$(DB_USER):$(DB_PASS)@localhost:$(DB_PORT)/$(DB_NAME)?sslmode=disable" down

dev-compose-up:
$(DOCKER_COMPOSE) -f "dev-docker-compose.yaml" up -d
Expand All @@ -52,3 +52,5 @@ coverage:
swagger:
swag init -g cmd/main/main.go

proto_gen:
protoc -I proto proto/*.proto --go_out=./ --go-grpc_out=./
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"]
81 changes: 81 additions & 0 deletions cmd/adverts/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
genAdverts "2024_1_TeaStealers/internal/pkg/adverts/delivery/grpc/gen"
advertsR "2024_1_TeaStealers/internal/pkg/adverts/repo"
advertsUc "2024_1_TeaStealers/internal/pkg/adverts/usecase"
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"

"database/sql"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"

"github.com/joho/godotenv"
_ "github.com/lib/pq"
"go.uber.org/zap"

grpcAdverts "2024_1_TeaStealers/internal/pkg/adverts/delivery/grpc"
metricsMw "2024_1_TeaStealers/internal/pkg/metrics/middleware"

"google.golang.org/grpc"
)

func main() {
if err := run(); err != nil {
os.Exit(1)
}
}

func run() (err error) {
_ = godotenv.Load()
logger := zap.Must(zap.NewDevelopment())

db, err := sql.Open("postgres", fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
os.Getenv("DB_USER"),
os.Getenv("DB_PASS"),
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_NAME")))
if err != nil {
panic("failed to connect database" + err.Error())
}

if err = db.Ping(); err != nil {
log.Println("fail ping postgres")
err = fmt.Errorf("error happened in db.Ping: %w", err)
log.Println(err)
}

http.Handle("/metrics", promhttp.Handler())

advertsRepo := advertsR.NewRepository(db, logger)
advertsUsecase := advertsUc.NewAdvertUsecase(advertsRepo, logger)
authHandler := grpcAdverts.NewServerAdvertsHandler(advertsUsecase, logger)
metricMw := metricsMw.Create()
gRPCServer := grpc.NewServer(grpc.UnaryInterceptor(metricMw.ServerMetricsInterceptor))
genAdverts.RegisterAdvertsServer(gRPCServer, authHandler)

go func() {
logger.Info(fmt.Sprintf("Start server on %s\n", ":8083"))
listener, err := net.Listen("tcp", ":8083")
if err != nil {
log.Fatal(err)
}
if err := gRPCServer.Serve(listener); err != nil {
log.Fatal(err)
}
}()

stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)

<-stop
gRPCServer.GracefulStop()
return nil
}
81 changes: 81 additions & 0 deletions cmd/auth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
genAuth "2024_1_TeaStealers/internal/pkg/auth/delivery/grpc/gen"
authR "2024_1_TeaStealers/internal/pkg/auth/repo"
authUc "2024_1_TeaStealers/internal/pkg/auth/usecase"
"net/http"

"github.com/prometheus/client_golang/prometheus/promhttp"

"database/sql"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"

"github.com/joho/godotenv"
_ "github.com/lib/pq"
"go.uber.org/zap"

grpcAuth "2024_1_TeaStealers/internal/pkg/auth/delivery/grpc"
metricsMw "2024_1_TeaStealers/internal/pkg/metrics/middleware"

"google.golang.org/grpc"
)

func main() {
if err := run(); err != nil {
os.Exit(1)
}
}

func run() (err error) {
_ = godotenv.Load()
logger := zap.Must(zap.NewDevelopment())

db, err := sql.Open("postgres", fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
os.Getenv("DB_USER"),
os.Getenv("DB_PASS"),
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_NAME")))
if err != nil {
panic("failed to connect database" + err.Error())
}

if err = db.Ping(); err != nil {
log.Println("fail ping postgres")
err = fmt.Errorf("error happened in db.Ping: %w", err)
log.Println(err)
}

http.Handle("/metrics", promhttp.Handler())

authRepo := authR.NewRepository(db, logger)
authUsecase := authUc.NewAuthUsecase(authRepo, logger)
authHandler := grpcAuth.NewServerAuthHandler(authUsecase, logger)
metricMw := metricsMw.Create()
gRPCServer := grpc.NewServer(grpc.UnaryInterceptor(metricMw.ServerMetricsInterceptor))
genAuth.RegisterAuthServer(gRPCServer, authHandler)

go func() {
logger.Info(fmt.Sprintf("Start server on %s\n", ":8081"))
listener, err := net.Listen("tcp", ":8081")
if err != nil {
log.Fatal(err)
}
if err := gRPCServer.Serve(listener); err != nil {
log.Fatal(err)
}
}()

stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)

<-stop
gRPCServer.GracefulStop()
return nil
}
Loading

0 comments on commit 4d81d2d

Please sign in to comment.