From 9f9a21342dd6591205275890e904022a64c24cc8 Mon Sep 17 00:00:00 2001 From: Pedro Correa Date: Mon, 21 Oct 2024 18:30:33 -0300 Subject: [PATCH] :sparkles: (Go) endpoint to list people qtt and hurl requests --- Go/webserver/request.hurl | 25 +++++++++++++++++++++++++ Go/webserver/src/http.go | 16 +++++++--------- 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 Go/webserver/request.hurl diff --git a/Go/webserver/request.hurl b/Go/webserver/request.hurl new file mode 100644 index 0000000..fb957b2 --- /dev/null +++ b/Go/webserver/request.hurl @@ -0,0 +1,25 @@ +POST http://localhost:8000/pessoas +Content-Type: application/json +{ + "name": "Ana Barbosa", + "nickname": "ana", + "birthdate": "1985-09-23T00:00:00Z", + "stack": [] +} +HTTP 200 +[Captures] +person_id: jsonpath "$.id" + + +GET http://localhost:8000/pessoas/{{person_id}} +HTTP 200 +[Asserts] +header "Content-Type" == "application/json" +jsonpath "$.name" == "Ana Barbosa" +jsonpath "$.nickname" == "ana" +jsonpath "$.birthdate" == "1985-09-23T00:00:00Z" +jsonpath "$.stack" count == 0 + +GET http://localhost:8000/contagem-pessoas +HTTP 200 +1 diff --git a/Go/webserver/src/http.go b/Go/webserver/src/http.go index 5a16740..98a366a 100644 --- a/Go/webserver/src/http.go +++ b/Go/webserver/src/http.go @@ -2,7 +2,9 @@ package src import ( "encoding/json" + "io" "net/http" + "strconv" ) var jsonContentType = "application/json" @@ -19,6 +21,7 @@ func NewHTTPServer(addr string) *http.Server { r := &http.ServeMux{} r.HandleFunc("GET /pessoas/{id}", server.handleGet) r.HandleFunc("POST /pessoas", server.handlePost) + r.HandleFunc("GET /contagem-pessoas", server.handleCount) return &http.Server{ Addr: addr, @@ -30,15 +33,6 @@ type IDDocument struct { Id string `json:"id"` } -func (h *httpServer) handlePeople(w http.ResponseWriter, req *http.Request) { - switch req.Method { - case http.MethodGet: - h.handleGet(w, req) - case http.MethodPost: - h.handlePost(w, req) - } -} - func (h *httpServer) handleGet(w http.ResponseWriter, req *http.Request) { id := req.PathValue("id") @@ -65,3 +59,7 @@ func (h *httpServer) handlePost(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Type", jsonContentType) json.NewEncoder(w).Encode(res) } + +func (h *httpServer) handleCount(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, strconv.Itoa(len(h.People.people))) +} -- 2.51.2