Skip to content

Commit

Permalink
Implemented api server and routers
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan923 committed Dec 20, 2023
1 parent 1f3f7b3 commit 220c4bc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 6 deletions.
31 changes: 31 additions & 0 deletions backend/api/api.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
9 changes: 8 additions & 1 deletion backend/api/handler/user_account_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions backend/api/router/base_router.go
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 10 additions & 3 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,3 +31,9 @@ type JwtConfig struct {
type AuthConfig struct {
BCryptCost int
}

type ServerConfig struct {
RunningMode string
InternalPort string
ExternalPort string
}
8 changes: 7 additions & 1 deletion backend/service/token_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion backend/service/user_account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 220c4bc

Please sign in to comment.