示例#1
0
func mpGetHandler(req *web.Request) {
	mpTempl.Execute(
		req.Respond(web.StatusOK, web.HeaderContentType, "text/html"),
		map[string]interface{}{
			"xsrf": req.Param.Get(web.XSRFParamName),
		})
}
示例#2
0
文件: main.go 项目: adamsxu/twister
// 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)
}
示例#3
0
文件: main.go 项目: ngerakines/gobook
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")

}
示例#4
0
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,
			},
		})
}
示例#5
0
文件: cache.go 项目: BradMuller/lwb
// 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."))
	}
}
示例#6
0
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)
}
示例#7
0
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)
	}
}
示例#8
0
文件: main.go 项目: ngerakines/orders
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")
}
示例#9
0
文件: main.go 项目: toddcblank/gobook
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))
}
示例#10
0
文件: main.go 项目: ngerakines/gobook
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"}))
}
示例#11
0
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"))
}
示例#12
0
文件: main.go 项目: toddcblank/gobook
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"}))
}
示例#13
0
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"))
}
示例#14
0
文件: main.go 项目: ngerakines/orders
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))
}
示例#15
0
文件: main.go 项目: ngerakines/gobook
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))
}
示例#16
0
文件: main.go 项目: ngerakines/orders
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")
}
示例#17
0
文件: final.go 项目: qial/twister
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)
	}
}
示例#18
0
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)
	}
}
示例#19
0
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)
	}
}
示例#20
0
文件: final.go 项目: repos-go/twister
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)
	}
}
示例#21
0
文件: main.go 项目: ngerakines/orders
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))
}
示例#22
0
文件: main.go 项目: ngerakines/gobook
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))
}
示例#23
0
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)
	}
}
示例#24
0
文件: main.go 项目: ngerakines/orders
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))
}
示例#25
0
文件: cache.go 项目: BradMuller/lwb
// 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)
}
示例#26
0
文件: main.go 项目: ngerakines/orders
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))
}
示例#27
0
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,
	})
}
示例#28
0
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)
	}
}
示例#29
0
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!"))
	}
}
示例#30
0
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)
	}
}