forked from drone/drone-wall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (84 loc) · 2.3 KB
/
main.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
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
package main
import (
"database/sql"
"encoding/json"
"flag"
"github.com/GeertJohan/go.rice"
. "github.com/drone/drone/shared/model"
_ "github.com/mattn/go-sqlite3"
"github.com/russross/meddler"
"html/template"
"log"
"net/http"
)
var (
repos = flag.String("repos", "", "Comma-delimited list of repos to watch")
team = flag.String("team", "", "Team slug to watch")
port = flag.String("port", ":8080", "")
refresh = flag.Int("refresh", 10, "Refresh interval")
driver = flag.String("driver", "sqlite3", "")
datasource = flag.String("datasource", "drone.sqlite", "")
wallTemplate *template.Template
)
// Wall display handler
func wall(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html; charset=utf-8")
err := wallTemplate.ExecuteTemplate(w, "_", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
// API endpoint for fetching the initial wall display data via AJAX
func wallData(w http.ResponseWriter, r *http.Request) {
var commits []*CommitRepo
var err error
if *team != "" {
log.Println("teams")
// list of recent team commits
commits, err = listTeamWallCommits()
} else {
log.Println("repos")
// list of recent commits
commits, err = listRepoWallCommits()
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(commits)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
func setupStatic() {
box := rice.MustFindBox("assets")
http.Handle("/fonts/", http.FileServer(box.HTTPBox()))
http.Handle("/css/", http.FileServer(box.HTTPBox()))
http.Handle("/js/", http.FileServer(box.HTTPBox()))
}
func setupTemplate() {
box := rice.MustFindBox("templates")
// parse the wall display template and then add to the global map
page, err := box.String("watcher.html")
if err != nil {
panic(err)
}
wallTemplate = template.Must(template.New("_").Parse(page))
}
func main() {
flag.Parse()
setupStatic()
setupTemplate()
meddler.Default = meddler.SQLite
var err error
db, err = sql.Open(*driver, *datasource)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/refresh", wallData)
http.HandleFunc("/", wall)
log.Fatal(http.ListenAndServe(*port, nil))
}