From 79fd165948f1515613843ff2665b3669ef326f0f Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Fri, 1 Nov 2019 15:48:31 -0400 Subject: [PATCH] decouple files from "." and allow data and view to live anywhere --- Makefile | 5 ++++- docs/changelog.txt | 2 ++ docs/honk.8 | 48 ++++++++++++++++++++++++++++++++++----- genschemago.sh | 6 +++++ honk.go | 26 ++++++++++++--------- schema.go | 38 +++++++++++++++++++++++++++++++ upgradedb.go | 5 +---- util.go | 20 +++++++---------- web.go | 56 +++++++++++++++++++++++++--------------------- 9 files changed, 148 insertions(+), 58 deletions(-) create mode 100644 genschemago.sh create mode 100644 schema.go diff --git a/Makefile b/Makefile index a29cf8a..510bc8c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,10 @@ all: honk -honk: *.go go.mod +schema.go: schema.sql + sh ./genschemago.sh + +honk: schema.go *.go go.mod go build -mod=`ls -d vendor 2> /dev/null` -o honk clean: diff --git a/docs/changelog.txt b/docs/changelog.txt index 710c3b3..315e67b 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -2,6 +2,8 @@ changelog -- next +++ Make it easier to upgrade by decoupling data dir from ".". + + Amend changelog for 0.8.0 in include omitted elements: Syntax highlighting for code blocks. Something resembling an actual manual. diff --git a/docs/honk.8 b/docs/honk.8 index 9c825b2..9506e68 100644 --- a/docs/honk.8 +++ b/docs/honk.8 @@ -49,6 +49,20 @@ to be installed. Run make. Please be patient. Even on fast machines, building from source can take several seconds. +.Ss OPTIONS +The following options control where +.Nm +looks for data. +.Bl -tag -width datadirx +.It Fl datadir +The root data directory, where the database and other user data are stored. +Requires write access. +Defaults to ".". +.It Fl viewdir +The root view directory, where html and other templates are stored. +Read only. +Defaults to ".". +.El .Ss INIT Run the .Ic init @@ -69,17 +83,17 @@ Log messages are sent to stderr and should probably be redirected to a file. .Ss CUSTOMIZATION Add custom memes (stickers) to the .Pa memes -directory. +data directory. Image and video files are supported. .Pp Add custom emus (emoji) to the .Pa emus -directory. +data directory. Image files are supported. .Pp Site CSS may be overridden by creating a .Pa views/local.css -file. +file in the data directory. A restart is required after changes. .Pp Custom HTML messages may be added to select pages by using the @@ -129,16 +143,40 @@ Restart. There's also a .Pa blob.db file which is important to backup and restore. -.Sh SECURITY +.Ss SECURITY .Nm is not currently hardened against SSRF, server side request forgery. Be mindful of what other services may be exposed via localhost or the local network. -.Sh DEBUG +.Ss DEBUG Debug mode may be enabled or disabled by running .Ic debug Ar on|off . In debug mode, secure cookies are disabled and templates are reloaded every request. +.Sh FILES +.Nm +files are split between the data directory and the view directory. +Both default to "." but may be specified by command line options. +.Pp +The data directory contains: +.Bl -tag -width views/local.css +.It Pa honk.db +The main database. +.It Pa blob.db +Media and attachment storage. +.It Pa emus +Custom emoji. +.It Pa memes +Stickers and such. +.It Pa views/local.css +Locally customized CSS. +.El +.Pp +The view directory contains: +.Bl -tag -width views +.It Pa views +HTML templates and CSS files. +.El .Sh ENVIRONMENT Image processing and scaling requires considerable memory. It is recommended to adjust the datasize ulimit to at least 1GB. diff --git a/genschemago.sh b/genschemago.sh new file mode 100644 index 0000000..8a8ed7f --- /dev/null +++ b/genschemago.sh @@ -0,0 +1,6 @@ +echo "package main" > schema.go +echo "var sqlSchema = \`" >> schema.go +cat schema.sql >> schema.go +echo "\`" >> schema.go +go fmt schema.go + diff --git a/honk.go b/honk.go index 916ff0d..3db9ee5 100644 --- a/honk.go +++ b/honk.go @@ -17,11 +17,11 @@ package main import ( "crypto/rsa" + "flag" "fmt" "html/template" "log" notrand "math/rand" - "os" "strconv" "strings" "time" @@ -184,6 +184,8 @@ const ( ) var serverName string +var dataDir = "." +var viewDir = "." var iconName = "icon.png" var serverMsg template.HTML var aboutMsg template.HTML @@ -193,9 +195,13 @@ func ElaborateUnitTests() { } func main() { + flag.StringVar(&dataDir, "datadir", dataDir, "data directory") + flag.StringVar(&viewDir, "viewdir", viewDir, "view directory") + flag.Parse() + args := flag.Args() cmd := "run" - if len(os.Args) > 1 { - cmd = os.Args[1] + if len(args) > 0 { + cmd = args[0] } switch cmd { case "init": @@ -220,10 +226,10 @@ func main() { case "admin": adminscreen() case "debug": - if len(os.Args) != 3 { + if len(args) != 2 { log.Fatal("need an argument: debug (on|off)") } - switch os.Args[2] { + switch args[1] { case "on": updateconfig("debug", 1) case "off": @@ -237,17 +243,17 @@ func main() { chpass() case "cleanup": arg := "30" - if len(os.Args) > 2 { - arg = os.Args[2] + if len(args) > 1 { + arg = args[1] } cleanupdb(arg) case "ping": - if len(os.Args) < 4 { + if len(args) < 3 { fmt.Printf("usage: honk ping from to\n") return } - name := os.Args[2] - targ := os.Args[3] + name := args[1] + targ := args[2] user, err := butwhatabout(name) if err != nil { log.Printf("unknown user") diff --git a/schema.go b/schema.go new file mode 100644 index 0000000..0f28eea --- /dev/null +++ b/schema.go @@ -0,0 +1,38 @@ +package main + +var sqlSchema = ` + +create table honks (honkid integer primary key, userid integer, what text, honker text, xid text, rid text, dt text, url text, audience text, noise text, convoy text, whofore integer, format text, precis text, oonker text, flags integer); +create table donks (honkid integer, fileid integer); +create table filemeta (fileid integer primary key, xid text, name text, description text, url text, media text, local integer); +create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, combos text, owner text); +create table xonkers (xonkerid integer primary key, name text, info text, flavor text); +create table zonkers (zonkerid integer primary key, userid integer, name text, wherefore text); +create table doovers(dooverid integer primary key, dt text, tries integer, userid integer, rcpt text, msg blob); +create table onts (ontology text, honkid integer); +create table honkmeta (honkid integer, genus text, json text); +create table hfcs (hfcsid integer primary key, userid integer, json text); + +create index idx_honksxid on honks(xid); +create index idx_honksconvoy on honks(convoy); +create index idx_honkshonker on honks(honker); +create index idx_honksoonker on honks(oonker); +create index idx_honkerxid on honkers(xid); +create index idx_xonkername on xonkers(name); +create index idx_zonkersname on zonkers(name); +create index idx_filesxid on filemeta(xid); +create index idx_filesurl on filemeta(url); +create index idx_ontology on onts(ontology); +create index idx_onthonkid on onts(honkid); +create index idx_honkmetaid on honkmeta(honkid); +create index idx_hfcsuser on hfcs(userid); + +create table config (key text, value text); + +create table users (userid integer primary key, username text, hash text, displayname text, about text, pubkey text, seckey text, options text); +create table auth (authid integer primary key, userid integer, hash text, expiry text); +CREATE index idxusers_username on users(username); +CREATE index idxauth_userid on auth(userid); +CREATE index idxauth_hash on auth(hash); + +` diff --git a/upgradedb.go b/upgradedb.go index d43babc..8b1ca05 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -125,10 +125,7 @@ func upgradedb() { case 21: // here we go... initblobdb() - blobdb, err := sql.Open("sqlite3", blobdbname) - if err != nil { - log.Fatal(err) - } + blobdb := openblobdb() tx, err := blobdb.Begin() if err != nil { log.Fatalf("can't begin: %s", err) diff --git a/util.go b/util.go index acf5e9e..af46c08 100644 --- a/util.go +++ b/util.go @@ -71,16 +71,11 @@ func getassetparam(file string) string { var dbtimeformat = "2006-01-02 15:04:05" var alreadyopendb *sql.DB -var dbname = "honk.db" -var blobdbname = "blob.db" var stmtConfig *sql.Stmt func initdb() { - schema, err := ioutil.ReadFile("schema.sql") - if err != nil { - log.Fatal(err) - } - _, err = os.Stat(dbname) + dbname := dataDir + "/honk.db" + _, err := os.Stat(dbname) if err == nil { log.Fatalf("%s already exists", dbname) } @@ -103,7 +98,7 @@ func initdb() { os.Exit(1) }() - for _, line := range strings.Split(string(schema), ";") { + for _, line := range strings.Split(sqlSchema, ";") { _, err = db.Exec(line) if err != nil { log.Print(err) @@ -168,6 +163,7 @@ func initdb() { } func initblobdb() { + blobdbname := dataDir + "/blob.db" _, err := os.Stat(blobdbname) if err == nil { log.Fatalf("%s already exists", blobdbname) @@ -337,8 +333,8 @@ func opendatabase() *sql.DB { if alreadyopendb != nil { return alreadyopendb } - var err error - _, err = os.Stat(dbname) + dbname := dataDir + "/honk.db" + _, err := os.Stat(dbname) if err != nil { log.Fatalf("unable to open database: %s", err) } @@ -355,8 +351,8 @@ func opendatabase() *sql.DB { } func openblobdb() *sql.DB { - var err error - _, err = os.Stat(blobdbname) + blobdbname := dataDir + "/blob.db" + _, err := os.Stat(blobdbname) if err != nil { log.Fatalf("unable to open database: %s", err) } diff --git a/web.go b/web.go index 91f06f7..59ced08 100644 --- a/web.go +++ b/web.go @@ -61,9 +61,9 @@ func getuserstyle(u *login.UserInfo) template.CSS { func getInfo(r *http.Request) map[string]interface{} { u := login.GetUserInfo(r) templinfo := make(map[string]interface{}) - templinfo["StyleParam"] = getassetparam("views/style.css") - templinfo["LocalStyleParam"] = getassetparam("views/local.css") - templinfo["JSParam"] = getassetparam("views/honkpage.js") + templinfo["StyleParam"] = getassetparam(viewDir + "/views/style.css") + templinfo["LocalStyleParam"] = getassetparam(viewDir + "/views/local.css") + templinfo["JSParam"] = getassetparam(viewDir + "/views/honkpage.js") templinfo["UserStyle"] = getuserstyle(u) templinfo["ServerName"] = serverName templinfo["IconName"] = iconName @@ -127,7 +127,7 @@ func homepage(w http.ResponseWriter, r *http.Request) { func showfunzone(w http.ResponseWriter, r *http.Request) { var emunames, memenames []string - dir, err := os.Open("emus") + dir, err := os.Open(dataDir + "/emus") if err == nil { emunames, _ = dir.Readdirnames(0) dir.Close() @@ -137,7 +137,7 @@ func showfunzone(w http.ResponseWriter, r *http.Request) { emunames[i] = e[:len(e)-4] } } - dir, err = os.Open("memes") + dir, err = os.Open(dataDir + "/memes") if err == nil { memenames, _ = dir.Readdirnames(0) dir.Close() @@ -1784,12 +1784,16 @@ func avatate(w http.ResponseWriter, r *http.Request) { func serveasset(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "max-age=7776000") - http.ServeFile(w, r, "views"+r.URL.Path) + dir := viewDir + if r.URL.Path == "/local.css" { + dir = dataDir + } + http.ServeFile(w, r, dir+"/views"+r.URL.Path) } func servehelp(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] w.Header().Set("Cache-Control", "max-age=3600") - http.ServeFile(w, r, "docs/"+name) + http.ServeFile(w, r, viewDir+"/docs/"+name) } func servehtml(w http.ResponseWriter, r *http.Request) { u := login.GetUserInfo(r) @@ -1807,12 +1811,12 @@ func servehtml(w http.ResponseWriter, r *http.Request) { func serveemu(w http.ResponseWriter, r *http.Request) { xid := mux.Vars(r)["xid"] w.Header().Set("Cache-Control", "max-age="+somedays()) - http.ServeFile(w, r, "emus/"+xid) + http.ServeFile(w, r, dataDir+"/emus/"+xid) } func servememe(w http.ResponseWriter, r *http.Request) { xid := mux.Vars(r)["xid"] w.Header().Set("Cache-Control", "max-age="+somedays()) - http.ServeFile(w, r, "memes/"+xid) + http.ServeFile(w, r, dataDir+"/memes/"+xid) } func servefile(w http.ResponseWriter, r *http.Request) { @@ -1918,25 +1922,25 @@ func serve() { debug := false getconfig("debug", &debug) readviews = templates.Load(debug, - "views/honkpage.html", - "views/honkfrags.html", - "views/honkers.html", - "views/hfcs.html", - "views/combos.html", - "views/honkform.html", - "views/honk.html", - "views/account.html", - "views/about.html", - "views/funzone.html", - "views/login.html", - "views/xzone.html", - "views/msg.html", - "views/header.html", - "views/onts.html", - "views/honkpage.js", + viewDir+"/views/honkpage.html", + viewDir+"/views/honkfrags.html", + viewDir+"/views/honkers.html", + viewDir+"/views/hfcs.html", + viewDir+"/views/combos.html", + viewDir+"/views/honkform.html", + viewDir+"/views/honk.html", + viewDir+"/views/account.html", + viewDir+"/views/about.html", + viewDir+"/views/funzone.html", + viewDir+"/views/login.html", + viewDir+"/views/xzone.html", + viewDir+"/views/msg.html", + viewDir+"/views/header.html", + viewDir+"/views/onts.html", + viewDir+"/views/honkpage.js", ) if !debug { - assets := []string{"views/style.css", "views/local.css", "views/honkpage.js"} + assets := []string{viewDir + "/views/style.css", dataDir + "/views/local.css", viewDir + "/views/honkpage.js"} for _, s := range assets { savedassetparams[s] = getassetparam(s) }