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 .Pp
Custom messages may be set by editing the database. Custom messages may be set by editing the database.
(Sorry.) (Sorry.)
.Dl insert into config values ('servermsg', 'message'); .Dl update config set value = 'message' where key = 'servermsg';
.Dl insert into config values ('aboutmsg', 'message'); .Dl update config set value = 'message' where key = 'aboutmsg';
.Dl insert into config values ('loginmsg', 'message'); .Dl update config set value = 'message' where key = 'loginmsg';
.Pp .Pp
.Ss ADMIN .Ss ADMIN
New users can be added with the New users can be added with the

10
honk.go
View File

@ -137,9 +137,9 @@ type Honker struct {
var serverName string var serverName string
var iconName = "icon.png" var iconName = "icon.png"
var serverMsg = template.HTML(`<h2>Things happen.</h2>`) var serverMsg template.HTML
var aboutMsg = template.HTML(`<h3>What is honk?</h3><p>Honk is amazing!`) var aboutMsg template.HTML
var loginMsg = template.HTML(`<h2>login</h2>`) var loginMsg template.HTML
func ElaborateUnitTests() { func ElaborateUnitTests() {
} }
@ -175,9 +175,9 @@ func main() {
} }
switch os.Args[2] { switch os.Args[2] {
case "on": case "on":
saveconfig("debug", 1) updateconfig("debug", 1)
case "off": case "off":
saveconfig("debug", 0) updateconfig("debug", 0)
default: default:
log.Fatal("argument must be on or off") log.Fatal("argument must be on or off")
} }

View File

@ -24,7 +24,7 @@ import (
"time" "time"
) )
var myVersion = 26 var myVersion = 27
func doordie(db *sql.DB, s string, args ...interface{}) { func doordie(db *sql.DB, s string, args ...interface{}) {
_, err := db.Exec(s, args...) _, err := db.Exec(s, args...)
@ -280,6 +280,29 @@ func upgradedb() {
doordie(db, "update config set value = 26 where key = 'dbversion'") doordie(db, "update config set value = 26 where key = 'dbversion'")
fallthrough fallthrough
case 26: 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: default:
log.Fatalf("can't upgrade unknown version %d", dbversion) log.Fatalf("can't upgrade unknown version %d", dbversion)

41
util.go
View File

@ -88,6 +88,7 @@ func initdb() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
alreadyopendb = db
defer func() { defer func() {
os.Remove(dbname) os.Remove(dbname)
os.Exit(1) os.Exit(1)
@ -129,11 +130,7 @@ func initdb() {
log.Print("that's way too short") log.Print("that's way too short")
return return
} }
_, err = db.Exec("insert into config (key, value) values (?, ?)", "listenaddr", addr) setconfig("listenaddr", addr)
if err != nil {
log.Print(err)
return
}
fmt.Printf("server name: ") fmt.Printf("server name: ")
addr, err = r.ReadString('\n') addr, err = r.ReadString('\n')
if err != nil { if err != nil {
@ -145,24 +142,17 @@ func initdb() {
log.Print("that's way too short") log.Print("that's way too short")
return return
} }
_, err = db.Exec("insert into config (key, value) values (?, ?)", "servername", addr) setconfig("servername", addr)
if err != nil {
log.Print(err)
return
}
var randbytes [16]byte var randbytes [16]byte
rand.Read(randbytes[:]) rand.Read(randbytes[:])
key := fmt.Sprintf("%x", randbytes) key := fmt.Sprintf("%x", randbytes)
_, err = db.Exec("insert into config (key, value) values (?, ?)", "csrfkey", key) setconfig("csrfkey", key)
if err != nil { setconfig("dbversion", myVersion)
log.Print(err)
return setconfig("servermsg", "<h2>Things happen.</h2>")
} setconfig("aboutmsg", "<h3>What is honk?</h3>\n<p>Honk is amazing!")
_, err = db.Exec("insert into config (key, value) values (?, ?)", "dbversion", myVersion) setconfig("loginmsg", "<h2>login</h2>")
if err != nil { setconfig("debug", 0)
log.Print(err)
return
}
initblobdb() initblobdb()
@ -374,9 +364,16 @@ func getconfig(key string, value interface{}) error {
return err return err
} }
func saveconfig(key string, val interface{}) { func setconfig(key string, val interface{}) error {
db := opendatabase() 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) { func openListener() (net.Listener, error) {