diff options
| author | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-11 18:31:20 +0200 |
|---|---|---|
| committer | Philipp Tanlak <philipp.tanlak@gmail.com> | 2023-08-11 18:31:20 +0200 |
| commit | 062b36fe5725d1267c66db2e506b4131d78ce772 (patch) | |
| tree | 998e5260feb1babac8dae512b56d67d8f20f7266 /api/api.go | |
| parent | 7e4cf39a0ba6ccbd5cc036700a8b1ff9358ecc3d (diff) | |
simplify project structure
Diffstat (limited to 'api/api.go')
| -rw-r--r-- | api/api.go | 84 |
1 files changed, 0 insertions, 84 deletions
diff --git a/api/api.go b/api/api.go deleted file mode 100644 index 893dd00..0000000 --- a/api/api.go +++ /dev/null @@ -1,84 +0,0 @@ -package api - -import ( - "encoding/json" - "net/http" - - "github.com/alexedwards/flow" -) - -type ScrapeRequest struct { - URL string `json:"url"` - Data map[string]any `json:"data"` -} - -type ScrapeResponse struct { - URL string `json:"url"` - Data any `json:"data"` -} - -//go:generate moq -out api_service_mock_test.go . Service -type Service interface { - ScrapeURL(url string, params map[string]any) (any, error) -} - -func NewHandler(svc Service) http.Handler { - h := &Handler{ - router: flow.New(), - svc: svc, - } - h.routes() - return h -} - -type Handler struct { - router *flow.Mux - svc Service -} - -func (h *Handler) routes() { - h.router.HandleFunc("/scrape", h.handleScrape, "POST") -} - -func (h *Handler) handleScrape(w http.ResponseWriter, r *http.Request) { - var req ScrapeRequest - if err := decodeRequest(r, &req); err != nil { - respondErr(w, http.StatusBadRequest, err) - return - } - - result, err := h.svc.ScrapeURL(req.URL, req.Data) - if err != nil { - respondErr(w, http.StatusInternalServerError, err) - return - } - - respond(w, ScrapeResponse{ - URL: req.URL, - Data: result, - }) -} - -func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - h.router.ServeHTTP(w, r) -} - -func decodeRequest(r *http.Request, v any) error { - return json.NewDecoder(r.Body).Decode(v) -} - -func respond(w http.ResponseWriter, v any) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(v) -} - -func respondErr(w http.ResponseWriter, statusCode int, err error) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(statusCode) - json.NewEncoder(w).Encode(struct { - Error string `json:"error"` - }{ - Error: err.Error(), - }) -} |