Made renderX provide content types to not make forgejo complain

Signed-off-by: Arzumify <jliwin98@danwin1210.de>
This commit is contained in:
Tracker-Friendly 2024-10-14 13:10:51 +01:00
parent f4ee272865
commit 1d445cb61b
1 changed files with 9 additions and 0 deletions

View File

@ -103,6 +103,13 @@ func renderTemplate(statusCode int, w http.ResponseWriter, data map[string]inter
renderString(500, w, "Sorry, something went wrong on our end. Error code: 01. Please report to the administrator.", information) renderString(500, w, "Sorry, something went wrong on our end. Error code: 01. Please report to the administrator.", information)
} else { } else {
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
if strings.HasSuffix(templatePath, ".html") {
w.Header().Set("Content-Type", "text/html")
} else if strings.HasSuffix(templatePath, ".json") {
w.Header().Set("Content-Type", "application/json")
} else {
w.Header().Set("Content-Type", "text/plain")
}
err = requestedTemplate.Execute(w, data) err = requestedTemplate.Execute(w, data)
if err != nil { if err != nil {
logFunc(err.Error(), 2, information) logFunc(err.Error(), 2, information)
@ -112,6 +119,7 @@ func renderTemplate(statusCode int, w http.ResponseWriter, data map[string]inter
} }
func renderString(statusCode int, w http.ResponseWriter, data string, information library.ServiceInitializationInformation) { func renderString(statusCode int, w http.ResponseWriter, data string, information library.ServiceInitializationInformation) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
_, err := w.Write([]byte(data)) _, err := w.Write([]byte(data))
if err != nil { if err != nil {
@ -120,6 +128,7 @@ func renderString(statusCode int, w http.ResponseWriter, data string, informatio
} }
func renderJSON(statusCode int, w http.ResponseWriter, data map[string]interface{}, information library.ServiceInitializationInformation) { func renderJSON(statusCode int, w http.ResponseWriter, data map[string]interface{}, information library.ServiceInitializationInformation) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
err := json.NewEncoder(w).Encode(data) err := json.NewEncoder(w).Encode(data)
if err != nil { if err != nil {