Compare commits
2 Commits
7ab49c9fc8
...
8df6f1c6e9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8df6f1c6e9 | ||
|
|
5e98af52fe |
@@ -8,6 +8,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (app *application) render(w http.ResponseWriter, status int, page string, data *templateData) {
|
func (app *application) render(w http.ResponseWriter, status int, page string, data *templateData) {
|
||||||
@@ -46,7 +47,8 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
@@ -68,11 +70,12 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
data := app.newTemplateData(r)
|
||||||
w.Header().Set("Allow", http.MethodPost)
|
|
||||||
app.clientError(w, http.StatusMethodNotAllowed)
|
app.render(w, http.StatusOK, "create.tmpl", data)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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-Kobayshi Issa"
|
content := "0 snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n-Kobayshi Issa"
|
||||||
expires := 7
|
expires := 7
|
||||||
@@ -82,5 +85,5 @@ func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
app.serverError(w, err)
|
app.serverError(w, err)
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,20 +3,26 @@ package main
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
"github.com/justinas/alice"
|
"github.com/justinas/alice"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (app *application) routes() http.Handler {
|
func (app *application) routes() http.Handler {
|
||||||
mux := http.NewServeMux()
|
router := httprouter.New()
|
||||||
|
|
||||||
|
router.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
app.notFound(w)
|
||||||
|
})
|
||||||
|
|
||||||
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/", 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)
|
||||||
}
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -5,5 +5,6 @@ go 1.25.0
|
|||||||
require (
|
require (
|
||||||
filippo.io/edwards25519 v1.1.0 // indirect
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
github.com/go-sql-driver/mysql v1.9.3 // indirect
|
github.com/go-sql-driver/mysql v1.9.3 // 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
|
||||||
)
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -2,5 +2,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
|||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo=
|
github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo=
|
||||||
github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
|
github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU=
|
||||||
|
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=
|
||||||
|
|||||||
23
ui/html/pages/create.tmpl
Normal file
23
ui/html/pages/create.tmpl
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
{{define "title"}}Create a New Snippet{{end}}
|
||||||
|
|
||||||
|
{{define "main"}}
|
||||||
|
<form action='/snippet/create' method='POST'>
|
||||||
|
<div>
|
||||||
|
<label>Title:</label>
|
||||||
|
<input type='text' name='title'>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label>Content:</label>
|
||||||
|
<textarea name='content'></textarea>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label>Delete in:</label>
|
||||||
|
<input type='radio' name='expires' value='365' checked> One Year
|
||||||
|
<input type='radio' name='expires' value='7'> One Week
|
||||||
|
<input type='radio' name='expires' value='1'> One Day
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type='submit' value='Publish snippet'>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{{end}}
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{{range .Snippets}}
|
{{range .Snippets}}
|
||||||
<tr>
|
<tr>
|
||||||
<td><a href='/snippet/view?id={{.ID}}'>{{.Title}}</a></td>
|
<td><a href='/snippet/view/{{.ID}}'>{{.Title}}</a></td>
|
||||||
<td>{{humanDate .Created}}</td>
|
<td>{{humanDate .Created}}</td>
|
||||||
<td>#{{.ID}}</td>
|
<td>#{{.ID}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
{{define "nav"}}
|
{{define "nav"}}
|
||||||
<nav>
|
<nav>
|
||||||
<a href='/'>Home</a>
|
<a href='/'>Home</a>
|
||||||
|
<a href='/snippet/create'>Create snippet</a>
|
||||||
</nav>
|
</nav>
|
||||||
{{end}}
|
{{end}}
|
||||||
Reference in New Issue
Block a user