initial commit

This commit is contained in:
2025-12-07 11:32:45 +01:00
commit c3daed67ac
35 changed files with 1315 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
package http_utils
import (
"encoding/json"
"log"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
)
func RespondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
func GetParam(r *http.Request, name string) string {
return chi.URLParam(r, name)
}
func GetParamUUID(r *http.Request, name string) (uuid.UUID, error) {
return uuid.Parse(GetParam(r, name))
}
type StartGenericHTTPServerContext struct {
Router *chi.Mux
}
func StartGenericHTTPServer(ctx StartGenericHTTPServerContext) {
r := chi.NewRouter()
r.Use(LoggingMiddleware)
r.Mount("/", ctx.Router)
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":80", r))
}
+13
View File
@@ -0,0 +1,13 @@
package http_utils
import (
"fmt"
"net/http"
)
func LoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%s %s\n", r.Method, r.RequestURI)
next.ServeHTTP(w, r)
})
}