You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB
Go

1 year ago
package main
import (
"fmt"
"html/template"
1 year ago
"net/http"
"strconv"
)
1 year ago
func (app *application) home(w http.ResponseWriter, r *http.Request) {
1 year ago
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
files := []string{
"./ui/html/base.tmpl",
"./ui/html/partials/nav.tmpl",
"./ui/html/pages/home.tmpl",
}
ts, err := template.ParseFiles(files...)
if err != nil {
1 year ago
app.serverError(w, err)
return
}
err = ts.ExecuteTemplate(w, "base", nil)
if err != nil {
1 year ago
app.serverError(w, err)
}
1 year ago
}
1 year ago
func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
1 year ago
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil || id < 1 {
1 year ago
app.notFound(w)
1 year ago
return
}
fmt.Fprintf(w, "Display a specific snippet with ID %d...", id)
}
1 year ago
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
1 year ago
if r.Method != "POST" {
w.Header().Set("Allow", http.MethodPost)
1 year ago
app.clientError(w, http.StatusMethodNotAllowed)
1 year ago
return
}
title := "0 Snail"
content := "O snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n Kobayashi Issa"
expires := 7
id, err := app.snippets.Insert(title, content, expires)
if err != nil {
app.serverError(w, err)
return
}
http.Redirect(w, r, fmt.Sprintf("/snippet/view?id=%d", id), http.StatusSeeOther)
1 year ago
}