-
Notifications
You must be signed in to change notification settings - Fork 399
/
main.go
119 lines (92 loc) · 2.95 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
117
118
119
package main
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/golang/glog"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/thangchung/go-coffeeshop/cmd/proxy/config"
mylog "github.com/thangchung/go-coffeeshop/pkg/logger"
gen "github.com/thangchung/go-coffeeshop/proto/gen"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func newGateway(
ctx context.Context,
cfg *config.Config,
opts []gwruntime.ServeMuxOption,
) (http.Handler, error) {
productEndpoint := fmt.Sprintf("%s:%d", cfg.ProductHost, cfg.ProductPort)
counterEndpoint := fmt.Sprintf("%s:%d", cfg.CounterHost, cfg.CounterPort)
mux := gwruntime.NewServeMux(opts...)
dialOpts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err := gen.RegisterProductServiceHandlerFromEndpoint(ctx, mux, productEndpoint, dialOpts)
if err != nil {
return nil, err
}
err = gen.RegisterCounterServiceHandlerFromEndpoint(ctx, mux, counterEndpoint, dialOpts)
if err != nil {
return nil, err
}
return mux, nil
}
func allowCORS(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
preflightHandler(w, r)
return
}
}
h.ServeHTTP(w, r)
})
}
func preflightHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
headers := []string{"*"}
w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
glog.Infof("preflight request for %s", r.URL.Path)
}
func withLogger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
glog.Infof("Run %s %s", r.Method, r.URL)
h.ServeHTTP(w, r)
})
}
func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
cfg, err := config.NewConfig()
if err != nil {
glog.Fatalf("Config error: %s", err)
}
logger := mylog.New(cfg.Log.Level)
logger.Info("Init %s %s\n", cfg.Name, cfg.Version)
mux := http.NewServeMux()
gw, err := newGateway(ctx, cfg, nil)
if err != nil {
logger.Fatal("%s", err)
}
mux.Handle("/", gw)
s := &http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
Handler: allowCORS(withLogger(mux)),
}
go func() {
<-ctx.Done()
glog.Infof("Shutting down the http server")
if err := s.Shutdown(context.Background()); err != nil {
glog.Errorf("Failed to shutdown http server: %v", err)
}
}()
glog.Infof("Starting listening at %s", fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
if err := s.ListenAndServe(); errors.Is(err, http.ErrServerClosed) {
glog.Errorf("Failed to listen and serve: %v", err)
}
}