Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions loopd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type config struct {
TLSPathSwapSrv string `long:"tlspathswapserver" description:"Path to swap server tls certificate. Only needed if the swap server uses a self-signed certificate."`
RPCListen string `long:"rpclisten" description:"Address to listen on for gRPC clients"`
RESTListen string `long:"restlisten" description:"Address to listen on for REST clients"`
CORSOrigin string `long:"corsorigin" description:"The value to send in the Access-Control-Allow-Origin header. Header will be omitted if empty."`

LogDir string `long:"logdir" description:"Directory to log output."`
MaxLogFiles int `long:"maxlogfiles" description:"Maximum logfiles to keep (0 for no rotation)"`
Expand Down
17 changes: 15 additions & 2 deletions loopd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func daemon(config *config, lisCfg *listenerCfg) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mux := proxy.NewServeMux(customMarshalerOption)
var restHandler http.Handler = mux
if config.CORSOrigin != "" {
restHandler = allowCORS(restHandler, config.CORSOrigin)
}
proxyOpts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithDefaultCallOptions(maxMsgRecvSize),
Expand All @@ -141,7 +145,7 @@ func daemon(config *config, lisCfg *listenerCfg) error {
log.Infof("Starting REST proxy listener")

defer restListener.Close()
proxy := &http.Server{Handler: mux}
proxy := &http.Server{Handler: restHandler}

go func() {
err := proxy.Serve(restListener)
Expand Down Expand Up @@ -214,7 +218,7 @@ func daemon(config *config, lisCfg *listenerCfg) error {
// Debug code to dump goroutines on hanging exit.
go func() {
time.Sleep(5 * time.Second)
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
_ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
}()

cancel()
Expand All @@ -225,3 +229,12 @@ func daemon(config *config, lisCfg *listenerCfg) error {

return nil
}

// allowCORS wraps the given http.Handler with a function that adds the
// Access-Control-Allow-Origin header to the response.
func allowCORS(handler http.Handler, origin string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", origin)
handler.ServeHTTP(w, r)
})
}