mirror of
https://github.com/safing/portmaster
synced 2025-09-02 02:29:12 +00:00
Add support for redir-404-to-index type apps
This commit is contained in:
parent
0ac67a01a0
commit
a98673cd88
1 changed files with 16 additions and 7 deletions
23
ui/serve.go
23
ui/serve.go
|
@ -28,7 +28,7 @@ func registerRoutes() error {
|
||||||
api.RegisterHandleFunc("/ui/modules/{moduleName:[a-z]+}", redirAddSlash).Methods("GET", "HEAD")
|
api.RegisterHandleFunc("/ui/modules/{moduleName:[a-z]+}", redirAddSlash).Methods("GET", "HEAD")
|
||||||
api.RegisterHandleFunc("/ui/modules/{moduleName:[a-z]+}/", ServeBundle("")).Methods("GET", "HEAD")
|
api.RegisterHandleFunc("/ui/modules/{moduleName:[a-z]+}/", ServeBundle("")).Methods("GET", "HEAD")
|
||||||
api.RegisterHandleFunc("/ui/modules/{moduleName:[a-z]+}/{resPath:[a-zA-Z0-9/\\._-]+}", ServeBundle("")).Methods("GET", "HEAD")
|
api.RegisterHandleFunc("/ui/modules/{moduleName:[a-z]+}/{resPath:[a-zA-Z0-9/\\._-]+}", ServeBundle("")).Methods("GET", "HEAD")
|
||||||
api.RegisterHandleFunc("/", RedirectToBase)
|
api.RegisterHandleFunc("/", redirectToDefault)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -97,13 +97,21 @@ func ServeFileFromBundle(w http.ResponseWriter, r *http.Request, bundleName stri
|
||||||
readCloser, err := bundle.Open(path)
|
readCloser, err := bundle.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == resources.ErrNotFound {
|
if err == resources.ErrNotFound {
|
||||||
log.Tracef("ui: requested resource \"%s\" not found in bundle %s: %s", path, bundleName, err)
|
// Check if there is a base index.html file we can serve instead.
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
var indexErr error
|
||||||
|
path = "index.html"
|
||||||
|
readCloser, indexErr = bundle.Open(path)
|
||||||
|
if indexErr != nil {
|
||||||
|
// If we cannot get an index, continue with handling the original error.
|
||||||
|
log.Tracef("ui: requested resource \"%s\" not found in bundle %s: %s", path, bundleName, err)
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Tracef("ui: error opening module %s: %s", bundleName, err)
|
log.Tracef("ui: error opening module %s: %s", bundleName, err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// set content type
|
// set content type
|
||||||
|
@ -131,9 +139,9 @@ func ServeFileFromBundle(w http.ResponseWriter, r *http.Request, bundleName stri
|
||||||
readCloser.Close()
|
readCloser.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RedirectToBase redirects the requests to the control app
|
// redirectToDefault redirects the request to the default UI module.
|
||||||
func RedirectToBase(w http.ResponseWriter, r *http.Request) {
|
func redirectToDefault(w http.ResponseWriter, r *http.Request) {
|
||||||
u, err := url.Parse("/ui/modules/base/")
|
u, err := url.Parse("/ui/modules/portmaster/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -141,6 +149,7 @@ func RedirectToBase(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, r.URL.ResolveReference(u).String(), http.StatusTemporaryRedirect)
|
http.Redirect(w, r, r.URL.ResolveReference(u).String(), http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// redirAddSlash redirects the request to the same, but with a trailing slash.
|
||||||
func redirAddSlash(w http.ResponseWriter, r *http.Request) {
|
func redirAddSlash(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, r.RequestURI+"/", http.StatusPermanentRedirect)
|
http.Redirect(w, r, r.RequestURI+"/", http.StatusPermanentRedirect)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue