Skip to content

Commit 0006bfa

Browse files
committed
feat: add cors
1 parent 6a545cd commit 0006bfa

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ module github.com/hammer-code/moonlight
33
go 1.19
44

55
require (
6+
github.com/felixge/httpsnoop v1.0.3 // indirect
67
github.com/fsnotify/fsnotify v1.6.0 // indirect
8+
github.com/gorilla/handlers v1.5.2 // indirect
79
github.com/gorilla/mux v1.8.0 // indirect
810
github.com/hashicorp/hcl v1.0.0 // indirect
911
github.com/lib/pq v1.10.7 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
5454
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
5555
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
5656
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
57+
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
58+
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
5759
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
5860
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
5961
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@@ -113,6 +115,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
113115
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
114116
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
115117
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
118+
github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE=
119+
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
116120
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
117121
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
118122
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"github.com/hammer-code/moonlight/app/route"
1818
"github.com/hammer-code/moonlight/config"
1919
"github.com/hammer-code/moonlight/pkg/logging"
20+
21+
muxHandlers "github.com/gorilla/handlers"
2022
)
2123

2224
func init() {
@@ -52,9 +54,20 @@ func main() {
5254

5355
handler := route.NewRoute(ctrl)
5456

57+
origins := []string{"*"}
58+
methods := []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}
59+
headers := []string{"Accept", "Authorization", "Content-Type"}
60+
61+
muxCorsWithRouter := muxHandlers.CORS(
62+
muxHandlers.AllowCredentials(),
63+
muxHandlers.AllowedHeaders(headers),
64+
muxHandlers.AllowedMethods(methods),
65+
muxHandlers.AllowedOrigins(origins),
66+
)(handler)
67+
5568
server := &http.Server{
5669
Addr: fmt.Sprintf("%s:%s", cfg.API.Host, strconv.Itoa(cfg.API.Port)),
57-
Handler: handler,
70+
Handler: muxCorsWithRouter,
5871
}
5972
cls := make(chan struct{})
6073

0 commit comments

Comments
 (0)