Skip to content

Commit

Permalink
refactor: code and add new functionality
Browse files Browse the repository at this point in the history
* Update build message in Makefile

* Refactor token refresh logic in authorize handler

* Refactor ping endpoint to use WriteString method

* Add route printing functionality to main.go

* Add CORS, NoCache, SecureJSON, and GenerateContextId handlers
  • Loading branch information
funnyzak authored Feb 16, 2024
1 parent 2208656 commit 5715a9a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ deps:
build:
@echo "Building binary..."
@bash ./script/build.sh $(build_args)
@echo "Binary built."
@echo "Binary built successfully."
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"fmt"
"go-gin/cmd/srv/controller"
"go-gin/pkg/mygin"
"go-gin/pkg/utils"
"go-gin/service/singleton"

"github.com/gin-gonic/gin"
"github.com/ory/graceful"
flag "github.com/spf13/pflag"
)
Expand Down Expand Up @@ -62,6 +64,10 @@ func main() {
fmt.Printf(" - %-7s: %s\n", "Network", utils.Colorize(utils.ColorGreen, fmt.Sprintf("http://%s:%d", ip, port)))
}
}

fmt.Println()
fmt.Println("Server available routes:")
mygin.PrintRoute(srv.Handler.(*gin.Engine))
fmt.Println()
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/srv/controller/common_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@ func (p *commonPage) home(c *gin.Context) {
}

func (p *commonPage) ping(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
c.Writer.WriteString("pong")
}
27 changes: 14 additions & 13 deletions internal/gogin/authorize_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ func Authorize(opt AuthorizeOption) gin.HandlerFunc {
token = strings.TrimSpace(token)
if token != "" {
_, isLogin = getUserFromToken(token, c)
if !isLogin {
refreshToken, _ := c.Cookie(singleton.Conf.JWT.RefreshTokenCookieName)
if refreshToken != "" {
newToken, err := auth.RefreshToken(refreshToken, singleton.Conf)
if err != nil {
singleton.Log.Err(err).Msgf("RefreshToken: %v", err)
ShowErrorPage(c, unauthorizedErr, opt.IsPage)
return
}

UserLoginSuccess(c, newToken)
token = newToken.AccessToken
_, isLogin = getUserFromToken(token, c)
}

if !isLogin {
refreshToken, err := c.Cookie(singleton.Conf.JWT.RefreshTokenCookieName)
if refreshToken != "" && err == nil {
newToken, err := auth.RefreshToken(refreshToken, singleton.Conf)
if err != nil {
singleton.Log.Err(err).Msgf("RefreshToken: %v", err)
ShowErrorPage(c, unauthorizedErr, opt.IsPage)
return
}

UserLoginSuccess(c, newToken)
token = newToken.AccessToken
_, isLogin = getUserFromToken(token, c)
}
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/mygin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mygin

import (
"fmt"
"go-gin/pkg/utils"
"net/http"
"strings"

Expand Down Expand Up @@ -64,3 +65,10 @@ func BindForm(c *gin.Context, isForm bool, form interface{}) error {
return c.ShouldBindJSON(form)
}
}

func PrintRoute(r *gin.Engine) {
routes := r.Routes()
for _, route := range routes {
fmt.Printf(" - Route Path: %s, Method: %s\n", utils.Colorize(utils.ColorGreen, route.Path), utils.Colorize(utils.ColorGreen, route.Method))
}
}
File renamed without changes.

0 comments on commit 5715a9a

Please sign in to comment.