Skip to content

Commit 04b01f4

Browse files
committed
Add CORS origin
1 parent 6de35e9 commit 04b01f4

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM --platform=$BUILDPLATFORM golang:1.21.1-alpine3.18 as build
1+
FROM --platform=$BUILDPLATFORM golang:1.22-alpine as build
22
WORKDIR /src
33
ARG TARGETOS TARGETARCH
44
RUN --mount=target=. --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build -o /out/backend -ldflags "-s -w"
@@ -9,4 +9,4 @@ FROM scratch
99
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
1010
WORKDIR /app
1111
COPY --from=build /out .
12-
ENTRYPOINT ["./backend", "-bind=:5001", "-callback-addr=http://backend:5001", "-moneropay=http://moneropay:5000", "-postgres=postgresql://postgres:sup3rs3cure@postgresql:5432/metronero?sslmode=disable", "-token-secret=xufeg6uoth4oM7CohK2D", "-callback-url=http://backend:5001/callback"]
12+
ENTRYPOINT ["./backend", "-bind=:5001", "-callback-addr=http://backend:5001", "-moneropay=http://moneropay:5000", "-postgres=postgresql://postgres:sup3rs3cure@postgresql:5432/metronero?sslmode=disable", "-callback-url=http://backend:5001/callback", "-cors-origin=http://*"]

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ toolchain go1.22.6
88

99
require (
1010
github.com/go-chi/chi/v5 v5.1.0
11+
github.com/go-chi/cors v1.2.1
1112
github.com/go-chi/jwtauth/v5 v5.3.1
1213
github.com/golang-migrate/migrate/v4 v4.18.1
1314
github.com/google/uuid v1.6.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2
3131
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
3232
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
3333
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
34+
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
35+
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
3436
github.com/go-chi/jwtauth/v5 v5.3.1 h1:1ePWrjVctvp1tyBq5b/2ER8Th/+RbYc7x4qNsc5rh5A=
3537
github.com/go-chi/jwtauth/v5 v5.3.1/go.mod h1:6Fl2RRmWXs3tJYE1IQGX81FsPoGqDwq9c15j52R5q80=
3638
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=

internal/utils/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
MoneroPay string
2222
TemplateDir string
2323
TemplateMaxSize int
24+
CorsOrigin string
2425
DefaultPaymentTemplate *template.Template
2526
)
2627

@@ -36,6 +37,7 @@ func Load() {
3637
var logFormat string
3738
flag.StringVar(&logFormat, "log-format", "pretty", "Log format (pretty or json)")
3839
flag.StringVar(&TemplateDir, "template-dir", "./data/merchant_templates", "Directory to save merchant templates.")
40+
flag.StringVar(&CorsOrigin, "cors-origin", "http://*", "Allowed CORS origin")
3941
flag.IntVar(&TemplateMaxSize, "template-max-size", 20, "Maximum template upload size in MiB")
4042
flag.Parse()
4143

internal/utils/server/router.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"gitea.com/go-chi/session"
55
"github.com/go-chi/chi/v5"
66
"github.com/go-chi/chi/v5/middleware"
7+
"github.com/go-chi/cors"
78

89
"gitlab.com/metronero/backend/internal/app/controllers"
10+
"gitlab.com/metronero/backend/internal/utils/config"
911
)
1012

1113
func registerRoutes() *chi.Mux {
@@ -20,6 +22,12 @@ func registerRoutes() *chi.Mux {
2022
CookieName: "MNSession",
2123
}))
2224

25+
r.Use(cors.Handler(cors.Options{
26+
AllowedOrigins: []string{config.CorsOrigin},
27+
AllowedMethods: []string{"POST", "GET", "PUT", "DELETE"},
28+
AllowCredentials: true,
29+
}))
30+
2331
r.Post("/login", controllers.PostLogin)
2432

2533
r.Group(func(r chi.Router) {

0 commit comments

Comments
 (0)