gontrol/src/server/webapp/router.go

46 lines
1.1 KiB
Go

package webapp
import (
"embed"
"io/fs"
"html/template"
"net/http"
)
//go:embed templates/*.html templates/partials/*.html static/*
var assets embed.FS
// App is defined in handlers.go and called in main.go
func BuildRouter(app *App) *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/", app.getHomePage)
mux.HandleFunc("/agents", app.agentsHandler)
mux.HandleFunc("/agents/{agentId}", app.agentsHandler)
mux.HandleFunc("/agentNames", app.getAgentNames)
mux.HandleFunc("/agentIds", app.getAgentIds)
mux.HandleFunc("/logs", app.logsHandler)
mux.HandleFunc("/logs/{level}", app.logsHandler)
mux.HandleFunc("/proxyAgent", app.proxyAgentHandler)
mux.HandleFunc("/proxyAgent/", app.proxyAgentHandler)
staticSub, _ := fs.Sub(assets, "static")
mux.Handle("/static/",
http.StripPrefix("/static/",
http.FileServer(http.FS(staticSub)),
),
)
return mux
}
// Parse all templates from the embedded FS
// Called in main() and passed into &App{Tmp: tmpl)
func ParseTemplates() (*template.Template, error) {
return template.ParseFS(
assets,
"templates/*.html",
"templates/partials/*.html",
)
}