-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
87 lines (73 loc) · 1.77 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
package main
import (
_ "embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/patrickmn/go-cache"
)
var c *cache.Cache
type Dashboard struct {
Owner string
Repo string
DateGenerated string
NextGeneration string
Data map[string][]Status
}
func main() {
c = cache.New(15*time.Minute, 30*time.Minute)
http.HandleFunc("/", handleRequest)
log.Print("Listening on :3000...")
err := http.ListenAndServe(":3000", nil) //nolint: gosec
if err != nil {
log.Fatal(err)
}
}
type Status struct {
SHA string
Status string
Event string
Conclusion string
TableStatus string
JobHTML string
WorkflowID int64
CreatedAt time.Time
PRUrl string
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "404 not found.", http.StatusNotFound)
return
}
switch r.Method {
case "GET":
input := fmt.Sprintf("%s/templates/input.html", http.Dir(os.Getenv("KO_DATA_PATH")))
http.ServeFile(w, r, input)
case "POST":
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
owner := r.FormValue("owner")
repo := r.FormValue("repo")
serveTemplate(w, r, owner, repo)
default:
fmt.Fprintf(w, "Sorry, only GET and POST methods are supported.")
}
}
func serveTemplate(w http.ResponseWriter, _ *http.Request, owner, repo string) {
dataReport := getJobs(c, owner, repo)
lp := filepath.Join(os.Getenv("KO_DATA_PATH"), "templates/dashboard.html")
tmpl, _ := template.ParseFiles(lp)
w.Header().Set("Content-Type", "text/html")
err := tmpl.ExecuteTemplate(w, "dashboard", dataReport)
if err != nil {
// Log the detailed error
log.Print(err.Error())
http.Error(w, http.StatusText(500), 500)
}
}