Initial Codebase (untested)

This commit is contained in:
James Mills
2021-01-30 14:05:04 +10:00
parent c1dc91b7e0
commit 4529ea3196
60 changed files with 9807 additions and 0 deletions

100
internal/handlers.go Normal file
View File

@@ -0,0 +1,100 @@
package internal
import (
"errors"
"fmt"
"html/template"
"net/http"
"strings"
rice "github.com/GeertJohan/go.rice"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/james4k/fmatter"
"github.com/julienschmidt/httprouter"
log "github.com/sirupsen/logrus"
)
const (
MediaResolution = 720 // 720x576
AvatarResolution = 360 // 360x360
AsyncTaskLimit = 5
MaxFailedLogins = 3 // By default 3 failed login attempts per 5 minutes
)
var (
ErrFeedImposter = errors.New("error: imposter detected, you do not own this feed")
)
func (s *Server) NotFoundHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Accept") == "application/json" {
w.Header().Set("Content-Type", "application/json")
http.Error(w, "Endpoint Not Found", http.StatusNotFound)
return
}
ctx := NewContext(s.config, s.db, r)
ctx.Title = "Page Not Found"
w.WriteHeader(http.StatusNotFound)
s.render("404", w, ctx)
}
type FrontMatter struct {
Title string
}
// PageHandler ...
func (s *Server) PageHandler(name string) httprouter.Handle {
box := rice.MustFindBox("pages")
mdTpl := box.MustString(fmt.Sprintf("%s.md", name))
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ctx := NewContext(s.config, s.db, r)
md, err := RenderString(mdTpl, ctx)
if err != nil {
log.WithError(err).Errorf("error rendering page %s", name)
ctx.Error = true
ctx.Message = "Error loading help page! Please contact support."
s.render("error", w, ctx)
return
}
var frontmatter FrontMatter
content, err := fmatter.Read([]byte(md), &frontmatter)
if err != nil {
log.WithError(err).Error("error parsing front matter")
ctx.Error = true
ctx.Message = "Error loading page! Please contact support."
s.render("error", w, ctx)
return
}
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
p := parser.NewWithExtensions(extensions)
htmlFlags := html.CommonFlags
opts := html.RendererOptions{
Flags: htmlFlags,
Generator: "",
}
renderer := html.NewRenderer(opts)
html := markdown.ToHTML(content, p, renderer)
var title string
if frontmatter.Title != "" {
title = frontmatter.Title
} else {
title = strings.Title(name)
}
ctx.Title = title
ctx.Page = name
ctx.Content = template.HTML(html)
s.render("page", w, ctx)
}
}