mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
Merge pull request #148 from guggero/cors-header
loopd: add option to configure CORS origin
This commit is contained in:
commit
ba7a018a5c
2 changed files with 16 additions and 2 deletions
|
|
@ -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)"`
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
@ -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)
|
||||
|
|
@ -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()
|
||||
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue