Refactor web #26

Merged
tardisx merged 8 commits from refactor-web into main 2023-11-20 07:38:17 +10:30
Showing only changes of commit ac286ec24e - Show all commits

View File

@ -32,18 +32,29 @@ var webFS embed.FS
func CreateRoutes(cs *config.ConfigService, dm *download.Manager, vm *version.Manager) *mux.Router {
r := mux.NewRouter()
// main index page
r.HandleFunc("/", homeHandler(cs, vm, dm))
// update info on the status page
r.HandleFunc("/rest/fetch", fetchInfoRESTHandler(dm))
// return static files
r.HandleFunc("/static/{filename}", staticHandler())
r.HandleFunc("/config", configHandler)
// return the config page
r.HandleFunc("/config", configHandler())
// handle config fetches/updates
r.HandleFunc("/rest/config", configRESTHandler(cs))
// create or present a download in the popup
r.HandleFunc("/fetch", fetchHandler(cs, vm, dm))
r.HandleFunc("/fetch/{id}", fetchHandler(cs, vm, dm))
// info for the list
r.HandleFunc("/rest/fetch", fetchInfoRESTHandler(dm))
// info for one, including update
// get/update info on a download
r.HandleFunc("/rest/fetch/{id}", fetchInfoOneRESTHandler(cs, dm))
// version information
r.HandleFunc("/rest/version", versionRESTHandler(vm))
r.HandleFunc("/rest/config", configRESTHandler(cs))
http.Handle("/", r)
return r
@ -124,7 +135,8 @@ func staticHandler() func(w http.ResponseWriter, r *http.Request) {
}
// configHandler returns the configuration page
func configHandler(w http.ResponseWriter, r *http.Request) {
func configHandler() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
t, err := template.ParseFS(webFS, "data/templates/layout.tmpl", "data/templates/menu.tmpl", "data/templates/config.tmpl")
@ -137,6 +149,7 @@ func configHandler(w http.ResponseWriter, r *http.Request) {
panic(err)
}
}
}
// configRESTHandler handles both reading and writing of the configuration
func configRESTHandler(cs *config.ConfigService) func(w http.ResponseWriter, r *http.Request) {