2017-03-12 19:37:53 +00:00
|
|
|
package board
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-13 15:04:00 +00:00
|
|
|
"errors"
|
2017-03-12 19:37:53 +00:00
|
|
|
"github.com/GeertJohan/go.rice"
|
|
|
|
"github.com/pressly/chi"
|
|
|
|
"github.com/tidwall/buntdb"
|
|
|
|
"go.rls.moe/nyx/http/errw"
|
|
|
|
"go.rls.moe/nyx/http/middle"
|
|
|
|
"go.rls.moe/nyx/resources"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var riceConf = rice.Config{
|
|
|
|
LocateOrder: []rice.LocateMethod{
|
|
|
|
rice.LocateWorkingDirectory,
|
|
|
|
rice.LocateEmbedded,
|
|
|
|
rice.LocateAppended,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var box = riceConf.MustFindBox("http/board/res/")
|
|
|
|
|
|
|
|
var (
|
2017-03-13 15:04:00 +00:00
|
|
|
tmpls = template.New("base")
|
|
|
|
//dirTmpl = template.New("board/dir")
|
|
|
|
//boardTmpl = template.New("board/board")
|
|
|
|
//threadTmpl = template.New("board/thread")
|
2017-03-12 19:37:53 +00:00
|
|
|
|
|
|
|
hdlFMap = template.FuncMap{
|
|
|
|
"renderText": resources.OperateReplyText,
|
2017-03-13 15:04:00 +00:00
|
|
|
"dict": func(values ...interface{}) (map[string]interface{}, error) {
|
|
|
|
if len(values)%2 != 0 {
|
|
|
|
return nil, errors.New("invalid dict call")
|
|
|
|
}
|
|
|
|
dict := make(map[string]interface{}, len(values)/2)
|
|
|
|
for i := 0; i < len(values); i += 2 {
|
|
|
|
key, ok := values[i].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("dict keys must be strings")
|
|
|
|
}
|
|
|
|
dict[key] = values[i+1]
|
|
|
|
}
|
|
|
|
return dict, nil
|
|
|
|
},
|
|
|
|
"rateSpam": resources.SpamScore,
|
|
|
|
"makeCaptcha": resources.MakeCaptcha,
|
2017-03-12 19:37:53 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
2017-03-13 15:04:00 +00:00
|
|
|
tmpls = tmpls.Funcs(hdlFMap)
|
|
|
|
tmpls, err = tmpls.New("thread/postlists").Parse(box.MustString("thread.tmpl.html"))
|
2017-03-12 19:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-13 15:04:00 +00:00
|
|
|
_, err = tmpls.New("board/dir").Parse(box.MustString("dir.html"))
|
2017-03-12 19:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-13 15:04:00 +00:00
|
|
|
_, err = tmpls.New("board/board").Parse(box.MustString("board.html"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = tmpls.New("board/thread").Parse(box.MustString("thread.html"))
|
2017-03-12 19:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Router(r chi.Router) {
|
|
|
|
r.Get("/", serveDir)
|
|
|
|
r.Get("/dir.html", serveDir)
|
|
|
|
r.Get("/:board/board.html", serveBoard)
|
|
|
|
r.Post("/:board/new_thread.sh", handleNewThread)
|
|
|
|
r.Get("/:board/:thread/thread.html", serveThread)
|
|
|
|
r.Get("/:board/:thread/:post/post.html", servePost)
|
|
|
|
r.Post("/:board/:thread/reply.sh", handleNewReply)
|
2017-03-13 15:04:00 +00:00
|
|
|
r.Handle("/captcha/:captchaId.png", resources.ServeCaptcha)
|
|
|
|
r.Handle("/captcha/:captchaId.wav", resources.ServeCaptcha)
|
|
|
|
r.Handle("/captcha/download/:captchaId.wav", resources.ServeCaptcha)
|
2017-03-12 19:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func servePost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveDir(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dat := bytes.NewBuffer([]byte{})
|
|
|
|
db := middle.GetDB(r)
|
|
|
|
ctx := middle.GetBaseCtx(r)
|
|
|
|
err := db.View(func(tx *buntdb.Tx) error {
|
|
|
|
bList, err := resources.ListBoards(tx, r.Host)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx["Boards"] = bList
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2017-03-13 15:04:00 +00:00
|
|
|
err = tmpls.ExecuteTemplate(dat, "board/dir", ctx)
|
2017-03-12 19:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
errw.ErrorWriter(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, "dir.html", time.Now(), bytes.NewReader(dat.Bytes()))
|
|
|
|
}
|