From 393b6603f9dda7c6dcf0973b404b4917f3491fc3 Mon Sep 17 00:00:00 2001 From: Jarrod Seccombe Date: Sun, 8 Dec 2024 18:33:03 -0500 Subject: [PATCH] server: drop Echo for net/http --- go.mod | 11 ++--------- go.sum | 16 ---------------- internal/server/error.go | 34 ++++++++++++++++++++++++++++++++++ internal/server/response.go | 18 ++++++++++++++++++ internal/server/routes.go | 19 +++++++------------ internal/server/seasons.go | 16 ++++++---------- 6 files changed, 67 insertions(+), 47 deletions(-) create mode 100644 internal/server/error.go create mode 100644 internal/server/response.go diff --git a/go.mod b/go.mod index 335c999..d8967ac 100644 --- a/go.mod +++ b/go.mod @@ -3,29 +3,22 @@ module github.com/jsec/f1-data-hub go 1.23.1 require ( + github.com/caarlos0/env/v11 v11.2.2 + github.com/chelnak/ysmrr v0.4.0 github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 github.com/jackc/pgx-shopspring-decimal v0.0.0-20220624020537-1d36b5a1853e github.com/jackc/pgx/v5 v5.7.1 - github.com/labstack/echo v3.3.10+incompatible github.com/shopspring/decimal v1.4.0 ) require ( - github.com/caarlos0/env/v11 v11.2.2 // indirect - github.com/chelnak/ysmrr v0.4.0 // indirect - github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/fatih/color v1.18.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect - github.com/joho/godotenv v1.5.1 // indirect - github.com/labstack/gommon v0.4.2 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/crypto v0.27.0 // indirect - golang.org/x/net v0.21.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.18.0 // indirect diff --git a/go.sum b/go.sum index 145f35d..d5a15c2 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,6 @@ github.com/chelnak/ysmrr v0.4.0/go.mod h1:8vCna4PJsPCb6eevtoG7Tljzfx3twpsO203Qj2 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= -github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 h1:FWNFq4fM1wPfcK40yHE5UO3RUdSNPaBC+j3PokzA6OQ= @@ -21,12 +19,6 @@ github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs= github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA= github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= -github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= -github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= -github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= @@ -41,20 +33,12 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= -github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= -github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= -golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= -golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= -golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= diff --git a/internal/server/error.go b/internal/server/error.go new file mode 100644 index 0000000..7c4300a --- /dev/null +++ b/internal/server/error.go @@ -0,0 +1,34 @@ +package server + +import ( + "encoding/json" + "net/http" +) + +type response struct { + Error string `json:"string"` +} + +func (s *Server) badRequest(w http.ResponseWriter, msg string) { + s.httpError(w, http.StatusBadRequest, msg) +} + +func (s *Server) notFound(w http.ResponseWriter, msg string) { + s.httpError(w, http.StatusNotFound, msg) +} + +func (s *Server) httpError(w http.ResponseWriter, code int, msg string) { + response := response{ + Error: msg, + } + + res, err := json.Marshal(response) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.Header().Set("content-type", "application/json") + w.WriteHeader(code) + w.Write(res) +} diff --git a/internal/server/response.go b/internal/server/response.go new file mode 100644 index 0000000..c843d91 --- /dev/null +++ b/internal/server/response.go @@ -0,0 +1,18 @@ +package server + +import ( + "encoding/json" + "net/http" +) + +func (s *Server) respond(w http.ResponseWriter, code int, payload interface{}) { + res, err := json.Marshal(payload) + if err != nil { + s.httpError(w, http.StatusInternalServerError, err.Error()) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + w.Write(res) +} diff --git a/internal/server/routes.go b/internal/server/routes.go index b8492f4..c5fe261 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -2,26 +2,21 @@ package server import ( "net/http" - - "github.com/labstack/echo" - "github.com/labstack/echo/middleware" ) func (s *Server) RegisterRoutes() http.Handler { - e := echo.New() - e.Use(middleware.Logger()) - e.Use(middleware.Recover()) - e.GET("/", s.helloWorldHandler) - e.GET("/seasons/:year/standings", s.driverStandingsByYearHandler) + mux := http.NewServeMux() + mux.HandleFunc("/", s.helloWorldHandler) + mux.HandleFunc("GET /seasons/{year}/standings", s.driverStandingsByYearHandler) - return e + return mux } -func (s *Server) helloWorldHandler(c echo.Context) error { - resp := map[string]string{ +func (s *Server) helloWorldHandler(w http.ResponseWriter, r *http.Request) { + res := map[string]string{ "hello": "world", } - return c.JSON(http.StatusOK, resp) + s.respond(w, http.StatusOK, res) } diff --git a/internal/server/seasons.go b/internal/server/seasons.go index 464bb47..22b7d13 100644 --- a/internal/server/seasons.go +++ b/internal/server/seasons.go @@ -1,24 +1,20 @@ package server import ( - "log" "net/http" "strconv" - - "github.com/labstack/echo" ) -func (s *Server) driverStandingsByYearHandler(c echo.Context) error { - year, err := strconv.Atoi(c.Param("year")) +func (s *Server) driverStandingsByYearHandler(w http.ResponseWriter, r *http.Request) { + year, err := strconv.Atoi(r.PathValue("year")) if err != nil { - return c.String(http.StatusBadRequest, "Invalid year") + s.badRequest(w, "Invalid year") } - standings, err := s.db.GetDriverStandingsByYear(c.Request().Context(), int32(year)) + standings, err := s.db.GetDriverStandingsByYear(r.Context(), int32(year)) if err != nil { - // TODO: build out error handling - log.Fatal("Things went bonk:", err) + s.httpError(w, http.StatusInternalServerError, err.Error()) } - return c.JSON(http.StatusOK, standings) + s.respond(w, http.StatusOK, standings) }