Using pat for routing
This commit is contained in:
parent
b9c8c2592d
commit
daef31f070
@ -27,10 +27,13 @@ func main() {
|
|||||||
|
|
||||||
render.NewTemplates(&app)
|
render.NewTemplates(&app)
|
||||||
|
|
||||||
http.HandleFunc("/", handlers.Repo.Home)
|
|
||||||
http.HandleFunc("/about", handlers.Repo.About)
|
|
||||||
|
|
||||||
fmt.Printf("Starting application on port %s\n", portNumber)
|
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
18
cmd/web/routes.go
Normal 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
2
go.mod
@ -1,3 +1,5 @@
|
|||||||
module go-udemy-web-1
|
module go-udemy-web-1
|
||||||
|
|
||||||
go 1.21.0
|
go 1.21.0
|
||||||
|
|
||||||
|
require github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f // indirect
|
||||||
|
Loading…
Reference in New Issue
Block a user