statping/core/core.go

163 lines
3.6 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-06-30 00:57:05 +00:00
package core
import (
2018-07-10 12:05:20 +00:00
"github.com/hunterlong/statup/notifiers"
2018-08-11 16:24:32 +00:00
"github.com/hunterlong/statup/source"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/types"
2018-08-16 02:22:10 +00:00
"github.com/hunterlong/statup/utils"
"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
2018-06-30 00:57:05 +00:00
}
var (
2018-08-11 16:24:32 +00:00
Configs *types.Config
CoreApp *Core
SetupMode 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-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 {
col := DbSession.Collection("core")
2018-07-17 09:18:20 +00:00
_, err := col.Insert(c.Core)
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()
CoreApp.SelectAllServices()
2018-07-02 06:21:41 +00:00
CheckServices()
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-08-20 03:48:28 +00:00
err := DbConnection(CoreApp.DbConnection, false, utils.Directory)
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 {
2018-08-16 20:55:30 +00:00
return source.UsingAssets(utils.Directory)
2018-06-30 00:57:05 +00:00
}
func (c Core) SassVars() string {
2018-08-16 20:55:30 +00:00
if !source.UsingAssets(utils.Directory) {
2018-06-30 00:57:05 +00:00
return ""
}
2018-08-16 02:22:10 +00:00
return source.OpenAsset(utils.Directory, "scss/variables.scss")
2018-06-30 00:57:05 +00:00
}
func (c Core) BaseSASS() string {
2018-08-16 20:55:30 +00:00
if !source.UsingAssets(utils.Directory) {
2018-06-30 00:57:05 +00:00
return ""
}
2018-08-16 02:22:10 +00:00
return source.OpenAsset(utils.Directory, "scss/base.scss")
2018-06-30 00:57:05 +00:00
}
func (c Core) MobileSASS() string {
2018-08-16 20:55:30 +00:00
if !source.UsingAssets(utils.Directory) {
return ""
}
2018-08-16 02:22:10 +00:00
return source.OpenAsset(utils.Directory, "scss/mobile.scss")
}
2018-06-30 00:57:05 +00:00
func (c Core) AllOnline() bool {
2018-08-21 06:54:39 +00:00
for _, s := range CoreApp.Services() {
2018-06-30 00:57:05 +00:00
if !s.Online {
return false
}
}
return true
}
func SelectLastMigration() (int64, error) {
2018-07-17 09:18:20 +00:00
var c *types.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) {
2018-07-17 09:18:20 +00:00
var c *types.Core
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.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
}
func (c *Core) Services() []*Service {
var services []*Service
2018-08-21 06:54:39 +00:00
for _, ser := range CoreApp.GetServices() {
services = append(services, ReturnService(ser))
}
return services
}