Skip to content

Commit

Permalink
merge routes/routes.go+register.go as router.go
Browse files Browse the repository at this point in the history
  • Loading branch information
axiaoxin committed Jun 14, 2022
1 parent d223ae6 commit df1319a
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 16 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
middlewares := DefaultGinMiddlewares()
app := webserver.NewGinEngine(middlewares...)
// 注册路由
routes.Register(app)
routes.InitRouter(app)
// 运行服务
webserver.Run(app)
}
Binary file added pink-lady
Binary file not shown.
2 changes: 1 addition & 1 deletion routes/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestPing(t *testing.T) {
viper.Set("basic_auth.username", "admin")
viper.Set("basic_auth.password", "admin")
defer viper.Reset()
Register(r)
InitRouter(r)
recorder, err := goutils.RequestHTTPHandler(
r,
"GET",
Expand Down
9 changes: 7 additions & 2 deletions routes/register.go → routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const (
DisableGinSwaggerEnvkey = "DISABLE_GIN_SWAGGER"
)

// Register 在 gin engine 上注册 url 对应的 HandlerFunc
func Register(httpHandler http.Handler) {
// InitRouter 在 gin engine 上注册 url 对应的 HandlerFunc
func InitRouter(httpHandler http.Handler) {
app, ok := httpHandler.(*gin.Engine)
if !ok {
panic("HTTP handler must be *gin.Engine")
Expand Down Expand Up @@ -88,3 +88,8 @@ func Register(httpHandler http.Handler) {
// 注册其他 gin HandlerFunc
Routes(app)
}

// Routes 注册 API URL 路由
func Routes(app *gin.Engine) {
// TODO: 在这里注册你的 gin API,如: app.GET("/", HandlerFunc)
}
4 changes: 2 additions & 2 deletions routes/register_test.go → routes/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRegisterRoutes(t *testing.T) {
func TestInitRouter(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
// Register 中的 basic auth 依赖 viper 配置
Expand All @@ -21,7 +21,7 @@ func TestRegisterRoutes(t *testing.T) {

services.Init()

Register(r)
InitRouter(r)
recorder, err := goutils.RequestHTTPHandler(
r,
"GET",
Expand Down
10 changes: 0 additions & 10 deletions routes/routes.go

This file was deleted.

0 comments on commit df1319a

Please sign in to comment.