mirror of
https://github.com/saymrwulf/KnowledgeRefinery.git
synced 2026-07-19 19:00:11 +00:00
31 lines
732 B
Go
31 lines
732 B
Go
|
|
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
|
||
|
|
}
|