2018-12-04 04:17:29 +00:00
|
|
|
// Statping
|
2018-08-16 06:22:20 +00:00
|
|
|
// Copyright (C) 2018. Hunter Long and the project contributors
|
|
|
|
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
|
|
|
//
|
2018-12-04 04:17:29 +00:00
|
|
|
// https://github.com/hunterlong/statping
|
2018-08-16 06:22:20 +00:00
|
|
|
//
|
|
|
|
// 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 (
|
2019-12-28 09:01:07 +00:00
|
|
|
"github.com/hunterlong/statping/utils"
|
|
|
|
|
2018-08-16 06:22:20 +00:00
|
|
|
"flag"
|
2018-06-10 01:47:57 +00:00
|
|
|
"fmt"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/core"
|
|
|
|
"github.com/hunterlong/statping/handlers"
|
|
|
|
"github.com/hunterlong/statping/plugin"
|
|
|
|
"github.com/hunterlong/statping/source"
|
2018-06-28 23:28:55 +00:00
|
|
|
"github.com/joho/godotenv"
|
2018-06-12 07:21:16 +00:00
|
|
|
"os"
|
2019-12-19 16:48:59 +00:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2018-06-10 01:31:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-12-04 04:17:29 +00:00
|
|
|
// VERSION stores the current version of Statping
|
2018-10-07 04:48:33 +00:00
|
|
|
VERSION string
|
2018-12-04 04:17:29 +00:00
|
|
|
// COMMIT stores the git commit hash for this version of Statping
|
2018-10-07 05:04:06 +00:00
|
|
|
COMMIT string
|
|
|
|
ipAddress string
|
2019-12-28 09:01:07 +00:00
|
|
|
envFile string
|
|
|
|
verboseMode int
|
2018-10-07 05:04:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +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() {
|
2019-12-28 09:01:07 +00:00
|
|
|
flag.StringVar(&ipAddress, "ip", "0.0.0.0", "IP address to run the Statping HTTP server")
|
|
|
|
flag.StringVar(&envFile, "env", "", "IP address to run the Statping HTTP server")
|
|
|
|
flag.IntVar(&port, "port", 8080, "Port to run the HTTP server")
|
|
|
|
flag.IntVar(&verboseMode, "verbose", 1, "Run in verbose mode to see detailed logs (1 - 4)")
|
2018-08-16 06:22:20 +00:00
|
|
|
flag.Parse()
|
2019-12-28 09:01:07 +00:00
|
|
|
|
2018-12-20 04:29:38 +00:00
|
|
|
if os.Getenv("PORT") != "" {
|
|
|
|
port = int(utils.ToInt(os.Getenv("PORT")))
|
|
|
|
}
|
|
|
|
if os.Getenv("IP") != "" {
|
|
|
|
ipAddress = os.Getenv("IP")
|
|
|
|
}
|
2019-12-28 09:01:07 +00:00
|
|
|
if os.Getenv("VERBOSE") != "" {
|
|
|
|
verboseMode = int(utils.ToInt(os.Getenv("VERBOSE")))
|
|
|
|
}
|
2018-08-16 06:22:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 04:17:29 +00:00
|
|
|
// main will run the Statping application
|
2018-06-10 01:31:13 +00:00
|
|
|
func main() {
|
2018-06-30 00:57:05 +00:00
|
|
|
var err error
|
2019-12-19 16:48:59 +00:00
|
|
|
go sigterm()
|
2018-08-16 06:22:20 +00:00
|
|
|
parseFlags()
|
2018-10-11 16:53:13 +00:00
|
|
|
loadDotEnvs()
|
2018-09-05 10:54:57 +00:00
|
|
|
source.Assets()
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.VerboseMode = verboseMode
|
2019-04-29 18:11:44 +00:00
|
|
|
if err := utils.InitLogs(); err != nil {
|
|
|
|
fmt.Printf("Statping Log Error: \n %v\n", err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2018-08-30 04:49:44 +00:00
|
|
|
args := flag.Args()
|
2018-08-16 06:22:20 +00:00
|
|
|
|
2018-08-30 04:49:44 +00:00
|
|
|
if len(args) >= 1 {
|
2018-10-11 16:53:13 +00:00
|
|
|
err := catchCLI(args)
|
2018-08-16 01:07:02 +00:00
|
|
|
if err != nil {
|
2018-08-16 06:22:20 +00:00
|
|
|
if err.Error() == "end" {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2018-08-16 01:07:02 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-06-24 06:17:31 +00:00
|
|
|
}
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.Log.Info(fmt.Sprintf("Starting Statping v%v", VERSION))
|
|
|
|
updateDisplay()
|
2018-11-21 08:45:55 +00:00
|
|
|
|
2018-10-11 16:53:13 +00:00
|
|
|
core.Configs, err = core.LoadConfigFile(utils.Directory)
|
2018-06-14 06:50:47 +00:00
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.Log.Errorln(err)
|
2018-06-30 00:57:05 +00:00
|
|
|
core.SetupMode = true
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.Log.Infoln(handlers.RunHTTPServer(ipAddress, port))
|
2018-08-16 06:22:20 +00:00
|
|
|
os.Exit(1)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
mainProcess()
|
|
|
|
}
|
|
|
|
|
2019-12-28 09:01:07 +00:00
|
|
|
// Close will gracefully stop the database connection, and log file
|
|
|
|
func Close() {
|
|
|
|
core.CloseDB()
|
|
|
|
utils.CloseLogs()
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:48:59 +00:00
|
|
|
// sigterm will attempt to close the database connections gracefully
|
|
|
|
func sigterm() {
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
|
|
|
|
<-sigs
|
2019-12-28 09:01:07 +00:00
|
|
|
Close()
|
2019-12-19 16:48:59 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-10-11 16:53:13 +00:00
|
|
|
// loadDotEnvs attempts to load database configs from a '.env' file in root directory
|
|
|
|
func loadDotEnvs() error {
|
2019-12-28 09:01:07 +00:00
|
|
|
err := godotenv.Load(envFile)
|
2018-06-30 00:57:05 +00:00
|
|
|
if err == nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.Log.Infoln("Environment file '.env' Loaded")
|
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-12-04 04:17:29 +00:00
|
|
|
// mainProcess will initialize the Statping application and run the HTTP server
|
2018-06-10 01:31:13 +00:00
|
|
|
func mainProcess() {
|
2018-09-05 10:54:57 +00:00
|
|
|
dir := utils.Directory
|
2018-06-10 01:31:13 +00:00
|
|
|
var err error
|
2018-09-05 10:54:57 +00:00
|
|
|
err = core.Configs.Connect(false, dir)
|
2018-06-14 06:50:47 +00:00
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.Log.Errorln(fmt.Sprintf("could not connect to database: %v", err))
|
2018-06-14 06:50:47 +00:00
|
|
|
}
|
2018-09-06 05:28:35 +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-11 00:43:23 +00:00
|
|
|
plugin.LoadPlugins()
|
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
|
|
|
}
|
|
|
|
}
|