feat: whole bunch of shit
This commit is contained in:
20
server/internal/httpx/error.go
Normal file
20
server/internal/httpx/error.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func WriteJSON(w http.ResponseWriter, code int, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
}
|
||||
|
||||
func WriteError(w http.ResponseWriter, code int, err error) {
|
||||
WriteJSON(w, code, ErrorResponse{Error: err.Error()})
|
||||
}
|
11
server/internal/httpx/router.go
Normal file
11
server/internal/httpx/router.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package httpx
|
||||
|
||||
import "net/http"
|
||||
|
||||
type Router struct{ mux *http.ServeMux }
|
||||
|
||||
func NewRouter() *Router { return &Router{mux: http.NewServeMux()} }
|
||||
func (r *Router) Handle(pattern string, h http.Handler) { r.mux.Handle(pattern, h) }
|
||||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
r.mux.ServeHTTP(w, req)
|
||||
}
|
Reference in New Issue
Block a user