func mpGetHandler(req *web.Request) { mpTempl.Execute( req.Respond(web.StatusOK, web.HeaderContentType, "text/html"), map[string]interface{}{ "xsrf": req.Param.Get(web.XSRFParamName), }) }
// home handles requests to the home page. func home(req *web.Request) { token, err := credentials(req, "tok") if err != nil { homeLoggedOut(req) return } param := make(web.Values) url := "http://api.twitter.com/1/statuses/home_timeline.json" oauthClient.SignParam(token, "GET", url, param) url = url + "?" + param.FormEncodedString() resp, err := http.Get(url) if err != nil { req.Error(web.StatusInternalServerError, err) return } defer resp.Body.Close() if resp.StatusCode != 200 { req.Error(web.StatusInternalServerError, os.NewError(fmt.Sprint("Status ", resp.StatusCode))) return } var d interface{} err = json.NewDecoder(resp.Body).Decode(&d) if err != nil { req.Error(web.StatusInternalServerError, err) return } homeTempl.Execute(req.Respond(web.StatusOK, web.HeaderContentType, web.ContentTypeHTML), d) }
func migrate(req *web.Request) { err := db.Query("select id, message, date from entries") if err != nil { panic(err) } result, err := db.StoreResult() if err != nil { panic(err) } itemIndex := make(map[string]Item, result.RowCount()) for _, entry_row := range result.FetchRows() { id := string([]uint8(entry_row[0].([]uint8))) message := string([]uint8(entry_row[1].([]uint8))) when := entry_row[2].(int64) itemIndex[id] = Item{message, "", make([]string, 0), bson.MongoTimestamp(when)} } db.FreeResult() for id, item := range itemIndex { tags := getTags(id) // log.Println(id, tags) // item.Tags = tags // itemIndex[id] = item createItem(item.Message, "", tags, int64(item.When)) } log.Println(itemIndex) w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") io.WriteString(w, "OK") }
func mpPostHandler(req *web.Request) { parts, err := web.ParseMultipartForm(req, -1) var ( filename, contentType string contentParam map[string]string size int ) if len(parts) > 0 { filename = parts[0].Filename contentType = parts[0].ContentType contentParam = parts[0].ContentParam size = len(parts[0].Data) } mpTempl.Execute( req.Respond(web.StatusOK, web.HeaderContentType, "text/html"), map[string]interface{}{ "xsrf": req.Param.Get(web.XSRFParamName), "result": map[string]interface{}{ "err": err, "hello": req.Param.Get("hello"), "foo": req.Param.Get("foo"), "filename": filename, "contentType": contentType, "contentParam": contentParam, "size": size, }, }) }
// Run simply runs the generator. func (c *DummyCache) Run(req *web.Request, fnGenerate func(w io.Writer) bool) { var buf = &bytes.Buffer{} if fnGenerate(buf) { req.Respond(web.StatusOK, web.HeaderContentType, "text/html").Write(buf.Bytes()) } else { req.Error(web.StatusNotFound, os.NewError("Not Found.")) } }
func ServeWeb(req *web.Request) { b, err := json.MarshalIndent(vars, "", " ") if err != nil { req.Error(web.StatusInternalServerError, err) return } req.Respond(web.StatusOK, web.HeaderContentType, "application/json; charset=utf-8").Write(b) }
func ServeHTTP(req *web.Request) { s := req.Param.GetDef("txt", "") ni, _ := strconv.Atoi(req.Param.GetDef("num", "")) wr := req.Respond(web.StatusOK) for i := 0; i < ni; i++ { fmt.Fprintf(wr, "%d: %s\n", i, s) } }
func createOrderHandler(req *web.Request) { name := req.Param.Get("name") companyId := req.Param.Get("company") id := NewUUID() storeOrder(id.String(), name, companyId) w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") io.WriteString(w, "ok") }
func displayArchive(req *web.Request) { entries := getEntries() entryGroups := flattenEntryGroups(groupEntries(entries)) w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") params := make(map[string]interface{}) params["entry_groups"] = entryGroups io.WriteString(w, RenderFile("templates/archive.html", params)) }
func createEntry(req *web.Request) { message := req.Param.Get("message") extra := req.Param.Get("extra") tags := splitTags(extra) when := getTime(tags) createItem(message, extra, tags, when) w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") io.WriteString(w, RenderFile("templates/index.html", map[string]string{"c": "world"})) }
func coreHandler(req *web.Request) { coreTempl.Execute(map[string]interface{}{ "req": req, "status": web.StatusOK, "message": "ok", "xsrf": req.Param.GetDef(web.XSRFParamName, ""), }, req.Respond(web.StatusOK, web.HeaderContentType, "text/html")) }
func createEntry(req *web.Request) { message := req.Param.Get("message") extra := req.Param.Get("extra") tags := splitTags(extra) id := NewUUID() storeEntry(id, message, tags) w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") io.WriteString(w, RenderFile("templates/index.html", map[string]string{"c": "world"})) }
func coreErrorHandler(req *web.Request, status int, message string) { coreTempl.Execute(map[string]interface{}{ "req": req, "status": status, "message": message, "xsrf": req.Param.GetDef(web.XSRFParamName, ""), }, req.Respond(status, web.HeaderContentType, "text/html")) }
func createOrderFormHandler(req *web.Request) { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") companies := listCompanies() params := make(map[string]interface{}) params["Companies"] = companies io.WriteString(w, RenderFile("templates/order-create.html", params)) }
func displayArchive(req *web.Request) { result := getItems(nil, 250) groupedItems := groupItems(result) flatItemGroups := flattenItemGroups(groupedItems) w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") params := make(map[string]interface{}) params["entry_groups"] = flatItemGroups io.WriteString(w, RenderFile("templates/archive.html", params)) }
func createCompanyHandler(req *web.Request) { name := req.Param.Get("name") address := req.Param.Get("address") phone := req.Param.Get("phone") url := req.Param.Get("url") id := NewUUID() storeCompany(id.String(), name, address, phone, url) w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") io.WriteString(w, "ok") }
func renderTemplate(req *web.Request, tmpl string, p *page) { err := templates[tmpl].Execute( req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset='utf-8'"), map[string]interface{}{ "page": p, "xsrf": req.Param.Get("xsrf"), }) if err != nil { log.Println("error rendering", tmpl, err) } }
func multipartHandler(req *web.Request) { files, err := web.ParseMultipartForm(req, -1) if err != nil { req.Error(web.StatusBadRequest, err) return } w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") if err := templates.ExecuteTemplate(w, "home.html", map[string]interface{}{"req": req, "files": files}); err != nil { log.Print(err) } }
func renderRssTemplate(req *web.Request, status int, tmpl string, results []*NewsItem) { fmt.Println(tmpl, "rendering") err := templates[tmpl].Execute( req.Respond(status, web.HeaderContentType, "application/rss+xml"), map[string]interface{}{ "results": results, }) if err != nil { log.Println("error rendering", tmpl, err) } }
func renderTemplate(req *web.Request, tmpl string, p *page) { err := templates[tmpl].Execute( req.Respond(web.StatusOK), map[string]interface{}{ "page": p, "xsrf": req.Param.Get("xsrf"), }) if err != nil { log.Println("error rendering", tmpl, err) } }
func indexHandler(req *web.Request) { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") orders := listOrders() params := make(map[string]interface{}) params["Orders"] = orders if len(orders) > 0 { params["LastOrder"] = orders[0] } io.WriteString(w, RenderFile("templates/index.html", params)) }
func displayTag(req *web.Request) { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") params := make(map[string]interface{}) if tag, ok := req.URLParam["tag"]; ok { result := getItems(bson.M{"tags": bson.M{"$in": []string{tag}}}, 1000) groupedItems := groupItems(result) params["tag"] = tag params["entry_groups"] = flattenItemGroups(groupedItems) } io.WriteString(w, RenderFile("templates/tag.html", params)) }
func renderEditTemplate(req *web.Request, tmpl string, n *NewsItem) { err := templates[tmpl].Execute( req.Respond(web.StatusOK), map[string]interface{}{ "item": n, "goversion": goVersion, "xsrf": req.Param.Get("xsrf"), }) if err != nil { log.Println("error rendering", tmpl, err) } }
func viewCompanyHandler(req *web.Request) { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") id := req.Param.Get("id") company, err := getCompany(id) if err != nil { io.WriteString(w, "ERROR: "+err.String()) return } params := make(map[string]interface{}) params["Company"] = company io.WriteString(w, RenderFile("templates/company-view.html", params)) }
// Run looks up the page in the cache and generates it if it does not exist, // placing it in the cache afterwards. func (c *Cache) Run(req *web.Request, fnGenerate func(w io.Writer) bool) { cached, found := c.items[req.URL.String()] if !found { var buf = &bytes.Buffer{} if !fnGenerate(buf) { req.Error(web.StatusNotFound, os.NewError("Not Found.")) return } cached = buf.Bytes() c.items[req.URL.String()] = cached } req.Respond(web.StatusOK, web.HeaderContentType, "text/html").Write(cached) }
func viewOrderHandler(req *web.Request) { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=\"utf-8\"") id := req.Param.Get("id") order, err := getOrder(id) if err != nil { io.WriteString(w, "ERROR: "+err.String()) return } params := make(map[string]interface{}) params["Order"] = order customers := getCustomers() params["Customers"] = customers io.WriteString(w, RenderFile("templates/order-view.html", params)) }
func statusHandler(req *web.Request) { w := req.Respond(web.StatusOK, web.HeaderContentType, "application/json; charset=utf-8", "Access-Control-Allow-Origin", "*") message := "No job with that ID found." done := true if v, err := cache.Get(req.URLParam["id"]); err == nil { message = v done = doneRegex.MatchString(message) } encoder := json.NewEncoder(w) encoder.Encode(JSON{ "message": message, "done": done, }) }
func handleMainPage(r *web.Request) { c := gae.Context(r) q := datastore.NewQuery("Greeting").Order("-Date").Limit(10) var gg []*Greeting _, err := q.GetAll(c, &gg) if err != nil { r.Error(web.StatusInternalServerError, err) return } w := r.Respond(200, "Content-Type", "text/html") if err := mainPage.Execute(w, map[string]interface{}{ "xsrf": r.Param.Get("xsrf"), "gg": gg}); err != nil { c.Logf("%v", err) } }
func coreHandler(req *web.Request) { if req.Param.Get("panic") == "before" { panic(os.NewError("Panic Attack!")) } coreTempl.Execute( req.Respond(web.StatusOK, web.HeaderContentType, "text/html"), map[string]interface{}{ "req": req, "status": web.StatusOK, "message": "ok", "xsrf": req.Param.Get(web.XSRFParamName), }) if req.Param.Get("panic") == "after" { panic(os.NewError("Panic Attack!")) } }
func renderListTemplate(req *web.Request, status int, tmpl string, results []*NewsItem, items []*NewsItem) { externals, _ := loadNewsItems(bson.M{"newscategory": "resources"}, "externals") err := templates[tmpl].Execute( req.Respond(status), map[string]interface{}{ "results": results, "newsItems": items, "externals": externals, "tags": tagList(), "goversion": goVersion, "xsrf": req.Param.Get("xsrf"), }) if err != nil { log.Println("error rendering", tmpl, err) } }