diff options
| author | Horus3 | 2015-02-21 14:50:00 +0100 |
|---|---|---|
| committer | Horus3 | 2015-02-21 14:50:00 +0100 |
| commit | b58e21a525e7e4aeda3ec51bfcb07390af8be465 (patch) | |
| tree | 9974d885ae2c81052e881e00207677e80d9f6fc7 /app | |
| parent | c92989a8fc738094b205094a1bdd458a40c23d9c (diff) | |
| download | statuspage-b58e21a525e7e4aeda3ec51bfcb07390af8be465.tar.gz | |
Index page is now cached.
Diffstat (limited to 'app')
| -rw-r--r-- | app/.s | 0 | ||||
| -rw-r--r-- | app/Makefile | 11 | ||||
| -rw-r--r-- | app/db.go | 5 | ||||
| -rwxr-xr-x | app/env.sh | 2 | ||||
| -rw-r--r-- | app/fetch.go | 38 | ||||
| -rw-r--r-- | app/handler.go | 32 | ||||
| -rw-r--r-- | app/main.go | 4 | ||||
| -rw-r--r-- | app/struct.go | 7 | ||||
| -rw-r--r-- | app/test.go | 17 | ||||
| -rw-r--r-- | app/utilities.go | 12 |
10 files changed, 107 insertions, 21 deletions
diff --git a/app/Makefile b/app/Makefile index 06aed05..15da585 100644 --- a/app/Makefile +++ b/app/Makefile @@ -32,6 +32,8 @@ clean: kill @rm $(IMPORT_FILE) || true @echo "Removing sqlite3 database..." @rm $(STATUS_DB_CREDENTIALS) || true + @echo "Removing tar archiv..." + @rm ../$(APP).tar.gz || true @echo "Removing binary..." @rm $(APP) @echo "Done." @@ -68,4 +70,11 @@ mysql: create_import postgresql: create_import @echo "import _ \"github.com/lib/pq\"" >> $(IMPORT_FILE) -database_all: create_import sqlite3 mysql postgresql +database_all: sqlite3 mysql postgresql + @echo "Created import file for all databases." + +pack: + @cd .. && \ + tar czf $(APP).tar.gz app/$(APP) app/env.sh app/Makefile views static db 2>/dev/null && \ + echo "../$(APP).tar.gz is ready." || \ + (echo "Run \"make build\" first." && exit 1) @@ -12,7 +12,10 @@ func InitDB() { if dberr != nil { log.Panic(dberr) } - Db.LogMode(true) + logMode := os.Getenv("STATUS_DB_LOG") + if logMode == "true" { + Db.LogMode(true) + } if err := Db.DB().Ping(); err != nil { log.Panic(err) } @@ -1,9 +1,11 @@ #!/bin/sh +# Call this script with make or source it. # Database export STATUS_DB_DRIVER=sqlite3 export STATUS_DB_CREDENTIALS=../db/status.db export STATUS_DB_IMPORT_DRIVER=github.com/mattn/go-sqlite3 +export STATUS_DB_LOG=true # Redis export STATUS_REDIS_SERVER=127.0.0.1 diff --git a/app/fetch.go b/app/fetch.go index 898329d..c8b0376 100644 --- a/app/fetch.go +++ b/app/fetch.go @@ -3,7 +3,7 @@ package main import ( "encoding/json" "fmt" - "strconv" + // "strconv" // "time" "log" ) @@ -26,14 +26,26 @@ const ( func healthCheck() { h := []Host{} db := Db + //db.Where("monitored = ?", true).Find(&h) db.Find(&h) c := pool.Get() defer c.Close() for k, v := range h { + if !h[k].Monitored { + continue + } + if resp, _, err := HttpGet(v.Url); err != nil { - fmt.Printf("Error! %v \n", err) + //fmt.Println(err) + + h[k].Status = "Error" + h[k].StatusCode = 0 + h[k].Success = false + h[k].Monitored = false + h[k].Reason = fmt.Sprintf("%v", err) + Db.Debug().Save(&h[k]) } else { // fmt.Println("Health check: Okay! ", resp.Status) @@ -41,17 +53,20 @@ func healthCheck() { h[k].Status = resp.Status h[k].StatusCode = int64(resp.StatusCode) h[k].Success = true + h[k].Reason = "" Db.Debug().Save(&h[k]) } // save struct in redis as serialized json // TODO - j, err := json.Marshal(h[k]) - if err != nil { - log.Println("Error: ", err) - continue - } - c.Do("SET", cache_prefix+strconv.Itoa(k), j) + /* + j, err := json.Marshal(h[k]) + if err != nil { + log.Println("Error: ", err) + continue + } + c.Do("SET", cache_prefix+strconv.Itoa(k), j) + */ //c.Do("SET", cache_prefix+strconv.Itoa(k), j) //c.Do("SET", k, j) @@ -60,4 +75,11 @@ func healthCheck() { } + js, err := json.Marshal(h) + + if err != nil { + log.Fatal(err) + } else { + c.Do("SET", cache_prefix+"database", js) + } } diff --git a/app/handler.go b/app/handler.go index ad66657..9a3352f 100644 --- a/app/handler.go +++ b/app/handler.go @@ -3,19 +3,39 @@ package main import ( "fmt" // "html/template" + "encoding/json" + "github.com/garyburd/redigo/redis" "log" "net/http" ) func IndexHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello World! \n") - // w.Write() -} + log.Println("Print IndexHandler") + + hosts := []Host{} + + c := pool.Get() + defer c.Close() + + j, err := redis.String(c.Do("GET", cache_prefix+"database")) + if err == nil { + err = json.Unmarshal([]byte(j), &hosts) + if err != nil { + log.Println("Error JSON decoding: ", err) + Db.Find(&hosts) + } + } else { + log.Println("Redis: ", err) + Db.Find(&hosts) + } + + index := mainTempl.Lookup("index.html") -/* -func StaticHandler(w http.ResponseWrite, r *http.Request) { + err = index.ExecuteTemplate(w, "index.html", hosts) + if err != nil { + log.Panic(err) + } } -*/ func RegisterHandler(w http.ResponseWriter, r *http.Request) { log.Println("Processing registration!") diff --git a/app/main.go b/app/main.go index 3850a4d..d51cf20 100644 --- a/app/main.go +++ b/app/main.go @@ -23,15 +23,17 @@ func main() { r := mux.NewRouter() r.HandleFunc("/", IndexHandler) - //r.HandleFunc("/static", StaticHandler) r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./../static")))) r.HandleFunc("/register", RegisterHandler).Methods("POST") 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) + ip := os.Getenv("STATUS_HTTP_IP") port := os.Getenv("STATUS_HTTP_PORT") + http.ListenAndServe(ip+":"+port, nil) } diff --git a/app/struct.go b/app/struct.go index d7135aa..37704d7 100644 --- a/app/struct.go +++ b/app/struct.go @@ -7,20 +7,19 @@ import ( /* Maybe worth saving uptime history? */ type Host struct { - Id int64 - UserId int64 - Url string + Id int64 + Url string // Protocoll string // e.g. http Monitored bool // disable monitoring on error Private bool Status string StatusCode int64 Success bool + Reason string // Connection failure /* Date time.Time Include string // Website must include this string Except string // Website must not include this string - Reason string // Include, Exclude, Connection failure Alert bool // True to send alert on failure */ CreatedAt time.Time diff --git a/app/test.go b/app/test.go index 0b56fae..3cd71bc 100644 --- a/app/test.go +++ b/app/test.go @@ -1,6 +1,23 @@ package main +import ( +//"encoding/json" +) + func insertHost() { h := Host{Url: "https://mx.iamfabulous.de/", Monitored: true, Private: false} Db.Create(&h) + h = Host{Url: "https://iamfabulousadsfadfdsf.de/", Monitored: true, Private: false} + Db.Create(&h) + + c := pool.Get() + defer c.Close() + + /* + host := []Host{} + Db.Find(&host) + + js, _ := json.Marshal(host) + c.Do("SET", cache_prefix, js) + */ } diff --git a/app/utilities.go b/app/utilities.go index 5c665e2..3ec1b60 100644 --- a/app/utilities.go +++ b/app/utilities.go @@ -1,7 +1,9 @@ package main import ( + "bytes" "crypto/md5" + "encoding/gob" "fmt" "github.com/garyburd/redigo/redis" "golang.org/x/crypto/bcrypt" @@ -95,3 +97,13 @@ func VerifyPassword(password, hash string) bool { } return true } + +func GetBytes(key interface{}) ([]byte, error) { + var buf bytes.Buffer + enc := gob.NewEncoder(&buf) + err := enc.Encode(key) + if err != nil { + return nil, err + } + return buf.Bytes(), nil +} |
