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 string
|
||||
// COMMIT stores the git commit hash for this version of Statping
|
||||
COMMIT string
|
||||
log = utils.Log.WithField("type", "cmd")
|
||||
confgs *configs.DbConfig
|
||||
COMMIT string
|
||||
log = utils.Log.WithField("type", "cmd")
|
||||
confgs *configs.DbConfig
|
||||
process chan struct{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
process = make(chan struct{})
|
||||
core.New(VERSION)
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
rootCmd.AddCommand(assetsCmd)
|
||||
|
@ -42,8 +44,8 @@ func init() {
|
|||
// exit will return an error and return an exit code 1 due to this error
|
||||
func exit(err error) {
|
||||
utils.SentryErr(err)
|
||||
Close()
|
||||
log.Fatalln(err)
|
||||
close(process)
|
||||
}
|
||||
|
||||
// Close will gracefully stop the database connection, and log file
|
||||
|
@ -55,14 +57,15 @@ func Close() {
|
|||
|
||||
// main will run the Statping application
|
||||
func main() {
|
||||
Execute()
|
||||
go Execute()
|
||||
<-process
|
||||
Close()
|
||||
}
|
||||
|
||||
// main will run the Statping application
|
||||
func start() {
|
||||
var err error
|
||||
go sigterm()
|
||||
|
||||
var err error
|
||||
if err := source.Assets(); err != nil {
|
||||
exit(err)
|
||||
}
|
||||
|
@ -75,14 +78,10 @@ func start() {
|
|||
|
||||
log.Info(fmt.Sprintf("Starting Statping v%s", VERSION))
|
||||
|
||||
//if err := updateDisplay(); err != nil {
|
||||
// log.Warnln(err)
|
||||
//}
|
||||
|
||||
confgs, err = configs.LoadConfigs(configFile)
|
||||
if err != nil {
|
||||
log.Infoln("Starting in Setup Mode")
|
||||
if err := SetupMode(); err != nil {
|
||||
if err = handlers.RunHTTPServer(); err != nil {
|
||||
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
|
||||
func sigterm() {
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigs
|
||||
Close()
|
||||
close(process)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
|
@ -160,7 +155,7 @@ func mainProcess() error {
|
|||
|
||||
services.LoadServicesYaml()
|
||||
|
||||
if err := handlers.RunHTTPServer(ipAddress, port); err != nil {
|
||||
if err := handlers.RunHTTPServer(); err != nil {
|
||||
log.Fatalln(err)
|
||||
return errors.Wrap(err, "http server")
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
|
@ -26,12 +25,23 @@ var (
|
|||
usingSSL bool
|
||||
mainTmpl = `{{define "main" }} {{ template "base" . }} {{ end }}`
|
||||
templates = []string{"base.gohtml"}
|
||||
httpError chan error
|
||||
)
|
||||
|
||||
// RunHTTPServer will start a HTTP server on a specific IP and port
|
||||
func RunHTTPServer(ip string, port int) error {
|
||||
host := fmt.Sprintf("%v:%v", ip, port)
|
||||
func StopHTTPServer(err error) {
|
||||
log.Infoln("Stopping HTTP Server")
|
||||
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")
|
||||
cert := utils.FileExists(utils.Directory + "/server.crt")
|
||||
|
||||
|
@ -47,37 +57,18 @@ func RunHTTPServer(ip string, port int) error {
|
|||
resetCookies()
|
||||
|
||||
if usingSSL {
|
||||
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,
|
||||
}
|
||||
return srv.ListenAndServeTLS(utils.Directory+"/server.crt", utils.Directory+"/server.key")
|
||||
go startSSLServer(ip)
|
||||
} else {
|
||||
httpServer = &http.Server{
|
||||
Addr: host,
|
||||
WriteTimeout: timeout,
|
||||
ReadTimeout: timeout,
|
||||
IdleTimeout: timeout,
|
||||
Handler: router,
|
||||
go startServer(host)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
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.
|
||||
func Assets() error {
|
||||
if utils.Params.GetBool("DISABLE_HTTP") {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
TmplBox, err = rice.FindBox("dist")
|
||||
return err
|
||||
|
|
|
@ -8,46 +8,23 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
Params *viper.Viper
|
||||
configLog = Log.WithField("type", "configs")
|
||||
Params *viper.Viper
|
||||
)
|
||||
|
||||
func initCLI() {
|
||||
func initEnvs() {
|
||||
Params = viper.New()
|
||||
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
|
||||
defaultDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
configLog.Errorln(err)
|
||||
Log.Errorln(err)
|
||||
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("GO_ENV", "")
|
||||
Params.SetDefault("DB_CONN", "")
|
||||
|
@ -72,6 +49,7 @@ func setDefaults() {
|
|||
Params.SetDefault("LOGS_MAX_COUNT", 5)
|
||||
Params.SetDefault("LOGS_MAX_AGE", 28)
|
||||
Params.SetDefault("LOGS_MAX_SIZE", 16)
|
||||
Params.SetDefault("DISABLE_COLORS", false)
|
||||
|
||||
dbConn := Params.GetString("DB_CONN")
|
||||
dbInt := Params.GetInt("DB_PORT")
|
||||
|
@ -83,4 +61,25 @@ func setDefaults() {
|
|||
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
|
||||
func InitLogs() error {
|
||||
initCLI()
|
||||
initEnvs()
|
||||
if Params.GetBool("DISABLE_LOGS") {
|
||||
return nil
|
||||
}
|
||||
|
@ -164,8 +164,8 @@ func InitLogs() error {
|
|||
Log.SetOutput(mw)
|
||||
|
||||
Log.SetFormatter(&Logger.TextFormatter{
|
||||
ForceColors: true,
|
||||
DisableColors: false,
|
||||
ForceColors: !Params.GetBool("DISABLE_COLORS"),
|
||||
DisableColors: Params.GetBool("DISABLE_COLORS"),
|
||||
})
|
||||
checkVerboseMode()
|
||||
return nil
|
||||
|
|
|
@ -254,11 +254,12 @@ func HttpRequest(url, method string, content interface{}, headers []string, body
|
|||
}
|
||||
defer resp.Body.Close()
|
||||
contents, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
// record HTTP metrics
|
||||
t2 := Now().Sub(t1).Milliseconds()
|
||||
httpMetric.Requests++
|
||||
httpMetric.Milliseconds += t2 / httpMetric.Requests
|
||||
httpMetric.Milliseconds += Now().Sub(t1).Milliseconds() / httpMetric.Requests
|
||||
httpMetric.Bytes += int64(len(contents))
|
||||
|
||||
return contents, resp, err
|
||||
|
|
Loading…
Reference in New Issue