-
Notifications
You must be signed in to change notification settings - Fork 3
/
router.go
63 lines (53 loc) · 1.46 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"net/http"
"github.com/gorilla/mux"
"github.com/josephgardner/isitcg/internal/isitcg"
)
const (
ROUTE_VIEW = "view"
)
func router(ingredientHandler isitcg.IngredientHandler, renders renders) *mux.Router {
router := mux.NewRouter()
router.NewRoute().
Path("/").
Methods(http.MethodGet).
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
renders.Index(w, isitcg.Product{})
})
router.NewRoute().
Path("/").
Methods(http.MethodPost).
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
if url, err := router.Get(ROUTE_VIEW).URL(
"hash",
ingredientHandler.CreateHash(
r.PostFormValue("productname"),
r.PostFormValue("ingredients"),
),
); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
http.Redirect(w, r, url.String(), http.StatusSeeOther)
}
})
router.NewRoute().
Name(ROUTE_VIEW).
Path("/view/{hash}").
Methods(http.MethodGet).
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := ingredientHandler.ResultsFromHash(mux.Vars(r)["hash"])
renders.Results(w, res)
})
router.NewRoute().
Path("/edit/{hash}").
Methods(http.MethodGet).
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
res := ingredientHandler.ProductFromHash(mux.Vars(r)["hash"])
renders.Index(w, res)
})
return router
}