func handler(w http.ResponseWriter, r *http.Request) { tpl, err := ace.Load("example", "", &ace.Options{DynamicReload: true}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := tpl.Execute(w, nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
func handler(w http.ResponseWriter, r *http.Request) { tpl, err := ace.Load("base", "inner", nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := tpl.Execute(w, nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
func handler(w http.ResponseWriter, r *http.Request) { tpl, err := ace.Load("views/example", "", &ace.Options{ Asset: Asset, }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := tpl.Execute(w, nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
func compileResultFromFile(baseFile, innerFile string) (string, error) { base := baseFile[:len(baseFile)-len(filepath.Ext(baseFile))] var inner string if len(innerFile) > 0 { inner = innerFile[:len(innerFile)-len(filepath.Ext(innerFile))] } name := base + ":" + inner tpl, err := ace.Load(base, inner, nil) if err != nil { return "", err } return tpl.Lookup(name).Tree.Root.String(), nil }
func handler(w http.ResponseWriter, r *http.Request) { tpl, err := ace.Load("example", "", &ace.Options{ DelimLeft: "<%", DelimRight: "%>", }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } data := map[string]interface{}{ "Msg": "Hello Ace", } if err := tpl.Execute(w, data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
func handler(w http.ResponseWriter, r *http.Request) { tpl, err := ace.Load("example", "", nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } data := map[string]interface{}{ "Pets": []Pet{ Pet{Species: "Dog", Name: "Taro", Age: 5}, Pet{Species: "Cat", Name: "Hanako", Age: 10}, Pet{Species: "Rabbit", Name: "Jiro", Age: 1}, }, } if err := tpl.Execute(w, data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
func handler(w http.ResponseWriter, r *http.Request) { funcMap := template.FuncMap{ "Greeting": func(s string) string { return "Hello " + s }, } tpl, err := ace.Load("example", "", &ace.Options{ FuncMap: funcMap, }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := tpl.Execute(w, nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
func handler(w http.ResponseWriter, r *http.Request) { tpl, err := ace.Load("example", "", nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } data := map[string]interface{}{ "Title": "Actions", "Msgs": []string{ "Message1", "Message2", "Message3", }, } if err := tpl.Execute(w, data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
func main() { initDb() mux := gmux.NewRouter() mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { var p LoginPage if r.FormValue("register") != "" { secret, _ := bcrypt.GenerateFromPassword([]byte(r.FormValue("password")), bcrypt.DefaultCost) user := User{r.FormValue("username"), secret} if err := dbmap.Insert(&user); err != nil { p.Error = err.Error() } else { sessions.GetSession(r).Set("User", user.Username) http.Redirect(w, r, "/", http.StatusFound) return } } else if r.FormValue("login") != "" { user, err := dbmap.Get(User{}, r.FormValue("username")) if err != nil { p.Error = err.Error() } else if user == nil { p.Error = "No such user found with Username: "******"username") } else { u := user.(*User) if err = bcrypt.CompareHashAndPassword(u.Secret, []byte(r.FormValue("password"))); err != nil { p.Error = err.Error() } else { sessions.GetSession(r).Set("User", u.Username) http.Redirect(w, r, "/", http.StatusFound) return } } } template, err := ace.Load("templates/login", "", nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err = template.Execute(w, p); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }) mux.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) { sessions.GetSession(r).Set("User", nil) sessions.GetSession(r).Set("Filter", nil) http.Redirect(w, r, "/login", http.StatusFound) }) mux.HandleFunc("/books", func(w http.ResponseWriter, r *http.Request) { var b []Book if !getBookCollection(&b, getStringFromSession(r, "SortBy"), r.FormValue("filter"), getStringFromSession(r, "User"), w) { return } sessions.GetSession(r).Set("Filter", r.FormValue("filter")) if err := json.NewEncoder(w).Encode(b); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }).Methods("GET").Queries("filter", "{filter:all|fiction|nonfiction}") mux.HandleFunc("/books", func(w http.ResponseWriter, r *http.Request) { var b []Book if !getBookCollection(&b, r.FormValue("sortBy"), getStringFromSession(r, "Filter"), getStringFromSession(r, "User"), w) { return } sessions.GetSession(r).Set("SortBy", r.FormValue("sortBy")) if err := json.NewEncoder(w).Encode(b); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }).Methods("GET").Queries("sortBy", "{sortBy:title|author|classification}") mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { template, err := ace.Load("templates/index", "", nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } p := Page{Books: []Book{}, Filter: getStringFromSession(r, "Filter"), User: getStringFromSession(r, "User")} if !getBookCollection(&p.Books, getStringFromSession(r, "SortBy"), getStringFromSession(r, "Filter"), p.User, w) { return } if err = template.Execute(w, p); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }).Methods("GET") mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) { var results []SearchResult var err error if results, err = search(r.FormValue("search")); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } encoder := json.NewEncoder(w) if err := encoder.Encode(results); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }).Methods("POST") mux.HandleFunc("/books", func(w http.ResponseWriter, r *http.Request) { var book ClassifyBookResponse var err error if book, err = find(r.FormValue("id")); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } b := Book{ PK: -1, Title: book.BookData.Title, Author: book.BookData.Author, Classification: book.Classification.MostPopular, ID: r.FormValue("id"), User: getStringFromSession(r, "User"), } if err = dbmap.Insert(&b); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := json.NewEncoder(w).Encode(b); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }).Methods("PUT") mux.HandleFunc("/books/{pk}", func(w http.ResponseWriter, r *http.Request) { pk, _ := strconv.ParseInt(gmux.Vars(r)["pk"], 10, 64) var b Book q := "select * from books where pk=" + dbmap.Dialect.BindVar(0) + " and \"user\"=" + dbmap.Dialect.BindVar(1) if err := dbmap.SelectOne(&b, q, pk, getStringFromSession(r, "User")); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } if _, err := dbmap.Delete(&b); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) }).Methods("DELETE") n := negroni.Classic() n.Use(sessions.Sessions("go-for-web-dev", cookiestore.New([]byte("my-secret-123")))) n.Use(negroni.HandlerFunc(verifyDatabase)) n.Use(negroni.HandlerFunc(verifyUser)) n.UseHandler(mux) port := os.Getenv("PORT") if port == "" { port = "8080" } n.Run(":" + port) }