Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:go-park-mail-ru/2023_2_Hamster i…
Browse files Browse the repository at this point in the history
…nto deploy
  • Loading branch information
DmitriyKomarovCoder committed Nov 29, 2023
2 parents 05430fe + 2f9fc5b commit 3ea3d60
Show file tree
Hide file tree
Showing 64 changed files with 5,412 additions and 1,014 deletions.
7 changes: 5 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ SECRET="vzqqEH6o5so_VGJZoLhrfdrH7Y7kvFe-7N2i3I3w314=п"

DB_CONTAINER_NAME=HammyWallet_DB
DB_NAME=HammyWallets
DB_NAME_QUESTION = HammyWallet_QUESTION
DB_USER=hamster
DB_PASSWORD=2003
DB_HOST=127.0.0.1
DB_HOST=hammy-postgres
DB_SSLMODE=disable
DB_PORT=5436
DB_PROD_MAIN=5436
DB_PROD_QUESTION = 8300
DB_PORT=5432

DB_NAME_PROD=HammyWallets
DB_USER_PROD=hamster
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/CD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
username: ${{ secrets.DEPLOY_USERNAME }}
key: ${{ secrets.SSHKEY }}
rm: true
source: docker-compose.yml, build/schema/initdb.sql, metrics/prometheus/prometheus.yml
source: docker-compose.yml, build/schema/initdb.sql, metrics/prometheus/prometheus.yml, build/account.Dockerfile, build/auth.Dockerfile, build/category.Dockerfile
target: ~/${{ env.FOLDER_COMPOSE }}

- name: Get docker form dockerhub via SSH action
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/RebuildDb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: When initdb.sql updates rebuid docker on deploy action
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN go mod download
RUN go clean --modcache
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -o server ./cmd/api/main.go

FROM golang:latest AS run
FROM scratch AS run

WORKDIR /docker-hammywallet/

Expand Down
19 changes: 19 additions & 0 deletions build/account.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#Builder
FROM golang:1.21.0-alpine AS builder

COPY . /github.com/go-park-mail-ru/2023_2_Hamster/
WORKDIR /github.com/go-park-mail-ru/2023_2_Hamster/

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

FROM scratch AS run

WORKDIR /docker-hammywallet/

COPY --from=builder /github.com/go-park-mail-ru/2023_2_Hamster/account .

EXPOSE 8020

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

COPY . /github.com/go-park-mail-ru/2023_2_Hamster/
WORKDIR /github.com/go-park-mail-ru/2023_2_Hamster/

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

FROM scratch AS run

WORKDIR /docker-hammywallet/

COPY --from=builder /github.com/go-park-mail-ru/2023_2_Hamster/auth .

EXPOSE 8010

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

COPY . /github.com/go-park-mail-ru/2023_2_Hamster/
WORKDIR /github.com/go-park-mail-ru/2023_2_Hamster/

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

FROM scratch AS run

WORKDIR /docker-hammywallet/

COPY --from=builder /github.com/go-park-mail-ru/2023_2_Hamster/category .

EXPOSE 8030

ENTRYPOINT ["./category"]
26 changes: 13 additions & 13 deletions build/schema/initdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ DECLARE
accountCardID UUID;
BEGIN
INSERT INTO category (user_id, parent_tag, "name", show_income, show_outcome, regular)
VALUES (NEW.id, NULL, 'Дети', false, true, false),
(NEW.id, NULL, 'Забота о себе', false, true, false),
(NEW.id, NULL, 'Зарплата', true, false, true),
(NEW.id, NULL, 'Здровье и фитнес', false, true, false),
(NEW.id, NULL, 'Кафе и рестораны', false, true, false),
(NEW.id, NULL, 'Машина', false, true, false),
(NEW.id, NULL, 'Образование', false, true, false),
(NEW.id, NULL, 'Отдых и развлечения', false, true, false),
(NEW.id, NULL, 'Подарки', false, true, false),
(NEW.id, NULL, 'Покупки: одежа, техника', false, true, false),
(NEW.id, NULL, 'Проезд', false, true, false),
(NEW.id, NULL, 'Подписки', false, true, true),
(NEW.id, NULL, 'Продукты', false, true, false);
VALUES (NEW.id, NULL, 'Дети', false, true, false),
(NEW.id, NULL, 'Забота о себе', false, true, false),
(NEW.id, NULL, 'Зарплата', true, false, true),
(NEW.id, NULL, 'Здровье и фитнес', false, true, false),
(NEW.id, NULL, 'Кафе и рестораны', false, true, false),
(NEW.id, NULL, 'Машина', false, true, false),
(NEW.id, NULL, 'Образование', false, true, false),
(NEW.id, NULL, 'Отдых и развлечения', false, true, false),
(NEW.id, NULL, 'Подарки', false, true, false),
(NEW.id, NULL, 'Покупки: одежда, техника', false, true, false),
(NEW.id, NULL, 'Проезд', false, true, false),
(NEW.id, NULL, 'Подписки', false, true, true),
(NEW.id, NULL, 'Продукты', false, true, false);

SELECT id INTO categoryID FROM category WHERE name = 'Продукты' AND user_id = NEW.id;

Expand Down
79 changes: 79 additions & 0 deletions cmd/account/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"context"
"fmt"
"net"
"net/http"
"os"
"time"

"github.com/go-park-mail-ru/2023_2_Hamster/cmd/api/init/db/postgresql"
"github.com/go-park-mail-ru/2023_2_Hamster/internal/common/logger"
accountHandler "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/delivery/grpc"
generatedAccount "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/delivery/grpc/generated"
accountRep "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/repository/postgresql"
accountUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/usecase"
"github.com/go-park-mail-ru/2023_2_Hamster/internal/middleware"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
)

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

func run() (err error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
log := logger.NewLogger(ctx)
db, err := postgresql.InitPostgresDB(ctx)
if err != nil {
log.Errorf("Error Initializing PostgreSQL database: %v", err)
return
}
defer func() {
db.Close()

log.Info("Db closed without errors")
}()

log.Info("Db connection successfully")

accountRepo := accountRep.NewRepository(db, *log)

accountUsecase := accountUsecase.NewUsecase(accountRepo, *log)

service := accountHandler.NewAccountGRPC(accountUsecase, *log)

srv, ok := net.Listen("tcp", ":8020")
if ok != nil {
log.Fatalln("can't listen port", err)
}

metricsMw := middleware.NewMetricsMiddleware()
metricsMw.Register(middleware.ServiceAccountName)

server := grpc.NewServer(grpc.UnaryInterceptor(metricsMw.ServerMetricsInterceptor))

generatedAccount.RegisterAccountServiceServer(server, service)
r := mux.NewRouter().PathPrefix("/api").Subrouter()
r.PathPrefix("/metrics").Handler(promhttp.Handler())

http.Handle("/", r)
httpSrv := http.Server{Handler: r, Addr: ":8021"}

go func() {
err := httpSrv.ListenAndServe()
if err != nil {
fmt.Print(err)
}
}()

fmt.Print("creator running on: ", srv.Addr())
return server.Serve(srv)

}
58 changes: 43 additions & 15 deletions cmd/api/init/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import (
"github.com/go-park-mail-ru/2023_2_Hamster/cmd/api/init/router"
"github.com/go-park-mail-ru/2023_2_Hamster/internal/common/logger"
"github.com/go-park-mail-ru/2023_2_Hamster/internal/middleware"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/jackc/pgx/v4/pgxpool"
"github.com/redis/go-redis/v9"

generatedAccount "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/delivery/grpc/generated"
generatedAuth "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/auth/delivery/grpc/generated"
authDelivery "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/auth/delivery/http"
authRep "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/auth/repository/postgresql"
authUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/auth/usecase"
generatedCategory "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/category/delivery/grpc/generated"

categoryDelivary "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/category/delivery/http"
categoryRep "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/category/repository/postgres"
categoryUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/category/usecase"

csrfDelivery "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/csrf/delivery/http"
csrfUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/csrf/usecase"

transactionDelivery "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/transaction/delivery/http"
transactionRep "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/transaction/repository/postgresql"
transactionUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/transaction/usecase"
Expand All @@ -27,8 +29,6 @@ import (
userUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/user/usecase"

accountDelivery "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/delivery/http"
accountRep "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/repository/postgresql"
accountUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/usecase"

sessionRep "github.com/go-park-mail-ru/2023_2_Hamster/internal/monolithic/sessions/repository/redis"
sessionUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/monolithic/sessions/usecase"
Expand All @@ -37,34 +37,62 @@ import (
)

func Init(db *pgxpool.Pool, redis *redis.Client, log *logger.Logger) *mux.Router {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.FailOnNonTempDialError(true),
}

authConn, err := grpc.Dial("auth:8010", opts...)
if err != nil {
log.Fatalf("Connection refused auth: %v\n", err)
}

authClient := generatedAuth.NewAuthServiceClient(authConn)

accountConn, err := grpc.Dial("account:8020", opts...)

if err != nil {
log.Fatalf("Connection refused account %v\n", err)
}

authRep := authRep.NewRepository(db, *log)
categoryConn, err := grpc.Dial("category:8030", opts...)

if err != nil {
log.Fatalf("Connection refused category %v\n", err)
}

categortClient := generatedCategory.NewCategoryServiceClient(categoryConn)

accountClient := generatedAccount.NewAccountServiceClient(accountConn)
// authRep := authRep.NewRepository(db, *log)
sessionRep := sessionRep.NewSessionRepository(redis)

userRep := userRep.NewRepository(db, *log)
transactionRep := transactionRep.NewRepository(db, *log)
categoryRep := categoryRep.NewRepository(db, *log)
accountRep := accountRep.NewRepository(db, *log)
//categoryRep := categoryRep.NewRepository(db, *log)
// accountRep := accountRep.NewRepository(db, *log)

authUsecase := authUsecase.NewUsecase(authRep, *log)
// authUsecase := authUsecase.NewUsecase(authRep, *log)
sessionUsecase := sessionUsecase.NewSessionUsecase(sessionRep)
userUsecase := userUsecase.NewUsecase(userRep, *log)
transactionUsecase := transactionUsecase.NewUsecase(transactionRep, *log)
categoryUsecase := categoryUsecase.NewUsecase(categoryRep, *log)
//categoryUsecase := categoryUsecase.NewUsecase(categoryRep, *log)
csrfUsecase := csrfUsecase.NewUsecase(*log)
accountUsecase := accountUsecase.NewUsecase(accountRep, *log)
// accountUsecase := accountUsecase.NewUsecase(accountRep, *log)

authHandler := authDelivery.NewHandler(sessionUsecase, authClient, *log)

authMiddlewear := middleware.NewAuthMiddleware(sessionUsecase, userRep, *log)
logMiddlewear := middleware.NewLoggingMiddleware(*log)
recoveryMiddlewear := middleware.NewRecoveryMiddleware(*log)
csrfMiddlewear := middleware.NewCSRFMiddleware(csrfUsecase, *log)

authHandler := authDelivery.NewHandler(authUsecase, userUsecase, sessionUsecase, *log)
userHandler := userDelivery.NewHandler(userUsecase, *log)
transactionHandler := transactionDelivery.NewHandler(transactionUsecase, *log)
categoryHandler := categoryDelivary.NewHandler(categoryUsecase, *log)
categoryHandler := categoryDelivary.NewHandler(categortClient, *log)
csrfHandler := csrfDelivery.NewHandler(csrfUsecase, *log)
accountHandler := accountDelivery.NewHandler(accountUsecase, *log)
accountHandler := accountDelivery.NewHandler(accountClient, *log)

return router.InitRouter(
authHandler,
Expand Down
4 changes: 2 additions & 2 deletions cmd/api/init/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func InitRouter(auth *auth.Handler,
r.Use(middleware.RequestID)
r.Use(logMid.LoggingMiddleware)
r.Use(recoveryMid.Recoverer)
r.Use(middleware.Timeout(1000000 * time.Second))
r.Use(middleware.Timeout(5 * time.Second))
r.Use(middleware.Heartbeat("ping"))

http.Handle("/", r)
Expand All @@ -64,7 +64,7 @@ func InitRouter(auth *auth.Handler,
authRouter.Methods("POST").Path("/signin").HandlerFunc(auth.Login)
authRouter.Methods("POST").Path("/signup").HandlerFunc(auth.SignUp)
authRouter.Methods("POST").Path("/checkAuth").HandlerFunc(auth.HealthCheck)
authRouter.Methods("GET").Path("/loginCheck/{login}").HandlerFunc(auth.CheckLoginUnique)
authRouter.Methods("POST").Path("/loginCheck").HandlerFunc(auth.CheckLoginUnique)
authRouter.Methods("POST").Path("/logout").HandlerFunc(auth.LogOut)
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// @name session_id

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10000000*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

log := logger.NewLogger(ctx)
Expand Down Expand Up @@ -95,3 +95,9 @@ func main() {

log.Info("Server exiting")
}

// internal/microservices/delivery/grpc/{server, clent}
// proto/*proto
//
// cmd/apiGateway/main.go
// cmd/api/main.go
Loading

0 comments on commit 3ea3d60

Please sign in to comment.