package main import ( "strings" "time" ) /** * SiteData * Contains configuration for the website */ type SiteData struct { Title string Ip string Port int SessionName string ServerDir string DevMode bool m *model mPath []string // The path in the db to this site data changed bool lastSave time.Time sessionSecret string } // NewSiteData returns a SiteData object with the default values func NewSiteData(m *model) *SiteData { ret := new(SiteData) ret.Title = "ribbit" ret.Ip = "127.0.0.1" ret.Port = 8080 ret.SessionName = "good-ol-ribbit" ret.ServerDir = "./" ret.mPath = []string{"site"} ret.m = m return ret } // load the site data out of the database // If fields don't exist in the DB, don't clobber what is already in s func (s *SiteData) LoadFromDB() error { if err := s.m.bolt.OpenDB(); err != nil { return err } defer s.m.bolt.CloseDB() if title, _ := s.m.bolt.GetValue(s.mPath, "title"); strings.TrimSpace(title) != "" { s.Title = title } if ip, err := s.m.bolt.GetValue(s.mPath, "ip"); err == nil { s.Ip = ip } if port, err := s.m.bolt.GetInt(s.mPath, "port"); err == nil { s.Port = port } if sessionName, _ := s.m.bolt.GetValue(s.mPath, "session-name"); strings.TrimSpace(sessionName) != "" { s.SessionName = sessionName } if serverDir, _ := s.m.bolt.GetValue(s.mPath, "server-dir"); strings.TrimSpace(serverDir) != "" { s.ServerDir = serverDir } s.changed = false if secret, _ := s.m.bolt.GetValue(s.mPath, "session-secret"); strings.TrimSpace(secret) != "" { s.sessionSecret = secret } return nil } // Return if the site data in memory has changed func (s *SiteData) NeedsSave() bool { return s.changed } // Save the site data into the DB func (s *SiteData) SaveToDB() error { s.lastSave = time.Now() var err error if err = s.m.bolt.OpenDB(); err != nil { return err } defer s.m.bolt.CloseDB() if err = s.m.bolt.SetValue(s.mPath, "title", s.Title); err != nil { return err } if err = s.m.bolt.SetValue(s.mPath, "ip", s.Ip); err != nil { return err } if err = s.m.bolt.SetInt(s.mPath, "port", s.Port); err != nil { return err } if err = s.m.bolt.SetValue(s.mPath, "session-name", s.SessionName); err != nil { return err } if err = s.m.bolt.SetValue(s.mPath, "server-dir", s.ServerDir); err != nil { return err } s.changed = false if err = s.m.bolt.SetValue(s.mPath, "session-secret", s.sessionSecret); err != nil { return err } return nil }