move all the messages into the database
Ted Unangst tedu@tedunangst.com
Sun, 20 Oct 2019 17:02:55 -0400
4 files changed,
51 insertions(+),
31 deletions(-)
M
docs/honk.8
→
docs/honk.8
@@ -83,9 +83,9 @@ file.
.Pp Custom messages may be set by editing the database. (Sorry.) -.Dl insert into config values ('servermsg', 'message'); -.Dl insert into config values ('aboutmsg', 'message'); -.Dl insert into config values ('loginmsg', 'message'); +.Dl update config set value = 'message' where key = 'servermsg'; +.Dl update config set value = 'message' where key = 'aboutmsg'; +.Dl update config set value = 'message' where key = 'loginmsg'; .Pp .Ss ADMIN New users can be added with the
M
honk.go
→
honk.go
@@ -137,9 +137,9 @@ }
var serverName string var iconName = "icon.png" -var serverMsg = template.HTML(`<h2>Things happen.</h2>`) -var aboutMsg = template.HTML(`<h3>What is honk?</h3><p>Honk is amazing!`) -var loginMsg = template.HTML(`<h2>login</h2>`) +var serverMsg template.HTML +var aboutMsg template.HTML +var loginMsg template.HTML func ElaborateUnitTests() { }@@ -175,9 +175,9 @@ log.Fatal("need an argument: debug (on|off)")
} switch os.Args[2] { case "on": - saveconfig("debug", 1) + updateconfig("debug", 1) case "off": - saveconfig("debug", 0) + updateconfig("debug", 0) default: log.Fatal("argument must be on or off") }
M
upgradedb.go
→
upgradedb.go
@@ -24,7 +24,7 @@ "strings"
"time" ) -var myVersion = 26 +var myVersion = 27 func doordie(db *sql.DB, s string, args ...interface{}) { _, err := db.Exec(s, args...)@@ -280,6 +280,29 @@ doordie(db, "alter table auth add column expiry text")
doordie(db, "update config set value = 26 where key = 'dbversion'") fallthrough case 26: + s := "" + getconfig("servermsg", &s) + if s == "" { + setconfig("servermsg", serverMsg) + } + s = "" + getconfig("aboutmsg", &s) + if s == "" { + setconfig("aboutmsg", aboutMsg) + } + s = "" + getconfig("loginmsg", &s) + if s == "" { + setconfig("loginmsg", loginMsg) + } + d := -1 + getconfig("debug", &d) + if d == -1 { + setconfig("debug", 0) + } + doordie(db, "update config set value = 27 where key = 'dbversion'") + fallthrough + case 27: default: log.Fatalf("can't upgrade unknown version %d", dbversion)
M
util.go
→
util.go
@@ -88,6 +88,7 @@ db, err := sql.Open("sqlite3", dbname)
if err != nil { log.Fatal(err) } + alreadyopendb = db defer func() { os.Remove(dbname) os.Exit(1)@@ -129,11 +130,7 @@ if len(addr) < 1 {
log.Print("that's way too short") return } - _, err = db.Exec("insert into config (key, value) values (?, ?)", "listenaddr", addr) - if err != nil { - log.Print(err) - return - } + setconfig("listenaddr", addr) fmt.Printf("server name: ") addr, err = r.ReadString('\n') if err != nil {@@ -145,24 +142,17 @@ if len(addr) < 1 {
log.Print("that's way too short") return } - _, err = db.Exec("insert into config (key, value) values (?, ?)", "servername", addr) - if err != nil { - log.Print(err) - return - } + setconfig("servername", addr) var randbytes [16]byte rand.Read(randbytes[:]) key := fmt.Sprintf("%x", randbytes) - _, err = db.Exec("insert into config (key, value) values (?, ?)", "csrfkey", key) - if err != nil { - log.Print(err) - return - } - _, err = db.Exec("insert into config (key, value) values (?, ?)", "dbversion", myVersion) - if err != nil { - log.Print(err) - return - } + setconfig("csrfkey", key) + setconfig("dbversion", myVersion) + + setconfig("servermsg", "<h2>Things happen.</h2>") + setconfig("aboutmsg", "<h3>What is honk?</h3>\n<p>Honk is amazing!") + setconfig("loginmsg", "<h2>login</h2>") + setconfig("debug", 0) initblobdb()@@ -374,9 +364,16 @@ }
return err } -func saveconfig(key string, val interface{}) { +func setconfig(key string, val interface{}) error { + db := opendatabase() + _, err := db.Exec("insert into config (key, value) values (?, ?)", key, val) + return err +} + +func updateconfig(key string, val interface{}) error { db := opendatabase() - db.Exec("update config set value = ? where key = ?", val, key) + _, err := db.Exec("update config set value = ? where key = ?", val, key) + return err } func openListener() (net.Listener, error) {