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}}">
|
<input type="text" name="surname" value="{{.Surname}}">
|
||||||
<label>phone</label>
|
<label>phone</label>
|
||||||
<input type="tel" name="phone" value="{{.Phone}}">
|
<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>
|
<label>date</label>
|
||||||
<input type="date" name="date" value="{{.Date}}">
|
<input type="date" name="date" value="{{.Date}}">
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
122
main.go
122
main.go
|
|
@ -3,49 +3,54 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func form(w http.ResponseWriter, r *http.Request) {
|
type cake struct {
|
||||||
fmt.Println("received a get request at: ", r.URL.String())
|
Name string
|
||||||
tmpl, err := template.ParseFiles("form.html")
|
ID int
|
||||||
if err != nil {
|
Price int
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
Amount int
|
||||||
return
|
}
|
||||||
|
|
||||||
|
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")
|
w.Header().Set("content-type", "text/html")
|
||||||
err = tmpl.Execute(w, data)
|
err := h.tmpl.ExecuteTemplate(w, "form.html", data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error executing the template: ", err)
|
fmt.Println("error executing the template: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func addOrder(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) addOrder(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println("received a post request at: ", r.URL.String())
|
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("can't parse the form")
|
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>"))
|
w.Write([]byte("accepted <a href='/'>back</a>"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func index(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) 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"})
|
|
||||||
|
|
||||||
w.Header().Set("content-type", "text/html")
|
w.Header().Set("content-type", "text/html")
|
||||||
err = tmpl.Execute(w, data)
|
err := h.tmpl.ExecuteTemplate(w, "index.html", h.orders)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error executing the template: ", err)
|
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() {
|
func main() {
|
||||||
http.HandleFunc("GET /form", form)
|
var h handler
|
||||||
http.HandleFunc("GET /", index)
|
templates, err := template.ParseFiles("index.html", "form.html")
|
||||||
http.HandleFunc("POST /", addOrder)
|
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)
|
http.ListenAndServe(":8080", nil)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue