lets-go:7.2 new route handler
This commit is contained in:
parent
d6d629884b
commit
d1b9b2b269
@ -6,15 +6,12 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"snippetbox.chaosfem.tw/internal/models"
|
||||
)
|
||||
|
||||
// home ...
|
||||
func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
snippets, err := app.snippets.Latest()
|
||||
if err != nil {
|
||||
app.serverError(w, r, err)
|
||||
@ -29,7 +26,9 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// snippetView ...
|
||||
func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(r.URL.Query().Get("id"))
|
||||
params := httprouter.ParamsFromContext(r.Context())
|
||||
|
||||
id, err := strconv.Atoi(params.ByName("id"))
|
||||
if err != nil || id < 1 {
|
||||
app.notFound(w)
|
||||
return
|
||||
@ -51,15 +50,13 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
||||
app.render(w, r, http.StatusOK, "view.tmpl", data)
|
||||
}
|
||||
|
||||
// snippetCreate ...
|
||||
// (app *application) snippetCreate ...
|
||||
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Allow", "POST")
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
app.clientError(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
w.Write([]byte("Form for new snippet, plz?"))
|
||||
}
|
||||
|
||||
// snippetCreatePost ...
|
||||
func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request) {
|
||||
title := "0 snail"
|
||||
content := "0 snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n - Kobayashi Issa"
|
||||
expires := 7
|
||||
@ -70,5 +67,5 @@ func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("/snippet/view?id=%d", id), http.StatusSeeOther)
|
||||
http.Redirect(w, r, fmt.Sprintf("/snippet/view/%d", id), http.StatusSeeOther)
|
||||
}
|
||||
|
@ -3,22 +3,24 @@ package main
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/justinas/alice"
|
||||
)
|
||||
|
||||
// routes ...
|
||||
func (app *application) routes() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
router := httprouter.New()
|
||||
|
||||
// setup server for static files
|
||||
fileServer := http.FileServer(http.Dir("./ui/static"))
|
||||
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
|
||||
router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer))
|
||||
|
||||
mux.HandleFunc("/", app.home)
|
||||
mux.HandleFunc("/snippet/view", app.snippetView)
|
||||
mux.HandleFunc("/snippet/create", app.snippetCreate)
|
||||
router.HandlerFunc(http.MethodGet, "/", app.home)
|
||||
router.HandlerFunc(http.MethodGet, "/snippet/view/:id", app.snippetView)
|
||||
router.HandlerFunc(http.MethodGet, "/snippet/create", app.snippetCreate)
|
||||
router.HandlerFunc(http.MethodPost, "/snippet/create", app.snippetCreatePost)
|
||||
|
||||
standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders)
|
||||
|
||||
return standard.Then(mux)
|
||||
return standard.Then(router)
|
||||
}
|
||||
|
@ -4,5 +4,6 @@ go 1.21.4
|
||||
|
||||
require (
|
||||
github.com/go-sql-driver/mysql v1.7.1 // indirect
|
||||
github.com/julienschmidt/httprouter v1.3.0 // indirect
|
||||
github.com/justinas/alice v1.2.0 // indirect
|
||||
)
|
||||
|
@ -1,4 +1,6 @@
|
||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
|
||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
|
||||
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
|
||||
|
@ -11,7 +11,7 @@
|
||||
</tr>
|
||||
{{range .Snippets}}
|
||||
<tr>
|
||||
<td><a href='/snippet/view?id={{.ID}}'>{{.Title}}</td>
|
||||
<td><a href='/snippet/view/{{.ID}}'>{{.Title}}</td>
|
||||
<td>{{humanDate .Created}}</td>
|
||||
<td>{{.ID}}</td>
|
||||
</tr>
|
||||
|
Loading…
Reference in New Issue
Block a user