1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
hosts := []Host{}
c := pool.Get()
defer c.Close()
j, err := GetCache(cache_prefix + "database")
if j != "" {
err = json.Unmarshal([]byte(j), &hosts)
if err != nil {
log.Println("Error JSON decoding: ", err)
Db.Find(&hosts)
CacheHosts(cache_prefix+"database", hosts)
}
} else {
log.Println("Cache miss on Index page.")
Db.Find(&hosts)
CacheHosts(cache_prefix+"database", hosts)
}
index := mainTempl.Lookup("index.html")
err = index.ExecuteTemplate(w, "index.html", hosts)
if err != nil {
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")
}
func PrintRegisterHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Printing register etc! \n")
}
func PrintNewJobHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Printing job")
session, err := store.Get(r, "_SID")
m := FlashMessages{}
m.Success = session.Flashes("success")
m.Error = session.Flashes("error")
session.Save(r, w)
job := mainTempl.Lookup("jobs.html")
err = job.ExecuteTemplate(w, "jobs.html", m)
if err != nil {
log.Panic(err)
}
}
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()
if err != nil {
log.Panic(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
host := Host{}
err = decoder.Decode(&host, r.PostForm)
if err != nil {
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)
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) {
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)
}
}
|