Implement request body handling on abstraction

This commit is contained in:
Curt Spark 2025-05-06 01:49:13 +01:00
parent a615c0aed9
commit 989dd67030
1 changed files with 41 additions and 13 deletions

54
main.go
View File

@ -3,11 +3,13 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"reflect" "reflect"
"strings"
"syscall" "syscall"
"time" "time"
) )
@ -43,32 +45,58 @@ type http_status_code int; const (
BAD_REQUEST http_status_code = http.StatusBadRequest BAD_REQUEST http_status_code = http.StatusBadRequest
) )
func http_new_route[T any](method http_method, route string, handle_func func(request *http.Request) (T, http_status_code)) { type empty_request_body struct {}
/* T Representing any request body struct
S Representing any response body struct */
func http_new_route[T any, S any](method http_method, route string, handle_func func(body T, raw_request *http.Request) (S, http_status_code)) {
http.HandleFunc(method.to_string() + " /" + route, func(response http.ResponseWriter, request *http.Request) { http.HandleFunc(method.to_string() + " /" + route, func(response http.ResponseWriter, request *http.Request) {
body, status_code := handle_func(request) request_body_bytes, err := io.ReadAll(request.Body)
result, err := json.Marshal(body)
if err != nil { if err != nil {
log.Fatal(err) response.WriteHeader(int(INTERNAL_SERVER_ERROR))
log.Panicln(err)
return
}
var decode_result T
if len(request_body_bytes) != 0 {
decoder := json.NewDecoder(strings.NewReader(string(request_body_bytes)))
decoder.DisallowUnknownFields()
err = decoder.Decode(&decode_result)
if err != nil {
response.WriteHeader(int(BAD_REQUEST))
response.Write([]byte(err.Error()))
log.Println(err)
return
}
}
body, status_code := handle_func(decode_result, request)
encode_result, err := json.Marshal(body)
if err != nil {
response.WriteHeader(int(INTERNAL_SERVER_ERROR))
log.Panicln(err)
return
} }
fmt.Println(reflect.VisibleFields(reflect.TypeOf(body))) fmt.Println(reflect.VisibleFields(reflect.TypeOf(body)))
response.WriteHeader(int(status_code)) response.WriteHeader(int(status_code))
response.Write(result) response.Write(encode_result)
}) })
} }
func main() { func main() {
type foo_request_body struct {
Arg1 int
Arg2 int
}
type foo_response_body struct { type foo_response_body struct {
Field1 string Field1 string
Field2 http.Header Field2 int
} }
foo_route_handler := func(request *http.Request) (foo_response_body, http_status_code) { foo_route_handler := func(body foo_request_body, raw_request *http.Request) (foo_response_body, http_status_code) {
fmt.Println(request.Header) return foo_response_body {
return foo_response_body{
Field1: "hi there on foo!", Field1: "hi there on foo!",
Field2: request.Header, Field2: body.Arg1 + body.Arg2,
}, OK }, OK
} }
http_new_route(GET, "foo", foo_route_handler) http_new_route(GET, "foo", foo_route_handler)
@ -77,8 +105,8 @@ func main() {
Response int Response int
Message string Message string
} }
bar_route_handler := func(request *http.Request) (bar_response_body, http_status_code) { bar_route_handler := func(body empty_request_body, raw_request *http.Request) (bar_response_body, http_status_code) {
return bar_response_body{ return bar_response_body {
Response: 400, Response: 400,
Message: "hi there on bar!", Message: "hi there on bar!",
}, INTERNAL_SERVER_ERROR }, INTERNAL_SERVER_ERROR