statping/main.go

140 lines
2.9 KiB
Go
Raw Normal View History

2018-06-10 01:31:13 +00:00
package main
import (
2018-06-10 01:47:57 +00:00
"fmt"
2018-06-10 01:31:13 +00:00
"github.com/GeertJohan/go.rice"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/handlers"
2018-06-12 05:23:30 +00:00
"github.com/hunterlong/statup/plugin"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/utils"
2018-06-28 23:28:55 +00:00
"github.com/joho/godotenv"
2018-06-10 01:31:13 +00:00
"io/ioutil"
2018-06-12 07:21:16 +00:00
"os"
2018-06-11 07:49:41 +00:00
plg "plugin"
"strings"
2018-06-10 01:31:13 +00:00
)
var (
2018-06-30 00:57:05 +00:00
VERSION string
2018-06-10 01:31:13 +00:00
)
2018-06-28 23:28:55 +00:00
func init() {
2018-06-29 00:01:43 +00:00
LoadDotEnvs()
2018-06-30 07:31:42 +00:00
core.VERSION = VERSION
2018-06-29 00:01:43 +00:00
}
2018-06-10 01:31:13 +00:00
func main() {
2018-06-30 00:57:05 +00:00
var err error
2018-06-24 06:17:31 +00:00
if len(os.Args) >= 2 {
2018-06-25 01:58:27 +00:00
CatchCLI(os.Args)
2018-06-24 06:17:31 +00:00
os.Exit(0)
}
2018-06-30 00:57:05 +00:00
utils.Log(1, fmt.Sprintf("Starting Statup v%v\n", VERSION))
2018-06-10 01:31:13 +00:00
RenderBoxes()
2018-06-30 00:57:05 +00:00
core.HasAssets()
2018-06-28 07:28:07 +00:00
2018-06-30 00:57:05 +00:00
core.Configs, err = core.LoadConfig()
2018-06-14 06:50:47 +00:00
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(2, "config.yml file not found - starting in setup mode")
core.SetupMode = true
handlers.RunHTTPServer()
2018-06-10 01:31:13 +00:00
}
mainProcess()
}
2018-06-30 00:57:05 +00:00
func RenderBoxes() {
core.SqlBox = rice.MustFindBox("source/sql")
core.CssBox = rice.MustFindBox("source/css")
core.ScssBox = rice.MustFindBox("source/scss")
core.JsBox = rice.MustFindBox("source/js")
core.TmplBox = rice.MustFindBox("source/tmpl")
core.EmailBox = rice.MustFindBox("source/emails")
}
func LoadDotEnvs() {
err := godotenv.Load()
if err == nil {
utils.Log(1, "Environment file '.env' Loaded")
}
2018-06-15 04:30:10 +00:00
}
2018-06-10 01:31:13 +00:00
func mainProcess() {
var err error
2018-06-30 00:57:05 +00:00
err = core.DbConnection(core.Configs.Connection)
2018-06-14 06:50:47 +00:00
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(3, err)
2018-06-14 06:50:47 +00:00
}
2018-06-30 00:57:05 +00:00
core.RunDatabaseUpgrades()
core.CoreApp, err = core.SelectCore()
2018-06-10 01:31:13 +00:00
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(2, "Core database was not found, Statup is not setup yet.")
handlers.RunHTTPServer()
2018-06-10 01:31:13 +00:00
}
2018-06-24 06:17:31 +00:00
2018-06-30 00:57:05 +00:00
core.CheckServices()
core.CoreApp.Communications, err = core.SelectAllCommunications()
if err != nil {
utils.Log(2, err)
}
core.LoadDefaultCommunications()
2018-06-24 06:17:31 +00:00
2018-06-30 00:57:05 +00:00
go core.DatabaseMaintence()
2018-06-24 06:17:31 +00:00
2018-06-30 00:57:05 +00:00
if !core.SetupMode {
2018-06-11 07:49:41 +00:00
LoadPlugins()
2018-06-30 00:57:05 +00:00
handlers.RunHTTPServer()
2018-06-10 01:31:13 +00:00
}
}
2018-06-11 09:58:41 +00:00
func ForEachPlugin() {
2018-06-30 00:57:05 +00:00
if len(core.CoreApp.Plugins) > 0 {
2018-06-11 09:58:41 +00:00
//for _, p := range core.Plugins {
// p.OnShutdown()
//}
}
}
2018-06-11 07:49:41 +00:00
func LoadPlugins() {
2018-06-12 07:21:16 +00:00
if _, err := os.Stat("./plugins"); os.IsNotExist(err) {
os.Mkdir("./plugins", os.ModePerm)
}
2018-06-11 09:58:41 +00:00
ForEachPlugin()
2018-06-11 07:49:41 +00:00
files, err := ioutil.ReadDir("./plugins")
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(2, fmt.Sprintf("Plugins directory was not found. Error: %v\n", err))
2018-06-11 07:49:41 +00:00
return
}
for _, f := range files {
ext := strings.Split(f.Name(), ".")
if len(ext) != 2 {
continue
}
2018-06-11 10:19:08 +00:00
if ext[1] != "so" {
continue
}
plug, err := plg.Open("plugins/" + f.Name())
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(2, fmt.Sprintf("Plugin '%v' could not load correctly.\n", f.Name()))
2018-06-11 10:19:08 +00:00
continue
2018-06-11 07:49:41 +00:00
}
2018-06-11 10:19:08 +00:00
symPlugin, err := plug.Lookup("Plugin")
2018-06-14 06:38:15 +00:00
2018-06-11 10:19:08 +00:00
var plugActions plugin.PluginActions
plugActions, ok := symPlugin.(plugin.PluginActions)
if !ok {
2018-06-30 00:57:05 +00:00
utils.Log(2, fmt.Sprintf("Plugin '%v' could not load correctly, error: %v\n", f.Name(), "unexpected type from module symbol"))
2018-06-11 10:19:08 +00:00
continue
}
2018-06-30 00:57:05 +00:00
//allPlugins = append(allPlugins, plugActions)
core.CoreApp.Plugins = append(core.CoreApp.Plugins, plugActions.GetInfo())
2018-06-11 07:49:41 +00:00
}
2018-06-14 01:19:00 +00:00
2018-06-30 00:57:05 +00:00
core.OnLoad(core.DbSession)
2018-06-11 09:58:41 +00:00
2018-06-30 00:57:05 +00:00
//utils.Log(1, fmt.Sprintf("Loaded %v Plugins\n", len(allPlugins)))
2018-06-11 09:58:41 +00:00
ForEachPlugin()
2018-06-11 07:49:41 +00:00
}