48 lines
890 B
Go
48 lines
890 B
Go
package joist
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type Router struct {
|
|
chi.Mux
|
|
cache RouteCacher
|
|
}
|
|
|
|
type RouteInfo struct {
|
|
Name string
|
|
Path string
|
|
Description string
|
|
Title string
|
|
}
|
|
|
|
type routeInfoKey struct{}
|
|
|
|
func NewRouter() Router {
|
|
cache, err := NewRouteCache(WithTTL(8 * time.Hour))
|
|
if err != nil {
|
|
log.Fatal("failed to create route cache")
|
|
}
|
|
|
|
return Router{cache: cache}
|
|
}
|
|
|
|
func WithRouteInfo(ri RouteInfo) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx := context.WithValue(r.Context(), routeInfoKey{}, ri)
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
}
|
|
|
|
func GetRouteInfo(r *http.Request) (RouteInfo, bool) {
|
|
info, ok := r.Context().Value(routeInfoKey{}).(RouteInfo)
|
|
return info, ok
|
|
}
|