A pastebin for markdown pages.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.2 KiB

package main
import (
8 years ago
"bytes"
8 years ago
"html/template"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
8 years ago
"sync"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/labstack/echo"
8 years ago
"github.com/labstack/gommon/log"
)
var (
stats = &sync.Map{}
ads []byte
)
8 years ago
8 years ago
type Template struct{ templates *template.Template }
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
e := echo.New()
8 years ago
e.Logger.SetLevel(log.DEBUG)
db, err := sql.Open("sqlite3", "./database.sqlite")
if err != nil {
e.Logger.Error(err)
}
defer db.Close()
adsFName := os.Getenv("ADS")
if adsFName != "" {
var err error
ads, err = ioutil.ReadFile(adsFName)
if err != nil {
e.Logger.Errorf("couldn't read file %q: %v", adsFName, err)
}
}
e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
e.File("/favicon.ico", "assets/public/favicon.ico")
e.File("/robots.txt", "assets/public/robots.txt")
e.File("/style.css", "assets/public/style.css")
e.File("/index.html", "assets/public/index.html")
e.File("/", "assets/public/index.html")
8 years ago
go persistStats(e.Logger, db, stats)
e.GET("/TOS.md", func(c echo.Context) error {
n, code := md2html(c, "TOS")
return c.Render(code, "Page", n)
})
e.GET("/:id", func(c echo.Context) error {
n, code := load(c, db)
n.prepare()
8 years ago
views := n.Views
if val, ok := stats.Load(n.ID); ok {
intVal, ok := val.(int)
if ok {
views = intVal
}
}
defer stats.Store(n.ID, views+1)
if n.Fraud() {
n.Ads = mdTmplHTML(ads)
}
8 years ago
c.Logger().Debugf("/%q requested; response code: %d", n.ID, code)
return c.Render(code, "Note", n)
})
e.GET("/:id/export", func(c echo.Context) error {
n, code := load(c, db)
8 years ago
c.Logger().Debugf("/%q/export requested; response code: %d", n.ID, code)
return c.String(code, n.Text)
})
8 years ago
e.GET("/:id/stats", func(c echo.Context) error {
n, code := load(c, db)
n.prepare()
8 years ago
buf := bytes.NewBuffer([]byte{})
e.Renderer.Render(buf, "Stats", n, c)
8 years ago
n.Content = template.HTML(buf.String())
8 years ago
c.Logger().Debugf("/%q/stats requested; response code: %d", n.ID, code)
8 years ago
return c.Render(code, "Note", n)
})
e.GET("/:id/edit", func(c echo.Context) error {
n, code := load(c, db)
8 years ago
c.Logger().Debugf("/%q/edit requested; response code: %d", n.ID, code)
return c.Render(code, "Form", n)
})
e.GET("/new", func(c echo.Context) error {
8 years ago
c.Logger().Debug("/new requested")
return c.Render(http.StatusOK, "Form", nil)
})
e.POST("/note", func(c echo.Context) error {
8 years ago
c.Logger().Debug("POST /note requested")
vals, err := c.FormParams()
if err != nil {
return err
}
if get(vals, "tos") != "on" {
code := http.StatusPreconditionFailed
8 years ago
c.Logger().Errorf("POST /note error: %d", code)
return c.Render(code, "Note", errPage(code))
}
text := get(vals, "text")
if 10 > len(text) || len(text) > 50000 {
code := http.StatusBadRequest
8 years ago
c.Logger().Errorf("POST /note error: %d", code)
return c.Render(code, "Note",
errPage(code, "note length not accepted"))
}
id := get(vals, "id")
n := &Note{
ID: id,
Text: text,
Password: get(vals, "password"),
}
n, err = save(c, db, n)
if err != nil {
c.Logger().Error(err)
code := http.StatusServiceUnavailable
if err == errorUnathorised {
code = http.StatusUnauthorized
} else if err == errorBadRequest {
code = http.StatusBadRequest
}
8 years ago
c.Logger().Errorf("POST /note error: %d", code)
return c.Render(code, "Note", errPage(code))
}
8 years ago
c.Logger().Debugf("note %q saved", n.ID)
return c.Redirect(http.StatusMovedPermanently, "/"+n.ID)
})
e.Logger.Fatal(e.Start(":3000"))
}
func get(vals url.Values, key string) string {
if list, found := vals[key]; found {
if len(list) == 1 {
return list[0]
}
}
return ""
8 years ago
}
func md2html(c echo.Context, name string) (Note, int) {
path := "assets/markdown/" + name + ".md"
mdContent, err := ioutil.ReadFile(path)
if err != nil {
c.Logger().Errorf("couldn't open markdown page %q: %v", path, err)
code := http.StatusServiceUnavailable
return errPage(code), code
}
8 years ago
c.Logger().Debugf("rendering markdown page %q", name)
return Note{Title: name, Content: mdTmplHTML(mdContent)}, http.StatusOK
}