gophertype/pkg/settings/settings.go
2019-11-25 19:55:28 -08:00

70 lines
1.4 KiB
Go

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)
}