|
@@ -14,9 +14,60 @@ import (
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
+ App = "contactsapp"
|
|
|
SecretKey = "hypermedia rocks"
|
|
|
)
|
|
|
|
|
|
+func MakeTemplate(names ...string) *template.Template {
|
|
|
+ names = append([]string{"layout"}, names...)
|
|
|
+ paths := make([]string, len(names))
|
|
|
+ for i, name := range names {
|
|
|
+ paths[i] = fmt.Sprintf("./templates/%s.gohtml", name)
|
|
|
+ }
|
|
|
+ tpl := template.Must(template.New("layout.html").
|
|
|
+ Funcs(sprig.FuncMap()).
|
|
|
+ Funcs(template.FuncMap{
|
|
|
+ "get_flashed_messages": func() []string {
|
|
|
+ return []string{"some message"}
|
|
|
+ },
|
|
|
+ }).
|
|
|
+ ParseFiles(paths...),
|
|
|
+ )
|
|
|
+ return tpl
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+@app.route("/contacts")
|
|
|
+def contacts():
|
|
|
+*/
|
|
|
+func RouteContacts(w http.ResponseWriter, r *http.Request) {
|
|
|
+ search := r.URL.Query().Get("q")
|
|
|
+ page, err := strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
+ if err != nil || page == 0 {
|
|
|
+ page = 1
|
|
|
+ }
|
|
|
+ var contactsSet []Contact
|
|
|
+ if search != "" {
|
|
|
+ contactsSet = (&Contact{}).Search(search)
|
|
|
+ if r.Header.Get("HX-Trigger") == "search" {
|
|
|
+ MakeTemplate("rows").Execute(w, anyMap{"contacts": contactsSet})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ contactsSet = (&Contact{}).All(page)
|
|
|
+ }
|
|
|
+ MakeTemplate(
|
|
|
+ "index",
|
|
|
+ "archive_ui",
|
|
|
+ "rows",
|
|
|
+ ).Execute(w, anyMap{
|
|
|
+ "contacts": contactsSet,
|
|
|
+ "search": search,
|
|
|
+ "page": page,
|
|
|
+ "archiver": NewArchiver(),
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
var (
|
|
|
/*
|
|
|
@app.route("/")
|
|
@@ -24,34 +75,6 @@ var (
|
|
|
*/
|
|
|
RouteFront = http.RedirectHandler("/contacts", http.StatusFound)
|
|
|
|
|
|
- /*
|
|
|
- @app.route("/contacts")
|
|
|
- def contacts():
|
|
|
- */
|
|
|
- MakeRouteContacts = func(templates *template.Template) http.HandlerFunc {
|
|
|
- return func(w http.ResponseWriter, r *http.Request) {
|
|
|
- search := r.URL.Query().Get("q")
|
|
|
- page, err := strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
- if err != nil || page == 0 {
|
|
|
- page = 1
|
|
|
- }
|
|
|
- var contactsSet []Contact
|
|
|
- if search != "" {
|
|
|
- contactsSet = (&Contact{}).Search(search)
|
|
|
- if r.Header.Get("HX-Trigger") == "search" {
|
|
|
- templates.ExecuteTemplate(w, "rows.html", anyMap{"contacts": contactsSet})
|
|
|
- return
|
|
|
- }
|
|
|
- } else {
|
|
|
- contactsSet = (&Contact{}).All(page)
|
|
|
- }
|
|
|
- templates.ExecuteTemplate(w, "index.html", anyMap{
|
|
|
- "contacts": contactsSet,
|
|
|
- "archiver": NewArchiver(),
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/*
|
|
|
@app.route("/contacts/archive", methods=["POST"])
|
|
|
def start_archive():
|
|
@@ -159,7 +182,10 @@ var (
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
contact_id, _ := strconv.Atoi(r.PathValue("contact_id"))
|
|
|
contact := (&Contact{}).Find(uint64(contact_id))
|
|
|
- templates.ExecuteTemplate(w, "edit.html", anyMap{"contact": contact})
|
|
|
+ MakeTemplate(
|
|
|
+ "edit",
|
|
|
+ ).
|
|
|
+ Execute(w, anyMap{"contact": contact})
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -336,7 +362,8 @@ func flash(message string) {
|
|
|
|
|
|
func setupRoutes(mux *http.ServeMux, templates *template.Template) {
|
|
|
mux.Handle("/", RouteFront)
|
|
|
- mux.Handle("/contacts", MakeRouteContacts(templates))
|
|
|
+ mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
|
|
|
+ mux.HandleFunc("/contacts", RouteContacts)
|
|
|
mux.Handle("POST /contacts/archive", MakeStartArchive(templates))
|
|
|
mux.Handle("GET /contacts/archive", MakeArchiveStatus(templates))
|
|
|
mux.Handle("GET /contacts/archive/file", ArchiveContent)
|