-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (64 loc) · 1.91 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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/go-kit/kit/endpoint"
gkhttp "github.com/go-kit/kit/transport/http"
"log"
"net/http"
)
type TalkToMe interface {
HowsTheWeather(string) (int, string, error)
}
type weatherRequest struct {
ZipCode string `json:"zip_code"`
}
type weatherResponse struct {
Temperature int `json:"temperature"`
Description string `json:"description"`
}
type talkToMe struct{}
func (*talkToMe) HowsTheWeather(zipCode string) (int, string, error) {
if zipCode == "23059" {
return 90, "it's hot!", nil
} else {
return -1, "i don't know", fmt.Errorf("ZipCodeException")
}
}
//Endpoints
//simple adapters to convert each of our service’s methods into an endpoint
//type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)
func weatherEndpoint(svc TalkToMe) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(weatherRequest)
temp, description, err := svc.HowsTheWeather(req.ZipCode)
if err != nil {
return weatherResponse{temp, description}, err
}
//response in json format
return weatherResponse{temp, description}, nil
}
}
//type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)
func decodeWeatherRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request weatherRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}
func main() {
t := talkToMe{}
//Transports to expose your service to the outside world
weatherHandler := gkhttp.NewServer(
weatherEndpoint(&t),
decodeWeatherRequest,
encodeResponse,
)
http.Handle("/weather", weatherHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}