lets-go:7.2 new route handler

This commit is contained in:
tamsin johnson 2024-01-25 22:01:12 -08:00
parent d6d629884b
commit d1b9b2b269
5 changed files with 25 additions and 23 deletions

View File

@ -6,15 +6,12 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"github.com/julienschmidt/httprouter"
"snippetbox.chaosfem.tw/internal/models" "snippetbox.chaosfem.tw/internal/models"
) )
// home ... // home ...
func (app *application) home(w http.ResponseWriter, r *http.Request) { func (app *application) home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
snippets, err := app.snippets.Latest() snippets, err := app.snippets.Latest()
if err != nil { if err != nil {
app.serverError(w, r, err) app.serverError(w, r, err)
@ -28,8 +25,10 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
} }
// snippetView ... // snippetView ...
func (app *application) snippetView(w http.ResponseWriter, r *http.Request) { 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 { if err != nil || id < 1 {
app.notFound(w) app.notFound(w)
return return
@ -51,15 +50,13 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
app.render(w, r, http.StatusOK, "view.tmpl", data) app.render(w, r, http.StatusOK, "view.tmpl", data)
} }
// snippetCreate ... // (app *application) snippetCreate ...
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) { func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Allow", "POST") w.Write([]byte("Form for new snippet, plz?"))
}
if r.Method != http.MethodPost {
app.clientError(w, http.StatusMethodNotAllowed)
return
}
// snippetCreatePost ...
func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request) {
title := "0 snail" title := "0 snail"
content := "0 snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n - Kobayashi Issa" content := "0 snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n - Kobayashi Issa"
expires := 7 expires := 7
@ -70,5 +67,5 @@ func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
return 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)
} }

View File

@ -3,22 +3,24 @@ package main
import ( import (
"net/http" "net/http"
"github.com/julienschmidt/httprouter"
"github.com/justinas/alice" "github.com/justinas/alice"
) )
// routes ... // routes ...
func (app *application) routes() http.Handler { func (app *application) routes() http.Handler {
mux := http.NewServeMux() router := httprouter.New()
// setup server for static files // setup server for static files
fileServer := http.FileServer(http.Dir("./ui/static")) 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) router.HandlerFunc(http.MethodGet, "/", app.home)
mux.HandleFunc("/snippet/view", app.snippetView) router.HandlerFunc(http.MethodGet, "/snippet/view/:id", app.snippetView)
mux.HandleFunc("/snippet/create", app.snippetCreate) router.HandlerFunc(http.MethodGet, "/snippet/create", app.snippetCreate)
router.HandlerFunc(http.MethodPost, "/snippet/create", app.snippetCreatePost)
standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders) standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders)
return standard.Then(mux) return standard.Then(router)
} }

View File

@ -4,5 +4,6 @@ go 1.21.4
require ( require (
github.com/go-sql-driver/mysql v1.7.1 // indirect 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 github.com/justinas/alice v1.2.0 // indirect
) )

View File

@ -1,4 +1,6 @@
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= 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/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 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA= github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=

View File

@ -11,7 +11,7 @@
</tr> </tr>
{{range .Snippets}} {{range .Snippets}}
<tr> <tr>
<td><a href='/snippet/view?id={{.ID}}'>{{.Title}}</td> <td><a href='/snippet/view/{{.ID}}'>{{.Title}}</td>
<td>{{humanDate .Created}}</td> <td>{{humanDate .Created}}</td>
<td>{{.ID}}</td> <td>{{.ID}}</td>
</tr> </tr>