statping/cmd/main.go

125 lines
3.1 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-10 01:31:13 +00:00
package main
import (
2018-08-16 06:22:20 +00:00
"flag"
2018-06-10 01:47:57 +00:00
"fmt"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/handlers"
_ "github.com/hunterlong/statup/notifiers"
2018-10-08 10:06:25 +00:00
"github.com/hunterlong/statup/plugin"
2018-08-10 04:38:54 +00:00
"github.com/hunterlong/statup/source"
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-12 07:21:16 +00:00
"os"
2018-06-10 01:31:13 +00:00
)
var (
2018-10-07 04:48:33 +00:00
// VERSION stores the current version of Statup
VERSION string
// COMMIT stores the git commit hash for this version of Statup
2018-10-07 05:04:06 +00:00
COMMIT string
ipAddress string
UsingDotEnv bool
port int
2018-06-10 01:31:13 +00:00
)
2018-06-28 23:28:55 +00:00
func init() {
2018-06-30 07:31:42 +00:00
core.VERSION = VERSION
2018-06-29 00:01:43 +00:00
}
// parseFlags will parse the application flags
// -ip = 0.0.0.0 IP address for outgoing HTTP server
// -port = 8080 Port number for outgoing HTTP server
2018-08-16 06:22:20 +00:00
func parseFlags() {
2018-08-16 06:52:13 +00:00
ip := flag.String("ip", "0.0.0.0", "IP address to run the Statup HTTP server")
2018-08-16 06:22:20 +00:00
p := flag.Int("port", 8080, "Port to run the HTTP server")
flag.Parse()
ipAddress = *ip
port = *p
}
// main will run the Statup application
2018-06-10 01:31:13 +00:00
func main() {
2018-06-30 00:57:05 +00:00
var err error
2018-08-16 06:22:20 +00:00
parseFlags()
LoadDotEnvs()
source.Assets()
2018-08-17 15:03:46 +00:00
utils.InitLogs()
args := flag.Args()
2018-08-16 06:22:20 +00:00
if len(args) >= 1 {
err := CatchCLI(args)
if err != nil {
2018-08-16 06:22:20 +00:00
if err.Error() == "end" {
os.Exit(0)
}
fmt.Println(err)
os.Exit(1)
}
2018-06-24 06:17:31 +00:00
}
2018-08-16 06:22:20 +00:00
utils.Log(1, fmt.Sprintf("Starting Statup v%v", VERSION))
core.Configs, err = core.LoadConfig(utils.Directory)
2018-06-14 06:50:47 +00:00
if err != nil {
utils.Log(3, err)
2018-06-30 00:57:05 +00:00
core.SetupMode = true
2018-08-16 06:22:20 +00:00
fmt.Println(handlers.RunHTTPServer(ipAddress, port))
os.Exit(1)
2018-06-10 01:31:13 +00:00
}
2018-09-28 04:02:07 +00:00
defer core.CloseDB()
2018-06-10 01:31:13 +00:00
mainProcess()
}
// LoadDotEnvs attempts to load database configs from a '.env' file in root directory
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-10-07 05:04:06 +00:00
UsingDotEnv = 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
}
// mainProcess will initialize the Statup application and run the HTTP server
2018-06-10 01:31:13 +00:00
func mainProcess() {
dir := utils.Directory
2018-06-10 01:31:13 +00:00
var err error
core.Configs, err = core.LoadConfig(dir)
if err != nil {
utils.Log(4, fmt.Sprintf("could not load config.yml %v", err))
}
err = core.Configs.Connect(false, dir)
2018-06-14 06:50:47 +00:00
if err != nil {
utils.Log(4, fmt.Sprintf("could not connect to database: %v", err))
2018-06-14 06:50:47 +00:00
}
core.Configs.MigrateDatabase()
2018-07-02 06:21:41 +00:00
core.InitApp()
2018-06-30 00:57:05 +00:00
if !core.SetupMode {
2018-10-08 10:06:25 +00:00
plugin.LoadPlugins(false)
2018-08-16 06:22:20 +00:00
fmt.Println(handlers.RunHTTPServer(ipAddress, port))
os.Exit(1)
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()
//}
}
}