Using pat for routing

This commit is contained in:
Muyao CHEN 2024-06-28 12:59:47 +02:00
parent b9c8c2592d
commit daef31f070
4 changed files with 29 additions and 4 deletions

View File

@ -27,10 +27,13 @@ func main() {
render.NewTemplates(&app)
http.HandleFunc("/", handlers.Repo.Home)
http.HandleFunc("/about", handlers.Repo.About)
fmt.Printf("Starting application on port %s\n", portNumber)
_ = http.ListenAndServe(portNumber, nil)
srv := &http.Server{
Addr: portNumber,
Handler: routes(&app),
}
err = srv.ListenAndServe()
log.Fatal(err)
}

18
cmd/web/routes.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"go-udemy-web-1/pkg/config"
"go-udemy-web-1/pkg/handlers"
"net/http"
"github.com/bmizerany/pat"
)
func routes(app *config.AppConfig) http.Handler {
mux := pat.New()
mux.Get("/", http.HandlerFunc(handlers.Repo.Home))
mux.Get("/about", http.HandlerFunc(handlers.Repo.About))
return mux
}

2
go.mod
View File

@ -1,3 +1,5 @@
module go-udemy-web-1
go 1.21.0
require github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f h1:gOO/tNZMjjvTKZWpY7YnXC72ULNLErRtp94LountVE8=
github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c=