2018-06-10 01:31:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-10 01:47:57 +00:00
|
|
|
"fmt"
|
2018-07-04 19:14:28 +00:00
|
|
|
"github.com/fatih/structs"
|
2018-06-30 00:57:05 +00:00
|
|
|
"github.com/hunterlong/statup/core"
|
|
|
|
"github.com/hunterlong/statup/handlers"
|
2018-07-17 09:18:20 +00:00
|
|
|
"github.com/hunterlong/statup/types"
|
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-07-28 01:50:13 +00:00
|
|
|
VERSION string
|
|
|
|
usingEnv bool
|
|
|
|
directory string
|
2018-06-10 01:31:13 +00:00
|
|
|
)
|
|
|
|
|
2018-06-28 23:28:55 +00:00
|
|
|
func init() {
|
2018-07-20 05:26:41 +00:00
|
|
|
utils.InitLogs()
|
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-07-01 10:24:35 +00:00
|
|
|
utils.Log(1, fmt.Sprintf("Starting Statup v%v", VERSION))
|
2018-07-27 04:45:42 +00:00
|
|
|
core.RenderBoxes()
|
2018-07-28 01:50:13 +00:00
|
|
|
core.HasAssets(directory)
|
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-07-07 05:02:47 +00:00
|
|
|
utils.Log(3, err)
|
2018-06-30 00:57:05 +00:00
|
|
|
core.SetupMode = true
|
|
|
|
handlers.RunHTTPServer()
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
mainProcess()
|
|
|
|
}
|
|
|
|
|
2018-07-27 04:45:42 +00:00
|
|
|
func LoadDotEnvs() error {
|
2018-06-30 00:57:05 +00:00
|
|
|
err := godotenv.Load()
|
|
|
|
if err == nil {
|
|
|
|
utils.Log(1, "Environment file '.env' Loaded")
|
2018-07-07 05:02:47 +00:00
|
|
|
usingEnv = true
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
2018-07-27 04:45:42 +00:00
|
|
|
return err
|
2018-06-15 04:30:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 01:31:13 +00:00
|
|
|
func mainProcess() {
|
|
|
|
var err error
|
2018-07-28 01:50:13 +00:00
|
|
|
err = core.DbConnection(core.Configs.Connection, false, ".")
|
2018-06-14 06:50:47 +00:00
|
|
|
if err != nil {
|
2018-07-12 04:49:18 +00:00
|
|
|
utils.Log(4, fmt.Sprintf("could not connect to database: %v", err))
|
2018-06-14 06:50:47 +00:00
|
|
|
}
|
2018-07-06 03:01:03 +00:00
|
|
|
|
2018-06-30 00:57:05 +00:00
|
|
|
core.RunDatabaseUpgrades()
|
2018-07-02 06:21:41 +00:00
|
|
|
core.InitApp()
|
2018-06-24 06:17:31 +00:00
|
|
|
|
2018-06-30 00:57:05 +00:00
|
|
|
if !core.SetupMode {
|
2018-07-04 19:14:28 +00:00
|
|
|
LoadPlugins(false)
|
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-07-04 19:14:28 +00:00
|
|
|
func LoadPlugins(debug bool) {
|
2018-07-03 21:39:56 +00:00
|
|
|
utils.Log(1, fmt.Sprintf("Loading any available Plugins from /plugins directory"))
|
2018-06-12 07:21:16 +00:00
|
|
|
if _, err := os.Stat("./plugins"); os.IsNotExist(err) {
|
|
|
|
os.Mkdir("./plugins", os.ModePerm)
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:39:56 +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 {
|
2018-07-03 21:39:56 +00:00
|
|
|
utils.Log(1, fmt.Sprintf("Attempting to load plugin '%v'", f.Name()))
|
2018-06-11 07:49:41 +00:00
|
|
|
ext := strings.Split(f.Name(), ".")
|
|
|
|
if len(ext) != 2 {
|
2018-07-03 21:39:56 +00:00
|
|
|
utils.Log(3, fmt.Sprintf("Plugin '%v' must end in .so extension", f.Name()))
|
2018-06-11 07:49:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-06-11 10:19:08 +00:00
|
|
|
if ext[1] != "so" {
|
2018-07-03 21:39:56 +00:00
|
|
|
utils.Log(3, fmt.Sprintf("Plugin '%v' must end in .so extension", f.Name()))
|
2018-06-11 10:19:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
plug, err := plg.Open("plugins/" + f.Name())
|
|
|
|
if err != nil {
|
2018-07-03 21:39:56 +00:00
|
|
|
utils.Log(3, fmt.Sprintf("Plugin '%v' could not load correctly. %v", f.Name(), err))
|
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-07-03 21:39:56 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Log(3, fmt.Sprintf("Plugin '%v' could not load correctly. %v", f.Name(), err))
|
|
|
|
continue
|
|
|
|
}
|
2018-06-14 06:38:15 +00:00
|
|
|
|
2018-07-04 19:14:28 +00:00
|
|
|
if debug {
|
|
|
|
utils.Log(1, fmt.Sprintf("Plugin '%v' struct:", f.Name()))
|
|
|
|
utils.Log(1, structs.Map(symPlugin))
|
|
|
|
}
|
|
|
|
|
2018-07-17 09:18:20 +00:00
|
|
|
var plugActions types.PluginActions
|
|
|
|
plugActions, ok := symPlugin.(types.PluginActions)
|
2018-06-11 10:19:08 +00:00
|
|
|
if !ok {
|
2018-07-03 21:39:56 +00:00
|
|
|
utils.Log(3, fmt.Sprintf("Plugin '%v' could not load correctly, error: %v", f.Name(), err))
|
2018-07-04 19:14:28 +00:00
|
|
|
if debug {
|
|
|
|
//fmt.Println(symPlugin.(plugin.PluginActions))
|
|
|
|
}
|
2018-06-11 10:19:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-07-04 19:14:28 +00:00
|
|
|
if debug {
|
|
|
|
TestPlugin(plugActions)
|
|
|
|
} else {
|
|
|
|
plugActions.OnLoad(core.DbSession)
|
|
|
|
core.CoreApp.Plugins = append(core.CoreApp.Plugins, plugActions.GetInfo())
|
|
|
|
core.CoreApp.AllPlugins = append(core.CoreApp.AllPlugins, plugActions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !debug {
|
|
|
|
utils.Log(1, fmt.Sprintf("Loaded %v Plugins\n", len(core.CoreApp.Plugins)))
|
2018-06-11 07:49:41 +00:00
|
|
|
}
|
|
|
|
}
|