Implement methods and status code into abstraction
This commit is contained in:
parent
bbd15460a3
commit
a615c0aed9
57
main.go
57
main.go
|
|
@ -7,19 +7,55 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"reflect"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func http_new_route[T any](route string, handle_func func(request *http.Request) (T)) {
|
type http_method uint8; const (
|
||||||
http.HandleFunc("/" + route, func(w http.ResponseWriter, r *http.Request) {
|
GET http_method = iota
|
||||||
result, err := json.Marshal(handle_func(r))
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
func http_new_route[T any](method http_method, route string, handle_func func(request *http.Request) (T, http_status_code)) {
|
||||||
|
http.HandleFunc(method.to_string() + " /" + route, func(response http.ResponseWriter, request *http.Request) {
|
||||||
|
body, status_code := handle_func(request)
|
||||||
|
result, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
fmt.Println(reflect.VisibleFields(reflect.TypeOf(body)))
|
||||||
|
|
||||||
w.Write(result)
|
response.WriteHeader(int(status_code))
|
||||||
|
response.Write(result)
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -27,28 +63,27 @@ func main() {
|
||||||
Field1 string
|
Field1 string
|
||||||
Field2 http.Header
|
Field2 http.Header
|
||||||
}
|
}
|
||||||
foo_route_handler := func(request *http.Request) foo_response_body {
|
foo_route_handler := func(request *http.Request) (foo_response_body, http_status_code) {
|
||||||
|
|
||||||
fmt.Println(request.Header)
|
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: request.Header,
|
||||||
}
|
}, OK
|
||||||
}
|
}
|
||||||
http_new_route("foo", foo_route_handler)
|
http_new_route(GET, "foo", foo_route_handler)
|
||||||
|
|
||||||
type bar_response_body struct {
|
type bar_response_body struct {
|
||||||
Response int
|
Response int
|
||||||
Message string
|
Message string
|
||||||
}
|
}
|
||||||
bar_route_handler := func(request *http.Request) bar_response_body {
|
bar_route_handler := func(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
|
||||||
}
|
}
|
||||||
http_new_route("bar", bar_route_handler)
|
http_new_route(GET, "bar", bar_route_handler)
|
||||||
|
|
||||||
http_server := &http.Server{
|
http_server := &http.Server{
|
||||||
Addr: ":8080",
|
Addr: ":8080",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue