func HandleGuard(c *webapp.Context) { var err error action := c.Request.FormValue("action") if action == "logout" { RevokeSessionTokon() c.Redirect("/guard", http.StatusFound) return } if c.Request.Method == "POST" { cert := c.Request.FormValue("certificate") if len(cert) == 0 { c.Redirect("/guard", http.StatusFound) return } if SHA256Sum(cert) == GetConfig().Certificate { cookie := new(http.Cookie) cookie.Name = "token" cookie.Path = "/" cookie.Value = GenerateSessionToken() http.SetCookie(c.Writer, cookie) c.Redirect("/writer", http.StatusFound) } else { err = RenderGuard(c, "Your password is not correct") if err != nil { c.Error(fmt.Sprintf("%s: %s", webapp.ErrInternalServerError, err), http.StatusInternalServerError) } } } else if c.Request.Method == "GET" { err = RenderGuard(c, "") if err != nil { c.Error(fmt.Sprintf("%s: %s", webapp.ErrInternalServerError, err), http.StatusInternalServerError) } } }
func HandleArticles(c *webapp.Context) { pos, _ := strconv.Atoi(c.Request.FormValue("pos")) if TattooDB.GetArticleCount() != 0 && pos > TattooDB.GetArticleCount()-1 { if HasTemplate("HOME") { c.Redirect("/articles/", http.StatusFound) } else { c.Redirect("/", http.StatusFound) } return } err := RenderArticles(c, pos) if err != nil { c.Error(fmt.Sprintf("%s: %s", webapp.ErrInternalServerError, err), http.StatusInternalServerError) } }
func Render404page(ctx *webapp.Context, msg string) error { if notFoundTPL != nil { vars := make(map[string]interface{}) vars["Message"] = msg vars["URL"] = ctx.Request.RequestURI vars["Referer"] = ctx.Request.Referer() data := MakeData(ctx, vars) err := ctx.Execute(notFoundTPL, &data) return err } else { ctx.Error(fmt.Sprintf("%s: %s", webapp.ErrNotFound, msg), http.StatusNotFound) return nil } return nil }
func HandleSingle(c *webapp.Context, pagename string) { if TattooDB.Has(pagename) { lastMeta := GetLastCommentMetadata(c) err := RenderSinglePage(c, pagename, lastMeta) if err != nil { c.Error(fmt.Sprintf("%s: %s", webapp.ErrInternalServerError, err), http.StatusInternalServerError) } meta, err := TattooDB.GetMeta(pagename) if err == nil { meta.Hits += 1 TattooDB.UpdateMetadata(meta) } } else { Render404page(c, NOT_FOUND_MESSAGE) } }
func HandleTag(c *webapp.Context, tag string) { tag = strings.Trim(tag, " ") if !TattooDB.HasTag(tag) { Render404page(c, NOT_FOUND_MESSAGE) } pos, _ := strconv.Atoi(c.Request.FormValue("pos")) if TattooDB.GetTagArticleCount(tag) != 0 && pos > TattooDB.GetTagArticleCount(tag)-1 { c.Redirect("/", http.StatusFound) return } err := RenderTagPage(c, pos, tag) if err != nil { c.Error(fmt.Sprintf("%s: %s", webapp.ErrInternalServerError, err), http.StatusInternalServerError) } }
func HandleFeed(c *webapp.Context, pathLevels []string) { if len(pathLevels) < 2 { c.Redirect("/feed/atom", http.StatusFound) return } if pathLevels[1] == "atom" { var meta *ArticleMetadata var err error if len(TattooDB.ArticleTimeline) != 0 { meta, err = TattooDB.GetMeta(TattooDB.ArticleTimeline[0]) if err == nil { TattooDB.SetVar("LastUpdatedTime", TimeRFC3339(meta.ModifiedTime)) } } err = RenderFeedAtom(c) if err != nil { c.Error(fmt.Sprintf("%s: %s", webapp.ErrInternalServerError, err), http.StatusInternalServerError) return } } }
func HandleHome(c *webapp.Context) { err := RenderHome(c) if err != nil { c.Error(fmt.Sprintf("%s: %s", webapp.ErrInternalServerError, err), http.StatusInternalServerError) } }
func HandleWriter(c *webapp.Context, pathLevels []string) { if ok := isAuthorized(c); !ok { c.Redirect("/guard", http.StatusFound) return } if c.Request.Method == "GET" { var err error if len(pathLevels) < 2 { c.Redirect("/writer/overview", http.StatusFound) return } if pathLevels[1] == "overview" { pos, _ := strconv.Atoi(c.Request.FormValue("pos")) err = RenderWriterOverview(c, pos) } else if pathLevels[1] == "pages" { pos, _ := strconv.Atoi(c.Request.FormValue("pos")) err = RenderWriterPages(c, pos) } else if pathLevels[1] == "comments" { pos, _ := strconv.Atoi(c.Request.FormValue("pos")) if pos > TattooDB.GetCommentCount()-1 { c.Redirect("/writer/comments", http.StatusFound) return } err = RenderWriterComments(c, pos) } else if pathLevels[1] == "settings" { err = RenderWriterSettings(c, "") } else if pathLevels[1] == "edit" { var article *Article = new(Article) var meta *ArticleMetadata = new(ArticleMetadata) var source []byte if len(pathLevels) >= 3 { name := strings.ToLower(url.QueryEscape(pathLevels[2])) meta, err = TattooDB.GetMeta(name) if err == nil { source, err = TattooDB.GetArticleSource(name) if err == nil { article.Metadata = *meta article.Text = template.HTML(string(source)) } } } else { article = new(Article) } err = RenderWriterEditor(c, article) } else if pathLevels[1] == "delete" { if len(pathLevels) >= 3 { name := strings.ToLower(url.QueryEscape(pathLevels[2])) if TattooDB.Has(name) { TattooDB.DeleteArticleTagIndex(name) TattooDB.DeleteArticle(name) TattooDB.DeleteMetadata(name) TattooDB.DeleteComments(name) TattooDB.Dump() TattooDB.RebuildTimeline() TattooDB.RebuildCommentTimeline() } } c.Redirect("/writer", http.StatusFound) } else if pathLevels[1] == "delete_comment" { if len(pathLevels) >= 3 { name := strings.ToLower(url.QueryEscape(pathLevels[2])) if TattooDB.HasComment(name) { TattooDB.DeleteComment(name) TattooDB.RebuildCommentTimeline() } } c.Redirect("/writer/comments", http.StatusFound) } else { Render404page(c, NOT_FOUND_MESSAGE) } if err != nil { c.Error(fmt.Sprintf("%s: %s", webapp.ErrInternalServerError, err), http.StatusInternalServerError) } } else if c.Request.Method == "POST" { if pathLevels[1] == "update" { HandleUpdateArticle(c) } else if pathLevels[1] == "settings" { HandleUpdateSystemSettings(c) } else { c.Redirect("/writer", http.StatusFound) return } } }