forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (131 loc) · 3.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"github.com/GeertJohan/go.rice"
"github.com/fatih/structs"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/handlers"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"github.com/joho/godotenv"
"io/ioutil"
"os"
plg "plugin"
"strings"
)
var (
VERSION string
usingEnv bool
)
func init() {
LoadDotEnvs()
core.VERSION = VERSION
}
func main() {
var err error
if len(os.Args) >= 2 {
CatchCLI(os.Args)
os.Exit(0)
}
utils.Log(1, fmt.Sprintf("Starting Statup v%v", VERSION))
RenderBoxes()
core.HasAssets()
core.Configs, err = core.LoadConfig()
if err != nil {
utils.Log(3, err)
core.SetupMode = true
handlers.RunHTTPServer()
}
mainProcess()
}
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")
}
func LoadDotEnvs() {
err := godotenv.Load()
if err == nil {
utils.Log(1, "Environment file '.env' Loaded")
usingEnv = true
}
}
func mainProcess() {
var err error
err = core.DbConnection(core.Configs.Connection)
if err != nil {
utils.Log(4, fmt.Sprintf("could not connect to database: %v", err))
}
core.RunDatabaseUpgrades()
core.InitApp()
if !core.SetupMode {
LoadPlugins(false)
handlers.RunHTTPServer()
}
}
func ForEachPlugin() {
if len(core.CoreApp.Plugins) > 0 {
//for _, p := range core.Plugins {
// p.OnShutdown()
//}
}
}
func LoadPlugins(debug bool) {
utils.Log(1, fmt.Sprintf("Loading any available Plugins from /plugins directory"))
if _, err := os.Stat("./plugins"); os.IsNotExist(err) {
os.Mkdir("./plugins", os.ModePerm)
}
//ForEachPlugin()
files, err := ioutil.ReadDir("./plugins")
if err != nil {
utils.Log(2, fmt.Sprintf("Plugins directory was not found. Error: %v\n", err))
return
}
for _, f := range files {
utils.Log(1, fmt.Sprintf("Attempting to load plugin '%v'", f.Name()))
ext := strings.Split(f.Name(), ".")
if len(ext) != 2 {
utils.Log(3, fmt.Sprintf("Plugin '%v' must end in .so extension", f.Name()))
continue
}
if ext[1] != "so" {
utils.Log(3, fmt.Sprintf("Plugin '%v' must end in .so extension", f.Name()))
continue
}
plug, err := plg.Open("plugins/" + f.Name())
if err != nil {
utils.Log(3, fmt.Sprintf("Plugin '%v' could not load correctly. %v", f.Name(), err))
continue
}
symPlugin, err := plug.Lookup("Plugin")
if err != nil {
utils.Log(3, fmt.Sprintf("Plugin '%v' could not load correctly. %v", f.Name(), err))
continue
}
if debug {
utils.Log(1, fmt.Sprintf("Plugin '%v' struct:", f.Name()))
utils.Log(1, structs.Map(symPlugin))
}
var plugActions types.PluginActions
plugActions, ok := symPlugin.(types.PluginActions)
if !ok {
utils.Log(3, fmt.Sprintf("Plugin '%v' could not load correctly, error: %v", f.Name(), err))
if debug {
//fmt.Println(symPlugin.(plugin.PluginActions))
}
continue
}
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)))
}
}