代码拉取完成,页面将自动刷新
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package web
import (
"context"
"errors"
"net/http"
"reflect"
"gitee.com/go-spring2/spring-base/knife"
"gitee.com/go-spring2/spring-base/util"
)
const (
httpRequestKey = "::HttpRequest::"
)
// bindHandler BIND 形式的 Web 处理接口
type bindHandler struct {
fn interface{}
fnType reflect.Type
fnValue reflect.Value
bindType reflect.Type
}
func (b *bindHandler) Invoke(ctx Context) {
err := knife.Store(ctx.Context(), httpRequestKey, ctx.Request())
util.Panic(err).When(err != nil)
RPCInvoke(ctx, b.call)
}
func (b *bindHandler) call(ctx Context) interface{} {
// 反射创建需要绑定请求参数
bindVal := reflect.New(b.bindType.Elem())
err := ctx.Bind(bindVal.Interface())
util.Panic(err).When(err != nil)
// 执行处理函数,并返回结果
ctxVal := reflect.ValueOf(ctx.Request().Context())
in := []reflect.Value{ctxVal, bindVal}
return b.fnValue.Call(in)[0].Interface()
}
func (b *bindHandler) FileLine() (file string, line int, fnName string) {
return util.FileLine(b.fn)
}
func validBindFn(fnType reflect.Type) bool {
// 必须是函数,必须有两个入参,必须有一个返回值
if fnType.Kind() != reflect.Func || fnType.NumIn() != 2 || fnType.NumOut() != 1 {
return false
}
// 第一个入参必须是 context.Context 类型
if !util.IsContextType(fnType.In(0)) {
return false
}
req := fnType.In(1) // 第二个入参必须是结构体指针
return req.Kind() == reflect.Ptr && req.Elem().Kind() == reflect.Struct
}
// BIND 转换成 BIND 形式的 Web 处理接口
func BIND(fn interface{}) Handler {
if fnType := reflect.TypeOf(fn); validBindFn(fnType) {
return &bindHandler{
fn: fn,
fnType: fnType,
fnValue: reflect.ValueOf(fn),
bindType: fnType.In(1),
}
}
panic(errors.New("fn should be func(context.Context, *struct})anything"))
}
// GetHTTPRequest returns the *http.Request storing in the context.Context.
func GetHTTPRequest(ctx context.Context) *http.Request {
v, err := knife.Load(ctx, httpRequestKey)
util.Panic(err).When(err != nil)
r, _ := v.(*http.Request)
return r
}
// RPCInvoke 可自定义的 rpc 执行函数。
var RPCInvoke = func(ctx Context, fn func(Context) interface{}) {
ctx.JSON(fn(ctx))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。