mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2025-01-27 11:47:37 +01:00
Parametrize route generation
This commit is contained in:
parent
221fe54545
commit
9766bddb97
56
routes.go
Normal file
56
routes.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ClusterCockpit/cc-backend/auth"
|
||||||
|
"github.com/ClusterCockpit/cc-backend/config"
|
||||||
|
"github.com/ClusterCockpit/cc-backend/graph"
|
||||||
|
"github.com/ClusterCockpit/cc-backend/templates"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InfoType map[string]interface{}
|
||||||
|
|
||||||
|
type Route struct {
|
||||||
|
Route string
|
||||||
|
Template string
|
||||||
|
Title string
|
||||||
|
Filter bool
|
||||||
|
Setup func(i InfoType, r *http.Request) InfoType
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupRoutes(router *mux.Router, resolver *graph.Resolver, routes []Route) {
|
||||||
|
for _, route := range routes {
|
||||||
|
router.HandleFunc(route.Route, func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
conf, err := config.GetUIConfig(r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
infos := map[string]interface{}{
|
||||||
|
"admin": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
if user := auth.GetUser(r.Context()); user != nil {
|
||||||
|
infos["username"] = user.Username
|
||||||
|
infos["admin"] = user.HasRole(auth.RoleAdmin)
|
||||||
|
} else {
|
||||||
|
infos["username"] = false
|
||||||
|
infos["admin"] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
infos = route.Setup(infos, r)
|
||||||
|
route.Title = strings.Replace(route.Title, "<ID>", infos["id"].(string), 1)
|
||||||
|
|
||||||
|
templates.Render(rw, r, route.Template, &templates.Page{
|
||||||
|
Title: route.Title,
|
||||||
|
Config: conf,
|
||||||
|
Infos: infos,
|
||||||
|
FilterPresets: buildFilterPresets(r.URL.Query()),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user