Skip to content

Commit

Permalink
refactor: made main more readable and changeable
Browse files Browse the repository at this point in the history
  • Loading branch information
xpmatteo committed Feb 9, 2024
1 parent f87e03c commit 74dc363
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
33 changes: 22 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,32 @@ func main() {
model.Add("baz")

templ := template.Must(template.ParseFiles("templates/index.html"))
web.GET("/",
http.Handle("/",
web.Metrics("index",
web.Logging(web.IndexHandler(templ, model))))
web.POST("/new-todo",
web.Logging(
web.GETonly(
web.IndexHandler(templ, model)))))
http.Handle("/new-todo",
web.Metrics("new-todo",
web.Slowdown(1000,
web.Logging(web.NewItemHandler(templ, model)))))
web.POST("/toggle",
web.Logging(
web.POSTonly(
web.Slowdown(1000,
web.NewItemHandler(templ, model))))))
http.Handle("/toggle",
web.Metrics("toggle",
web.Logging(
web.ToggleHandler(templ, model))))
web.POST("/edit",
web.Logging(web.EditHandler(templ, model)))
web.POST("/destroy",
web.Logging(web.DestroyHandler(templ, model)))
web.POSTonly(
web.ToggleHandler(templ, model)))))
http.Handle("/edit",
web.Metrics("edit",
web.Logging(
web.POSTonly(
web.EditHandler(templ, model)))))
http.Handle("/destroy",
web.Metrics("destroy",
web.Logging(
web.POSTonly(
web.DestroyHandler(templ, model)))))

http.Handle("/metrics", promhttp.Handler())

Expand Down
4 changes: 0 additions & 4 deletions web/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ func GET(pattern string, handler http.Handler) {
http.Handle(pattern, GETonly(handler))
}

func POST(pattern string, handler http.Handler) {
http.Handle(pattern, POSTonly(handler))
}

func Logging(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
Expand Down

0 comments on commit 74dc363

Please sign in to comment.