Skip to content

Commit

Permalink
refactor: refact router package
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayheaven committed Jan 19, 2024
1 parent e5904fa commit 2f97635
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 49 deletions.
13 changes: 5 additions & 8 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cmd

import (
config "go-gin-boilerplate/configs"
"go-gin-boilerplate/configs"
"go-gin-boilerplate/internal/pkg/logger"
"go-gin-boilerplate/internal/pkg/mysql"
"go-gin-boilerplate/internal/pkg/redis"
routes "go-gin-boilerplate/internal/router"
"go-gin-boilerplate/internal/router"

"net/http"

Expand Down Expand Up @@ -41,17 +41,14 @@ func init() {

func start() {

// init routes
r, err := routes.Init()
if err != nil {
panic(err)
}
// init router
r := router.Router

// init logger
logger := logger.LogrusLogger

// load env config
EnvConfig := config.EnvConfig
EnvConfig := configs.EnvConfig

// connect database
mysql.Connect(&EnvConfig.Mysql)
Expand Down
2 changes: 2 additions & 0 deletions internal/middlewares/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (

// Cors middleware
func Cors() gin.HandlerFunc {

return cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
})

}
45 changes: 26 additions & 19 deletions internal/middlewares/errorHandle.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package middlewares

import (
"log"
"net/http"
"runtime/debug"

"github.com/gin-gonic/gin"
)

// ErrorHandle is a middleware to handle panic
func ErrorHandle(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
// log error

// print stack trace
debug.PrintStack()

// return error response
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"message": "Internal Server Error",
})

// abort request
c.Abort()
}
}()
c.Next()
func ErrorHandle() gin.HandlerFunc {
return func(c *gin.Context) {

defer func() {
if err := recover(); err != nil {
// log error
log.Println(err)

// print stack trace
debug.PrintStack()

// return error response
c.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
"message": "Internal Server Error",
})

// abort request
c.Abort()
}
}()

c.Next()
}

}
1 change: 0 additions & 1 deletion internal/middlewares/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func Jwt() gin.HandlerFunc {
}

ctx.Next()

}

}
2 changes: 1 addition & 1 deletion internal/middlewares/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/gin-gonic/gin"
// loads all middlewares, ErrorHandle, Cors
func LoadMiddlewares(r *gin.Engine) *gin.Engine {

r.Use(ErrorHandle)
r.Use(ErrorHandle())
r.Use(Cors())

return r
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (

LogrusLogger *logrus.Logger

// export Logger
Logger *logrus.Logger
)

Expand Down
2 changes: 1 addition & 1 deletion internal/router/example.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes
package router

import (
"go-gin-boilerplate/internal/controllers"
Expand Down
2 changes: 1 addition & 1 deletion internal/router/public.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes
package router

import (
"go-gin-boilerplate/internal/controllers"
Expand Down
29 changes: 12 additions & 17 deletions internal/router/router.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes
package router

import (
"go-gin-boilerplate/internal/middlewares"
Expand All @@ -10,29 +10,24 @@ import (
_ "go-gin-boilerplate/api/swagger" // docs.go
)

func Init() (*gin.Engine, error) {
r := gin.Default()
var Router *gin.Engine

// public := LoadPublicRoutes(r)
// api := r.Group("/api")
// {
// public
// }
func init() {
Router = gin.Default()

// Global middlewares
Router.Use(middlewares.ErrorHandle())
Router.Use(middlewares.Cors())

// public routes, no auth required
LoadPublicRoutes(r)
LoadPublicRoutes(Router)

// user routes
LoadUserRoutes(r)

// Loading middlewares
middlewares.LoadMiddlewares(r)
LoadUserRoutes(Router)

// example routes
LoadExampleRoutes(r)
LoadExampleRoutes(Router)

// init swagger
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))

return r, nil
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
}
2 changes: 1 addition & 1 deletion internal/router/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes
package router

import (
"go-gin-boilerplate/internal/controllers"
Expand Down

0 comments on commit 2f97635

Please sign in to comment.