func main() { render := render.New(render.Options{ Directory: "src/views", Extensions: []string{".html"}, }) http.HandleFunc("/img/", serveResource) http.HandleFunc("/css/", serveResource) http.HandleFunc("/js/", serveResource) goji.Get("/hello/:name", func(c web.C, w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"]) }) goji.Get("/wow", func(c web.C, w http.ResponseWriter, r *http.Request) { render.HTML(w, http.StatusOK, "index", nil) }) goji.Get("/bar", func(c web.C, w http.ResponseWriter, r *http.Request) { render.HTML(w, http.StatusOK, "bar", nil) }) goji.Get("/", func(c web.C, w http.ResponseWriter, r *http.Request) { render.HTML(w, http.StatusOK, "index", nil) }) goji.Serve() }
func ShowItemsPages(w http.ResponseWriter, r *http.Request, i int) { // Loop through rows using only one struct items := []Item{} err := db.Select(&items, "SELECT * FROM items ORDER BY id ASC") if err != nil { log.Println("Is this the problem?") log.Println(err) return } max := len(items) maxparam := max - 3 factor := i - 1 one, two, three := factor*3, (factor*3)+1, (factor*3)+2 log.Println("%d\n %d\n %d\n", one, two, three) log.Println(max) render := render.New(render.Options{ IndentJSON: true, }) if one >= maxparam { if max%3 == 0 { render.HTML(w, http.StatusOK, "home", map[string]interface{}{ "item0": items[one], "item1": items[two], "item2": items[three], }) } else if max%3 == 2 { render.HTML(w, http.StatusOK, "home", map[string]interface{}{ "item0": items[one], "item1": items[two], }) } else if max%3 == 1 { render.HTML(w, http.StatusOK, "home", map[string]interface{}{ "item0": items[one], }) } } else if one < maxparam { render.HTML(w, http.StatusOK, "home", map[string]interface{}{ "item0": items[one], "item1": items[two], "item2": items[three], }) } else { http.Redirect(w, r, "/", http.StatusFound) } }
func Checkout(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { stripe.SetKey("sk_test_s7zyOcXwo4E9YLlBXpL4Ie44") r.ParseMultipartForm(5) sess, err := globalSessions.SessionStart(w, r) defer sess.SessionRelease(w) user := sess.Get("email") cartcookie := sess.Get("cart") badstr := "There was an error with your purchase." cart := GetCart(cartcookie) total := CartTotal(cart) totalcent := total * 100 log.Println(totalcent) totalcentint64 := int64(totalcent) name := r.PostFormValue("nameoncard") cardnum := r.PostFormValue("cardnumber") expmonth := r.PostFormValue("expirationmonth") expyear := r.PostFormValue("expirationyear") if user != nil && err == nil { err = sess.Delete("cart") expmonthnum, _ := strconv.ParseInt(expmonth, 0, 64) expyearnum, _ := strconv.ParseInt(expyear, 0, 64) expyearnumint := int(expyearnum) expmonthnumint := int(expmonthnum) log.Println(totalcentint64) params := stripe.ChargeParams{ Desc: "Cart", Amount: totalcentint64, Currency: "usd", Card: &stripe.CardParams{ Name: name, Number: cardnum, ExpYear: expyearnumint, ExpMonth: expmonthnumint, }, } log.Println(params) charge, err := stripe.Charges.Create(¶ms) log.Println(charge) log.Println(err) if err == nil { render := render.New(render.Options{}) render.HTML(w, http.StatusOK, "checkout", nil) } else { BadPage(w, r, badstr) } } else { BadPage(w, r, badstr) } }
func Search(w http.ResponseWriter, r *http.Request, i int) { usersearch := r.FormValue("usersearch") itemsearch := []Item{} err := db.Select(&itemsearch, "SELECT * FROM items ORDER BY id ASC") if err != nil { log.Println("Is this the problem?") log.Println(err) return } compared, results := Compare(itemsearch, usersearch) log.Println("%#v\n", results) max := len(results) factor := i - 1 one, two, three := factor*3, (factor*3)+1, (factor*3)+2 if compared && max < 4 { render := render.New(render.Options{}) if max == 3 { render.HTML(w, http.StatusOK, "results", map[string]interface{}{ "result0": results[one], "result1": results[two], "result2": results[three], }) } else if max == 2 { render.HTML(w, http.StatusOK, "results", map[string]interface{}{ "result0": results[one], "result1": results[two], }) } else if max == 1 { render.HTML(w, http.StatusOK, "results", map[string]interface{}{ "result0": results[one], }) } } else { http.Redirect(w, r, "/noresults", http.StatusFound) } }
func RenderCart(w http.ResponseWriter, r *http.Request, itemsincart []Item) { max := len(itemsincart) render := render.New(render.Options{}) if max == 5 { total := CartTotal(itemsincart) render.HTML(w, http.StatusOK, "cart", map[string]interface{}{ "total": total, "item0": itemsincart[0], "item1": itemsincart[1], "item2": itemsincart[2], "item3": itemsincart[3], "item4": itemsincart[4], }) } else if max == 4 { total := CartTotal(itemsincart) render.HTML(w, http.StatusOK, "cart", map[string]interface{}{ "total": total, "item0": itemsincart[0], "item1": itemsincart[1], "item2": itemsincart[2], "item3": itemsincart[3], }) } else if max == 3 { total := CartTotal(itemsincart) render.HTML(w, http.StatusOK, "cart", map[string]interface{}{ "total": total, "item0": itemsincart[0], "item1": itemsincart[1], "item2": itemsincart[2], }) } else if max == 2 { total := CartTotal(itemsincart) render.HTML(w, http.StatusOK, "cart", map[string]interface{}{ "total": total, "item0": itemsincart[0], "item1": itemsincart[1], }) } else if max == 1 { total := CartTotal(itemsincart) render.HTML(w, http.StatusOK, "cart", map[string]interface{}{ "total": total, "item0": itemsincart[0], }) } else if max == 0 { SimplePage(w, r, "cart") } else if max > 5 { total := CartTotal(itemsincart) maxitems := true render.HTML(w, http.StatusOK, "cart", map[string]interface{}{ "total": total, "max": maxitems, "item0": itemsincart[0], "item1": itemsincart[1], "item2": itemsincart[2], "item3": itemsincart[3], "item4": itemsincart[4], }) } }
func ShowItemid(w http.ResponseWriter, r *http.Request, s string) { fmt.Println(s) itemid := Item{} err := db.Get(&itemid, "SELECT * FROM items WHERE id=$1", s) if err != nil { log.Print("This must be the problem") } if itemid.Id == 0 { http.Redirect(w, r, "/", 302) } render := render.New(render.Options{ IndentJSON: true, }) render.HTML(w, http.StatusOK, "item", &itemid) }
func Buy(w http.ResponseWriter, r *http.Request) { sess, err := globalSessions.SessionStart(w, r) defer sess.SessionRelease(w) user := sess.Get("email") cart := sess.Get("cart") itemsincart := GetCart(cart) total := CartTotal(itemsincart) badstr := "you must be signed in to access this page" if user != nil && err == nil { render := render.New(render.Options{}) render.HTML(w, http.StatusOK, "buy", total) } else { BadPage(w, r, badstr) } }
func ShowItemsHome(w http.ResponseWriter, r *http.Request) { // Loop through rows using only one struct items := []Item{} err := db.Select(&items, "SELECT * FROM items ORDER BY id ASC") if err != nil { log.Println("Is this the problem?") log.Println(err) return } log.Println("%#v\n", items) render := render.New(render.Options{ IndentJSON: true, }) render.HTML(w, http.StatusOK, "home", map[string]interface{}{ "item0": items[0], "item1": items[1], "item2": items[2], }) }
func BadPage(w http.ResponseWriter, r *http.Request, str string) { render := render.New(render.Options{}) render.HTML(w, http.StatusOK, "badpage", str) }