diff --git a/LetsGo/internal/server/http.go b/LetsGo/internal/server/http.go index 3a480fc..c180a2c 100644 --- a/LetsGo/internal/server/http.go +++ b/LetsGo/internal/server/http.go @@ -4,6 +4,7 @@ package server import ( "encoding/json" "net/http" + "strconv" "github.com/gorilla/mux" ) @@ -12,7 +13,7 @@ func NewHTTPServer(addr string) *http.Server { httpsrv := newHTTPServer() r := mux.NewRouter() r.HandleFunc("/", httpsrv.handleProduce).Methods("POST") - r.HandleFunc("/", httpsrv.handleConsume).Methods("GET") + r.HandleFunc("/{offset:[0-9]+}", httpsrv.handleConsume).Methods("GET") return &http.Server{ Addr: addr, Handler: r, @@ -40,10 +41,6 @@ type ProduceResponse struct { Offset uint64 `json:"offset"` } -type ConsumeRequest struct { - Offset uint64 `json:"offset"` -} - type ConsumeResponse struct { Record Record `json:"record"` } @@ -75,13 +72,13 @@ func (s *httpServer) handleProduce(w http.ResponseWriter, r *http.Request) { // START:consume func (s *httpServer) handleConsume(w http.ResponseWriter, r *http.Request) { - var req ConsumeRequest - err := json.NewDecoder(r.Body).Decode(&req) + vars := mux.Vars(r) + offset, err := strconv.ParseUint(vars["offset"], 10, 64) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } - record, err := s.Log.Read(req.Offset) + record, err := s.Log.Read(offset) if err == ErrOffsetNotFound { http.Error(w, err.Error(), http.StatusNotFound) return