Skip to content
Closed
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
3 changes: 1 addition & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ ARG DB_URL
ARG PORT
ARG APP_ENV
ARG CLERK_SECRET_KEY
ARG CLIENT_URL

COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /server ./cmd/server/

FROM alpine:latest
ENV CLIENT_URL=https://scintillating-commitment-production-2429.up.railway.app
COPY --from=builder /server /server
EXPOSE 8080
CMD ["/server"]
5 changes: 3 additions & 2 deletions backend/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ func main() {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "OK")
})
url:=requiredEnv("CLIENT_URL")
check(http.ListenAndServe(httpAddress(), corsmiddleware.CORS(mux,url)))
//url:=requiredEnv("CLIENT_URL")
check(http.ListenAndServe(httpAddress(), corsmiddleware.CORS(mux)))


}
func check(err error) {
Expand Down
54 changes: 45 additions & 9 deletions backend/internal/middleware/cors/cors.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package middleware

import
("net/http"

import (
"net/http"
"os"
"strings"
//"fmt"

)

func CORS(next http.Handler,url string) http.Handler {

func CORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Access-Control-Allow-Origin", url)
origin := r.Header.Get("Origin")

w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Origin", origin)

w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")

w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS")

// preflight
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
Expand All @@ -26,3 +27,38 @@ func CORS(next http.Handler,url string) http.Handler {
next.ServeHTTP(w, r)
})
}

func allowedOriginsFromEnv() map[string]struct{} {
origins := map[string]struct{}{}

for _, envKey := range []string{"CLIENT_URL", "CORS_ALLOWED_ORIGINS"} {
raw := strings.TrimSpace(os.Getenv(envKey))
if raw == "" {
continue
}

for _, candidate := range strings.Split(raw, ",") {
origin := strings.TrimSpace(strings.TrimSuffix(candidate, "/"))
if origin == "" {
continue
}
origins[origin] = struct{}{}
}
}

if len(origins) == 0 {
origins["http://localhost:5173"] = struct{}{}
}

return origins
}

func isAllowedOrigin(origin string, allowed map[string]struct{}) bool {
if origin == "" {
return false
}

normalized := strings.TrimSuffix(strings.TrimSpace(origin), "/")
_, ok := allowed[normalized]
return ok
}
Loading