/
server.go
70 lines (57 loc) · 1.31 KB
/
server.go
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
package main
import (
"os"
"http"
"template"
)
//DB fields to be used in the template
type Entry struct {
Package string
Version string
Cause string
Datecreated string
Component string
Content string
URL string
}
type P struct {
Entries map[string]Entry
}
var p P
func fillEntries() {
var entry *Entry
p = P{}
p.Entries = make(map[string]Entry)
q := collection.Find(nil)
q.For(&entry, func() os.Error {
p.Entries[entry.Package] = *entry
return nil
})
}
func viewHandle(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFile("templates/view.html")
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
t.Execute(w, p)
}
func logViewHandle(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFile("templates/logview.html")
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
pkg := r.URL.Path[9:]
t.Execute(w, p.Entries[pkg])
}
//Start the web server
func runServer(port string, s chan int) {
http.HandleFunc("/", viewHandle)
http.HandleFunc("/logview/", logViewHandle)
pwd, _ := os.Getwd()
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(pwd+"/static/"))))
err := http.ListenAndServe(":"+port, nil)
if err != nil {
println(err.String())
}
s <- 1
}