Embedded Resources

This commit is contained in:
Tim Schuster 2017-03-15 14:33:27 +01:00
parent 477d3477df
commit 37ba7255fe
No known key found for this signature in database
GPG Key ID: F9E27097EFB77F61
9 changed files with 257 additions and 43 deletions

View File

@ -2,6 +2,7 @@ package config
import (
"flag"
"github.com/GeertJohan/go.rice"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
@ -25,6 +26,8 @@ type Config struct {
const (
CaptchaRecaptcha = "recaptcha"
CaptchaInternal = "internal"
CaptchaHybrid = "hybrid"
CaptchaDisabled = "disabled"
)

View File

@ -11,24 +11,18 @@ import (
"time"
)
var riceConf = rice.Config{
LocateOrder: []rice.LocateMethod{
rice.LocateWorkingDirectory,
rice.LocateEmbedded,
rice.LocateAppended,
},
}
var box = riceConf.MustFindBox("http/admin/res/")
var (
panelTmpl = template.New("admin/panel")
loginTmpl = template.New("admin/login")
statusTmpl = template.New("admin/status")
)
func init() {
func LoadTemplates() error {
var err error
box, err := rice.FindBox("res/")
if err != nil {
panic(err)
}
panelTmpl, err = panelTmpl.Parse(box.MustString("panel.html"))
if err != nil {
panic(err)

55
http/admin/rice-box.go Normal file

File diff suppressed because one or more lines are too long

View File

@ -15,16 +15,6 @@ import (
"time"
)
var riceConf = rice.Config{
LocateOrder: []rice.LocateMethod{
rice.LocateWorkingDirectory,
rice.LocateEmbedded,
rice.LocateAppended,
},
}
var box = riceConf.MustFindBox("http/board/res/")
var (
tmpls = template.New("base")
@ -57,24 +47,27 @@ var (
}
)
func init() {
var err error
func LoadTemplates() error {
box, err := rice.FindBox("res/")
if err != nil {
return err
}
tmpls = tmpls.Funcs(hdlFMap)
tmpls, err = tmpls.New("thread/postlists").Parse(box.MustString("thread.tmpl.html"))
if err != nil {
panic(err)
return err
}
_, err = tmpls.New("board/dir").Parse(box.MustString("dir.html"))
if err != nil {
panic(err)
return err
}
_, err = tmpls.New("board/board").Parse(box.MustString("board.html"))
if err != nil {
panic(err)
return err
}
_, err = tmpls.New("board/thread").Parse(box.MustString("thread.html"))
if err != nil {
panic(err)
return err
}
}

62
http/board/rice-box.go Normal file

File diff suppressed because one or more lines are too long

View File

@ -9,26 +9,20 @@ import (
"net/http"
)
var riceConf = rice.Config{
LocateOrder: []rice.LocateMethod{
rice.LocateWorkingDirectory,
rice.LocateEmbedded,
rice.LocateAppended,
},
}
var box = riceConf.MustFindBox("http/errw/res/")
var (
errorTmpl = template.New("errw/error")
)
func init() {
var err error
func LoadTemplates() error {
box, err := rice.FindBox("res/")
if err != nil {
return err
}
errorTmpl, err = errorTmpl.Parse(box.MustString("error.html"))
if err != nil {
panic(err)
return err
}
return nil
}
type ErrorWithTitle interface {

41
http/errw/rice-box.go Normal file
View File

@ -0,0 +1,41 @@
package errw
import (
"github.com/GeertJohan/go.rice/embedded"
"time"
)
func init() {
// define files
file2 := &embedded.EmbeddedFile{
Filename: `error.html`,
FileModTime: time.Unix(1489238440, 0),
Content: string("<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>{{.Config.Site.Title}} Admin Login</title>\n <style>\n div.error {\n border: 1px solid black;\n width: 500px;\n margin: auto;\n margin-top: 100px;\n }\n div.error h1 {\n margin-bottom: 0px;\n text-align: center;\n }\n div.error h2 {\n text-align: center;\n }\n div.error h3 {\n margin-top: 0px;\n text-align: center;\n color: #888;\n }\n </style>\n</head>\n<body>\n<div class=\"error\">\n <h1>{{.Error.Title}}</h1><br/>\n <h3>{{.Error.Code}}</h3><br/>\n <h2>{{.Error.Description}}</h2>\n</div>\n</body>\n</html>"),
}
// define dirs
dir1 := &embedded.EmbeddedDir{
Filename: ``,
DirModTime: time.Unix(1489240168, 0),
ChildFiles: []*embedded.EmbeddedFile{
file2, // error.html
},
}
// link ChildDirs
dir1.ChildDirs = []*embedded.EmbeddedDir{}
// register embeddedBox
embedded.RegisterEmbeddedBox(`res/`, &embedded.EmbeddedBox{
Name: `res/`,
Time: time.Unix(1489240168, 0),
Dirs: map[string]*embedded.EmbeddedDir{
"": dir1,
},
Files: map[string]*embedded.EmbeddedFile{
"error.html": file2,
},
})
}

55
http/rice-box.go Normal file
View File

@ -0,0 +1,55 @@
package http
import (
"github.com/GeertJohan/go.rice/embedded"
"time"
)
func init() {
// define files
file2 := &embedded.EmbeddedFile{
Filename: `admin.css`,
FileModTime: time.Unix(1489250860, 0),
Content: string("/* CUSTOM CSS */\ndiv.admin.login {\n border: 1px solid black;\n width: 500px;\n margin: auto;\n margin-top: 100px;\n}\n.admin.form.row {\n margin: auto;\n padding: 5px;\n width: 90%;\n height: 22px;\n left: 0;\n right: 0;\n display: flex;\n}\n.admin.form.input {\n font-family: \"monospace\";\n width: 100%;\n height: 100%;\n padding: 2px;\n display: inline;\n}\n.admin.form.input.halfsize {\n width: 50%;\n}"),
}
file3 := &embedded.EmbeddedFile{
Filename: `custom.css`,
FileModTime: time.Unix(1489426703, 0),
Content: string("h1 {\n font-size: 32px;\n}\n\nh2 {\n font-size: 24px;\n}\n\nh3 {\n font-size: 16px;\n}\n\ndiv {\n display: block;\n margin: 0;\n padding: 0;\n}\n\nblockquote blockquote {\n word-wrap: break-word;\n word-break: break-all;\n white-space: normal;\n padding: 2px;\n margin-bottom: 1em;\n margin-top: 1em;\n margin-left: 40px;\n margin-right: 40px;\n}\n\n\n.delform {\n display: inline;\n margin: 0;\n padding: 0;\n}\n.delform input {\n display: inline;\n}\n\n.deleted {\n color: #707070;\n}\n\n.reply-table {\n display: block;\n}\n\n.reply {\n display: table;\n}"),
}
file4 := &embedded.EmbeddedFile{
Filename: `style.css`,
FileModTime: time.Unix(1489426859, 0),
Content: string("/* The following CSS is mostly taken from Wakaba, big thanks for the devs there! <3 */\n\nhtml, body {\n background:#FFFFEE;\n color:#800000;\n}\na {\n color:#0000EE;\n}\na:hover {\n color:#DD0000;\n}\n.adminbar {\n text-align:right;\n clear:both;\n float:right;\n}\n.logo {\n clear:both;\n text-align:center;\n font-size:2em;\n color:#800000;\n width:100%;\n}\n.theader {\n background:#E04000;\n text-align:center;\n padding:2px;\n color:#FFFFFF;\n width:100%;\n}\n.postarea {\n}\n.rules {\n font-size:0.7em;\n}\n.postblock {\n background:#EEAA88;\n color:#800000;\n font-weight:800;\n}\n.footer {\n text-align:center;\n font-size:12px;\n font-family:serif;\n}\n.passvalid {\n background:#EEAA88;\n text-align:center;\n width:100%;\n color:#ffffff;\n}\n.dellist {\n font-weight: bold;\n text-align:center;\n}\n.delbuttons {\n text-align:center;\n padding-bottom:4px;\n\n}\n.managehead {\n background:#AAAA66;\n color:#400000;\n padding:0px;\n}\n.postlists {\n background:#FFFFFF;\n width:100%;\n padding:0px;\n color:#800000;\n}\n.row1 {\n background:#EEEECC;\n color:#800000;\n}\n.row2 {\n background:#DDDDAA;\n color:#800000;\n}\n.unkfunc {\n background:inert;\n color:#789922;\n}\n.filesize {\n text-decoration:none;\n}\n.filetitle {\n background:inherit;\n font-size:1.2em;\n color:#CC1105;\n font-weight:800;\n}\n.postername {\n color:#117743;\n font-weight:bold;\n}\n.postertrip {\n color:#228854;\n}\n.oldpost {\n color:#CC1105;\n font-weight:800;\n}\n.omittedposts {\n color:#707070;\n}\n.reply {\n background:#F0E0D6;\n color:#800000;\n}\n.doubledash {\n vertical-align:top;\n clear:both;\n float:left;\n}\n.replytitle {\n font-size: 1.2em;\n color:#CC1105;\n font-weight:800;\n}\n.commentpostername {\n color:#117743;\n font-weight:800;\n}\n.thumbnailmsg {\n font-size: small;\n color:#800000;\n}\n\n\n\n.abbrev {\n color:#707070;\n}\n.highlight {\n background:#F0E0D6;\n color:#800000;\n border: 2px dashed #EEAA88;\n}\n\n/* From pl files */\n\n/* futaba_style.pl */\n\nform { margin-bottom: 0px }\nform .trap { display:none }\n.postarea { text-align: center }\n.postarea table { margin: 0px auto; text-align: left }\n.thumb { border: none; float: left; margin: 2px 20px }\n.nothumb { float: left; background: #eee; border: 2px dashed #aaa; text-align: center; margin: 2px 20px; padding: 1em 0.5em 1em 0.5em; }\n\n.reflink a { color: inherit; text-decoration: none }\n.reply .filesize { margin-left: 20px }\n.userdelete { float: right; text-align: center; white-space: nowrap }\n.replypage .replylink { display: none }"),
}
// define dirs
dir1 := &embedded.EmbeddedDir{
Filename: ``,
DirModTime: time.Unix(1489426859, 0),
ChildFiles: []*embedded.EmbeddedFile{
file2, // admin.css
file3, // custom.css
file4, // style.css
},
}
// link ChildDirs
dir1.ChildDirs = []*embedded.EmbeddedDir{}
// register embeddedBox
embedded.RegisterEmbeddedBox(`res/`, &embedded.EmbeddedBox{
Name: `res/`,
Time: time.Unix(1489426859, 0),
Dirs: map[string]*embedded.EmbeddedDir{
"": dir1,
},
Files: map[string]*embedded.EmbeddedFile{
"admin.css": file2,
"custom.css": file3,
"style.css": file4,
},
})
}

View File

@ -8,6 +8,7 @@ import (
"go.rls.moe/nyx/config"
"go.rls.moe/nyx/http/admin"
"go.rls.moe/nyx/http/board"
"go.rls.moe/nyx/http/errw"
"go.rls.moe/nyx/http/middle"
"net/http"
"time"
@ -21,7 +22,20 @@ var riceConf = rice.Config{
},
}
func Start(config *config.Config) {
func Start(config *config.Config) error {
err := admin.LoadTemplates()
if err != nil {
return err
}
err = board.LoadTemplates()
if err != nil {
return err
}
err = errw.LoadTemplates()
if err != nil {
return err
}
r := chi.NewRouter()
fmt.Println("Setting up Router")
@ -37,7 +51,7 @@ func Start(config *config.Config) {
{
mw, err := middle.Database(config)
if err != nil {
panic(err)
return err
}
r.Use(mw)
}
@ -45,7 +59,10 @@ func Start(config *config.Config) {
r.Route("/admin/", admin.AdminRouter)
r.Route("/mod/", admin.ModRouter)
{
box := riceConf.MustFindBox("http/res")
box, err := rice.FindBox("res/")
if err != nil {
return err
}
atFileServer := http.StripPrefix("/@/", http.FileServer(box.HTTPBox()))
r.Mount("/@/", atFileServer)
}