|
|
@@ -6,10 +6,11 @@ import (
|
|
|
"context"
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
|
|
"net/http"
|
|
|
- "strings"
|
|
|
|
|
|
gw "github.com/jamesread/OliveTin/gen/grpc"
|
|
|
|
|
|
+ cors "github.com/jamesread/OliveTin/pkg/cors"
|
|
|
+
|
|
|
config "github.com/jamesread/OliveTin/pkg/config"
|
|
|
)
|
|
|
|
|
|
@@ -25,7 +26,8 @@ func Start(listenAddressRest string, listenAddressGrpc string, globalConfig *con
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
defer cancel()
|
|
|
|
|
|
- mux := runtime.NewServeMux()
|
|
|
+ // The JSONPb.EmitDefaults is necssary, so "empty" fields are returned in JSON.
|
|
|
+ mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
|
|
|
opts := []grpc.DialOption{grpc.WithInsecure()}
|
|
|
|
|
|
err := gw.RegisterOliveTinApiHandlerFromEndpoint(ctx, mux, listenAddressGrpc, opts)
|
|
|
@@ -34,27 +36,5 @@ func Start(listenAddressRest string, listenAddressGrpc string, globalConfig *con
|
|
|
log.Fatalf("gw error %v", err)
|
|
|
}
|
|
|
|
|
|
- return http.ListenAndServe(listenAddressRest, allowCors(mux))
|
|
|
-}
|
|
|
-
|
|
|
-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) {
|
|
|
- headers := []string{"Content-Type", "Accept"}
|
|
|
- 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, ","))
|
|
|
- log.Infof("preflight request for %s", r.URL.Path)
|
|
|
- return
|
|
|
+ return http.ListenAndServe(listenAddressRest, cors.AllowCors(mux))
|
|
|
}
|