Skip to content

Commit

Permalink
Merge pull request #26 from go-park-mail-ru/task/PRI-41
Browse files Browse the repository at this point in the history
Task/pri 41
  • Loading branch information
vovangy authored Apr 10, 2024
2 parents cca4ee0 + ce417ee commit 0866106
Show file tree
Hide file tree
Showing 40 changed files with 823 additions and 299 deletions.
43 changes: 22 additions & 21 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/joho/godotenv"
_ "github.com/lib/pq"
httpSwagger "github.com/swaggo/http-swagger"
"go.uber.org/zap"
)

// @title Sample Project API
Expand All @@ -47,6 +48,7 @@ import (
// @schemes http https
func main() {
_ = 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"),
Expand All @@ -68,25 +70,25 @@ func main() {
r.HandleFunc("/ping", pingPongHandler).Methods(http.MethodGet)
r.PathPrefix("/docs/").Handler(httpSwagger.WrapHandler)

authRepo := authR.NewRepository(db)
authUsecase := authUc.NewAuthUsecase(authRepo)
autHandler := authH.NewAuthHandler(authUsecase)
authRepo := authR.NewRepository(db, logger)
authUsecase := authUc.NewAuthUsecase(authRepo, logger)
autHandler := authH.NewAuthHandler(authUsecase, logger)

jwtMd := middleware.NewAuthMiddleware(authUsecase)
jwtMd := middleware.NewAuthMiddleware(authUsecase, logger)

auth := r.PathPrefix("/auth").Subrouter()
auth.HandleFunc("/signup", autHandler.SignUp).Methods(http.MethodPost, http.MethodOptions)
auth.HandleFunc("/login", autHandler.Login).Methods(http.MethodPost, http.MethodOptions)
auth.Handle("/logout", jwtMd.JwtTMiddleware(http.HandlerFunc(autHandler.Logout))).Methods(http.MethodGet, http.MethodOptions)
auth.Handle("/check_auth", jwtMd.JwtTMiddleware(http.HandlerFunc(autHandler.CheckAuth))).Methods(http.MethodGet, http.MethodOptions)

advertRepo := advertsR.NewRepository(db)
advertUsecase := advertsUc.NewAdvertUsecase(advertRepo)
advertHandler := advertsH.NewAdvertHandler(advertUsecase)
advertRepo := advertsR.NewRepository(db, logger)
advertUsecase := advertsUc.NewAdvertUsecase(advertRepo, logger)
advertHandler := advertsH.NewAdvertHandler(advertUsecase, logger)

imageRepo := imageR.NewRepository(db)
imageUsecase := imageUc.NewImageUsecase(imageRepo)
imageHandler := imageH.NewImageHandler(imageUsecase)
imageRepo := imageR.NewRepository(db, logger)
imageUsecase := imageUc.NewImageUsecase(imageRepo, logger)
imageHandler := imageH.NewImageHandler(imageUsecase, logger)

advert := r.PathPrefix("/adverts").Subrouter()
advert.HandleFunc("/{id}", advertHandler.GetAdvertById).Methods(http.MethodGet, http.MethodOptions)
Expand All @@ -113,18 +115,18 @@ func main() {
user.Handle("/password", jwtMd.JwtTMiddleware(http.HandlerFunc(userHandler.UpdateUserPassword))).Methods(http.MethodPost, http.MethodOptions)
user.Handle("/myadverts", jwtMd.JwtTMiddleware(http.HandlerFunc(advertHandler.GetUserAdverts))).Methods(http.MethodGet, http.MethodOptions)

companyRepo := companyR.NewRepository(db)
companyUsecase := companyUc.NewCompanyUsecase(companyRepo)
companyHandler := companyH.NewCompanyHandler(companyUsecase)
companyRepo := companyR.NewRepository(db, logger)
companyUsecase := companyUc.NewCompanyUsecase(companyRepo, logger)
companyHandler := companyH.NewCompanyHandler(companyUsecase, logger)

company := r.PathPrefix("/companies").Subrouter()
company.HandleFunc("/", companyHandler.CreateCompany).Methods(http.MethodPost, http.MethodOptions)
company.HandleFunc("/{id}", companyHandler.GetCompanyById).Methods(http.MethodGet, http.MethodOptions)
company.HandleFunc("/images/{id}", companyHandler.UpdateCompanyPhoto).Methods(http.MethodPost, http.MethodOptions)

complexRepo := complexR.NewRepository(db)
complexUsecase := complexUc.NewComplexUsecase(complexRepo)
complexHandler := complexH.NewComplexHandler(complexUsecase)
complexRepo := complexR.NewRepository(db, logger)
complexUsecase := complexUc.NewComplexUsecase(complexRepo, logger)
complexHandler := complexH.NewComplexHandler(complexUsecase, logger)

complex := r.PathPrefix("/complexes").Subrouter()
complex.HandleFunc("/", complexHandler.CreateComplex).Methods(http.MethodPost, http.MethodOptions)
Expand All @@ -147,21 +149,20 @@ func main() {
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)

go func() {
log.Printf("Start server on %s\n", srv.Addr)
logger.Info(fmt.Sprintf("Start server on %s\n", srv.Addr))
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
logger.Error(fmt.Sprintf("listen: %s\n", err))
}
}()

sig := <-signalCh
log.Printf("Received signal: %v\n", sig)
logger.Info(fmt.Sprintf("Received signal: %v\n", sig))

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

if err := srv.Shutdown(ctx); err != nil {
log.Panic("Server shutdown failed: ", err, '\n')

logger.Error(fmt.Sprintf("Server shutdown failed: %s\n", err))
}
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/swaggo/files v1.0.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/tools v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg=
github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down
Loading

0 comments on commit 0866106

Please sign in to comment.