extract HTTP logic to shared module
1 file changed, 7 insertions(+), 23 deletions(-)
changed files
M domain/web/mux.go → domain/web/mux.go
@@ -9,9 +9,9 @@ "slices" "strings" calendar "alin.ovh/homestead/domain/calendar/templates" - ihttp "alin.ovh/homestead/domain/web/middleware" "alin.ovh/homestead/domain/web/server" "alin.ovh/homestead/domain/web/templates" + ihttp "alin.ovh/homestead/shared/http" "github.com/kevinpollet/nego" )@@ -21,18 +21,13 @@ if r.URL.Query().Get("resource") == website.acctResource { w.Header().Add("Content-Type", "application/jrd+json") w.Header().Add("Access-Control-Allow-Origin", "*") if err := json.NewEncoder(w).Encode(website.me); err != nil { - return &ihttp.Error{ - Code: http.StatusInternalServerError, - Cause: err, - } + return ihttp.InternalServerError("Failed to encode webfinger response", err) } return nil } - return &ihttp.Error{ - Code: http.StatusNotFound, - } + return ihttp.NotFound("Resource not found") } func (website *Website) ErrorHandler(err *ihttp.Error, w http.ResponseWriter, r *http.Request) {@@ -63,18 +58,12 @@ file, err := website.reader.GetFile(urlPath) if err != nil { website.log.Warn("Error reading file", "error", err) - return &ihttp.Error{ - Message: "Error reading file", - Code: http.StatusInternalServerError, - } + return ihttp.InternalServerError("Error reading file", err) } if file == nil { website.counter.Count(r, "404") - return &ihttp.Error{ - Message: "File not found", - Code: http.StatusNotFound, - } + return ihttp.NotFound("File not found") } website.counter.Count(r, file.Title) w.Header().Add("ETag", file.Etag)@@ -133,16 +122,11 @@ case r.URL.Query().Has("go-get") && r.URL.Query().Get("go-get") == "1": return website.ServeHTTP(w, r) case slices.Contains(website.config.Domains, r.Host): path, _ := website.reader.CanonicalisePath(r.URL.Path) - ihttp.Redirect( - w, - r, - website.config.BaseURL.JoinPath(path), - http.StatusMovedPermanently, - ) + ihttp.PermanentRedirect(w, r, website.config.BaseURL.JoinPath(path)) case re.MatchString(r.Host): url := website.config.BaseURL.JoinPath() url.Host = re.ReplaceAllString(r.Host, replace) - ihttp.Redirect(w, r, url, http.StatusTemporaryRedirect) + ihttp.TemporaryRedirect(w, r, url) case true: http.NotFound(w, r) }