134 lines
3.0 KiB
Go
134 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"reflect"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
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 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) {
|
|
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)
|
|
})
|
|
|
|
}
|
|
|
|
func main() {
|
|
type foo_request_body struct {
|
|
Arg1 int
|
|
Arg2 int
|
|
}
|
|
type foo_response_body struct {
|
|
Field1 string
|
|
Field2 int
|
|
}
|
|
foo_route_handler := func(body foo_request_body, raw_request *http.Request) (foo_response_body, http_status_code) {
|
|
return foo_response_body {
|
|
Field1: "hi there on foo!",
|
|
Field2: body.Arg1 + body.Arg2,
|
|
}, OK
|
|
}
|
|
http_new_route(GET, "foo", foo_route_handler)
|
|
|
|
type bar_response_body struct {
|
|
Response int
|
|
Message string
|
|
}
|
|
bar_route_handler := func(body empty_request_body, raw_request *http.Request) (bar_response_body, http_status_code) {
|
|
return bar_response_body {
|
|
Response: 400,
|
|
Message: "hi there on bar!",
|
|
}, INTERNAL_SERVER_ERROR
|
|
}
|
|
http_new_route(GET, "bar", bar_route_handler)
|
|
|
|
http_server := &http.Server{
|
|
Addr: ":8080",
|
|
Handler: nil,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
MaxHeaderBytes: 1 << 20,
|
|
}
|
|
go func() {
|
|
log.Fatal(http_server.ListenAndServe())
|
|
}()
|
|
fmt.Println("Server started! Listening in on ", http_server.Addr)
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
|
<-sigs
|
|
fmt.Println("Server stopped!")
|
|
os.Exit(1)
|
|
}
|