Compare commits
2
Commits
8284ffbc43
...
35ecaf84e0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35ecaf84e0 | ||
|
|
01c1f0389a |
@@ -0,0 +1,47 @@
|
|||||||
|
package proxmoxaas_common_lib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HTTPExecuteHTMLTemplate(w http.ResponseWriter, t *template.Template, name string, d any) {
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
body := bytes.Buffer{}
|
||||||
|
err := t.ExecuteTemplate(&body, name, d)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
w.Write(body.Bytes())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HTTPExecuteHTMLFragmentTemplate(w http.ResponseWriter, t *template.Template, name string, d any) {
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
body := bytes.Buffer{}
|
||||||
|
err := t.ExecuteTemplate(&body, name, d)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
w.Write(body.Bytes())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HTTPWriteJson(w http.ResponseWriter, code int, obj any) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(code)
|
||||||
|
w.Write(json)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user