-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (101 loc) · 3.36 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-kit/kit/endpoint"
"github.com/short-loop/shortloop-go/shortloopgin"
"net/http"
)
func TestEndpoint(ctx context.Context, request interface{}) (interface{}, error) {
obj := map[string]string{
"message": "test",
}
return obj, nil
}
type DecodeRequestFunc func(context.Context, *gin.Context) (interface{}, error)
type EncodeResponseFunc func(context.Context, *gin.Context, interface{}) error
func DecodeRequest(_ context.Context, g *gin.Context) (interface{}, error) {
return g.Request.Body, nil
}
func EncodeJSONResponse(_ context.Context, c *gin.Context, response interface{}) error {
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
code := http.StatusOK
c.Writer.WriteHeader(code)
return json.NewEncoder(c.Writer).Encode(response)
}
func NewHTTPHandler(ep endpoint.Endpoint, dec DecodeRequestFunc, enc EncodeResponseFunc) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
req, err := dec(ctx, c)
if err != nil {
fmt.Println("Error decoding request: ", err)
return
}
resp, err := ep(ctx, req)
if err != nil {
fmt.Println("Error calling endpoint: ", err)
return
}
// ctx not available here
if err := enc(ctx, c, resp); err != nil {
fmt.Println("Error encoding response: ", err)
return
}
}
}
func main() {
router := gin.Default()
shortloopSdk, err := shortloopgin.Init(shortloopgin.Options{
ShortloopEndpoint: "http://localhost:8080", // the shortloop url for your org. (Provided by ShortLoop team.)
ApplicationName: "go-kit-9", // your application name here.
Environment: "stage", // for e.g stage or prod
LoggingEnabled: true, // enable logging
LogLevel: "INFO",
// no auth key added since its critical
})
if err != nil {
fmt.Println("Error initializing shortloopgin: ", err)
} else {
router.Use(shortloopSdk.Filter())
}
v1 := router.Group("/v1")
v1.GET("/test", NewHTTPHandler(TestEndpoint, DecodeRequest, EncodeJSONResponse))
v1.GET("/hello2", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "hello2"})
})
router.Run(":8300")
}
//helloEndpoint := func(c context.Context, request interface{}) (interface{}, error) {
//
// obj := map[string]string{
// "message": "hello",
// }
// return obj, nil
//}
// Create the HTTP transport handler for the endpoint.
//helloHandler := httptransport.NewServer(
// makeEndpoint(helloEndpoint),
// decodeRequest,
// encodeResponse,
//)
//router.GET("/hello", func(c *gin.Context) {
// helloHandler.ServeHTTP(c.Writer, c.Request)
//})
// makeEndpoint creates an endpoint from a function that takes a context and a request,
// and returns a response and an error.
//func makeEndpoint(fn endpoint.Endpoint) endpoint.Endpoint {
// return func(ctx context.Context, request interface{}) (interface{}, error) {
// return fn(ctx, request)
// }
//}
// decodeRequest decodes an HTTP request into a request object.
//func decodeRequest(_ context.Context, r *http.Request) (interface{}, error) {
// return r, nil
//}
// encodeResponse encodes a response object into an HTTP response.
//func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
// w.Header().Set("Content-Type", "application/json; charset=utf-8")
// return json.NewEncoder(w).Encode(response)
//}