Skip to content

Commit

Permalink
fix: 调整了 middleware 的检查
Browse files Browse the repository at this point in the history
  • Loading branch information
tangx committed Dec 6, 2021
1 parent 3c92617 commit 989c25f
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 6 deletions.
18 changes: 18 additions & 0 deletions internal/demo-test/gin-basicauth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

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

func main() {
r := gin.Default()

r.Use(gin.BasicAuth(authUers))

r.GET("/index", func(c *gin.Context) {
c.String(200, "index")
})
_ = r.Run()
}

var authUers = map[string]string{
"user1": "tangxin",
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions internal/example/apis/auth/mid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package auth

import "github.com/tangx/rum-gonic/pkg/middlewares"

var AdminUsersMiddelware = middlewares.BasicAuth(map[string]string{
"user1": "tangxin",
})
2 changes: 2 additions & 0 deletions internal/example/apis/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apis

import (
"github.com/tangx/rum-gonic/internal/example/apis/auth"
"github.com/tangx/rum-gonic/internal/example/apis/index"
"github.com/tangx/rum-gonic/internal/example/apis/pingpong"
"github.com/tangx/rum-gonic/rum"
Expand All @@ -16,6 +17,7 @@ func init() {
RouterRoot.Register(RouterV0)

{
RouterV0.Register(auth.AdminUsersMiddelware)
// rum handler mode
RouterV0.Register(&pingpong.PingPong{})
RouterV0.Register(&index.Index{})
Expand Down
3 changes: 2 additions & 1 deletion internal/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func main() {
)

// 注册中间件
r.Register(middlewares.NoCacheIndex())
r.Register(middlewares.DefaultNoCacheIndex())
// r.Register(auth.AdminUsers)

// 添加路由组 / 路由
r.Register(apis.RouterRoot)
Expand Down
10 changes: 8 additions & 2 deletions internal/example/main.http
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ GET http://127.0.0.1:8080/rum/v0/ping/zhangsan?age=20

### GET index
GET http://127.0.0.1:8080/index?name=zhangsan

# Authorization: Basic user1:tangxin

### GET v0/ping
GET http://127.0.0.1:8080/v0/ping/wangwu?age=102


### user
GET http://127.0.0.1:8080/index
GET http://127.0.0.1:8080/index



### Get /v0/index
GET http://127.0.0.1:8080/v0/index?name=zhangsan
Authorization: Basic user1:tangxin
18 changes: 18 additions & 0 deletions pkg/middlewares/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middlewares

import (
"github.com/gin-gonic/gin"
"github.com/tangx/rum-gonic/rum"
)

func BasicAuth(accounts gin.Accounts) rum.MiddlewareOperator {
return rum.NewMiddleware(
gin.BasicAuth(accounts),
)
}

func BasicAuthRealm(accounts gin.Accounts, realm string) rum.MiddlewareOperator {
return rum.NewMiddleware(
gin.BasicAuthForRealm(accounts, realm),
)
}
4 changes: 2 additions & 2 deletions pkg/middlewares/no_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"github.com/tangx/rum-gonic/rum"
)

func NoCacheIndex() *rum.Middleware {
func DefaultNoCacheIndex() rum.MiddlewareOperator {

pages := []string{"/", "/index", "index.html"}

return NoCahe(pages)
}

func NoCahe(pages []string) *rum.Middleware {
func NoCahe(pages []string) rum.MiddlewareOperator {

mid := func(c *gin.Context) {
path := c.Request.URL.Path
Expand Down
7 changes: 6 additions & 1 deletion rum/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type MiddlewareOperator interface {
MiddlewareFunc() HandlerFunc
Operator
}

// 接口检查
Expand All @@ -14,7 +15,7 @@ var _ MiddlewareOperator = (*Middleware)(nil)

type Middleware struct {
middwareFunc HandlerFunc
Operator
// Operator
}

type HandlerFunc = gin.HandlerFunc
Expand All @@ -29,3 +30,7 @@ func (mid *Middleware) MiddlewareFunc() HandlerFunc {
// fmt.Println("注册中间件咯")
return mid.middwareFunc
}

func (mid *Middleware) Output(c *gin.Context) (interface{}, error) {
return nil, nil
}

0 comments on commit 989c25f

Please sign in to comment.