-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
70 lines (55 loc) · 1.59 KB
/
server.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
package server
import (
"context"
"fmt"
dsp "demo/proto/demo_service_proto"
"github.com/valyala/fasthttp"
"google.golang.org/protobuf/proto"
"logger"
)
const (
http_response_content_type = "application/octet-stream"
)
// DemoService
type DemoService struct {
// Service state variables
}
// create a new DemoService
func New(ctx context.Context) (*DemoService, error) {
return &DemoService{}, nil
}
// GetFeature
func (*DemoService) GetFeature(ctx context.Context, req *dsp.DemoRequest) (*dsp.DemoResponse, error) {
var sum int64 = 0
logger.Infof("GetFeature called: %v", req.String())
for _, v := range req.GetKeys() {
sum += int64(v)
}
return &dsp.DemoResponse{
Answer: sum,
}, nil
}
func (demo *DemoService) GetFeatureHTTP(httpctx *fasthttp.RequestCtx) {
req := &dsp.DemoRequest{}
if err := proto.Unmarshal(httpctx.PostBody(), req); err != nil {
handleError(httpctx, fasthttp.StatusBadRequest, "bad request: error=%v", err)
return
}
resp, err := demo.GetFeature(context.Background(), req)
if err != nil {
handleError(httpctx, fasthttp.StatusInternalServerError, "failed to process request: error=%v", err)
}
payload, _ := proto.Marshal(resp)
httpctx.SetStatusCode(fasthttp.StatusOK)
httpctx.SetContentType(http_response_content_type)
httpctx.Write(payload)
}
func (*DemoService) HealthHTTP(httpctx *fasthttp.RequestCtx) {
httpctx.SetStatusCode(fasthttp.StatusOK)
httpctx.SetBodyString("OK")
}
func handleError(httpctx *fasthttp.RequestCtx, code int, format string, args ...interface{}) {
msg := fmt.Sprintf(format, args)
httpctx.Error(msg, code)
logger.Errorf(format, msg)
}