gophertype/pkg/settings/settings.go

70 lines
1.4 KiB
Go
Raw Normal View History

2019-11-26 03:55:28 +00:00
package settings
import (
"crypto/rand"
"encoding/base64"
"git.kirsle.net/apps/gophertype/pkg/session"
)
// Current holds the current app settings. When the app settings have never
// been initialized, this struct holds the default values with a random secret
// key. The config is not saved to DB until you call Save() on it.
var Current = Load()
// Spec singleton holds the app configuration.
type Spec struct {
// Sets to `true` when the site's initial setup has run and an admin created.
Initialized bool
// Site information
Title string
Description string
Email string // primary email for notifications
NSFW bool
BaseURL string
// Blog settings
PostsPerPage int
// Mail settings
MailEnabled bool
MailSender string
MailHost string
MailPort int
MailUsername string
MailPassword string
// Security
SecretKey string `json:"-"`
}
// Load gets or creates the App Settings.
func Load() Spec {
var s = Spec{
Title: "Untitled Site",
Description: "Just another web blog.",
SecretKey: MakeSecretKey(),
}
session.SetSecretKey([]byte(s.SecretKey))
return s
}
// Save the settings to DB.
func (s Spec) Save() error {
Current = s
session.SetSecretKey([]byte(s.SecretKey))
return nil
}
// MakeSecretKey generates a secret key for signing HTTP cookies.
func MakeSecretKey() string {
keyLength := 32
b := make([]byte, keyLength)
rand.Read(b)
return base64.URLEncoding.EncodeToString(b)
}