Compare commits
3
Commits
01c1f0389a
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e7341e3d9e | ||
|
|
8b73652c9b | ||
|
|
35ecaf84e0 |
No files matched your search
@@ -2,8 +2,11 @@ package proxmoxaas_common_lib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func HTTPExecuteHTMLTemplate(w http.ResponseWriter, t *template.Template, name string, d any) {
|
||||
@@ -31,3 +34,24 @@ func HTTPExecuteHTMLFragmentTemplate(w http.ResponseWriter, t *template.Template
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func LoggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
next.ServeHTTP(w, r)
|
||||
log.Printf("%s %s %s", r.Method, r.URL.Path, time.Since(start))
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user