Skip to content

Commit

Permalink
router restructure done
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhihao committed Sep 14, 2022
1 parent e23e521 commit 4afa590
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions TigoWeb/Handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/karldoenitz/Tigo/binding"
"github.com/karldoenitz/Tigo/logger"
"html/template"
Expand Down Expand Up @@ -485,6 +486,18 @@ func (baseHandler *BaseHandler) GetJsonValue(key string) interface{} {
return mapResult[key]
}

// GetPathParam 根据key获取Url上的参数
func (baseHandler *BaseHandler) GetPathParam(key string) (value PathParam) {
vars := mux.Vars(baseHandler.Request)
value = PathParam(vars[key])
return
}

// GetPathParamStr 根据key获取Url上的参数
func (baseHandler *BaseHandler) GetPathParamStr(key string) string {
return mux.Vars(baseHandler.Request)[key]
}

// BeforeRequest 在每次响应HTTP请求之前执行此函数
func (baseHandler *BaseHandler) BeforeRequest() {
return
Expand Down
32 changes: 32 additions & 0 deletions TigoWeb/Struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,35 @@ func (jsonParam *ReqParams) To(result interface{}) {
logger.Warning.Println(err.Error())
}
}

/////////////////////////////////////////////// Path Param /////////////////////////////////////////////////////////////

type PathParam string

// ToBool 将从url路径中获取参数转换为布尔型
func (p PathParam) ToBool() bool {
value := string(p)
if strings.ToLower(value) == "true" || strings.ToLower(value) == "1" {
return true
}
return false
}

// ToFloat 将url路径中的参数转换为浮点型
func (p PathParam) ToFloat() (float64, error) {
value := string(p)
result, err := strconv.ParseFloat(value, 64)
return result, err
}

// ToInt 将url路径中的参数转换成整型
func (p PathParam) ToInt() (int, error) {
value := string(p)
result, err := strconv.Atoi(value)
return result, err
}

// ToStr 将url路径中的参数转换为字符串型
func (p PathParam) ToStr() string {
return string(p)
}
1 change: 1 addition & 0 deletions demo/GetParams/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (testHandler *TestHandler) Post() {

var url = []TigoWeb.Pattern{
{"/test", TestHandler{}, nil},
{"/user/{userId}/info/{isShowExtra}", TestHandler{}, nil},
}

func main() {
Expand Down

0 comments on commit 4afa590

Please sign in to comment.