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/types"
|
2018-07-07 05:02:47 +00:00
|
|
|
"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-17 09:18:20 +00:00
|
|
|
*types.Core
|
|
|
|
Services []*Service
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-07-07 05:02:47 +00:00
|
|
|
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
|
|
|
|
SetupMode bool
|
|
|
|
UsingAssets bool
|
2018-07-07 05:02:47 +00:00
|
|
|
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-17 09:18:20 +00:00
|
|
|
CoreApp.Core = new(types.Core)
|
|
|
|
CoreApp.Started = time.Now()
|
2018-07-04 09:00:16 +00:00
|
|
|
return CoreApp
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 09:18:20 +00:00
|
|
|
func InsertCore(c *Core) error {
|
2018-07-12 04:49:18 +00:00
|
|
|
col := DbSession.Collection("core")
|
2018-07-17 09:18:20 +00:00
|
|
|
_, err := col.Insert(c.Core)
|
2018-07-12 04:49:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-17 09:18:20 +00:00
|
|
|
func (c *Core) ToCore() *types.Core {
|
|
|
|
return c.Core
|
|
|
|
}
|
|
|
|
|
2018-07-02 06:21:41 +00:00
|
|
|
func InitApp() {
|
|
|
|
SelectCore()
|
2018-07-27 04:45:42 +00:00
|
|
|
InsertNotifierDB()
|
2018-07-02 06:21:41 +00:00
|
|
|
SelectAllServices()
|
|
|
|
CheckServices()
|
2018-07-11 03:06:21 +00:00
|
|
|
CoreApp.Communications = notifiers.Load()
|
2018-07-02 06:21:41 +00:00
|
|
|
go DatabaseMaintence()
|
|
|
|
}
|
|
|
|
|
2018-07-27 04:45:42 +00:00
|
|
|
func InsertNotifierDB() error {
|
|
|
|
if DbSession == nil {
|
2018-07-28 01:50:13 +00:00
|
|
|
err := DbConnection(CoreApp.DbConnection, false, ".")
|
2018-07-27 04:45:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("database connection has not been created")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifiers.Collections = DbSession.Collection("communication")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-17 09:18:20 +00:00
|
|
|
func UpdateCore(c *Core) (*Core, error) {
|
2018-06-30 00:57:05 +00:00
|
|
|
res := DbSession.Collection("core").Find().Limit(1)
|
2018-07-17 09:18:20 +00:00
|
|
|
err := res.Update(c.Core)
|
2018-07-04 09:00:16 +00:00
|
|
|
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 ""
|
|
|
|
}
|
2018-07-28 01:50:13 +00:00
|
|
|
return OpenAsset(".", "scss/variables.scss")
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Core) BaseSASS() string {
|
|
|
|
if !UsingAssets {
|
|
|
|
return ""
|
|
|
|
}
|
2018-07-28 01:50:13 +00:00
|
|
|
return OpenAsset(".", "scss/base.scss")
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 10:24:35 +00:00
|
|
|
func (c Core) MobileSASS() string {
|
|
|
|
if !UsingAssets {
|
|
|
|
return ""
|
|
|
|
}
|
2018-07-28 01:50:13 +00:00
|
|
|
return OpenAsset(".", "scss/mobile.scss")
|
2018-07-01 10:24:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-30 00:57:05 +00:00
|
|
|
func (c Core) AllOnline() bool {
|
2018-07-14 02:37:39 +00:00
|
|
|
for _, ser := range CoreApp.Services {
|
|
|
|
s := ser.ToService()
|
2018-06-30 00:57:05 +00:00
|
|
|
if !s.Online {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-07-06 03:01:03 +00:00
|
|
|
func SelectLastMigration() (int64, error) {
|
2018-07-17 09:18:20 +00:00
|
|
|
var c *types.Core
|
2018-07-07 05:02:47 +00:00
|
|
|
if DbSession == nil {
|
|
|
|
return 0, errors.New("Database connection has not been created yet")
|
|
|
|
}
|
2018-07-06 03:01:03 +00:00
|
|
|
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) {
|
2018-07-17 09:18:20 +00:00
|
|
|
var c *types.Core
|
2018-07-12 04:49:18 +00:00
|
|
|
exists := DbSession.Collection("core").Exists()
|
|
|
|
if !exists {
|
|
|
|
return nil, errors.New("core database has not been setup yet.")
|
|
|
|
}
|
2018-06-30 00:57:05 +00:00
|
|
|
err := DbSession.Collection("core").Find().One(&c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-17 09:18:20 +00:00
|
|
|
CoreApp.Core = c
|
2018-07-04 09:00:16 +00:00
|
|
|
CoreApp.DbConnection = Configs.Connection
|
|
|
|
CoreApp.Version = VERSION
|
|
|
|
CoreApp.Services, _ = SelectAllServices()
|
2018-07-07 05:02:47 +00:00
|
|
|
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
|
|
|
|
}
|