statping/cmd/main.go

192 lines
4.3 KiB
Go
Raw Normal View History

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 (
2020-03-06 22:18:06 +00:00
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/hunterlong/statping/source"
2020-03-04 10:29:00 +00:00
"github.com/hunterlong/statping/database"
2020-03-06 22:18:06 +00:00
"github.com/hunterlong/statping/handlers"
2020-03-04 10:29:00 +00:00
"github.com/hunterlong/statping/types/configs"
"github.com/hunterlong/statping/types/core"
"github.com/hunterlong/statping/types/services"
"github.com/hunterlong/statping/utils"
2020-02-29 23:36:31 +00:00
"github.com/pkg/errors"
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
envFile string
verboseMode int
2018-10-07 05:04:06 +00:00
port int
2019-12-30 08:08:51 +00:00
log = utils.Log.WithField("type", "cmd")
2020-03-04 10:29:00 +00:00
httpServer = make(chan bool)
2018-06-10 01:31:13 +00:00
)
2018-06-28 23:28:55 +00:00
func init() {
2020-03-04 10:29:00 +00:00
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
2020-02-26 05:38:03 +00:00
// environment variables WILL overwrite flags
2018-08-16 06:22:20 +00:00
func parseFlags() {
2020-02-26 05:38:03 +00:00
envPort := utils.Getenv("PORT", 8080).(int)
envIpAddress := utils.Getenv("IP", "0.0.0.0").(string)
envVerbose := utils.Getenv("VERBOSE", 2).(int)
flag.StringVar(&ipAddress, "ip", envIpAddress, "IP address to run the Statping HTTP server")
flag.StringVar(&envFile, "env", "", "IP address to run the Statping HTTP server")
2020-02-26 05:38:03 +00:00
flag.IntVar(&port, "port", envPort, "Port to run the HTTP server")
flag.IntVar(&verboseMode, "verbose", envVerbose, "Run in verbose mode to see detailed logs (1 - 4)")
2018-08-16 06:22:20 +00:00
flag.Parse()
}
2020-03-03 06:42:37 +00:00
func exit(err error) {
panic(err)
//log.Fatalln(err)
//os.Exit(2)
}
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
go sigterm()
2020-03-04 10:29:00 +00:00
2018-08-16 06:22:20 +00:00
parseFlags()
2020-03-04 10:29:00 +00:00
if err := source.Assets(); err != nil {
exit(err)
}
utils.VerboseMode = verboseMode
2020-03-04 10:29:00 +00:00
if err := utils.InitLogs(); err != nil {
2019-12-30 11:12:14 +00:00
log.Errorf("Statping Log Error: %v\n", err)
}
2020-03-04 10:29:00 +00:00
args := flag.Args()
2018-08-16 06:22:20 +00:00
if len(args) >= 1 {
2018-10-11 16:53:13 +00:00
err := catchCLI(args)
if err != nil {
2018-08-16 06:22:20 +00:00
if err.Error() == "end" {
os.Exit(0)
2020-03-03 06:42:37 +00:00
return
2018-08-16 06:22:20 +00:00
}
2020-03-03 06:42:37 +00:00
exit(err)
}
2018-06-24 06:17:31 +00:00
}
2019-12-30 08:08:51 +00:00
log.Info(fmt.Sprintf("Starting Statping v%v", VERSION))
2020-03-03 06:42:37 +00:00
if err := updateDisplay(); err != nil {
log.Warnln(err)
}
2020-03-04 10:29:00 +00:00
c, err := configs.LoadConfigs()
if err != nil {
if err := SetupMode(); err != nil {
2020-03-03 06:42:37 +00:00
exit(err)
}
}
2020-03-06 22:18:06 +00:00
if err = configs.ConnectConfigs(c, true); err != nil {
2020-03-04 10:29:00 +00:00
exit(err)
}
2020-03-04 14:20:47 +00:00
if err := c.MigrateDatabase(); err != nil {
2020-03-04 10:29:00 +00:00
exit(err)
2018-06-10 01:31:13 +00:00
}
2020-02-26 06:39:54 +00:00
2019-12-30 11:12:14 +00:00
if err := mainProcess(); err != nil {
2020-03-03 06:42:37 +00:00
exit(err)
2019-12-30 11:12:14 +00:00
}
2018-06-10 01:31:13 +00:00
}
// Close will gracefully stop the database connection, and log file
func Close() {
utils.CloseLogs()
2020-03-04 10:29:00 +00:00
database.Close()
}
func SetupMode() error {
return handlers.RunHTTPServer(ipAddress, port)
}
// sigterm will attempt to close the database connections gracefully
func sigterm() {
sigs := make(chan os.Signal, 1)
2020-03-06 22:18:06 +00:00
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
2020-03-06 22:18:06 +00:00
fmt.Println("Shutting down Statping")
Close()
os.Exit(1)
}
2018-12-04 04:17:29 +00:00
// mainProcess will initialize the Statping application and run the HTTP server
2019-12-30 11:12:14 +00:00
func mainProcess() error {
2020-03-04 10:29:00 +00:00
if err := services.ServicesFromEnvFile(); err != nil {
2020-02-29 23:36:31 +00:00
errStr := "error 'SERVICE' environment variable"
log.Errorln(errStr)
return errors.Wrap(err, errStr)
2020-02-26 05:38:03 +00:00
}
2020-02-29 23:36:31 +00:00
2020-02-26 05:38:03 +00:00
if err := core.InitApp(); err != nil {
return err
}
2020-03-04 10:29:00 +00:00
if err := handlers.RunHTTPServer(ipAddress, port); err != nil {
log.Fatalln(err)
return errors.Wrap(err, "http server")
2018-06-10 01:31:13 +00:00
}
2020-02-29 23:36:31 +00:00
return nil
2018-06-10 01:31:13 +00:00
}
2020-03-04 10:29:00 +00:00
func StartHTTPServer() {
httpServer = make(chan bool)
go httpServerProcess(httpServer)
}
func StopHTTPServer() {
}
func httpServerProcess(process <-chan bool) {
for {
select {
case <-process:
fmt.Println("HTTP Server has stopped")
return
default:
if err := handlers.RunHTTPServer(ipAddress, port); err != nil {
log.Errorln(err)
2020-03-04 14:20:47 +00:00
exit(err)
2020-03-04 10:29:00 +00:00
}
}
}
}