Пример #1
0
func editHandler(w http.ResponseWriter, r *http.Request) {
	title := remux.Vars(r)[0]
	p, err := loadPage(title)
	if err != nil {
		p = &Page{Title: title}
	}
	renderTemplate(w, "edit.html", p)
}
Пример #2
0
func viewHandler(w http.ResponseWriter, r *http.Request) {
	title := remux.Vars(r)[0]
	p, err := loadPage(title)
	if err != nil {
		http.Redirect(w, r, "/edit/"+title, http.StatusFound)
		return
	}
	renderTemplate(w, "view.html", p)
}
Пример #3
0
func saveHandler(w http.ResponseWriter, r *http.Request) {
	title := remux.Vars(r)[0]
	body := r.FormValue("body")
	p := &Page{Title: title, Body: []byte(body)}
	err := p.save()
	if err != nil {
		log.Println(err.Error())
		http.Error(w, http.StatusText(500), 500)
		return
	}
	http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
Пример #4
0
func CometQueueHandler(w http.ResponseWriter, r *http.Request) {
	vars := remux.Vars(r)
	conn, buf, err := sse.ServeEvents(w)
	if err != nil {
		fmt.Println(err)
		http.Error(w, http.StatusText(500), 500)
		return
	}
	defer conn.Close()
	cs := queues.Join(vars[0])
	for s := range cs {
		err = sse.SendEvent(buf, &sse.MessageEvent{Data: s})
		if err != nil {
			break
		}
	}
	queues.Part(vars[0], cs)
}
Пример #5
0
func QueueHandler(w http.ResponseWriter, r *http.Request) {
	queue := remux.Vars(r)[0]
	switch r.Method {
	case "GET":
		var soft bool
		if r.FormValue("soft") == "" {
			soft = false
		} else {
			soft = true
		}
		item, err := rmq.Get(queue, soft)
		if err != nil {
			http.Error(w, http.StatusText(503), 503)
			return
		}
		s := item.String()
		if s == "" {
			http.Error(w, "Empty queue", 404)
			return
		}
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprintf(w, s)
	case "POST":
		v := r.FormValue("value")
		if v == "" {
			http.Error(w, "Empty value", 400)
			return
		}
		item, err := rmq.Add(queue, v)
		if err != nil {
			http.Error(w, http.StatusText(503), 503)
			return
		}
		s := item.String()
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprintf(w, s)
		queues.Say(queue, s)
	default:
		w.Header().Set("Allow", "GET, POST")
		http.Error(w, http.StatusText(405), 405)
	}
}