Skip to content

Commit 82f7f2e

Browse files
daemon: add CORS middleware
1 parent 6ab318a commit 82f7f2e

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

cmd/arduino-app-cli/daemon.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/arduino/arduino-app-cli/pkg/httprecover"
1313

1414
dockerClient "github.com/docker/docker/client"
15+
"github.com/jub0bs/cors"
1516
"github.com/spf13/cobra"
1617
)
1718

@@ -43,9 +44,36 @@ func httpHandler(ctx context.Context, dockerClient *dockerClient.Client, daemonP
4344
slog.Info("Starting HTTP server", slog.String("address", ":"+daemonPort))
4445
apiSrv := api.NewHTTPRouter(dockerClient, Version)
4546

47+
corsMiddlware, err := cors.NewMiddleware(
48+
cors.Config{
49+
Origins: []string{
50+
"wails://wails.localhost:34115",
51+
"http://localhost:*", "https://localhost:*",
52+
},
53+
Methods: []string{
54+
http.MethodGet,
55+
http.MethodPost,
56+
http.MethodPut,
57+
http.MethodOptions,
58+
http.MethodDelete,
59+
http.MethodPatch,
60+
},
61+
RequestHeaders: []string{
62+
"Accept",
63+
"Authorization",
64+
"Content-Type",
65+
},
66+
MaxAgeInSeconds: 86400,
67+
ResponseHeaders: []string{},
68+
},
69+
)
70+
if err != nil {
71+
panic(err)
72+
}
73+
4674
httpSrv := http.Server{
4775
Addr: ":" + daemonPort,
48-
Handler: httprecover.RecoverPanic(apiSrv),
76+
Handler: httprecover.RecoverPanic(corsMiddlware.Wrap(apiSrv)),
4977
ReadHeaderTimeout: 60 * time.Second,
5078
}
5179
go func() {

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ require (
2222
gopkg.in/yaml.v3 v3.0.1
2323
)
2424

25+
require github.com/jub0bs/cors v0.5.9 // indirect
26+
2527
require (
2628
dario.cat/mergo v1.0.0 // indirect
2729
fortio.org/safecast v1.0.0 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
284284
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
285285
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
286286
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
287+
github.com/jub0bs/cors v0.5.9 h1:DZcrSGvh8xiDT2OjWPCOw6JGNQsy8FhgWk8EZgEvbTM=
288+
github.com/jub0bs/cors v0.5.9/go.mod h1:nvEts2Hrlwj3bHxTAXX0oglPQ0V4P+Ymq1z33ks77Z8=
287289
github.com/juju/errors v1.0.0 h1:yiq7kjCLll1BiaRuNY53MGI0+EQ3rF6GB+wvboZDefM=
288290
github.com/juju/errors v1.0.0/go.mod h1:B5x9thDqx0wIMH3+aLIMP9HjItInYWObRovoCFM5Qe8=
289291
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
@@ -857,8 +859,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
857859
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
858860
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
859861
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
862+
mkuznets.com/go/tabwriter v0.1.2 h1:qU4i7b1O3aEc68qFX2FLrCpcRG9Htas85gOdZyZJFfs=
863+
mkuznets.com/go/tabwriter v0.1.2/go.mod h1:shVL+F79yfTqgB7IzYqNo0QOgxdRYrhHsOA4ANHo4j8=
860864
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
861865
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
862866
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
863-
mkuznets.com/go/tabwriter v0.1.2 h1:qU4i7b1O3aEc68qFX2FLrCpcRG9Htas85gOdZyZJFfs=
864-
mkuznets.com/go/tabwriter v0.1.2/go.mod h1:shVL+F79yfTqgB7IzYqNo0QOgxdRYrhHsOA4ANHo4j8=

0 commit comments

Comments
 (0)