move all the messages into the database

This commit is contained in:
Ted Unangst 2019-10-20 17:02:55 -04:00
parent 9988f5c4b4
commit dbb376d883
4 changed files with 51 additions and 31 deletions

View File

@ -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

10
honk.go
View File

@ -137,9 +137,9 @@ type Honker struct {
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 @@ func main() {
}
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")
}

View File

@ -24,7 +24,7 @@ import (
"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 @@ func upgradedb() {
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)

41
util.go
View File

@ -88,6 +88,7 @@ func initdb() {
if err != nil {
log.Fatal(err)
}
alreadyopendb = db
defer func() {
os.Remove(dbname)
os.Exit(1)
@ -129,11 +130,7 @@ func initdb() {
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 @@ func initdb() {
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 @@ func getconfig(key string, value interface{}) error {
return err
}
func saveconfig(key string, val interface{}) {
func setconfig(key string, val interface{}) error {
db := opendatabase()
db.Exec("update config set value = ? where key = ?", val, key)
_, err := db.Exec("insert into config (key, value) values (?, ?)", key, val)
return err
}
func updateconfig(key string, val interface{}) error {
db := opendatabase()
_, err := db.Exec("update config set value = ? where key = ?", val, key)
return err
}
func openListener() (net.Listener, error) {