statping/core/core.go

160 lines
4.4 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-09-12 04:14:22 +00:00
"github.com/hunterlong/statup/core/notifier"
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 (
Configs *DbConfig // Configs holds all of the config.yml and database info
CoreApp *Core // CoreApp is a global variable that contains many elements
SetupMode bool // SetupMode will be true if Statup does not have a database connection
VERSION string // VERSION is set on build automatically by setting a -ldflag
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 (c *Core) ToCore() *types.Core {
return c.Core
}
// InitApp will initialize Statup
2018-07-02 06:21:41 +00:00
func InitApp() {
SelectCore()
2018-09-12 04:14:22 +00:00
insertNotifierDB()
CoreApp.SelectAllServices()
checkServices()
2018-09-12 04:14:22 +00:00
CoreApp.Notifications = notifier.Load()
2018-07-02 06:21:41 +00:00
go DatabaseMaintence()
}
2018-09-12 04:14:22 +00:00
func insertNotifierDB() error {
2018-07-27 04:45:42 +00:00
if DbSession == nil {
err := Configs.Connect(false, utils.Directory)
2018-07-27 04:45:42 +00:00
if err != nil {
return errors.New("database connection has not been created")
}
}
2018-09-12 04:14:22 +00:00
notifier.SetDB(DbSession)
2018-07-27 04:45:42 +00:00
return nil
}
// UpdateCore will update the CoreApp variable inside of the 'core' table in database
2018-07-17 09:18:20 +00:00
func UpdateCore(c *Core) (*Core, error) {
db := coreDB().Update(&c)
return c, db.Error
2018-06-30 00:57:05 +00:00
}
// UsingAssets will return true if /assets folder is present
func (c Core) CurrentTime() string {
t := time.Now().UTC()
current := utils.Timezoner(t, c.Timezone)
ansic := "Monday 03:04:05 PM"
return current.Format(ansic)
}
// UsingAssets will return true if /assets folder is present
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
}
// SassVars opens the file /assets/scss/variables.scss to be edited in Theme
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
}
// BaseSASS is the base design , this opens the file /assets/scss/base.scss to be edited in Theme
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
}
// MobileSASS is the -webkit responsive custom css designs. This opens the
// file /assets/scss/mobile.scss to be edited in Theme
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")
}
// AllOnline will be true if all services are online
2018-06-30 00:57:05 +00:00
func (c Core) AllOnline() bool {
for _, s := range CoreApp.Services {
2018-09-12 04:14:22 +00:00
if !s.Select().Online {
2018-06-30 00:57:05 +00:00
return false
}
}
return true
}
// SelectCore will return the CoreApp global variable and the settings/configs for Statup
2018-06-30 00:57:05 +00:00
func SelectCore() (*Core, error) {
if DbSession == nil {
return nil, errors.New("database has not been initiated yet.")
}
exists := DbSession.HasTable("core")
if !exists {
return nil, errors.New("core database has not been setup yet.")
}
db := coreDB().First(&CoreApp)
if db.Error != nil {
return nil, db.Error
2018-06-30 00:57:05 +00:00
}
CoreApp.DbConnection = Configs.DbConn
2018-07-04 09:00:16 +00:00
CoreApp.Version = VERSION
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, db.Error
2018-06-30 00:57:05 +00:00
}
// ServiceOrder will reorder the services based on 'order_id' (Order)
type ServiceOrder []types.ServiceInterface
func (c ServiceOrder) Len() int { return len(c) }
func (c ServiceOrder) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c ServiceOrder) Less(i, j int) bool { return c[i].(*Service).Order < c[j].(*Service).Order }