Skip to content

Commit

Permalink
refactor: update the routes to the new go 1.22 syntax, simplifying ha…
Browse files Browse the repository at this point in the history
…ndlers a bit
  • Loading branch information
xpmatteo committed Feb 15, 2024
1 parent adc7466 commit a7dfd65
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 93 deletions.
13 changes: 8 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
module github.com/xpmatteo/todomvc-golang

go 1.21.6
go 1.22.0

require github.com/stretchr/testify v1.8.4
require (
github.com/dlmiddlecote/sqlstats v1.0.2
github.com/prometheus/client_golang v1.18.0
github.com/stretchr/testify v1.8.4
modernc.org/sqlite v1.28.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlmiddlecote/sqlstats v1.0.2 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand All @@ -33,7 +37,6 @@ require (
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/sqlite v1.28.0 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/token v1.0.1 // indirect
)
49 changes: 27 additions & 22 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,45 @@ func main() {
collector := sqlstats.NewStatsCollector("todo_db", pool)
prometheus.MustRegister(collector)

// I have to use a new ServeMux because the default mux is polluted by
// expvar, a package that I have no idea how it gets included in the project.
// Problem is that expvar declares a route for "/debug/vars" that (for mysterious reasons)
// conflicts with the route "/"
mux := http.NewServeMux()
templ := template.Must(template.ParseFiles("templates/index.html"))
http.Handle("/",
mux.Handle("GET /{$}",
web.Metrics("index",
web.Logging(
web.GETonly(
web.IndexHandler(templ, repository)))))
http.Handle("/new-todo",
web.IndexHandler(templ, repository))))
mux.Handle("GET /active",
web.Metrics("active",
web.Logging(
web.IndexHandler(templ, repository))))
mux.Handle("GET /completed",
web.Metrics("completed",
web.Logging(
web.IndexHandler(templ, repository))))
mux.Handle("POST /new-todo",
web.Metrics("new-todo",
web.Logging(
web.POSTonly(
web.Slowdown(1000,
web.NewItemHandler(templ, repository))))))
http.Handle("/toggle",
web.Slowdown(1000,
web.NewItemHandler(templ, repository)))))
mux.Handle("POST /toggle",
web.Metrics("toggle",
web.Logging(
web.POSTonly(
web.ToggleHandler(templ, repository)))))
http.Handle("/edit",
web.ToggleHandler(templ, repository))))
mux.Handle("POST /edit",
web.Metrics("edit",
web.Logging(
web.POSTonly(
web.EditHandler(templ, repository)))))
http.Handle("/destroy",
web.EditHandler(templ, repository))))
mux.Handle("POST /destroy",
web.Metrics("destroy",
web.Logging(
web.POSTonly(
web.DestroyHandler(templ, repository)))))

http.Handle("/metrics", promhttp.Handler())
web.DestroyHandler(templ, repository))))

web.GET("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("./public/img"))))
web.GET("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./public/css"))))
web.GET("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("./public/js"))))
mux.Handle("GET /metrics", promhttp.Handler())
mux.Handle("GET /", http.FileServer(http.Dir("./public/")))

log.Println("Listening on port " + port)
web.GracefulListenAndServe(":"+port, nil)
web.GracefulListenAndServe(":"+port, mux)
}
4 changes: 0 additions & 4 deletions web/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ type Repository interface {

func IndexHandler(templ *template.Template, repo ListFinder) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" && r.URL.Path != pathActive && r.URL.Path != pathCompleted {
http.Error(w, "Not found", http.StatusNotFound)
return
}
model, err := repo.FindList()
if err != nil {
internalServerError(w, err)
Expand Down
10 changes: 0 additions & 10 deletions web/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ func Test_indexHandler_ok(t *testing.T) {
assert.Equal(t, "items: item0,item1,", w.Body.String())
}

func Test_indexHandler_unexpectedPath(t *testing.T) {
w, r := httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/foo", nil)
repository := db.FakeRepository()

IndexHandler(templ, repository).ServeHTTP(w, r)

assert.Equal(t, 404, w.Code)
assert.Equal(t, "Not found\n", w.Body.String())
}

func Test_indexHandler_editItem(t *testing.T) {
w, r := httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/?edit=3", nil)
templ := template.Must(template.New("index").Parse("<p>{{.EditingItemId}}</p>"))
Expand Down
24 changes: 0 additions & 24 deletions web/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,6 @@ import (
"time"
)

func GETonly(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
} else {
h.ServeHTTP(w, r)
}
})
}

func POSTonly(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
} else {
h.ServeHTTP(w, r)
}
})
}

func GET(pattern string, handler http.Handler) {
http.Handle(pattern, GETonly(handler))
}

func Logging(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
Expand Down
28 changes: 0 additions & 28 deletions web/middleware_test.go

This file was deleted.

0 comments on commit a7dfd65

Please sign in to comment.