Files
vt/fx/routes.go
2026-01-22 17:39:04 +01:00

57 lines
1.2 KiB
Go

package fx
import (
"log/slog"
"net/http"
"os"
connectcors "connectrpc.com/cors"
"github.com/rs/cors"
)
type Handler interface {
Handler() http.Handler
Path() string
}
type route struct {
path string
handler http.Handler
}
func NewRoute(path string, handler http.Handler) Handler {
return &route{
path: path,
handler: handler,
}
}
func (r *route) Handler() http.Handler {
return r.handler
}
func (r *route) Path() string {
return r.path
}
// NewServeMux builds a ServeMux that will route requests
// to the given EchoHandler.
func NewServeMux(handlers []Handler, logger *slog.Logger) http.Handler {
mux := http.NewServeMux()
for _, h := range handlers {
logger.Debug("Registering route", "path", h.Path())
mux.Handle(h.Path(), h.Handler())
}
handler := cors.New(cors.Options{
AllowedOrigins: []string{"http://10.8.0.3:3001", "http://10.8.0.3:3001/", os.Getenv("FRONTEND_URI")}, // replace with your domain
AllowedMethods: connectcors.AllowedMethods(),
AllowedHeaders: append(connectcors.AllowedHeaders(), "authentication", "Authentication"),
ExposedHeaders: connectcors.ExposedHeaders(),
AllowCredentials: true,
}).Handler(mux)
return handler
}