diff options
| author | Horus3 | 2015-02-24 16:33:24 +0100 |
|---|---|---|
| committer | Horus3 | 2015-02-24 16:33:24 +0100 |
| commit | 8763022c77f0ad8edf6578962a524715d99e8d39 (patch) | |
| tree | c5b2557c56ebad2929cd02951b719aeff68b73ba /app/handler.go | |
| parent | 70d080b69a1bce3125d8d0b83e23775880241763 (diff) | |
| download | statuspage-8763022c77f0ad8edf6578962a524715d99e8d39.tar.gz | |
Session storage. Add new jobs over Webpage. Improved makefile.
Diffstat (limited to 'app/handler.go')
| -rw-r--r-- | app/handler.go | 91 |
1 files changed, 77 insertions, 14 deletions
diff --git a/app/handler.go b/app/handler.go index 510514a..7dd8cc4 100644 --- a/app/handler.go +++ b/app/handler.go @@ -3,9 +3,9 @@ package main import ( "encoding/json" "fmt" - "github.com/garyburd/redigo/redis" "log" "net/http" + "net/url" ) func IndexHandler(w http.ResponseWriter, r *http.Request) { @@ -14,8 +14,8 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { c := pool.Get() defer c.Close() - j, err := redis.String(c.Do("GET", cache_prefix+"database")) - if err == nil { + j, err := GetCache(cache_prefix + "database") + if j != "" { err = json.Unmarshal([]byte(j), &hosts) if err != nil { log.Println("Error JSON decoding: ", err) @@ -23,7 +23,7 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { CacheHosts(cache_prefix+"database", hosts) } } else { - log.Println("Redis: ", err) + log.Println("Cache miss on Index page.") Db.Find(&hosts) CacheHosts(cache_prefix+"database", hosts) } @@ -32,10 +32,19 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { err = index.ExecuteTemplate(w, "index.html", hosts) if err != nil { - log.Panic(err) + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return } } +func SessionHandler(w http.ResponseWriter, r *http.Request) { + session, _ := store.Get(r, "session-name") + session.Values["foo"] = "bar" + session.Values[42] = 43 + session.Save(r, w) +} + func RegisterHandler(w http.ResponseWriter, r *http.Request) { log.Println("Processing registration!") fmt.Fprintf(w, "Processing registration! \n") @@ -48,10 +57,24 @@ func PrintRegisterHandler(w http.ResponseWriter, r *http.Request) { func PrintNewJobHandler(w http.ResponseWriter, r *http.Request) { log.Println("Printing job") + var t string - job := mainTempl.Lookup("jobs.html") - - err := job.ExecuteTemplate(w, "jobs.html", nil) + session, err := store.Get(r, "_SID") + flashes := session.Flashes("success") + if len(flashes) > 0 { + t = "jobs.html" + } else { + flashes = session.Flashes("error") + t = "jobs_error.html" + } + session.Save(r, w) + job := mainTempl.Lookup(t) + + //flashes := session.Flashes() + //flashes := session.Flashes("success") + //flashes := session.Flashes("error") + fmt.Println(flashes) + err = job.ExecuteTemplate(w, t, flashes) if err != nil { log.Panic(err) } @@ -59,21 +82,61 @@ func PrintNewJobHandler(w http.ResponseWriter, r *http.Request) { func AddNewJobHandler(w http.ResponseWriter, r *http.Request) { log.Printf("Add new job") + session, err := store.Get(r, "_SID") + if err != nil { + log.Println(err) + } - err := r.ParseForm() + err = r.ParseForm() if err != nil { log.Panic(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return } - host := &Host{} - err = decoder.Decode(host, r.PostForm) + host := Host{} + err = decoder.Decode(&host, r.PostForm) if err != nil { - log.Panic(err) + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return } + u, err := url.Parse(host.Url) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if u.Scheme != "http" && u.Scheme != "https" { + log.Println("Scheme neither http nor https.", u.Scheme) + session.AddFlash("Unsurportted scheme. Only http:// and https:// is valid.", "error") + session.Save(r, w) + http.Redirect(w, r, "/new", 302) + // http.Error(w, "Scheme neither http nor https.", http.StatusInternalServerError) + return + } + + host.Monitored = true + log.Printf("%v", host) - fmt.Fprintf(w, "%s", host.Url) - Db.Debug().Save(host) + Db.Debug().Save(&host) + + go func() { + h := []Host{} + h = append(h, host) + h = CheckPage(h) + for k, _ := range h { + Db.Debug().Save(&h[k]) + } + }() + + session.AddFlash("Job added!", "success") + session.Save(r, w) + + http.Redirect(w, r, "/new", 302) + } func ShowJobHandler(w http.ResponseWriter, r *http.Request) { |
