CORS Middleware (middleware/cors.go)

mem9 · server/internal/middleware/cors.go · go · Apache-2.0
infra, security, integration, lang:go

Go HTTP middleware handling Cross-Origin Resource Sharing preflight requests. Use when configuring API routes to accept cross-origin browser requests.

// Source: https://github.com/mem9-ai/mem9 | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
package middleware

import (
	"net/http"

	"github.com/qiffang/mnemos/server/internal/reqid"
)

const corsMaxAge = "86400"

// CORS returns middleware for browser clients. It handles preflight before auth
// so API-key protected routes can be exercised from the public docs.
func CORS(allowedOrigins []string) func(http.Handler) http.Handler {
	allowed := make(map[string]struct{}, len(allowedOrigins))
	for _, origin := range allowedOrigins {
		if origin != "" {
			allowed[origin] = struct{}{}
		}
	}

	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseW

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key