Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: code and add new functionality #9

Merged
merged 5 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.