12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package endpoint
- import (
- "context"
- "encoding/json"
- "github.com/go-chi/chi"
- kitlog "github.com/go-kit/kit/log"
- "github.com/longjoy/micro-go-course/section19/cargo/service/tracking"
- "net/http"
- )
- type trackingHandler struct {
- s tracking.Service
- logger kitlog.Logger
- }
- func (h *trackingHandler) router() chi.Router {
- r := chi.NewRouter()
- r.Get("/cargos/{trackingID}", h.track)
- r.Method("GET", "/docs", http.StripPrefix("/tracking/v1/docs", http.FileServer(http.Dir("tracking/docs"))))
- return r
- }
- func (h *trackingHandler) track(w http.ResponseWriter, r *http.Request) {
- ctx := context.Background()
- trackingID := chi.URLParam(r, "trackingID")
- c, err := h.s.Track(trackingID)
- if err != nil {
- encodeError(ctx, err, w)
- return
- }
- var response = struct {
- Cargo *tracking.Cargo `json:"cargo"`
- }{Cargo: &c}
- w.Header().Set("Content-Type", "application/json; charset=utf-8")
- if err := json.NewEncoder(w).Encode(response); err != nil {
- h.logger.Log("error", err)
- encodeError(ctx, err, w)
- return
- }
- }
|