mirror of https://github.com/statping/statping
code cleanup
parent
60100461c9
commit
29bda29874
31
cmd/main.go
31
cmd/main.go
|
@ -20,12 +20,14 @@ var (
|
||||||
// VERSION stores the current version of Statping
|
// VERSION stores the current version of Statping
|
||||||
VERSION string
|
VERSION string
|
||||||
// COMMIT stores the git commit hash for this version of Statping
|
// COMMIT stores the git commit hash for this version of Statping
|
||||||
COMMIT string
|
COMMIT string
|
||||||
log = utils.Log.WithField("type", "cmd")
|
log = utils.Log.WithField("type", "cmd")
|
||||||
confgs *configs.DbConfig
|
confgs *configs.DbConfig
|
||||||
|
process chan struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
process = make(chan struct{})
|
||||||
core.New(VERSION)
|
core.New(VERSION)
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
rootCmd.AddCommand(assetsCmd)
|
rootCmd.AddCommand(assetsCmd)
|
||||||
|
@ -42,8 +44,8 @@ func init() {
|
||||||
// exit will return an error and return an exit code 1 due to this error
|
// exit will return an error and return an exit code 1 due to this error
|
||||||
func exit(err error) {
|
func exit(err error) {
|
||||||
utils.SentryErr(err)
|
utils.SentryErr(err)
|
||||||
Close()
|
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
close(process)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close will gracefully stop the database connection, and log file
|
// Close will gracefully stop the database connection, and log file
|
||||||
|
@ -55,14 +57,15 @@ func Close() {
|
||||||
|
|
||||||
// main will run the Statping application
|
// main will run the Statping application
|
||||||
func main() {
|
func main() {
|
||||||
Execute()
|
go Execute()
|
||||||
|
<-process
|
||||||
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// main will run the Statping application
|
// main will run the Statping application
|
||||||
func start() {
|
func start() {
|
||||||
var err error
|
|
||||||
go sigterm()
|
go sigterm()
|
||||||
|
var err error
|
||||||
if err := source.Assets(); err != nil {
|
if err := source.Assets(); err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
}
|
}
|
||||||
|
@ -75,14 +78,10 @@ func start() {
|
||||||
|
|
||||||
log.Info(fmt.Sprintf("Starting Statping v%s", VERSION))
|
log.Info(fmt.Sprintf("Starting Statping v%s", VERSION))
|
||||||
|
|
||||||
//if err := updateDisplay(); err != nil {
|
|
||||||
// log.Warnln(err)
|
|
||||||
//}
|
|
||||||
|
|
||||||
confgs, err = configs.LoadConfigs(configFile)
|
confgs, err = configs.LoadConfigs(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Infoln("Starting in Setup Mode")
|
log.Infoln("Starting in Setup Mode")
|
||||||
if err := SetupMode(); err != nil {
|
if err = handlers.RunHTTPServer(); err != nil {
|
||||||
exit(err)
|
exit(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,16 +138,12 @@ func start() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupMode() error {
|
|
||||||
return handlers.RunHTTPServer(ipAddress, port)
|
|
||||||
}
|
|
||||||
|
|
||||||
// sigterm will attempt to close the database connections gracefully
|
// sigterm will attempt to close the database connections gracefully
|
||||||
func sigterm() {
|
func sigterm() {
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
<-sigs
|
<-sigs
|
||||||
Close()
|
close(process)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,7 +155,7 @@ func mainProcess() error {
|
||||||
|
|
||||||
services.LoadServicesYaml()
|
services.LoadServicesYaml()
|
||||||
|
|
||||||
if err := handlers.RunHTTPServer(ipAddress, port); err != nil {
|
if err := handlers.RunHTTPServer(); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
return errors.Wrap(err, "http server")
|
return errors.Wrap(err, "http server")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
|
@ -26,12 +25,23 @@ var (
|
||||||
usingSSL bool
|
usingSSL bool
|
||||||
mainTmpl = `{{define "main" }} {{ template "base" . }} {{ end }}`
|
mainTmpl = `{{define "main" }} {{ template "base" . }} {{ end }}`
|
||||||
templates = []string{"base.gohtml"}
|
templates = []string{"base.gohtml"}
|
||||||
|
httpError chan error
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunHTTPServer will start a HTTP server on a specific IP and port
|
func StopHTTPServer(err error) {
|
||||||
func RunHTTPServer(ip string, port int) error {
|
log.Infoln("Stopping HTTP Server")
|
||||||
host := fmt.Sprintf("%v:%v", ip, port)
|
httpError <- err
|
||||||
|
close(httpError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunHTTPServer will start a HTTP server on a specific IP and port
|
||||||
|
func RunHTTPServer() error {
|
||||||
|
if utils.Params.GetBool("DISABLE_HTTP") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ip := utils.Params.GetString("HOST")
|
||||||
|
host := fmt.Sprintf("%v:%v", ip, utils.Params.GetInt64("PORT"))
|
||||||
key := utils.FileExists(utils.Directory + "/server.key")
|
key := utils.FileExists(utils.Directory + "/server.key")
|
||||||
cert := utils.FileExists(utils.Directory + "/server.crt")
|
cert := utils.FileExists(utils.Directory + "/server.crt")
|
||||||
|
|
||||||
|
@ -47,37 +57,18 @@ func RunHTTPServer(ip string, port int) error {
|
||||||
resetCookies()
|
resetCookies()
|
||||||
|
|
||||||
if usingSSL {
|
if usingSSL {
|
||||||
cfg := &tls.Config{
|
go startSSLServer(ip)
|
||||||
MinVersion: tls.VersionTLS12,
|
|
||||||
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
|
|
||||||
PreferServerCipherSuites: true,
|
|
||||||
CipherSuites: []uint16{
|
|
||||||
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
|
||||||
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
|
||||||
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
|
|
||||||
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
srv := &http.Server{
|
|
||||||
Addr: fmt.Sprintf("%v:%v", ip, 443),
|
|
||||||
Handler: router,
|
|
||||||
TLSConfig: cfg,
|
|
||||||
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
|
|
||||||
WriteTimeout: timeout,
|
|
||||||
ReadTimeout: timeout,
|
|
||||||
IdleTimeout: timeout,
|
|
||||||
}
|
|
||||||
return srv.ListenAndServeTLS(utils.Directory+"/server.crt", utils.Directory+"/server.key")
|
|
||||||
} else {
|
} else {
|
||||||
httpServer = &http.Server{
|
go startServer(host)
|
||||||
Addr: host,
|
}
|
||||||
WriteTimeout: timeout,
|
|
||||||
ReadTimeout: timeout,
|
for {
|
||||||
IdleTimeout: timeout,
|
select {
|
||||||
Handler: router,
|
case err := <-httpError:
|
||||||
|
return err
|
||||||
|
default:
|
||||||
|
|
||||||
}
|
}
|
||||||
httpServer.SetKeepAlivesEnabled(false)
|
|
||||||
return httpServer.ListenAndServe()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
|
"github.com/statping/statping/utils"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func startServer(host string) {
|
||||||
|
httpError = make(chan error)
|
||||||
|
httpServer = &http.Server{
|
||||||
|
Addr: host,
|
||||||
|
WriteTimeout: timeout,
|
||||||
|
ReadTimeout: timeout,
|
||||||
|
IdleTimeout: timeout,
|
||||||
|
Handler: router,
|
||||||
|
}
|
||||||
|
httpServer.SetKeepAlivesEnabled(false)
|
||||||
|
if err := httpServer.ListenAndServe(); err != nil {
|
||||||
|
httpError <- err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startSSLServer(ip string) {
|
||||||
|
httpError = make(chan error)
|
||||||
|
cfg := &tls.Config{
|
||||||
|
MinVersion: tls.VersionTLS12,
|
||||||
|
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
|
||||||
|
PreferServerCipherSuites: true,
|
||||||
|
CipherSuites: []uint16{
|
||||||
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
||||||
|
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
||||||
|
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
|
||||||
|
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
srv := &http.Server{
|
||||||
|
Addr: fmt.Sprintf("%v:%v", ip, 443),
|
||||||
|
Handler: router,
|
||||||
|
TLSConfig: cfg,
|
||||||
|
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
|
||||||
|
WriteTimeout: timeout,
|
||||||
|
ReadTimeout: timeout,
|
||||||
|
IdleTimeout: timeout,
|
||||||
|
}
|
||||||
|
if err := srv.ListenAndServeTLS(utils.Directory+"/server.crt", utils.Directory+"/server.key"); err != nil {
|
||||||
|
httpError <- err
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,9 @@ var (
|
||||||
|
|
||||||
// Assets will load the Rice boxes containing the CSS, SCSS, JS, and HTML files.
|
// Assets will load the Rice boxes containing the CSS, SCSS, JS, and HTML files.
|
||||||
func Assets() error {
|
func Assets() error {
|
||||||
|
if utils.Params.GetBool("DISABLE_HTTP") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
var err error
|
var err error
|
||||||
TmplBox, err = rice.FindBox("dist")
|
TmplBox, err = rice.FindBox("dist")
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -8,46 +8,23 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Params *viper.Viper
|
Params *viper.Viper
|
||||||
configLog = Log.WithField("type", "configs")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func initCLI() {
|
func initEnvs() {
|
||||||
Params = viper.New()
|
Params = viper.New()
|
||||||
Params.AutomaticEnv()
|
Params.AutomaticEnv()
|
||||||
setDefaults()
|
|
||||||
Directory = Params.GetString("STATPING_DIR")
|
|
||||||
//Params.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
|
||||||
Params.SetConfigName("config")
|
|
||||||
Params.SetConfigType("yml")
|
|
||||||
Params.AddConfigPath(Directory)
|
|
||||||
|
|
||||||
Params.ReadInConfig()
|
|
||||||
|
|
||||||
Params.AddConfigPath(Directory)
|
|
||||||
Params.SetConfigFile(".env")
|
|
||||||
Params.ReadInConfig()
|
|
||||||
|
|
||||||
Params.Set("VERSION", version)
|
|
||||||
|
|
||||||
// check if logs are disabled
|
|
||||||
if !Params.GetBool("DISABLE_LOGS") {
|
|
||||||
Log.Out = ioutil.Discard
|
|
||||||
|
|
||||||
Log.Debugln("current working directory: ", Directory)
|
|
||||||
Log.AddHook(new(hook))
|
|
||||||
Log.SetNoLock()
|
|
||||||
checkVerboseMode()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func setDefaults() {
|
|
||||||
var err error
|
var err error
|
||||||
defaultDir, err := os.Getwd()
|
defaultDir, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
configLog.Errorln(err)
|
Log.Errorln(err)
|
||||||
defaultDir = "."
|
defaultDir = "."
|
||||||
}
|
}
|
||||||
|
Params.Set("VERSION", version)
|
||||||
|
Params.SetDefault("PORT", 8080)
|
||||||
|
Params.SetDefault("HOST", "0.0.0.0")
|
||||||
|
Params.SetDefault("DISABLE_HTTP", false)
|
||||||
Params.SetDefault("STATPING_DIR", defaultDir)
|
Params.SetDefault("STATPING_DIR", defaultDir)
|
||||||
Params.SetDefault("GO_ENV", "")
|
Params.SetDefault("GO_ENV", "")
|
||||||
Params.SetDefault("DB_CONN", "")
|
Params.SetDefault("DB_CONN", "")
|
||||||
|
@ -72,6 +49,7 @@ func setDefaults() {
|
||||||
Params.SetDefault("LOGS_MAX_COUNT", 5)
|
Params.SetDefault("LOGS_MAX_COUNT", 5)
|
||||||
Params.SetDefault("LOGS_MAX_AGE", 28)
|
Params.SetDefault("LOGS_MAX_AGE", 28)
|
||||||
Params.SetDefault("LOGS_MAX_SIZE", 16)
|
Params.SetDefault("LOGS_MAX_SIZE", 16)
|
||||||
|
Params.SetDefault("DISABLE_COLORS", false)
|
||||||
|
|
||||||
dbConn := Params.GetString("DB_CONN")
|
dbConn := Params.GetString("DB_CONN")
|
||||||
dbInt := Params.GetInt("DB_PORT")
|
dbInt := Params.GetInt("DB_PORT")
|
||||||
|
@ -83,4 +61,25 @@ func setDefaults() {
|
||||||
Params.SetDefault("DB_PORT", 3306)
|
Params.SetDefault("DB_PORT", 3306)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Directory = Params.GetString("STATPING_DIR")
|
||||||
|
//Params.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
||||||
|
Params.SetConfigName("config")
|
||||||
|
Params.SetConfigType("yml")
|
||||||
|
Params.AddConfigPath(Directory)
|
||||||
|
Params.ReadInConfig()
|
||||||
|
|
||||||
|
Params.AddConfigPath(Directory)
|
||||||
|
Params.SetConfigFile(".env")
|
||||||
|
Params.ReadInConfig()
|
||||||
|
|
||||||
|
// check if logs are disabled
|
||||||
|
if Params.GetBool("DISABLE_LOGS") {
|
||||||
|
Log.Out = ioutil.Discard
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Log.Debugln("current working directory: ", Directory)
|
||||||
|
Log.AddHook(new(hook))
|
||||||
|
Log.SetNoLock()
|
||||||
|
checkVerboseMode()
|
||||||
}
|
}
|
|
@ -146,7 +146,7 @@ func createLog(dir string) error {
|
||||||
|
|
||||||
// InitLogs will create the '/logs' directory and creates a file '/logs/statup.log' for application logging
|
// InitLogs will create the '/logs' directory and creates a file '/logs/statup.log' for application logging
|
||||||
func InitLogs() error {
|
func InitLogs() error {
|
||||||
initCLI()
|
initEnvs()
|
||||||
if Params.GetBool("DISABLE_LOGS") {
|
if Params.GetBool("DISABLE_LOGS") {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -164,8 +164,8 @@ func InitLogs() error {
|
||||||
Log.SetOutput(mw)
|
Log.SetOutput(mw)
|
||||||
|
|
||||||
Log.SetFormatter(&Logger.TextFormatter{
|
Log.SetFormatter(&Logger.TextFormatter{
|
||||||
ForceColors: true,
|
ForceColors: !Params.GetBool("DISABLE_COLORS"),
|
||||||
DisableColors: false,
|
DisableColors: Params.GetBool("DISABLE_COLORS"),
|
||||||
})
|
})
|
||||||
checkVerboseMode()
|
checkVerboseMode()
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -254,11 +254,12 @@ func HttpRequest(url, method string, content interface{}, headers []string, body
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
contents, err := ioutil.ReadAll(resp.Body)
|
contents, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, resp, err
|
||||||
|
}
|
||||||
// record HTTP metrics
|
// record HTTP metrics
|
||||||
t2 := Now().Sub(t1).Milliseconds()
|
|
||||||
httpMetric.Requests++
|
httpMetric.Requests++
|
||||||
httpMetric.Milliseconds += t2 / httpMetric.Requests
|
httpMetric.Milliseconds += Now().Sub(t1).Milliseconds() / httpMetric.Requests
|
||||||
httpMetric.Bytes += int64(len(contents))
|
httpMetric.Bytes += int64(len(contents))
|
||||||
|
|
||||||
return contents, resp, err
|
return contents, resp, err
|
||||||
|
|
Loading…
Reference in New Issue