diff --git a/backend/api/api.go b/backend/api/api.go new file mode 100644 index 0000000..20e39d5 --- /dev/null +++ b/backend/api/api.go @@ -0,0 +1,31 @@ +package api + +import ( + "backend/api/router" + "backend/config" + "fmt" + "github.com/gin-gonic/gin" +) + +func StartServer(config *config.Config) { + gin.SetMode(config.Server.RunningMode) + engine := gin.New() + + RegisterRoutes(engine, config) + + err := engine.Run(fmt.Sprintf(":%s", config.Server.InternalPort)) + if err != nil { + fmt.Println("Error while starting server...") + } +} + +func RegisterRoutes(engine *gin.Engine, config *config.Config) { + apiRoute := engine.Group("/api") + + v1Route := apiRoute.Group("/v1") + { + userAccountsRoute := v1Route.Group("/auth") + + router.StartAuthRouter(userAccountsRoute, config) + } +} diff --git a/backend/api/handler/user_account_handler.go b/backend/api/handler/user_account_handler.go index b2afa70..af39dda 100644 --- a/backend/api/handler/user_account_handler.go +++ b/backend/api/handler/user_account_handler.go @@ -3,13 +3,20 @@ package handler import ( "backend/api/dto" "backend/api/response" + "backend/config" "backend/service" "github.com/gin-gonic/gin" "net/http" ) type UserAccountHandler struct { - userAccountService service.UserAccountService + userAccountService *service.UserAccountService +} + +func NewUserAccountHandler(config *config.Config) *UserAccountHandler { + return &UserAccountHandler{ + userAccountService: service.NewUserAccountService(config), + } } func (handler UserAccountHandler) Login(context *gin.Context) { diff --git a/backend/api/router/base_router.go b/backend/api/router/base_router.go new file mode 100644 index 0000000..f9e0c6f --- /dev/null +++ b/backend/api/router/base_router.go @@ -0,0 +1,14 @@ +package router + +import ( + "backend/api/handler" + "backend/config" + "github.com/gin-gonic/gin" +) + +func StartAuthRouter(router *gin.RouterGroup, config *config.Config) { + userAccountHandler := handler.NewUserAccountHandler(config) + + router.POST("/login", userAccountHandler.Login) + router.POST("/register", userAccountHandler.Register) +} diff --git a/backend/config/config.go b/backend/config/config.go index b2c09be..d01be0e 100644 --- a/backend/config/config.go +++ b/backend/config/config.go @@ -3,9 +3,10 @@ package config import "time" type Config struct { - Database DatabaseConfig - JWT JwtConfig - AuthConfig AuthConfig + Database DatabaseConfig + JWT JwtConfig + Auth AuthConfig + Server ServerConfig } type DatabaseConfig struct { @@ -30,3 +31,9 @@ type JwtConfig struct { type AuthConfig struct { BCryptCost int } + +type ServerConfig struct { + RunningMode string + InternalPort string + ExternalPort string +} diff --git a/backend/service/token_service.go b/backend/service/token_service.go index 07791e3..16be37a 100644 --- a/backend/service/token_service.go +++ b/backend/service/token_service.go @@ -16,7 +16,13 @@ const ( ) type TokenService struct { - config config.Config + config *config.Config +} + +func NewTokenService(config *config.Config) *TokenService { + return &TokenService{ + config: config, + } } func (service *TokenService) GenerateToken(token *dto.TokenRequest) (*dto.TokenDetail, error) { diff --git a/backend/service/user_account_service.go b/backend/service/user_account_service.go index 29f9d43..9f57fa3 100644 --- a/backend/service/user_account_service.go +++ b/backend/service/user_account_service.go @@ -19,6 +19,17 @@ type UserAccountService struct { config *config.Config } +func NewUserAccountService(config *config.Config) *UserAccountService { + return &UserAccountService{ + userAccountRepository: repository.NewUserAccountRepository(), + userService: NewUserService(), + tokenService: NewTokenService(config), + roleService: NewRoleService(), + userRoleService: NewUserRoleService(), + config: config, + } +} + func (service UserAccountService) Login(request *dto.LoginRequest) (*dto.TokenDetail, error) { userAccount, err := service.userAccountRepository.FindByEmail(request.Email) if err != nil { @@ -51,7 +62,7 @@ func (service UserAccountService) Login(request *dto.LoginRequest) (*dto.TokenDe } func (service UserAccountService) Register(context context.Context, request *dto.RegisterRequest) (*dto.TokenDetail, error) { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(request.Password), service.config.AuthConfig.BCryptCost) + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(request.Password), service.config.Auth.BCryptCost) if err != nil { return nil, err }