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 (
|
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"
|
|
|
|
"github.com/hunterlong/statping/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-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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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() {
|
2018-12-04 04:17:29 +00:00
|
|
|
ip := flag.String("ip", "0.0.0.0", "IP address to run the Statping 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
|
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")
|
|
|
|
}
|
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
|
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-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
|
|
|
}
|
2018-12-04 04:17:29 +00:00
|
|
|
utils.Log(1, fmt.Sprintf("Starting Statping v%v", VERSION))
|
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 {
|
2018-07-07 05:02:47 +00:00
|
|
|
utils.Log(3, err)
|
2018-06-30 00:57:05 +00:00
|
|
|
core.SetupMode = true
|
2018-11-21 08:45:55 +00:00
|
|
|
utils.Log(1, handlers.RunHTTPServer(ipAddress, port))
|
2018-08-16 06:22:20 +00:00
|
|
|
os.Exit(1)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
mainProcess()
|
|
|
|
}
|
|
|
|
|
2018-10-11 16:53:13 +00:00
|
|
|
// loadDotEnvs attempts to load database configs from a '.env' file in root directory
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 {
|
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-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
|
|
|
}
|
|
|
|
}
|