statping/core/core.go

138 lines
3.0 KiB
Go
Raw Normal View History

2018-06-30 00:57:05 +00:00
package core
import (
"github.com/GeertJohan/go.rice"
2018-07-10 12:05:20 +00:00
"github.com/hunterlong/statup/notifiers"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/plugin"
"github.com/hunterlong/statup/types"
"github.com/pkg/errors"
"os"
2018-07-04 09:00:16 +00:00
"time"
2018-06-30 00:57:05 +00:00
)
type PluginJSON types.PluginJSON
type PluginRepos types.PluginRepos
type Core struct {
2018-07-03 21:39:56 +00:00
Name string `db:"name" json:"name"`
Description string `db:"description" json:"name"`
Config string `db:"config" json:"-"`
ApiKey string `db:"api_key" json:"-"`
ApiSecret string `db:"api_secret" json:"-"`
Style string `db:"style" json:"-"`
Footer string `db:"footer" json:"-"`
Domain string `db:"domain" json:"domain,omitempty"`
Version string `db:"version" json:"version,omitempty"`
MigrationId int64 `db:"migration_id" json:"-"`
UseCdn bool `db:"use_cdn" json:"-"`
2018-07-03 21:39:56 +00:00
Services []*Service `json:"services,omitempty"`
Plugins []plugin.Info
Repos []PluginJSON
AllPlugins []plugin.PluginActions
Communications []notifiers.AllNotifiers
2018-07-04 09:00:16 +00:00
DbConnection string
started time.Time
2018-06-30 00:57:05 +00:00
}
var (
Configs *types.Config
2018-06-30 00:57:05 +00:00
CoreApp *Core
SqlBox *rice.Box
CssBox *rice.Box
ScssBox *rice.Box
JsBox *rice.Box
TmplBox *rice.Box
EmailBox *rice.Box
SetupMode bool
UsingAssets bool
VERSION string
2018-06-30 00:57:05 +00:00
)
func init() {
2018-07-04 09:00:16 +00:00
CoreApp = NewCore()
}
func NewCore() *Core {
2018-06-30 00:57:05 +00:00
CoreApp = new(Core)
2018-07-04 09:00:16 +00:00
CoreApp.started = time.Now()
return CoreApp
2018-06-30 00:57:05 +00:00
}
2018-07-02 06:21:41 +00:00
func InitApp() {
SelectCore()
2018-07-10 12:05:20 +00:00
notifiers.Collections = DbSession.Collection("communication")
2018-07-02 06:21:41 +00:00
SelectAllServices()
CheckServices()
CoreApp.Communications = notifiers.Load()
2018-07-02 06:21:41 +00:00
go DatabaseMaintence()
}
2018-06-30 00:57:05 +00:00
func (c *Core) Update() (*Core, error) {
res := DbSession.Collection("core").Find().Limit(1)
2018-07-04 09:00:16 +00:00
err := res.Update(c)
return c, err
2018-06-30 00:57:05 +00:00
}
func (c Core) UsingAssets() bool {
return UsingAssets
}
func (c Core) SassVars() string {
if !UsingAssets {
return ""
}
return OpenAsset("scss/variables.scss")
}
func (c Core) BaseSASS() string {
if !UsingAssets {
return ""
}
return OpenAsset("scss/base.scss")
}
func (c Core) MobileSASS() string {
if !UsingAssets {
return ""
}
return OpenAsset("scss/mobile.scss")
}
2018-06-30 00:57:05 +00:00
func (c Core) AllOnline() bool {
for _, s := range CoreApp.Services {
if !s.Online {
return false
}
}
return true
}
func SelectLastMigration() (int64, error) {
var c *Core
if DbSession == nil {
return 0, errors.New("Database connection has not been created yet")
}
err := DbSession.Collection("core").Find().One(&c)
if err != nil {
return 0, err
}
return c.MigrationId, err
}
2018-06-30 00:57:05 +00:00
func SelectCore() (*Core, error) {
var c *Core
err := DbSession.Collection("core").Find().One(&c)
if err != nil {
return nil, err
}
CoreApp = c
2018-07-04 09:00:16 +00:00
CoreApp.DbConnection = Configs.Connection
CoreApp.Version = VERSION
CoreApp.Services, _ = SelectAllServices()
if os.Getenv("USE_CDN") == "true" {
CoreApp.UseCdn = true
}
2018-06-30 00:57:05 +00:00
//store = sessions.NewCookieStore([]byte(core.ApiSecret))
return CoreApp, err
}