Example #1
0
func renameTag(req *web.Request) {
	// oldTag := req.Param.Get("oldTag")
	newTag := req.Param.Get("newTag")
	// updateTag(oldTag, newTag)
	url := fmt.Sprintf("/tag/%s", url.QueryEscape(newTag))
	req.Redirect(url, false)
}
Example #2
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,
			},
		})
}
Example #3
0
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")

}
Example #4
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),
		})
}
Example #5
0
func serveSymbol(req *web.Request) {
	var p []byte
	if req.Method == "POST" {
		var err os.Error
		p, err = req.BodyBytes(-1)
		if err != nil {
			req.Error(web.StatusInternalServerError, err)
			return
		}
	} else {
		p = []byte(req.URL.RawQuery)
	}

	w := respondText(req)
	io.WriteString(w, "num_symbols: 1\n")
	for len(p) > 0 {
		var a []byte
		if i := bytes.IndexByte(p, '+'); i >= 0 {
			a = p[:i]
			p = p[i+1:]
		} else {
			a = p
			p = nil
		}
		if pc, _ := strconv.Btoui64(string(a), 0); pc != 0 {
			if f := runtime.FuncForPC(uintptr(pc)); f != nil {
				fmt.Fprintf(w, "%#x %s\n", pc, f.Name())
			}
		}
	}
}
Example #6
0
// loginHandler redirects to Facebook OAuth2 authorization page.
func loginHandler(req *web.Request) {
	m := web.NewValues(
		"client_id", appID, // defined in settings.go
		"scope", "read_stream",
		"redirect_uri", req.URL.Scheme+"://"+req.URL.Host+"/callback")
	req.Redirect("https://graph.facebook.com/oauth/authorize?"+m.FormEncodedString(), false)
}
Example #7
0
// 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."))
	}
}
Example #8
0
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))
}
Example #9
0
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")
}
Example #10
0
func createCustomerHandler(req *web.Request) {
	orderId := req.Param.Get("order")
	name := req.Param.Get("name")
	email := req.Param.Get("email")
	id := NewUUID()
	storeCustomer(id.String(), name, email)
	req.Redirect("/order/?id="+orderId, false)
}
Example #11
0
func createEventHandler(req *web.Request) {
	orderId := req.Param.Get("order")
	name := req.Param.Get("name")
	value := req.Param.Get("value")
	id := NewUUID()
	storeEvent(id.String(), orderId, name, value)
	req.Redirect("/order/?id="+orderId, false)
}
Example #12
0
func pathHandler(req *web.Request, targetPattern string) {
	if newPath := req.Param.Get("path"); newPath == "" {
		req.Error(web.StatusNotFound, os.NewError("Not Found."))
	} else {
		newUrl := fmt.Sprintf(targetPattern, req.URL.Scheme, req.URL.Host, newPath)
		req.Redirect(newUrl, true)
	}
}
Example #13
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)
	}
}
Example #14
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)
}
Example #15
0
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"}))
}
Example #16
0
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))
}
Example #17
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"))
}
Example #18
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"))
}
Example #19
0
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"}))
}
Example #20
0
func viewHandler(req *web.Request) {
	title := req.URLParam["title"]
	p, err := loadPage(title)
	if err != nil {
		req.Redirect("/edit/"+title, false)
		return
	}
	renderTemplate(req, "view", p)
}
Example #21
0
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))
}
Example #22
0
// login redirects the user to the Twitter authorization page.
func login(req *web.Request) {
	callback := req.URL.Scheme + "://" + req.URL.Host + "/callback"
	temporaryCredentials, err := oauthClient.RequestTemporaryCredentials(http.DefaultClient, callback)
	if err != nil {
		req.Error(web.StatusInternalServerError, err)
		return
	}
	req.Redirect(oauthClient.AuthorizationURL(temporaryCredentials), false,
		web.HeaderSetCookie, credentialsCookie("tmp", temporaryCredentials, 0))
}
Example #23
0
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")
}
Example #24
0
func saveHandler(req *web.Request) {
	body := req.Param.Get("body")
	title := req.URLParam["title"]
	p := &page{Title: title, Body: []byte(body)}
	err := p.save()
	if err != nil {
		req.Error(web.StatusInternalServerError, err)
		return
	}
	req.Redirect("/view/"+title, false)
}
Example #25
0
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)
	}
}
Example #26
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)
	}
}
Example #27
0
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)
	}
}
Example #28
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)
	}
}
Example #29
0
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))
}
Example #30
0
func serveProfile(req *web.Request) {
	sec, _ := strconv.Atoi64(req.Param.Get("seconds"))
	if sec == 0 {
		sec = 30
	}
	if err := pprof.StartCPUProfile(&lazyResponder{req, nil}); err != nil {
		req.Error(web.StatusInternalServerError, err)
		return
	}
	time.Sleep(sec * 1e9)
	pprof.StopCPUProfile()
}