diff options
| author | Horus3 | 2015-02-19 16:55:23 +0100 |
|---|---|---|
| committer | Horus3 | 2015-02-19 16:55:23 +0100 |
| commit | 9fd1b6a54c77f78df1031a620fe3fb3887eda56d (patch) | |
| tree | a22686f2eac23c7a47e9ccd9aa3962aa80a5bf64 | |
| parent | 5da13e75c013688f4fda1e57a5b3968332caa760 (diff) | |
| download | statuspage-9fd1b6a54c77f78df1031a620fe3fb3887eda56d.tar.gz | |
Add health check.
| -rw-r--r-- | app/Makefile | 6 | ||||
| -rw-r--r-- | app/db.go | 1 | ||||
| -rw-r--r-- | app/fetch.go | 28 | ||||
| -rw-r--r-- | app/handler.go | 8 | ||||
| -rw-r--r-- | app/main.go | 4 | ||||
| -rw-r--r-- | app/struct.go | 14 | ||||
| -rw-r--r-- | app/utilities.go | 14 |
7 files changed, 52 insertions, 23 deletions
diff --git a/app/Makefile b/app/Makefile index 4356e21..c614492 100644 --- a/app/Makefile +++ b/app/Makefile @@ -12,14 +12,14 @@ IMPORT_FILE:=import.go all: kill build run -clean: +clean: kill @echo "Removing import file..." @rm $(IMPORT_FILE) || true @echo "Removing sqlite3 database..." - @rm $(STATUS_DB_CREDENTIALS) + @rm $(STATUS_DB_CREDENTIALS) || true @echo "Removing binary..." @rm statuspage - @echo "Done" + @echo "Done." build: @echo "package main" > $(IMPORT_FILE) @@ -21,7 +21,6 @@ func InitDB() { h := Host{} Db.Debug().AutoMigrate(&u) db := Db - log.Println(db) db.Debug().AutoMigrate(&h) Db.Model(&u).AddUniqueIndex("idx_user_name", "name") diff --git a/app/fetch.go b/app/fetch.go index cd8206b..a03504e 100644 --- a/app/fetch.go +++ b/app/fetch.go @@ -2,16 +2,32 @@ package main import ( "fmt" - "github.com/robfig/cron" - "time" + // "time" ) func run() { - c := cron.New() - c.AddFunc("@every 1m", printStatus) + c := c + c.AddFunc("@every 30s", healthCheck) c.Start() + jobs := c.Entries() + + for _, i := range jobs { + fmt.Printf("Job: %v, Schedule: %v; Next %v; Prev %v \n", i.Job, i.Schedule, i.Next, i.Prev) + } } -func printStatus() { - fmt.Println(time.Now()) +func healthCheck() { + h := []Host{} + db := Db + db.Find(&h) + + for _, v := range h { + fmt.Printf("Id: %v, Url: %v \n", v.Id, v.Url) + + if resp, _, err := HttpGet(v.Url); err != nil { + fmt.Printf("Error! %v \n", err) + } else { + fmt.Println("Health check: Okay! ", resp.Status) + } + } } diff --git a/app/handler.go b/app/handler.go index d76782f..ad66657 100644 --- a/app/handler.go +++ b/app/handler.go @@ -58,3 +58,11 @@ func AddNewJobHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "%s", host.Url) Db.Debug().Save(host) } + +func ShowJobHandler(w http.ResponseWriter, r *http.Request) { + jobs := c.Entries() + + for _, i := range jobs { + fmt.Fprintf(w, "Job: %v, Schedule: %v; Next %v; Prev %v \n", i.Job, i.Schedule, i.Next, i.Prev) + } +} diff --git a/app/main.go b/app/main.go index ac2f27b..35a162a 100644 --- a/app/main.go +++ b/app/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/gorilla/mux" "github.com/gorilla/schema" + "github.com/robfig/cron" "html/template" "net/http" ) @@ -11,6 +12,8 @@ var decoder = schema.NewDecoder() var mainTempl = template.Must(template.New("global").ParseGlob("../views/*.html")) +var c = cron.New() + func main() { run() InitDB() @@ -23,6 +26,7 @@ func main() { r.HandleFunc("/register", PrintRegisterHandler).Methods("GET") r.HandleFunc("/new", AddNewJobHandler).Methods("POST") r.HandleFunc("/new", PrintNewJobHandler).Methods("GET") + r.HandleFunc("/jobs", ShowJobHandler) http.Handle("/", r) http.ListenAndServe(":8080", nil) } diff --git a/app/struct.go b/app/struct.go index 26c462b..15c238f 100644 --- a/app/struct.go +++ b/app/struct.go @@ -7,12 +7,14 @@ import ( /* Maybe worth saving uptime history? */ type Host struct { - Id int64 - UserId int64 - Url string - Protocoll string // e.g. http - Private bool - Response int64 + Id int64 + UserId int64 + Url string + Protocoll string // e.g. http + Monitored bool // disable monitoring on error + Private bool + Status string + StatusCode int64 /* Date time.Time Success bool diff --git a/app/utilities.go b/app/utilities.go index bc6c0ca..75967b1 100644 --- a/app/utilities.go +++ b/app/utilities.go @@ -14,19 +14,19 @@ import ( ) // Returns headers as map and the content of a webpage as string -func HttpGet(url string) (http.Header, string, error) { - response, err := http.Get(url) +func HttpGet(url string) (*http.Response, string, error) { + r, err := http.Get(url) if err != nil { - return nil, "Get request failed.", err + return r, "Get request failed.", err } - defer response.Body.Close() - contents, err := ioutil.ReadAll(response.Body) + defer r.Body.Close() + contents, err := ioutil.ReadAll(r.Body) if err != nil { - return nil, "Reading body failed.", err + return r, "Reading body failed.", err } - return response.Header, string(contents), nil + return r, string(contents), nil } // Hashs and returns a string (md5) |
