This repository has been archived by the owner on Mar 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhambone.go
145 lines (132 loc) · 3.16 KB
/
hambone.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
v1 "github.com/outof-coffee/api-outof-coffee/api/hambone/v1"
"google.golang.org/grpc"
)
const (
apiVersion = "v1"
)
type hamboneJSONData struct {
Name string `json:"name,omitempty"`
Img string `json:"img,omitempty"`
Position string `json:"position,omitempty"`
}
// HamboneServiceServerV1 implement the gRPC bridged JSON service for hambones
type HamboneServiceServerV1 struct {
data []hamboneJSONData
wg sync.WaitGroup
}
func New(data io.Reader) *HamboneServiceServerV1 {
var jsonData []hamboneJSONData
jsonData = make([]hamboneJSONData, 0)
jsonBytes, err := ioutil.ReadAll(data)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = json.Unmarshal(jsonBytes, &jsonData)
if err != nil {
panic(err)
}
fmt.Println(jsonData)
return &HamboneServiceServerV1{data: jsonData}
}
func (h *HamboneServiceServerV1) checkAPI(api string) error {
var err error
if len(api) > 0 {
if apiVersion != api {
return fmt.Errorf("incompatible api version; service implements %s, requested %s", apiVersion, api)
}
}
return err // assume v1 for now
}
func (h HamboneServiceServerV1) GetHambones(ctx context.Context, req *v1.GetRequest) (*v1.GetResponse, error) {
var err error
if err = h.checkAPI(req.Api); err != nil {
return nil, err
}
hambones := make([]*v1.Hambone, len(h.data))
for i, hambone := range h.data {
fmt.Println("processing hambone: ", hambone)
pHambone := v1.Hambone{
Name: hambone.Name,
Img: hambone.Img,
}
hambones[i] = &pHambone // preserve range order; TODO: preserve data order, too
}
return &v1.GetResponse{
Api: apiVersion,
Hambones: hambones,
}, err
}
func (h *HamboneServiceServerV1) Start() {
backChannel := make(chan struct{})
h.wg.Add(1)
go func() {
err := h.startGRPC(backChannel)
if err != nil {
fmt.Println(err)
}
h.wg.Done()
}()
h.wg.Add(1)
time.Sleep(2 * time.Second)
go func() {
err := h.startREST(backChannel)
if err != nil {
fmt.Println(err)
}
h.wg.Done()
}()
}
func (h *HamboneServiceServerV1) startGRPC(backChannel chan<- struct{}) error {
fmt.Println("starting gRPC host...")
listen, err := net.Listen("tcp", "localhost:8070")
if err != nil {
return err
}
grpcServer := grpc.NewServer()
v1.RegisterHamboneServiceServer(grpcServer, h)
backChannel <- struct{}{}
err = grpcServer.Serve(listen)
return err
}
func (h *HamboneServiceServerV1) startREST(backChannel <-chan struct{}) error {
fmt.Println("waiting for gRPC host...")
_ = <-backChannel
fmt.Println("starting REST host...")
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := v1.RegisterHamboneServiceHandlerFromEndpoint(ctx, mux, "localhost:8070", opts)
if err != nil {
return err
}
return http.ListenAndServe(":8080", mux)
}
func main() {
var data io.Reader
dataFile, err := os.Open("data.json")
defer dataFile.Close()
data = dataFile
if err != nil {
fmt.Println(err)
os.Exit(1)
}
h := New(data)
h.Start()
h.wg.Wait()
}