func (h *RestQueueHandler) Put(ctx *web.Context, val string) { queue := redisq.NewRedisQueue(h.redis, val) if !queue.Exists() { h.logger.Printf("Queue [%s] didn't existst, will be ceated.", val) } if mesg, ok := ctx.Params["value"]; ok { var i interface{} err := json.Unmarshal([]byte(mesg), &i) if err != nil { writeError(ctx, 400, JsonDecodeError) return } err = queue.Put(i) if err != nil { writeError(ctx, 500, PostError) if Settings.Debug { debug := fmt.Sprintf("Debug: %s", err) ctx.WriteString(debug) } h.logger.Printf("Put message into [%s] Error:%s", val, err) return } h.logger.Printf("Put message into queue [%s]", val) } else { writeError(ctx, 400, LackPostValue) } }
func adminPost(ctx *web.Context) { level := ctx.Params["godlevel"] godlevel := godHash(level) if ctx.Params["what"] == "login" { if godlevel == admin_pass { ctx.SetSecureCookie("godlevel", level, 3600) ctx.Redirect(301, "/admin") return } ctx.SetSecureCookie("godlevel", "fefe", 3600) ctx.Redirect(301, "/") return } if !checkGodLevel(ctx) { ctx.SetSecureCookie("godlevel", "fefe", 3600) ctx.Redirect(301, "/") return } if ctx.Params["what"] == "post" { err := createNewPost(ctx.Params["content"]) if err != nil { ctx.WriteString("couldn't post: " + err.Error()) ctx.WriteString("<br><br><A href='/'>Index</a>") return } ctx.WriteString(successpage) return } }
func (c *Controller) Publish(ctx *web.Context) { if !c.sessionManager.LoggedIn(ctx) { ctx.Redirect(303, "/login") return } ctx.WriteString(c.Page("publish.html")) }
func (h *RestQueueHandler) Get(ctx *web.Context, val string) { queue := redisq.NewRedisQueue(h.redis, val) if !queue.Exists() { writeError(ctx, 404, QueueNotFound) return } if queue.Empty() { writeError(ctx, 400, EmptyQueue) return } mesg, err := queue.GetNoWait() if err != nil { writeError(ctx, 500, GetError) if Settings.Debug { debug := fmt.Sprintf("Debug: %s", err) ctx.WriteString(debug) } h.logger.Printf("Dequeue from <%s> Error:%s", val, err) return } resp, _ := json.Marshal(mesg) //mesg.(type) is iteface{} //resp.(type) is []byte ctx.SetHeader("Content-Type", "application/json; charset=UTF-8", true) ctx.WriteString(string(resp)) }
func Render(ctx *web.Context, tmpl string, config *Config, name string, data interface{}) { tmpl = filepath.Join(config.Get("datadir"), tmpl) ctx.WriteString(mustache.RenderFile(tmpl, map[string]interface{}{ "config": config, name: data})) }
func (h *RestQueueHandler) List(ctx *web.Context) { var keys []string keys, _ = h.redis.Keys("*") resp, _ := json.Marshal(keys) ctx.SetHeader("Content-Type", "application/json; charset=UTF-8", true) ctx.WriteString(string(resp)) }
func Sendstatic(ctx *web.Context, val string) { file, _err := ioutil.ReadFile("static/" + val) if _err != nil { return } filetype := strings.Split(val, ".") ctx.ContentType(filetype[len(filetype)-1]) ctx.WriteString(string(file)) }
func hello(ctx *web.Context, num string) { flusher, _ := ctx.ResponseWriter.(http.Flusher) flusher.Flush() n, _ := strconv.ParseInt(num, 10, 64) for i := int64(0); i < n; i++ { ctx.WriteString("<br>hello world</br>") flusher.Flush() time.Sleep(1e9) } }
func uploader(ctx *web.Context, key string) { updateRemoteAddr(ctx) if _, ok := uploadRequests[key]; ok { // key exists ctx.Forbidden() return } wait := make(chan string) defer delete(uploadRequests, key) uploadRequests[key] = uploadRequest{ctx.Request, wait} var result string select { case result = <-wait: case <-time.After(WAIT_CLIENT_TIMEOUT): result = "wait client timeout" } if result == "connected" { // wait actual result result = <-wait } var body string if xrw := ctx.Request.Header.Get("X-Requested-With"); xrw == "XMLHttpRequest" { body = "{\"result\": \"" + result + "\"}\n" ctx.SetHeader("Content-Type", "application/json", true) } else { tmpl, err := template.New("uploader").Parse(uploadTemplate) if err != nil { panic(err) } var buf bytes.Buffer tmpl.Execute(&buf, UploadTemplateValue{result}) body = buf.String() } if result == "ok" { ctx.WriteString(body) } else { ctx.Abort(500, body) ctx.Request.Body.Close() } }
func handleFilter(ctx *web.Context, val string) { order_id, err := strconv.ParseInt(ctx.Params["id"], 10, 64) if err != nil { // handle error } facts := getFactsFromRedis(order_id) response, err := json.Marshal(facts) if err != nil { // handle error } ctx.WriteString(string(response[:])) }
func (h *RestQueueHandler) Clear(ctx *web.Context, val string) { queue := redisq.NewRedisQueue(h.redis, val) if !queue.Exists() { writeError(ctx, 404, QueueNotFound) return } err := queue.Clear() if err != nil { writeError(ctx, 500, ClearError) if Settings.Debug { debug := fmt.Sprintf("Debug: %s", err) ctx.WriteString(debug) } h.logger.Printf("Delete queue [%s] Error:%s", val, err) return } h.logger.Printf("Queue [%s] deleted sucess", val) }
func (c *Controller) LoginPost(ctx *web.Context) { user := ctx.Params["user"] pass := ctx.Params["pass"] dest := "/login" if c.sessionManager.Login(ctx, user, pass) { ctx.WriteString(c.themeEngine.Theme("You are logged in")) dest = "/publish" } else { ctx.WriteString(c.themeEngine.Theme("Incorrect Username/Password")) } ctx.WriteString("<meta HTTP-EQUIV=\"REFRESH\" content=\"2; url=" + dest + "\">") }
func editPost(ctx *web.Context) { if !checkGodLevel(ctx) { ctx.Redirect(301, "/") return } Db := DBGet() defer Db.Close() id, _ := strconv.ParseInt(ctx.Params["postid"], 10, 64) post, err := Db.GetPost(id) if err != nil { ctx.WriteString("couldn't load post with given id!") return } post.Content = ctx.Params["content"] _, err = Db.StorePost(&post) if err != nil { ctx.WriteString("couldn't store post!") return } ctx.WriteString(successpage) }
func index(webContext *web.Context) { webContext.WriteString("Powered by <a href=\"http://www.github.com/Altonymous/forum/\">Altonymous forum!</a>") }
func hello(ctx *web.Context, val string) { ctx.WriteString("hello " + val) }
func logName(ctx *web.Context, val string) { name := ctx.Params["name"] fmt.Println(name) ctx.WriteString("Hi, " + name) }
func writeError(ctx *web.Context, statusCode int, errorMesg string) { ctx.ResponseWriter.WriteHeader(statusCode) ctx.WriteString(errorMesg) ctx.WriteString("\r\n") }