KnowledgeRefinery/daemon-go/internal/server/server.go

31 lines
732 B
Go
Raw Permalink Normal View History

package server
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
// CORSMiddleware allows all origins (local-only daemon).
func CORSMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
// NewRouter creates a chi router with standard middleware.
func NewRouter() *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Use(CORSMiddleware)
return r
}