ictgj-voting/model_sitedata.go

157 lines
3.4 KiB
Go
Raw Normal View History

2017-10-11 23:03:27 +00:00
package main
2017-10-19 19:53:07 +00:00
import (
"errors"
"strconv"
2017-10-21 22:47:01 +00:00
"strings"
2017-10-19 19:53:07 +00:00
)
2017-10-18 22:18:12 +00:00
2017-10-11 23:03:27 +00:00
/**
* SiteData
* Contains configuration for the website
*/
type siteData struct {
2017-10-19 19:53:07 +00:00
Title string
2018-06-18 13:49:15 +00:00
Ip string
2017-10-19 19:53:07 +00:00
Port int
SessionName string
ServerDir string
2017-10-11 23:03:27 +00:00
authMode int
publicMode int
DevMode bool
Mode int
m *model
2017-10-18 22:18:12 +00:00
mPath []string // The path in the db to this site data
2017-10-11 23:03:27 +00:00
changed bool
2018-01-26 17:47:13 +00:00
sessionSecret string
2017-10-11 23:03:27 +00:00
}
// NewSiteData returns a siteData object with the default values
func NewSiteData(m *model) *siteData {
ret := new(siteData)
ret.Title = "ICT GameJam"
2018-06-18 13:49:15 +00:00
ret.Ip = "127.0.0.1"
2017-10-11 23:03:27 +00:00
ret.Port = 8080
ret.SessionName = "ict-gamejam"
ret.ServerDir = "./"
2017-10-18 22:18:12 +00:00
ret.mPath = []string{"site"}
2017-10-11 23:03:27 +00:00
ret.m = m
return ret
}
2017-10-18 22:18:12 +00:00
// Authentication Modes: Flags for which clients are able to vote
const (
AuthModeAuthentication = iota
AuthModeAll
AuthModeError
)
2017-10-11 23:03:27 +00:00
// Mode flags for how the site is currently running
const (
SiteModeWaiting = iota
SiteModeVoting
SiteModeError
)
// load the site data out of the database
// If fields don't exist in the DB, don't clobber what is already in s
2017-10-18 22:18:12 +00:00
func (s *siteData) LoadFromDB() error {
2017-10-11 23:03:27 +00:00
if err := s.m.openDB(); err != nil {
return err
}
defer s.m.closeDB()
2017-10-21 22:47:01 +00:00
if title, _ := s.m.bolt.GetValue(s.mPath, "title"); strings.TrimSpace(title) != "" {
2017-10-11 23:03:27 +00:00
s.Title = title
}
2018-06-18 13:49:15 +00:00
if ip, err := s.m.bolt.GetValue(s.mPath, "ip"); err == nil {
s.Ip = ip
}
2017-10-18 22:18:12 +00:00
if port, err := s.m.bolt.GetInt(s.mPath, "port"); err == nil {
2017-10-11 23:03:27 +00:00
s.Port = port
}
2017-10-21 22:47:01 +00:00
if sessionName, _ := s.m.bolt.GetValue(s.mPath, "session-name"); strings.TrimSpace(sessionName) != "" {
2017-10-11 23:03:27 +00:00
s.SessionName = sessionName
}
2017-10-21 22:47:01 +00:00
if serverDir, _ := s.m.bolt.GetValue(s.mPath, "server-dir"); strings.TrimSpace(serverDir) != "" {
2017-10-11 23:03:27 +00:00
s.ServerDir = serverDir
}
s.changed = false
2018-01-26 17:47:13 +00:00
if secret, _ := s.m.bolt.GetValue(s.mPath, "session-secret"); strings.TrimSpace(secret) != "" {
s.sessionSecret = secret
}
2017-10-11 23:03:27 +00:00
return nil
}
2017-10-18 22:18:12 +00:00
// Return if the site data in memory has changed
func (s *siteData) NeedsSave() bool {
2017-10-11 23:03:27 +00:00
return s.changed
}
2017-10-18 22:18:12 +00:00
// Save the site data into the DB
func (s *siteData) SaveToDB() error {
2017-10-19 19:53:07 +00:00
var err error
if err = s.m.openDB(); err != nil {
2017-10-11 23:03:27 +00:00
return err
}
defer s.m.closeDB()
2017-10-18 22:18:12 +00:00
if err = s.m.bolt.SetValue(s.mPath, "title", s.Title); err != nil {
2017-10-11 23:03:27 +00:00
return err
}
2018-06-18 13:49:15 +00:00
if err = s.m.bolt.SetValue(s.mPath, "ip", s.Ip); err != nil {
return err
}
2017-10-18 22:18:12 +00:00
if err = s.m.bolt.SetInt(s.mPath, "port", s.Port); err != nil {
2017-10-11 23:03:27 +00:00
return err
}
2017-10-18 22:18:12 +00:00
if err = s.m.bolt.SetValue(s.mPath, "session-name", s.SessionName); err != nil {
2017-10-11 23:03:27 +00:00
return err
}
2017-10-18 22:18:12 +00:00
if err = s.m.bolt.SetValue(s.mPath, "server-dir", s.ServerDir); err != nil {
2017-10-11 23:03:27 +00:00
return err
}
s.changed = false
2018-01-26 17:47:13 +00:00
if err = s.m.bolt.SetValue(s.mPath, "session-secret", s.sessionSecret); err != nil {
return err
}
2017-10-11 23:03:27 +00:00
return nil
}
2017-10-18 22:18:12 +00:00
// Return the Auth Mode
func (s *siteData) GetAuthMode() int {
2017-10-11 23:03:27 +00:00
return s.authMode
}
2017-10-18 22:18:12 +00:00
// Set the auth mode
func (s *siteData) SetAuthMode(mode int) error {
if mode < AuthModeAuthentication || mode >= AuthModeError {
2017-10-19 19:53:07 +00:00
return errors.New("Invalid Authentication Mode: " + strconv.Itoa(mode))
2017-10-18 22:18:12 +00:00
}
2017-10-11 23:03:27 +00:00
if mode != s.authMode {
s.authMode = mode
s.changed = true
}
2017-10-19 19:53:07 +00:00
return nil
2017-10-11 23:03:27 +00:00
}
2017-10-18 22:18:12 +00:00
// Return the public site mode
func (s *siteData) GetPublicMode() int {
2017-10-11 23:03:27 +00:00
return s.publicMode
}
2017-10-18 22:18:12 +00:00
// Set the public site mode
func (s *siteData) SetPublicMode(mode int) error {
if mode < SiteModeWaiting || mode >= SiteModeError {
2017-10-19 19:53:07 +00:00
return errors.New("Invalid Public Mode: " + strconv.Itoa(mode))
2017-10-18 22:18:12 +00:00
}
2017-10-11 23:03:27 +00:00
if mode != s.publicMode {
s.publicMode = mode
s.changed = true
}
2017-10-19 19:53:07 +00:00
return nil
2017-10-11 23:03:27 +00:00
}