Backend/tubson/tubson.go

81 lines
1.8 KiB
Go

package tubson
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"reflect"
"strings"
)
type Http_method uint8; const (
GET Http_method = iota
POST
PATCH
PUT
DELETE
)
func (method Http_method) to_string() string {
switch method {
case GET:
return http.MethodGet
case POST:
return http.MethodPost
case PATCH:
return http.MethodPatch
case PUT:
return http.MethodPut
case DELETE:
return http.MethodDelete
}
return ""
}
type Http_status_code int; const (
OK Http_status_code = http.StatusOK
INTERNAL_SERVER_ERROR Http_status_code = http.StatusInternalServerError
BAD_REQUEST Http_status_code = http.StatusBadRequest
)
type Empty_request_body struct {}
/* T Representing any request body struct
S Representing any response body struct */
func 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) {
request_body_bytes, err := io.ReadAll(request.Body)
if err != nil {
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)))
response.WriteHeader(int(status_code))
response.Write(encode_result)
})
}