-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
60 lines (46 loc) · 1.1 KB
/
service.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
package server
import (
"net/http"
"github.com/gin-gonic/gin"
servertiming "github.com/p768lwy3/gin-server-timing"
"github.com/simiancreative/simiango/service"
)
var kinds = map[service.Kind]func(
service.Config,
service.Req,
) (interface{}, *service.ResultError){
service.DEFAULT: handleDefault,
service.DIRECT: handleDirect,
}
func handleService(config service.Config) gin.HandlerFunc {
return func(c *gin.Context) {
timing := servertiming.FromContext(c)
timer := timing.NewMetric("total").Start()
req := parseRequest(c)
var result interface{}
var err *service.ResultError
handler, ok := kinds[config.Kind]
if !ok {
result, err = handleDefault(config, req)
}
if ok {
result, err = handler(config, req)
}
timer.Stop()
servertiming.WriteHeader(c)
go handleAfter(config, req)
if handleErrorResp(err, c) != nil {
c.JSON(err.GetStatus(), err.GetDetails())
return
}
if result == nil {
c.Writer.WriteHeader(http.StatusNoContent)
return
}
if config.IsStream {
handleStreamingServiceResult(result, c)
return
}
c.JSON(http.StatusOK, result)
}
}