-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
118 lines (97 loc) · 2.7 KB
/
Copy pathmain.go
File metadata and controls
118 lines (97 loc) · 2.7 KB
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
117
118
// main.go
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
)
func main() {
var (
httpAddr = flag.String("http.addr", ":5000", "HTTP listen address")
)
flag.Parse()
logger := log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
var svc MyService
svc = myService{} // Implement your service
// Define endpoints
endpoints := MakeEndpoints(svc)
// Create router and HTTP handler
r := mux.NewRouter()
r.Methods("POST").Path("/whatsapp").Handler(httptransport.NewServer(
endpoints.MyEndpoint,
DecodeMyRequest,
EncodeResponse,
))
http.Handle("/", r)
errs := make(chan error, 2)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
go func() {
logger.Log("transport", "HTTP", "addr", *httpAddr)
errs <- http.ListenAndServe(*httpAddr, nil)
}()
logger.Log("exit", <-errs)
}
// MyService represents your service interface
type MyService interface {
MyEndpoint(context.Context, MyRequest) (MyResponse, error)
}
// myService is the implementation of MyService
type myService struct{}
func (s myService) MyEndpoint(ctx context.Context, req MyRequest) (MyResponse, error) {
// Implement your service logic here
return MyResponse{Message: "Hello, " + req.Name + "!"}, nil
}
// MyRequest represents the request data
type MyRequest struct {
Name string `json:"name"`
}
// MyResponse represents the response data
type MyResponse struct {
Message string `json:"message"`
}
// MakeEndpoints creates endpoints for your service
func MakeEndpoints(svc MyService) Endpoints {
return Endpoints{
MyEndpoint: MakeMyEndpoint(svc),
}
}
// Endpoints struct holds all Go-Kit endpoints
type Endpoints struct {
MyEndpoint endpoint.Endpoint
}
// MakeMyEndpoint creates a Go-Kit endpoint for MyEndpoint
func MakeMyEndpoint(svc MyService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(MyRequest)
response, err := svc.MyEndpoint(ctx, req)
if err != nil {
return nil, err
}
return response, nil
}
}
// DecodeMyRequest decodes the HTTP request into MyRequest struct
func DecodeMyRequest(_ context.Context, r *http.Request) (interface{}, error) {
var req MyRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, err
}
return req, nil
}
// EncodeResponse encodes the response as JSON
func EncodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}