From 7aad07fe1c2e45d0d0f677814c62c48ed558ff17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ovidiu=20=C8=98tefan=20Popescu?= Date: Tue, 16 Jan 2024 19:08:39 +0200 Subject: [PATCH] Prepared entire project to build and deploy it on Docker --- backend/cmd/main.go | 5 - backend/config/config.go | 39 ------ backend/config/development-config.yml | 10 -- backend/data/database/migration/1_init.go | 96 --------------- backend/src/Dockerfile | 25 ++++ backend/{ => src}/api/api.go | 4 +- backend/{ => src}/api/dto/user.go | 0 backend/{ => src}/api/handler/base_handler.go | 38 +++--- .../api/handler/user_account_handler.go | 24 ++-- .../{ => src}/api/response/base_response.go | 4 +- .../api/response/http_status_mapping.go | 2 +- backend/{ => src}/api/router/base_router.go | 4 +- .../api/validator/validation_error.go | 0 backend/src/cmd/main.go | 22 ++++ backend/src/config/config.go | 107 +++++++++++++++++ backend/src/config/development-config.yml | 21 ++++ backend/src/config/docker-config.yml | 21 ++++ backend/src/data/database/migration/1_init.go | 112 ++++++++++++++++++ backend/{ => src}/data/database/postgres.go | 4 +- backend/{ => src}/data/model/base.go | 0 backend/{ => src}/data/model/base_model.go | 17 +-- backend/{ => src}/data/model/post.go | 0 backend/{ => src}/data/model/property.go | 0 backend/{ => src}/data/model/user.go | 0 .../data}/repository/base_repository.go | 6 +- .../data}/repository/role_repository.go | 4 +- .../repository/user_account_repository.go | 4 +- .../data}/repository/user_role_repository.go | 4 +- backend/src/docker-compose.yml | 56 +++++++++ backend/{ => src}/error/error_code.go | 0 backend/{ => src}/error/internal_error.go | 0 backend/{ => src}/go.mod | 18 ++- backend/{ => src}/go.sum | 34 ++++++ backend/{ => src}/service/base_service.go | 4 +- backend/{ => src}/service/role_service.go | 4 +- backend/{ => src}/service/token_service.go | 10 +- .../{ => src}/service/user_account_service.go | 16 +-- .../{ => src}/service/user_role_service.go | 4 +- backend/{ => src}/service/user_service.go | 4 +- backend/{ => src}/util/type_converter.go | 0 40 files changed, 495 insertions(+), 228 deletions(-) delete mode 100644 backend/cmd/main.go delete mode 100644 backend/config/config.go delete mode 100644 backend/config/development-config.yml delete mode 100644 backend/data/database/migration/1_init.go create mode 100644 backend/src/Dockerfile rename backend/{ => src}/api/api.go (85%) rename backend/{ => src}/api/dto/user.go (100%) rename backend/{ => src}/api/handler/base_handler.go (53%) rename backend/{ => src}/api/handler/user_account_handler.go (59%) rename backend/{ => src}/api/response/base_response.go (93%) rename backend/{ => src}/api/response/http_status_mapping.go (88%) rename backend/{ => src}/api/router/base_router.go (74%) rename backend/{ => src}/api/validator/validation_error.go (100%) create mode 100644 backend/src/cmd/main.go create mode 100644 backend/src/config/config.go create mode 100644 backend/src/config/development-config.yml create mode 100644 backend/src/config/docker-config.yml create mode 100644 backend/src/data/database/migration/1_init.go rename backend/{ => src}/data/database/postgres.go (91%) rename backend/{ => src}/data/model/base.go (100%) rename backend/{ => src}/data/model/base_model.go (68%) rename backend/{ => src}/data/model/post.go (100%) rename backend/{ => src}/data/model/property.go (100%) rename backend/{ => src}/data/model/user.go (100%) rename backend/{ => src/data}/repository/base_repository.go (94%) rename backend/{ => src/data}/repository/role_repository.go (83%) rename backend/{ => src/data}/repository/user_account_repository.go (90%) rename backend/{ => src/data}/repository/user_role_repository.go (73%) create mode 100644 backend/src/docker-compose.yml rename backend/{ => src}/error/error_code.go (100%) rename backend/{ => src}/error/internal_error.go (100%) rename backend/{ => src}/go.mod (69%) rename backend/{ => src}/go.sum (76%) rename backend/{ => src}/service/base_service.go (92%) rename backend/{ => src}/service/role_service.go (83%) rename backend/{ => src}/service/token_service.go (89%) rename backend/{ => src}/service/user_account_service.go (86%) rename backend/{ => src}/service/user_role_service.go (82%) rename backend/{ => src}/service/user_service.go (74%) rename backend/{ => src}/util/type_converter.go (100%) diff --git a/backend/cmd/main.go b/backend/cmd/main.go deleted file mode 100644 index 70dfb1f..0000000 --- a/backend/cmd/main.go +++ /dev/null @@ -1,5 +0,0 @@ -package cmd - -func main() { - -} diff --git a/backend/config/config.go b/backend/config/config.go deleted file mode 100644 index d01be0e..0000000 --- a/backend/config/config.go +++ /dev/null @@ -1,39 +0,0 @@ -package config - -import "time" - -type Config struct { - Database DatabaseConfig - JWT JwtConfig - Auth AuthConfig - Server ServerConfig -} - -type DatabaseConfig struct { - Host string - Port string - User string - Password string - DbName string - SSLMode string - MaxIdleConnections int - MaxOpenConnections int - ConnMaxLifetime time.Duration -} - -type JwtConfig struct { - AccessTokenExpireDurationMinutes time.Duration - RefreshTokenExpireDurationMinutes time.Duration - AccessTokenSecret string - RefreshTokenSecret string -} - -type AuthConfig struct { - BCryptCost int -} - -type ServerConfig struct { - RunningMode string - InternalPort string - ExternalPort string -} diff --git a/backend/config/development-config.yml b/backend/config/development-config.yml deleted file mode 100644 index e6a7336..0000000 --- a/backend/config/development-config.yml +++ /dev/null @@ -1,10 +0,0 @@ -postgres: - host: localhost - port: 5432 - user: postgres - password: admin - dbName: property_market_db - sslMode: disable - maxIdleConnections: 15 - maxOpenConnections: 100 - connMaxLifetime: 5 \ No newline at end of file diff --git a/backend/data/database/migration/1_init.go b/backend/data/database/migration/1_init.go deleted file mode 100644 index 31eb597..0000000 --- a/backend/data/database/migration/1_init.go +++ /dev/null @@ -1,96 +0,0 @@ -package migration - -import ( - db "backend/data/database" - "backend/data/model" - "fmt" - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" - "log" -) - -const ( - bcryptHashCost int = 10 - adminUserEmail string = "admin@estate-market.com" - adminUserPassword string = "admin" -) - -func Run() { - database := db.GetDatabase() - - createAllTables(database) - createRolesAndAdminUser(database) -} - -func createAllTables(database *gorm.DB) { - var tables []interface{} - - addTable(database, &tables, model.Country{}) - - err := database.Migrator().CreateTable(tables...) - if err != nil { - log.Println("Error while running `1_init` migration: " + err.Error()) - return - } - log.Println("Migration `1_init` has successfully run.") -} - -func addTable(database *gorm.DB, tables *[]interface{}, model interface{}) { - if !database.Migrator().HasTable(model) { - *tables = append(*tables, model) - } -} - -func createRolesAndAdminUser(database *gorm.DB) { - adminRole := model.Role{Name: "ADMIN"} - - createRoleIfNotExists(database, &adminRole) - createRoleIfNotExists(database, &model.Role{Name: "USER"}) - createAdminUserInformation(database, adminRole) -} - -func createRoleIfNotExists(database *gorm.DB, role *model.Role) { - exists := 0 - database. - Model(&model.Role{}). - Select("1"). - Where("name = ?", role.Name). - First(&exists) - if exists == 0 { - database.Create(role) - } -} - -func createAdminUserInformation(database *gorm.DB, role model.Role) { - encryptedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(adminUserPassword), bcryptHashCost) - if err != nil { - log.Println("Error while encrypting user password: ", err) - } - - encryptedPassword := fmt.Sprintf("%s", encryptedPasswordBytes) - exists := 0 - database. - Model(&model.UserAccount{}). - Select("1"). - Where("email = ?", adminUserEmail). - First(&exists) - if exists == 0 { - userAccount := model.UserAccount{Email: adminUserEmail, Password: encryptedPassword} - database.Create(userAccount) - - user := model.User{ - FirstName: "Admin", - LastName: "Account", - PhoneNumber: "0555512345", - Enabled: true, - UserAccount: userAccount, - } - database.Create(user) - - userRole := model.UserRole{ - User: user, - Role: role, - } - database.Create(userRole) - } -} diff --git a/backend/src/Dockerfile b/backend/src/Dockerfile new file mode 100644 index 0000000..688d327 --- /dev/null +++ b/backend/src/Dockerfile @@ -0,0 +1,25 @@ +FROM --platform=linux/amd64 golang:1.21-bookworm as builder + +WORKDIR /app + +COPY go.* ./ +RUN go mod download + +COPY . ./ + +RUN CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -v -o server ./cmd/main.go + +FROM --platform=linux/amd64 debian:bookworm-slim +RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \ + ca-certificates && \ + rm -rf /var/lib/apt/lists/* + +COPY --from=builder /app/server /app/server +COPY --from=builder /app/config/docker-config.yml /app/config/docker-config.yml + +ENV APP_ENV docker +ENV PORT ${Port} + +RUN chmod +x /app/server + +CMD ["/app/server"] \ No newline at end of file diff --git a/backend/api/api.go b/backend/src/api/api.go similarity index 85% rename from backend/api/api.go rename to backend/src/api/api.go index 20e39d5..9a166eb 100644 --- a/backend/api/api.go +++ b/backend/src/api/api.go @@ -1,9 +1,9 @@ package api import ( - "backend/api/router" - "backend/config" "fmt" + "github.com/Stefan923/go-estate-market/api/router" + "github.com/Stefan923/go-estate-market/config" "github.com/gin-gonic/gin" ) diff --git a/backend/api/dto/user.go b/backend/src/api/dto/user.go similarity index 100% rename from backend/api/dto/user.go rename to backend/src/api/dto/user.go diff --git a/backend/api/handler/base_handler.go b/backend/src/api/handler/base_handler.go similarity index 53% rename from backend/api/handler/base_handler.go rename to backend/src/api/handler/base_handler.go index a283d08..a23e011 100644 --- a/backend/api/handler/base_handler.go +++ b/backend/src/api/handler/base_handler.go @@ -1,9 +1,9 @@ package handler import ( - "backend/api/response" - error2 "backend/error" "context" + response2 "github.com/Stefan923/go-estate-market/api/response" + error3 "github.com/Stefan923/go-estate-market/error" "github.com/gin-gonic/gin" "net/http" "strconv" @@ -14,17 +14,17 @@ func Create[T any, R any](context *gin.Context, caller func(context context.Cont err := context.ShouldBindJSON(&request) if err != nil { context.AbortWithStatusJSON(http.StatusBadRequest, - response.GenerateResponseWithValidationError(nil, false, err)) + response2.GenerateResponseWithValidationError(nil, false, err)) return } responseObject, err := caller(context, request) if err != nil { - context.AbortWithStatusJSON(response.TranslateErrorToStatusCode(err), - response.GenerateResponseWithError(nil, false, err)) + context.AbortWithStatusJSON(response2.TranslateErrorToStatusCode(err), + response2.GenerateResponseWithError(nil, false, err)) return } - context.JSON(http.StatusCreated, response.GenerateResponse(responseObject, true)) + context.JSON(http.StatusCreated, response2.GenerateResponse(responseObject, true)) } func Update[T any, R any](context *gin.Context, caller func(context context.Context, id int, request *T) (*R, error)) { @@ -33,51 +33,51 @@ func Update[T any, R any](context *gin.Context, caller func(context context.Cont err := context.ShouldBindJSON(&request) if err != nil { context.AbortWithStatusJSON(http.StatusBadRequest, - response.GenerateResponseWithValidationError(nil, false, err)) + response2.GenerateResponseWithValidationError(nil, false, err)) return } responseObject, err := caller(context, id, request) if err != nil { - context.AbortWithStatusJSON(response.TranslateErrorToStatusCode(err), - response.GenerateResponseWithError(nil, false, err)) + context.AbortWithStatusJSON(response2.TranslateErrorToStatusCode(err), + response2.GenerateResponseWithError(nil, false, err)) return } - context.JSON(http.StatusOK, response.GenerateResponse(responseObject, true)) + context.JSON(http.StatusOK, response2.GenerateResponse(responseObject, true)) } func Delete(context *gin.Context, caller func(context context.Context, id int) error) { id, _ := strconv.Atoi(context.Params.ByName("id")) if id == 0 { context.AbortWithStatusJSON(http.StatusNotFound, - response.GenerateResponseWithError(nil, false, &error2.InternalError{ - EndUserMessage: error2.InvalidPathParameter, + response2.GenerateResponseWithError(nil, false, &error3.InternalError{ + EndUserMessage: error3.InvalidPathParameter, })) return } err := caller(context, id) if err != nil { - context.AbortWithStatusJSON(response.TranslateErrorToStatusCode(err), - response.GenerateResponseWithError(nil, false, err)) + context.AbortWithStatusJSON(response2.TranslateErrorToStatusCode(err), + response2.GenerateResponseWithError(nil, false, err)) return } - context.JSON(http.StatusOK, response.GenerateResponse(nil, true)) + context.JSON(http.StatusOK, response2.GenerateResponse(nil, true)) } func GetById[R any](context *gin.Context, caller func(context context.Context, id int) (*R, error)) { id, _ := strconv.Atoi(context.Params.ByName("id")) if id == 0 { context.AbortWithStatusJSON(http.StatusNotFound, - response.GenerateResponse(nil, false)) + response2.GenerateResponse(nil, false)) return } responseObject, err := caller(context, id) if err != nil { - context.AbortWithStatusJSON(response.TranslateErrorToStatusCode(err), - response.GenerateResponseWithError(nil, false, err)) + context.AbortWithStatusJSON(response2.TranslateErrorToStatusCode(err), + response2.GenerateResponseWithError(nil, false, err)) return } - context.JSON(http.StatusOK, response.GenerateResponse(responseObject, true)) + context.JSON(http.StatusOK, response2.GenerateResponse(responseObject, true)) } diff --git a/backend/api/handler/user_account_handler.go b/backend/src/api/handler/user_account_handler.go similarity index 59% rename from backend/api/handler/user_account_handler.go rename to backend/src/api/handler/user_account_handler.go index af39dda..5c9a761 100644 --- a/backend/api/handler/user_account_handler.go +++ b/backend/src/api/handler/user_account_handler.go @@ -1,10 +1,10 @@ package handler import ( - "backend/api/dto" - "backend/api/response" - "backend/config" - "backend/service" + "github.com/Stefan923/go-estate-market/api/dto" + response2 "github.com/Stefan923/go-estate-market/api/response" + "github.com/Stefan923/go-estate-market/config" + "github.com/Stefan923/go-estate-market/service" "github.com/gin-gonic/gin" "net/http" ) @@ -25,19 +25,19 @@ func (handler UserAccountHandler) Login(context *gin.Context) { if err != nil { context.AbortWithStatusJSON( http.StatusBadRequest, - response.GenerateResponseWithValidationError(nil, false, err)) + response2.GenerateResponseWithValidationError(nil, false, err)) return } tokenDetail, err := handler.userAccountService.Login(request) if err != nil { context.AbortWithStatusJSON( - response.TranslateErrorToStatusCode(err), - response.GenerateResponseWithError(nil, false, err)) + response2.TranslateErrorToStatusCode(err), + response2.GenerateResponseWithError(nil, false, err)) return } - context.JSON(http.StatusCreated, response.GenerateResponse(tokenDetail, true)) + context.JSON(http.StatusCreated, response2.GenerateResponse(tokenDetail, true)) } func (handler UserAccountHandler) Register(context *gin.Context) { @@ -46,17 +46,17 @@ func (handler UserAccountHandler) Register(context *gin.Context) { if err != nil { context.AbortWithStatusJSON( http.StatusBadRequest, - response.GenerateResponseWithValidationError(nil, false, err)) + response2.GenerateResponseWithValidationError(nil, false, err)) return } tokenDetail, err := handler.userAccountService.Register(context, request) if err != nil { context.AbortWithStatusJSON( - response.TranslateErrorToStatusCode(err), - response.GenerateResponseWithError(nil, false, err)) + response2.TranslateErrorToStatusCode(err), + response2.GenerateResponseWithError(nil, false, err)) return } - context.JSON(http.StatusCreated, response.GenerateResponse(tokenDetail, false)) + context.JSON(http.StatusCreated, response2.GenerateResponse(tokenDetail, false)) } diff --git a/backend/api/response/base_response.go b/backend/src/api/response/base_response.go similarity index 93% rename from backend/api/response/base_response.go rename to backend/src/api/response/base_response.go index e374b27..f6b79a1 100644 --- a/backend/api/response/base_response.go +++ b/backend/src/api/response/base_response.go @@ -1,6 +1,8 @@ package response -import "backend/api/validator" +import ( + "github.com/Stefan923/go-estate-market/api/validator" +) type BaseHttpResponse struct { Result any `json:"result"` diff --git a/backend/api/response/http_status_mapping.go b/backend/src/api/response/http_status_mapping.go similarity index 88% rename from backend/api/response/http_status_mapping.go rename to backend/src/api/response/http_status_mapping.go index 45789f4..c08e342 100644 --- a/backend/api/response/http_status_mapping.go +++ b/backend/src/api/response/http_status_mapping.go @@ -1,7 +1,7 @@ package response import ( - error2 "backend/error" + error2 "github.com/Stefan923/go-estate-market/error" "net/http" ) diff --git a/backend/api/router/base_router.go b/backend/src/api/router/base_router.go similarity index 74% rename from backend/api/router/base_router.go rename to backend/src/api/router/base_router.go index f9e0c6f..455b98a 100644 --- a/backend/api/router/base_router.go +++ b/backend/src/api/router/base_router.go @@ -1,8 +1,8 @@ package router import ( - "backend/api/handler" - "backend/config" + "github.com/Stefan923/go-estate-market/api/handler" + "github.com/Stefan923/go-estate-market/config" "github.com/gin-gonic/gin" ) diff --git a/backend/api/validator/validation_error.go b/backend/src/api/validator/validation_error.go similarity index 100% rename from backend/api/validator/validation_error.go rename to backend/src/api/validator/validation_error.go diff --git a/backend/src/cmd/main.go b/backend/src/cmd/main.go new file mode 100644 index 0000000..edb0237 --- /dev/null +++ b/backend/src/cmd/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "github.com/Stefan923/go-estate-market/api" + "github.com/Stefan923/go-estate-market/config" + "github.com/Stefan923/go-estate-market/data/database" + "github.com/Stefan923/go-estate-market/data/database/migration" + "log" +) + +func main() { + appConfig := config.GetConfig() + + err := database.InitDatabase(appConfig) + defer database.CloseDatabase() + if err != nil { + log.Println("Error while initializing database: ", err.Error()) + } + migration.Run() + + api.StartServer(appConfig) +} diff --git a/backend/src/config/config.go b/backend/src/config/config.go new file mode 100644 index 0000000..5d2319f --- /dev/null +++ b/backend/src/config/config.go @@ -0,0 +1,107 @@ +package config + +import ( + "errors" + "github.com/spf13/viper" + "log" + "os" + "time" +) + +type Config struct { + Database DatabaseConfig + JWT JwtConfig + Auth AuthConfig + Server ServerConfig +} + +type DatabaseConfig struct { + Host string + Port string + User string + Password string + DatabaseName string + SSLMode string + MaxIdleConnections int + MaxOpenConnections int + ConnMaxLifetime time.Duration +} + +type JwtConfig struct { + AccessTokenExpireDurationMinutes time.Duration + RefreshTokenExpireDurationMinutes time.Duration + AccessTokenSecret string + RefreshTokenSecret string +} + +type AuthConfig struct { + BCryptCost int +} + +type ServerConfig struct { + RunningMode string + InternalPort string + ExternalPort string +} + +func GetConfig() *Config { + cfgPath := getConfigPath(os.Getenv("APP_ENV")) + configReader, err := LoadConfig(cfgPath, "yml") + if err != nil { + log.Fatalf("Error while loading config: %v", err) + } + + config, err := ParseConfig(configReader) + envPort := os.Getenv("PORT") + if envPort != "" { + config.Server.ExternalPort = envPort + log.Printf("Set external port from environment -> %s", config.Server.ExternalPort) + } else { + config.Server.ExternalPort = config.Server.InternalPort + log.Printf("Set external port from environment -> %s", config.Server.ExternalPort) + } + if err != nil { + log.Fatalf("Error while parsing config: %v", err) + } + + return config +} + +func ParseConfig(configReader *viper.Viper) (*Config, error) { + var config Config + err := configReader.Unmarshal(&config) + if err != nil { + log.Printf("Unable to parse config: %v", err) + return nil, err + } + return &config, nil +} + +func LoadConfig(filename string, fileType string) (*viper.Viper, error) { + configReader := viper.New() + configReader.SetConfigType(fileType) + configReader.SetConfigName(filename) + configReader.AddConfigPath(".") + configReader.AutomaticEnv() + + err := configReader.ReadInConfig() + if err != nil { + log.Printf("Unable to read config: %v", err) + var configFileNotFoundError viper.ConfigFileNotFoundError + if errors.As(err, &configFileNotFoundError) { + return nil, errors.New("could not find configuration file") + } + return nil, err + } + return configReader, nil +} + +func getConfigPath(env string) string { + if env == "docker" { + return "/app/config/docker-config" + } else if env == "production" { + return "/app/config/production-config" + } else { + return "/app/config/development-config" + } +} diff --git a/backend/src/config/development-config.yml b/backend/src/config/development-config.yml new file mode 100644 index 0000000..233204e --- /dev/null +++ b/backend/src/config/development-config.yml @@ -0,0 +1,21 @@ +postgres: + host: "localhost" + port: "5432" + user: "postgres" + password: "admin" + databaseName: "property_market_db" + sslMode: "disable" + maxIdleConnections: 15 + maxOpenConnections: 100 + connMaxLifetime: 5 +jwt: + accessTokenSecret: "accessSecret" + refreshTokenSecret: "refreshSecret" + accessTokenExpireDurationMinutes: 1440 + refreshTokenExpireDurationMinutes: 60 +auth: + bCryptCost: 10 +server: + internalPort: 5005 + externalPort: 5005 + runningMode: debug \ No newline at end of file diff --git a/backend/src/config/docker-config.yml b/backend/src/config/docker-config.yml new file mode 100644 index 0000000..6d66488 --- /dev/null +++ b/backend/src/config/docker-config.yml @@ -0,0 +1,21 @@ +database: + host: "postgres_container" + port: "5432" + user: "postgres" + password: "admin" + databaseName: "property_market_db" + sslMode: "disable" + maxIdleConnections: 15 + maxOpenConnections: 100 + connMaxLifetime: 5 +jwt: + accessTokenSecret: "accessSecret" + refreshTokenSecret: "refreshSecret" + accessTokenExpireDurationMinutes: 1440 + refreshTokenExpireDurationMinutes: 60 +auth: + bCryptCost: 10 +server: + internalPort: 5005 + externalPort: 5005 + runningMode: debug \ No newline at end of file diff --git a/backend/src/data/database/migration/1_init.go b/backend/src/data/database/migration/1_init.go new file mode 100644 index 0000000..e4de435 --- /dev/null +++ b/backend/src/data/database/migration/1_init.go @@ -0,0 +1,112 @@ +package migration + +import ( + "fmt" + db "github.com/Stefan923/go-estate-market/data/database" + model2 "github.com/Stefan923/go-estate-market/data/model" + "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" + "log" +) + +const ( + bcryptHashCost int = 10 + adminUserEmail string = "admin@estate-market.com" + adminUserPassword string = "admin" +) + +func Run() { + database := db.GetDatabase() + + createAllTables(database) + createRolesAndAdminUser(database) +} + +func createAllTables(database *gorm.DB) { + var tables []interface{} + + addTable(database, &tables, model2.UserAccount{}) + addTable(database, &tables, model2.User{}) + addTable(database, &tables, model2.Role{}) + addTable(database, &tables, model2.UserRole{}) + + addTable(database, &tables, model2.Country{}) + addTable(database, &tables, model2.State{}) + addTable(database, &tables, model2.City{}) + addTable(database, &tables, model2.Address{}) + + addTable(database, &tables, model2.PropertyCategory{}) + addTable(database, &tables, model2.Property{}) + addTable(database, &tables, model2.PropertyDetails{}) + addTable(database, &tables, model2.PropertyPrice{}) + + addTable(database, &tables, model2.Post{}) + addTable(database, &tables, model2.PostComment{}) + + err := database.Migrator().CreateTable(tables...) + if err != nil { + log.Println("Error while running `1_init` migration: " + err.Error()) + return + } + log.Println("Migration `1_init` has successfully run.") +} + +func addTable(database *gorm.DB, tables *[]interface{}, model interface{}) { + if !database.Migrator().HasTable(model) { + *tables = append(*tables, model) + } +} + +func createRolesAndAdminUser(database *gorm.DB) { + adminRole := model2.Role{Name: "ADMIN"} + + createRoleIfNotExists(database, &adminRole) + createRoleIfNotExists(database, &model2.Role{Name: "USER"}) + createAdminUserInformation(database, adminRole) +} + +func createRoleIfNotExists(database *gorm.DB, role *model2.Role) { + exists := 0 + database. + Model(&model2.Role{}). + Select("1"). + Where("name = ?", role.Name). + First(&exists) + if exists == 0 { + database.Create(role) + } +} + +func createAdminUserInformation(database *gorm.DB, role model2.Role) { + exists := 0 + database. + Model(&model2.UserAccount{}). + Select("1"). + Where("email = ?", adminUserEmail). + First(&exists) + if exists == 0 { + encryptedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(adminUserPassword), bcryptHashCost) + if err != nil { + log.Println("Error while encrypting user password: ", err) + } + encryptedPassword := fmt.Sprintf("%s", encryptedPasswordBytes) + + userAccount := model2.UserAccount{Email: adminUserEmail, Password: encryptedPassword} + database.Create(userAccount) + + user := model2.User{ + FirstName: "Admin", + LastName: "Account", + PhoneNumber: "0555512345", + Enabled: true, + UserAccount: userAccount, + } + database.Create(user) + + userRole := model2.UserRole{ + User: user, + Role: role, + } + database.Create(userRole) + } +} diff --git a/backend/data/database/postgres.go b/backend/src/data/database/postgres.go similarity index 91% rename from backend/data/database/postgres.go rename to backend/src/data/database/postgres.go index 39d085f..e9c04da 100644 --- a/backend/data/database/postgres.go +++ b/backend/src/data/database/postgres.go @@ -1,8 +1,8 @@ package database import ( - "backend/config" "fmt" + "github.com/Stefan923/go-estate-market/config" "gorm.io/driver/postgres" "gorm.io/gorm" "log" @@ -15,7 +15,7 @@ func InitDatabase(config *config.Config) error { var err error connectionDetails := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s TimeZone=Europe/Bucharest", config.Database.Host, config.Database.Port, config.Database.User, config.Database.Password, - config.Database.DbName, config.Database.SSLMode) + config.Database.DatabaseName, config.Database.SSLMode) databaseClient, err = gorm.Open(postgres.Open(connectionDetails), &gorm.Config{}) if err != nil { diff --git a/backend/data/model/base.go b/backend/src/data/model/base.go similarity index 100% rename from backend/data/model/base.go rename to backend/src/data/model/base.go diff --git a/backend/data/model/base_model.go b/backend/src/data/model/base_model.go similarity index 68% rename from backend/data/model/base_model.go rename to backend/src/data/model/base_model.go index 0d6a9f5..8e2da7d 100644 --- a/backend/data/model/base_model.go +++ b/backend/src/data/model/base_model.go @@ -2,6 +2,7 @@ package model import ( "database/sql" + "gorm.io/gorm" "time" ) @@ -9,21 +10,21 @@ type BaseModel struct { Id uint `gorm:"primarykey"` CreatedAt time.Time `gorm:"type:TIMESTAMP with time zone; not null;"` - ModifiedAt sql.NullTime `gorm:"type:TIMESTAMP with time zone; not null;"` - DeletedAt sql.NullTime `gorm:"type:TIMESTAMP with time zone; not null;"` + ModifiedAt sql.NullTime `gorm:"type:TIMESTAMP with time zone; null;"` + DeletedAt sql.NullTime `gorm:"type:TIMESTAMP with time zone; null;"` } -func (m *BaseModel) BeforeCreate() { +func (m *BaseModel) BeforeCreate(*gorm.DB) error { m.CreatedAt = time.Now().UTC() - return + return nil } -func (m *BaseModel) BeforeUpdate() { +func (m *BaseModel) BeforeUpdate(*gorm.DB) error { m.ModifiedAt = sql.NullTime{Time: time.Now().UTC(), Valid: true} - return + return nil } -func (m *BaseModel) BeforeDelete() { +func (m *BaseModel) BeforeDelete(*gorm.DB) error { m.DeletedAt = sql.NullTime{Time: time.Now().UTC(), Valid: true} - return + return nil } diff --git a/backend/data/model/post.go b/backend/src/data/model/post.go similarity index 100% rename from backend/data/model/post.go rename to backend/src/data/model/post.go diff --git a/backend/data/model/property.go b/backend/src/data/model/property.go similarity index 100% rename from backend/data/model/property.go rename to backend/src/data/model/property.go diff --git a/backend/data/model/user.go b/backend/src/data/model/user.go similarity index 100% rename from backend/data/model/user.go rename to backend/src/data/model/user.go diff --git a/backend/repository/base_repository.go b/backend/src/data/repository/base_repository.go similarity index 94% rename from backend/repository/base_repository.go rename to backend/src/data/repository/base_repository.go index 7d40594..8857cfd 100644 --- a/backend/repository/base_repository.go +++ b/backend/src/data/repository/base_repository.go @@ -1,11 +1,11 @@ package repository import ( - db "backend/data/database" - "backend/data/model" - "backend/util" "context" "database/sql" + db "github.com/Stefan923/go-estate-market/data/database" + "github.com/Stefan923/go-estate-market/data/model" + "github.com/Stefan923/go-estate-market/util" "gorm.io/gorm" "log" "time" diff --git a/backend/repository/role_repository.go b/backend/src/data/repository/role_repository.go similarity index 83% rename from backend/repository/role_repository.go rename to backend/src/data/repository/role_repository.go index cc17cb9..d878bad 100644 --- a/backend/repository/role_repository.go +++ b/backend/src/data/repository/role_repository.go @@ -1,8 +1,8 @@ package repository import ( - "backend/data/database" - "backend/data/model" + "github.com/Stefan923/go-estate-market/data/database" + "github.com/Stefan923/go-estate-market/data/model" ) type RoleRepository struct { diff --git a/backend/repository/user_account_repository.go b/backend/src/data/repository/user_account_repository.go similarity index 90% rename from backend/repository/user_account_repository.go rename to backend/src/data/repository/user_account_repository.go index fd1dd4c..f2e16d2 100644 --- a/backend/repository/user_account_repository.go +++ b/backend/src/data/repository/user_account_repository.go @@ -1,8 +1,8 @@ package repository import ( - db "backend/data/database" - "backend/data/model" + db "github.com/Stefan923/go-estate-market/data/database" + "github.com/Stefan923/go-estate-market/data/model" ) type UserAccountRepository struct { diff --git a/backend/repository/user_role_repository.go b/backend/src/data/repository/user_role_repository.go similarity index 73% rename from backend/repository/user_role_repository.go rename to backend/src/data/repository/user_role_repository.go index 62ff290..1b1a546 100644 --- a/backend/repository/user_role_repository.go +++ b/backend/src/data/repository/user_role_repository.go @@ -1,8 +1,8 @@ package repository import ( - "backend/data/database" - "backend/data/model" + "github.com/Stefan923/go-estate-market/data/database" + "github.com/Stefan923/go-estate-market/data/model" ) type UserRoleRepository struct { diff --git a/backend/src/docker-compose.yml b/backend/src/docker-compose.yml new file mode 100644 index 0000000..53d111a --- /dev/null +++ b/backend/src/docker-compose.yml @@ -0,0 +1,56 @@ +version: '3.7' + +services: + postgres: + image: postgres + container_name: postgres_container + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: admin + POSTGRES_DB: property_market_db + PGDATA: /data/postgres + volumes: + - postgres:/data/postgres + ports: + - "5432:5432" + networks: + - estate_market_network + restart: unless-stopped + + pgadmin: + image: dpage/pgadmin4 + container_name: pgadmin_container + environment: + PGADMIN_DEFAULT_EMAIL: stefanpopescu923@gmail.com + PGADMIN_DEFAULT_PASSWORD: 123456 + volumes: + - pgadmin:/var/lib/pgadmin + ports: + - "8090:80" + networks: + - estate_market_network + restart: unless-stopped + depends_on: + - postgres + + backend: + build: ../src/ + image: backend-image:latest + container_name: backend + environment: + - PORT=9003 + ports: + - "9003:5000" + networks: + - estate_market_network + depends_on: + - postgres + restart: unless-stopped + +volumes: + postgres: + pgadmin: + +networks: + estate_market_network: + driver: bridge \ No newline at end of file diff --git a/backend/error/error_code.go b/backend/src/error/error_code.go similarity index 100% rename from backend/error/error_code.go rename to backend/src/error/error_code.go diff --git a/backend/error/internal_error.go b/backend/src/error/internal_error.go similarity index 100% rename from backend/error/internal_error.go rename to backend/src/error/internal_error.go diff --git a/backend/go.mod b/backend/src/go.mod similarity index 69% rename from backend/go.mod rename to backend/src/go.mod index 1f6dfd8..3362d0e 100644 --- a/backend/go.mod +++ b/backend/src/go.mod @@ -1,4 +1,4 @@ -module backend +module github.com/Stefan923/go-estate-market go 1.20 @@ -11,6 +11,7 @@ require ( github.com/bytedance/sonic v1.10.2 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-gonic/gin v1.9.1 // indirect @@ -19,6 +20,7 @@ require ( github.com/go-playground/validator/v10 v10.16.0 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/jackc/pgx/v5 v5.5.0 // indirect @@ -28,19 +30,33 @@ require ( github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect github.com/leodido/go-urn v1.2.4 // indirect + github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.18.2 // indirect github.com/stretchr/testify v1.8.4 // indirect + github.com/subosito/gotenv v1.6.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect + go.uber.org/atomic v1.11.0 // indirect + go.uber.org/multierr v1.11.0 // indirect golang.org/x/arch v0.6.0 // indirect golang.org/x/crypto v0.16.0 // indirect + golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/backend/go.sum b/backend/src/go.sum similarity index 76% rename from backend/go.sum rename to backend/src/go.sum index b377cf8..4afbef2 100644 --- a/backend/go.sum +++ b/backend/src/go.sum @@ -12,6 +12,9 @@ github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLI github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -31,6 +34,8 @@ github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzq github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= @@ -51,8 +56,12 @@ github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZY github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -62,6 +71,21 @@ github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOS github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -73,15 +97,23 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= +go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE= +golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= @@ -97,6 +129,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/backend/service/base_service.go b/backend/src/service/base_service.go similarity index 92% rename from backend/service/base_service.go rename to backend/src/service/base_service.go index 70d5f3b..ac30ce0 100644 --- a/backend/service/base_service.go +++ b/backend/src/service/base_service.go @@ -1,9 +1,9 @@ package service import ( - "backend/repository" - "backend/util" "context" + "github.com/Stefan923/go-estate-market/data/repository" + "github.com/Stefan923/go-estate-market/util" ) type BaseService[T any, Tc any, Tu any, Tr any] struct { diff --git a/backend/service/role_service.go b/backend/src/service/role_service.go similarity index 83% rename from backend/service/role_service.go rename to backend/src/service/role_service.go index 97b11e5..ef68329 100644 --- a/backend/service/role_service.go +++ b/backend/src/service/role_service.go @@ -1,8 +1,8 @@ package service import ( - "backend/data/model" - "backend/repository" + "github.com/Stefan923/go-estate-market/data/model" + "github.com/Stefan923/go-estate-market/data/repository" ) type RoleService struct { diff --git a/backend/service/token_service.go b/backend/src/service/token_service.go similarity index 89% rename from backend/service/token_service.go rename to backend/src/service/token_service.go index 16be37a..5e3a44d 100644 --- a/backend/service/token_service.go +++ b/backend/src/service/token_service.go @@ -1,9 +1,9 @@ package service import ( - "backend/api/dto" - "backend/config" - error2 "backend/error" + "github.com/Stefan923/go-estate-market/api/dto" + "github.com/Stefan923/go-estate-market/config" + error3 "github.com/Stefan923/go-estate-market/error" "github.com/golang-jwt/jwt" "time" ) @@ -61,7 +61,7 @@ func (service *TokenService) VerifyToken(token string) (*jwt.Token, error) { accessToken, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) { _, ok := token.Method.(*jwt.SigningMethodHMAC) if !ok { - return nil, &error2.InternalError{EndUserMessage: error2.InvalidToken} + return nil, &error3.InternalError{EndUserMessage: error3.InvalidToken} } return []byte(service.config.JWT.AccessTokenSecret), nil }) @@ -88,5 +88,5 @@ func (service *TokenService) GetClaims(token string) (map[string]interface{}, er return claimsMap, nil } - return nil, &error2.InternalError{EndUserMessage: error2.ClaimsNotFound} + return nil, &error3.InternalError{EndUserMessage: error3.ClaimsNotFound} } diff --git a/backend/service/user_account_service.go b/backend/src/service/user_account_service.go similarity index 86% rename from backend/service/user_account_service.go rename to backend/src/service/user_account_service.go index 9f57fa3..a8b43c6 100644 --- a/backend/service/user_account_service.go +++ b/backend/src/service/user_account_service.go @@ -1,12 +1,12 @@ package service import ( - "backend/api/dto" - "backend/config" - "backend/data/model" - error2 "backend/error" - "backend/repository" "context" + "github.com/Stefan923/go-estate-market/api/dto" + "github.com/Stefan923/go-estate-market/config" + "github.com/Stefan923/go-estate-market/data/model" + "github.com/Stefan923/go-estate-market/data/repository" + error3 "github.com/Stefan923/go-estate-market/error" "golang.org/x/crypto/bcrypt" ) @@ -38,12 +38,12 @@ func (service UserAccountService) Login(request *dto.LoginRequest) (*dto.TokenDe user, err := service.userService.GetById(userAccount.Id) if err != nil { - return nil, &error2.InternalError{EndUserMessage: error2.InvalidCredentials} + return nil, &error3.InternalError{EndUserMessage: error3.InvalidCredentials} } err = bcrypt.CompareHashAndPassword([]byte(userAccount.Password), []byte(request.Password)) if err != nil { - return nil, &error2.InternalError{EndUserMessage: error2.InvalidCredentials} + return nil, &error3.InternalError{EndUserMessage: error3.InvalidCredentials} } var roles []string @@ -68,7 +68,7 @@ func (service UserAccountService) Register(context context.Context, request *dto } if exists, _ := service.userAccountRepository.ExistsByEmail(request.Email); !exists { - return nil, &error2.InternalError{EndUserMessage: error2.EmailAlreadyUsed} + return nil, &error3.InternalError{EndUserMessage: error3.EmailAlreadyUsed} } userAccount := model.UserAccount{ diff --git a/backend/service/user_role_service.go b/backend/src/service/user_role_service.go similarity index 82% rename from backend/service/user_role_service.go rename to backend/src/service/user_role_service.go index b758c11..36de36f 100644 --- a/backend/service/user_role_service.go +++ b/backend/src/service/user_role_service.go @@ -1,8 +1,8 @@ package service import ( - "backend/data/model" - "backend/repository" + "github.com/Stefan923/go-estate-market/data/model" + "github.com/Stefan923/go-estate-market/data/repository" ) type UserRoleService struct { diff --git a/backend/service/user_service.go b/backend/src/service/user_service.go similarity index 74% rename from backend/service/user_service.go rename to backend/src/service/user_service.go index 4351202..8884371 100644 --- a/backend/service/user_service.go +++ b/backend/src/service/user_service.go @@ -1,8 +1,8 @@ package service import ( - "backend/data/model" - "backend/repository" + "github.com/Stefan923/go-estate-market/data/model" + "github.com/Stefan923/go-estate-market/data/repository" ) type UserService struct { diff --git a/backend/util/type_converter.go b/backend/src/util/type_converter.go similarity index 100% rename from backend/util/type_converter.go rename to backend/src/util/type_converter.go