Commit: Bulk unfinished work
This commit is contained in:
56
fx/routes.go
Normal file
56
fx/routes.go
Normal file
@@ -0,0 +1,56 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user