logging, and handler struct
This commit is contained in:
parent
40fc76c7fc
commit
bc4c9d62f0
2 changed files with 75 additions and 52 deletions
|
|
@ -14,6 +14,11 @@
|
|||
<input type="text" name="surname" value="{{.Surname}}">
|
||||
<label>phone</label>
|
||||
<input type="tel" name="phone" value="{{.Phone}}">
|
||||
<label>location</label>
|
||||
<select name="location">
|
||||
<option value="Kartuzy">Kartuzy</option>
|
||||
<option value="Somonino">Somonino</option>
|
||||
</select>
|
||||
<label>date</label>
|
||||
<input type="date" name="date" value="{{.Date}}">
|
||||
</div>
|
||||
|
|
|
|||
122
main.go
122
main.go
|
|
@ -3,49 +3,54 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func form(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("received a get request at: ", r.URL.String())
|
||||
tmpl, err := template.ParseFiles("form.html")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
type cake struct {
|
||||
Name string
|
||||
ID int
|
||||
Price int
|
||||
Amount int
|
||||
}
|
||||
|
||||
type order struct {
|
||||
ID int
|
||||
Name string
|
||||
Surname string
|
||||
Phone string
|
||||
Location string
|
||||
Date time.Time
|
||||
Paid int // increments of 0.01
|
||||
Cakes []cake
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
tmpl *template.Template
|
||||
cakes []cake
|
||||
orders []order
|
||||
}
|
||||
|
||||
func (h *handler) form(w http.ResponseWriter, r *http.Request) {
|
||||
data := order{
|
||||
Name: "",
|
||||
Surname: "",
|
||||
Phone: "",
|
||||
Location: "",
|
||||
Date: time.Now(),
|
||||
Cakes: h.cakes,
|
||||
}
|
||||
type cake struct {
|
||||
Name string
|
||||
ID int
|
||||
Price int
|
||||
Amount int
|
||||
}
|
||||
type tmplData struct {
|
||||
Name string
|
||||
Surname string
|
||||
Phone string
|
||||
Date string
|
||||
Cakes []cake
|
||||
}
|
||||
data := tmplData{
|
||||
Name: "",
|
||||
Surname: "",
|
||||
Phone: "",
|
||||
Date: "",
|
||||
Cakes: make([]cake, 0),
|
||||
}
|
||||
data.Cakes = append(data.Cakes, cake{"Hello", 0, 100, 0})
|
||||
data.Cakes = append(data.Cakes, cake{"World", 1, 120, 0})
|
||||
w.Header().Set("content-type", "text/html")
|
||||
err = tmpl.Execute(w, data)
|
||||
err := h.tmpl.ExecuteTemplate(w, "form.html", data)
|
||||
if err != nil {
|
||||
fmt.Println("error executing the template: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func addOrder(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("received a post request at: ", r.URL.String())
|
||||
func (h *handler) addOrder(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
fmt.Println("can't parse the form")
|
||||
|
|
@ -58,33 +63,46 @@ func addOrder(w http.ResponseWriter, r *http.Request) {
|
|||
w.Write([]byte("accepted <a href='/'>back</a>"))
|
||||
}
|
||||
|
||||
func index(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("received a get request at: ", r.URL.String())
|
||||
tmpl, err := template.ParseFiles("index.html")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
type tmplData struct {
|
||||
ID int
|
||||
Name string
|
||||
Surname string
|
||||
}
|
||||
data := make([]tmplData, 0)
|
||||
data = append(data, tmplData{0, "Jan", "Kowalski"})
|
||||
data = append(data, tmplData{1, "Jakub", "Bronk"})
|
||||
|
||||
func (h *handler) index(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("content-type", "text/html")
|
||||
err = tmpl.Execute(w, data)
|
||||
err := h.tmpl.ExecuteTemplate(w, "index.html", h.orders)
|
||||
if err != nil {
|
||||
fmt.Println("error executing the template: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func logger(in http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
log := fmt.Sprint(r.Method, " ", r.URL.String())
|
||||
in(w, r)
|
||||
log += fmt.Sprint(" ", time.Since(start))
|
||||
fmt.Println(log)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("GET /form", form)
|
||||
http.HandleFunc("GET /", index)
|
||||
http.HandleFunc("POST /", addOrder)
|
||||
var h handler
|
||||
templates, err := template.ParseFiles("index.html", "form.html")
|
||||
if err != nil {
|
||||
log.Fatal("can't parse templates: ", err)
|
||||
}
|
||||
|
||||
h.tmpl = templates
|
||||
|
||||
orders := make([]order, 0)
|
||||
orders = append(orders, order{0, "Albert", "Camus", "123456789", "Kartuzy", time.Now().AddDate(0, 0, 7), 0, nil})
|
||||
orders = append(orders, order{1, "George", "Orwell", "", "Kartuzy", time.Now().AddDate(0, 1, 0), 0, nil})
|
||||
orders = append(orders, order{2, "Karl", "Marx", "0700", "Somonino", time.Now(), 0, nil})
|
||||
h.orders = orders
|
||||
|
||||
cakes := make([]cake, 0)
|
||||
cakes = append(cakes, cake{"Sernik", 0, 120, 0})
|
||||
cakes = append(cakes, cake{"Malinowa chmurka", 1, 120, 0})
|
||||
h.cakes = cakes
|
||||
|
||||
http.HandleFunc("GET /form", logger(h.form))
|
||||
http.HandleFunc("GET /", logger(h.index))
|
||||
http.HandleFunc("POST /", logger(h.addOrder))
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue