Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions LetsGo/internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package server
import (
"encoding/json"
"net/http"
"strconv"

"github.com/gorilla/mux"
)
Expand All @@ -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,
Expand Down Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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
Expand Down