|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package web |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "net/http" |
| 23 | + "reflect" |
| 24 | + |
| 25 | + "github.com/go-spring-projects/go-spring/internal/utils" |
| 26 | + "github.com/go-spring-projects/go-spring/web/binding" |
| 27 | + "github.com/go-spring-projects/go-spring/web/render" |
| 28 | +) |
| 29 | + |
| 30 | +type Renderer interface { |
| 31 | + Render(ctx context.Context, err error, result interface{}) render.Renderer |
| 32 | +} |
| 33 | + |
| 34 | +type RendererFunc func(ctx context.Context, err error, result interface{}) render.Renderer |
| 35 | + |
| 36 | +func (fn RendererFunc) Render(ctx context.Context, err error, result interface{}) render.Renderer { |
| 37 | + return fn(ctx, err, result) |
| 38 | +} |
| 39 | + |
| 40 | +// Bind convert fn to HandlerFunc. |
| 41 | +// |
| 42 | +// func(ctx context.Context) |
| 43 | +// |
| 44 | +// func(ctx context.Context) R |
| 45 | +// |
| 46 | +// func(ctx context.Context, req T) R |
| 47 | +// |
| 48 | +// func(ctx context.Context, req T) (R, error) |
| 49 | +// |
| 50 | +// func(writer http.ResponseWriter, request *http.Request) |
| 51 | +func Bind(fn interface{}, render Renderer) http.HandlerFunc { |
| 52 | + |
| 53 | + fnValue := reflect.ValueOf(fn) |
| 54 | + fnType := fnValue.Type() |
| 55 | + |
| 56 | + switch h := fn.(type) { |
| 57 | + case http.HandlerFunc: |
| 58 | + return h |
| 59 | + case http.Handler: |
| 60 | + return h.ServeHTTP |
| 61 | + case func(http.ResponseWriter, *http.Request): |
| 62 | + return h |
| 63 | + default: |
| 64 | + // valid func |
| 65 | + if err := validMappingFunc(fnType); nil != err { |
| 66 | + panic(err) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return func(writer http.ResponseWriter, request *http.Request) { |
| 71 | + |
| 72 | + // param of context |
| 73 | + webCtx := &Context{Writer: writer, Request: request} |
| 74 | + ctx := WithContext(request.Context(), webCtx) |
| 75 | + ctxValue := reflect.ValueOf(ctx) |
| 76 | + |
| 77 | + defer func() { |
| 78 | + if nil != request.MultipartForm { |
| 79 | + request.MultipartForm.RemoveAll() |
| 80 | + } |
| 81 | + request.Body.Close() |
| 82 | + }() |
| 83 | + |
| 84 | + var returnValues []reflect.Value |
| 85 | + var err error |
| 86 | + |
| 87 | + defer func() { |
| 88 | + if r := recover(); nil != r { |
| 89 | + if e, ok := r.(error); ok { |
| 90 | + err = fmt.Errorf("%s: %w", request.URL.Path, e) |
| 91 | + } else { |
| 92 | + err = fmt.Errorf("%s: %v", request.URL.Path, r) |
| 93 | + } |
| 94 | + |
| 95 | + // render error response |
| 96 | + render.Render(ctx, err, nil).Render(writer) |
| 97 | + } |
| 98 | + }() |
| 99 | + |
| 100 | + switch fnType.NumIn() { |
| 101 | + case 1: |
| 102 | + returnValues = fnValue.Call([]reflect.Value{ctxValue}) |
| 103 | + case 2: |
| 104 | + paramType := fnType.In(1) |
| 105 | + pointer := false |
| 106 | + if reflect.Ptr == paramType.Kind() { |
| 107 | + paramType = paramType.Elem() |
| 108 | + pointer = true |
| 109 | + } |
| 110 | + |
| 111 | + // new param instance with paramType. |
| 112 | + paramValue := reflect.New(paramType) |
| 113 | + // bind paramValue with request |
| 114 | + if err = binding.Bind(paramValue.Interface(), webCtx); nil != err { |
| 115 | + break |
| 116 | + } |
| 117 | + if !pointer { |
| 118 | + paramValue = paramValue.Elem() |
| 119 | + } |
| 120 | + returnValues = fnValue.Call([]reflect.Value{ctxValue, paramValue}) |
| 121 | + default: |
| 122 | + panic("unreachable here") |
| 123 | + } |
| 124 | + |
| 125 | + var result interface{} |
| 126 | + |
| 127 | + if nil == err { |
| 128 | + switch len(returnValues) { |
| 129 | + case 0: |
| 130 | + // nothing |
| 131 | + return |
| 132 | + case 1: |
| 133 | + // write response |
| 134 | + result = returnValues[0].Interface() |
| 135 | + case 2: |
| 136 | + // check error |
| 137 | + result = returnValues[0].Interface() |
| 138 | + if e, ok := returnValues[1].Interface().(error); ok && nil != e { |
| 139 | + err = e |
| 140 | + } |
| 141 | + default: |
| 142 | + panic("unreachable here") |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // render response |
| 147 | + render.Render(ctx, err, result).Render(writer) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func validMappingFunc(fnType reflect.Type) error { |
| 152 | + // func(ctx context.Context) |
| 153 | + // func(ctx context.Context) R |
| 154 | + // func(ctx context.Context, req T) R |
| 155 | + // func(ctx context.Context, req T) (R, error) |
| 156 | + if !utils.IsFuncType(fnType) { |
| 157 | + return fmt.Errorf("%s: not a func", fnType.String()) |
| 158 | + } |
| 159 | + |
| 160 | + if fnType.NumIn() < 1 || fnType.NumIn() > 2 { |
| 161 | + return fmt.Errorf("%s: invalid input parameter count", fnType.String()) |
| 162 | + } |
| 163 | + |
| 164 | + if fnType.NumOut() > 2 { |
| 165 | + return fmt.Errorf("%s: invalid output parameter count", fnType.String()) |
| 166 | + } |
| 167 | + |
| 168 | + if !utils.IsContextType(fnType.In(0)) { |
| 169 | + return fmt.Errorf("%s: first input param type (%s) must be context", fnType.String(), fnType.In(0).String()) |
| 170 | + } |
| 171 | + |
| 172 | + if fnType.NumIn() > 1 { |
| 173 | + argType := fnType.In(1) |
| 174 | + if !(reflect.Struct == argType.Kind() || (reflect.Ptr == argType.Kind() && reflect.Struct == argType.Elem().Kind())) { |
| 175 | + return fmt.Errorf("%s: second input param type (%s) must be struct/*struct", fnType.String(), argType.String()) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if 0 < fnType.NumOut() && utils.IsErrorType(fnType.Out(0)) { |
| 180 | + return fmt.Errorf("%s: first output param type not be error", fnType.String()) |
| 181 | + } |
| 182 | + |
| 183 | + if 1 < fnType.NumOut() && !utils.IsErrorType(fnType.Out(1)) { |
| 184 | + return fmt.Errorf("%s: second output type (%s) must a error", fnType.String(), fnType.Out(1).String()) |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
0 commit comments