Skip to content

Implement CORs#13

Merged
markmnl merged 3 commits intomainfrom
cors
Apr 27, 2026
Merged

Implement CORs#13
markmnl merged 3 commits intomainfrom
cors

Conversation

@markmnl
Copy link
Copy Markdown
Owner

@markmnl markmnl commented Apr 27, 2026

No description provided.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-party CORS support to the Gin-based API so browser clients can call fmsg-webapi (including successful preflight handling) while keeping existing server-to-server behavior unchanged.

Changes:

  • Introduces a configurable CORS middleware (CORSConfig, DefaultCORSConfig, NewCORS) with preflight handling.
  • Wires CORS into main.go via FMSG_CORS_ORIGINS, ensuring it runs before JWT auth middleware.
  • Adds middleware unit tests and updates README configuration/docs (including TLS/port behavior).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/middleware/cors.go New CORS middleware implementation and configuration defaults
src/middleware/cors_test.go Test coverage for allowed/disallowed origins, wildcard, and preflight behavior
src/main.go Enables CORS via env var and ensures middleware ordering; makes TLS port configurable via FMSG_API_PORT
README.md Documents FMSG_CORS_ORIGINS and clarifies TLS/port guidance

Comment thread src/middleware/cors.go
Comment on lines +94 to +98
if c.Request.Method == http.MethodOptions {
// Preflight.
c.Writer.Header().Add("Vary", "Access-Control-Request-Method")
c.Writer.Header().Add("Vary", "Access-Control-Request-Headers")
if methods != "" {
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewCORS currently short-circuits all OPTIONS requests from an allowed Origin with a 204 response. That treats non-preflight OPTIONS calls (no Access-Control-Request-Method header) as preflight and prevents legitimate OPTIONS handlers from running. Consider only taking the preflight path when Access-Control-Request-Method is present (and otherwise c.Next()).

Copilot uses AI. Check for mistakes.
Comment thread src/middleware/cors.go Outdated
Comment thread README.md
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@markmnl markmnl merged commit f76e07d into main Apr 27, 2026
1 check passed
@markmnl markmnl deleted the cors branch April 27, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants