-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
80 lines (74 loc) · 2.19 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright 2024 hopeio. All rights reserved.
* Licensed under the MIT License that can be found in the LICENSE file.
* @Created by jyb
*/
package pick
import (
"encoding/json"
"github.com/hopeio/context/httpctx"
"github.com/hopeio/utils/errors/errcode"
"github.com/hopeio/utils/log"
httpi "github.com/hopeio/utils/net/http"
"github.com/hopeio/utils/net/http/binding"
http_fs "github.com/hopeio/utils/net/http/fs"
"go.uber.org/zap"
"io"
"net/http"
"reflect"
)
var (
HttpContextType = reflect.TypeOf((*httpctx.Context)(nil))
)
func CommonHandler(w http.ResponseWriter, req *http.Request, handle *reflect.Value) {
handleTyp := handle.Type()
handleNumIn := handleTyp.NumIn()
if handleNumIn != 0 {
params := make([]reflect.Value, handleNumIn)
ctxi := httpctx.FromRequest(httpctx.RequestCtx{
Request: req,
Response: w,
})
defer ctxi.RootSpan().End()
for i := 0; i < handleNumIn; i++ {
if handleTyp.In(i).ConvertibleTo(HttpContextType) {
params[i] = reflect.ValueOf(ctxi)
} else {
params[i] = reflect.New(handleTyp.In(i).Elem())
binding.Bind(req, params[i].Interface())
}
}
result := handle.Call(params)
ResWriteReflect(Writer{w}, ctxi.TraceID(), result)
}
}
func ResWriteReflect(w httpi.ICommonResponseWriter, traceId string, result []reflect.Value) error {
if !result[1].IsNil() {
err := errcode.ErrHandle(result[1].Interface())
log.Errorw(err.Error(), zap.String(log.FieldTraceId, traceId))
w.Set(httpi.HeaderContentType, httpi.ContentTypeJsonUtf8)
return json.NewEncoder(w).Encode(err)
}
data := result[0].Interface()
if info, ok := data.(*http_fs.File); ok {
w.Set(httpi.HeaderContentType, httpi.ContentTypeOctetStream)
w.Set(httpi.HeaderContentDisposition, "attachment;filename="+info.Name)
defer info.File.Close()
_, err := io.Copy(w, info.File)
return err
}
if info, ok := data.(httpi.IHttpResponse); ok {
_, err := httpi.CommonResponseWrite(w, info)
return err
}
if info, ok := data.(httpi.IHttpResponseTo); ok {
if rw, ok := w.(http.ResponseWriter); ok {
_, err := info.Response(rw)
return err
}
}
w.Set(httpi.HeaderContentType, httpi.ContentTypeJsonUtf8)
return json.NewEncoder(w).Encode(httpi.ResAnyData{
Data: data,
})
}