Skip to content

Commit

Permalink
vue
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlong committed Mar 3, 2020
1 parent d5ffa3a commit eb5e291
Show file tree
Hide file tree
Showing 29 changed files with 355 additions and 542 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ install:

generate:
cd source && go generate
cd handlers/graphql && go generate

# remove files for a clean compile/build
clean:
rm -rf ./{logs,assets,plugins,*.db,config.yml,.sass-cache,config.yml,statping,build,.sass-cache,index.html,vendor}
rm -rf cmd/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log,*.html,*.json}
rm -rf core/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log}
rm -rf core/notifier/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log}
rm -rf handlers/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log}
rm -rf notifiers/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log}
rm -rf source/{logs,assets,plugins,*.db,config.yml,.sass-cache,*.log}
Expand All @@ -104,7 +104,7 @@ clean:
find . -name "*.out" -type f -delete
find . -name "*.cpu" -type f -delete
find . -name "*.mem" -type f -delete
rm -rf {build,tmp,docker}
rm -rf {build,tmp}

print_details:
@echo \==== Statping Development Instance ====
Expand Down
76 changes: 37 additions & 39 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ import (
"encoding/json"
"fmt"
"github.com/hunterlong/statping/core"
"github.com/hunterlong/statping/handlers"
"github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"io/ioutil"
"net/http/httptest"
"time"
)

Expand Down Expand Up @@ -72,25 +69,25 @@ func catchCLI(args []string) error {
updateDisplay()
return errors.New("end")
case "static":
var err error
if err = runLogs(); err != nil {
return err
}
if err = runAssets(); err != nil {
return err
}
fmt.Printf("Statping v%v Exporting Static 'index.html' page...\n", VERSION)
if _, err = core.LoadConfigFile(dir); err != nil {
log.Errorln("config.yml file not found")
return err
}
indexSource := ExportIndexHTML()
//core.CloseDB()
if err = utils.SaveFile(dir+"/index.html", indexSource); err != nil {
log.Errorln(err)
return err
}
log.Infoln("Exported Statping index page: 'index.html'")
//var err error
//if err = runLogs(); err != nil {
// return err
//}
//if err = runAssets(); err != nil {
// return err
//}
//fmt.Printf("Statping v%v Exporting Static 'index.html' page...\n", VERSION)
//if _, err = core.LoadConfigFile(dir); err != nil {
// log.Errorln("config.yml file not found")
// return err
//}
//indexSource := ExportIndexHTML()
////core.CloseDB()
//if err = utils.SaveFile(dir+"/index.html", indexSource); err != nil {
// log.Errorln(err)
// return err
//}
//log.Infoln("Exported Statping index page: 'index.html'")
case "help":
HelpEcho()
return errors.New("end")
Expand All @@ -103,10 +100,11 @@ func catchCLI(args []string) error {
if err = runAssets(); err != nil {
return err
}
if _, err = core.LoadConfigFile(dir); err != nil {
configs, err := core.LoadConfigFile(dir)
if err != nil {
return err
}
if err = core.CoreApp.Connect(false, dir); err != nil {
if err = core.CoreApp.Connect(configs, false, dir); err != nil {
return err
}
if data, err = core.ExportSettings(); err != nil {
Expand Down Expand Up @@ -167,19 +165,19 @@ func catchCLI(args []string) error {
}

// ExportIndexHTML returns the HTML of the index page as a string
func ExportIndexHTML() []byte {
source.Assets()
core.CoreApp.Connect(false, utils.Directory)
core.SelectAllServices(false)
core.CoreApp.UseCdn = types.NewNullBool(true)
for _, srv := range core.Services() {
core.CheckService(srv, true)
}
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/", nil)
handlers.ExecuteResponse(w, r, "index.gohtml", nil, nil)
return w.Body.Bytes()
}
//func ExportIndexHTML() []byte {
// source.Assets()
// core.CoreApp.Connect(core.CoreApp., utils.Directory)
// core.SelectAllServices(false)
// core.CoreApp.UseCdn = types.NewNullBool(true)
// for _, srv := range core.Services() {
// core.CheckService(srv, true)
// }
// w := httptest.NewRecorder()
// r := httptest.NewRequest("GET", "/", nil)
// handlers.ExecuteResponse(w, r, "index.gohtml", nil, nil)
// return w.Body.Bytes()
//}

func updateDisplay() error {
gitCurrent, err := checkGithubUpdates()
Expand All @@ -202,11 +200,11 @@ func updateDisplay() error {

// runOnce will initialize the Statping application and check each service 1 time, will not run HTTP server
func runOnce() error {
_, err := core.LoadConfigFile(utils.Directory)
configs, err := core.LoadConfigFile(utils.Directory)
if err != nil {
return errors.Wrap(err, "config.yml file not found")
}
err = core.CoreApp.Connect(false, utils.Directory)
err = core.CoreApp.Connect(configs, false, utils.Directory)
if err != nil {
return errors.Wrap(err, "issue connecting to database")
}
Expand Down
46 changes: 36 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func parseFlags() {
flag.Parse()
}

func exit(err error) {
fmt.Printf("%+v", core.Configs())
panic(err)
//log.Fatalln(err)
//os.Exit(2)
}

// main will run the Statping application
func main() {
var err error
Expand All @@ -80,35 +87,54 @@ func main() {
if err != nil {
if err.Error() == "end" {
os.Exit(0)
return
}
fmt.Println(err)
os.Exit(1)
exit(err)
}
}
log.Info(fmt.Sprintf("Starting Statping v%v", VERSION))
updateDisplay()

config, err := core.LoadConfigFile(utils.Directory)
if err := updateDisplay(); err != nil {
log.Warnln(err)
}

// check if DB_CONN was set, and load config from that
autoConfigDb := utils.Getenv("DB_CONN", "").(string)
if autoConfigDb != "" {
log.Infof("Environment variable 'DB_CONN' was set to %s, loading configs from ENV.", autoConfigDb)
if _, err := core.LoadUsingEnv(); err != nil {
exit(err)
return
} else {
afterConfigLoaded()
}
}

// attempt to load config.yml file from current directory, if no file, then start in setup mode.
_, err = core.LoadConfigFile(utils.Directory)
if err != nil {
log.Errorln(err)
core.CoreApp.Setup = false
writeAble, err := utils.DirWritable(utils.Directory)
if err != nil {
log.Fatalln(err)
exit(err)
return
}
if !writeAble {
log.Fatalf("Statping does not have write permissions at: %v\nYou can change this directory by setting the STATPING_DIR environment variable to a dedicated path before starting.", utils.Directory)
return
}
if err := handlers.RunHTTPServer(ipAddress, port); err != nil {
log.Fatalln(err)
}
} else {
afterConfigLoaded()
}
}

core.CoreApp.Config = config.DbConfig

func afterConfigLoaded() {
if err := mainProcess(); err != nil {
log.Fatalln(err)
os.Exit(2)
exit(err)
}
}

Expand Down Expand Up @@ -140,7 +166,7 @@ func loadDotEnvs() error {
func mainProcess() error {
dir := utils.Directory
var err error
err = core.CoreApp.Connect(false, dir)
err = core.CoreApp.Connect(core.Configs(), false, dir)
if err != nil {
log.Errorln(fmt.Sprintf("could not connect to database: %v", err))
return err
Expand Down
61 changes: 0 additions & 61 deletions core/checkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package core

import (
"fmt"
"github.com/ararog/timeago"
"github.com/hunterlong/statping/database"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
Expand Down Expand Up @@ -108,37 +107,6 @@ func SelectCheckin(api string) *Checkin {
return nil
}

// AllHits returns all of the CheckinHits for a given Checkin
func (c *Checkin) AllHits() []*types.CheckinHit {
var checkins []*types.CheckinHit
Database(&types.CheckinHit{}).Where("checkin = ?", c.Id).Order("id DESC").Find(&checkins)
return checkins
}

// Hits returns all of the CheckinHits for a given Checkin
func (c *Checkin) AllFailures() []*types.Failure {
var failures []*types.Failure
Database(&types.Failure{}).
Where("checkin = ?", c.Id).
Where("method = 'checkin'").
Order("id desc").
Find(&failures)

return failures
}

func (c *Checkin) GetFailures(count int) []*types.Failure {
var failures []*types.Failure
Database(&types.Failure{}).
Where("checkin = ?", c.Id).
Where("method = 'checkin'").
Limit(count).
Order("id desc").
Find(&failures)

return failures
}

// Create will create a new Checkin
func (c *Checkin) Delete() {
c.Close()
Expand Down Expand Up @@ -171,35 +139,6 @@ func (c *Checkin) Create() (int64, error) {
return c.Id, err
}

// Update will update a Checkin
func (c *Checkin) Update() (int64, error) {
row := Database(c).Update(&c)
if row.Error() != nil {
log.Warnln(row.Error())
return 0, row.Error()
}
return c.Id, row.Error()
}

// Create will create a new successful checkinHit
func (c *CheckinHit) Create() (int64, error) {
if c.CreatedAt.IsZero() {
c.CreatedAt = utils.Now()
}
row := Database(c).Create(&c)
if row.Error() != nil {
log.Warnln(row.Error())
return 0, row.Error()
}
return c.Id, row.Error()
}

// Ago returns the duration of time between now and the last successful checkinHit
func (c *CheckinHit) Ago() string {
got, _ := timeago.TimeAgoWithTime(utils.Now(), c.CreatedAt)
return got
}

// RecheckCheckinFailure will check if a Service Checkin has been reported yet
func (c *Checkin) RecheckCheckinFailure(guard chan struct{}) {
between := utils.Now().Sub(utils.Now()).Seconds()
Expand Down
Loading

0 comments on commit eb5e291

Please sign in to comment.