forked from gitleakstest/gronit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
145 lines (131 loc) · 3.58 KB
/
server.go
File metadata and controls
145 lines (131 loc) · 3.58 KB
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
142
143
144
145
package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/boltdb/bolt"
"log"
"math/rand"
"net/http"
"regexp"
"strconv"
"sync"
"time"
)
const serverStartMsg = `
_____ _ _
/ ____| (_) |
| | __ _ __ ___ _ __ _| |_
| | |_ | '__/ _ \| '_ \| | __|
| |__| | | | (_) | | | | | |_
\_____|_| \___/|_| |_|_|\__|
Cron Monitoring
`
var mu sync.Mutex
var db *bolt.DB
// serverStart starts the gronit server which routes a few
// paths: add, update, list, remove, logs
func serverStart(opts *Options, _db *bolt.DB) {
db = _db
fmt.Printf("%s", serverStartMsg)
http.HandleFunc("/", create)
http.HandleFunc("/create", create)
http.HandleFunc("/run/", run)
http.HandleFunc("/complete/", complete)
http.HandleFunc("/clear/", clear)
http.HandleFunc("/status/", status)
http.HandleFunc("/history/", history)
http.HandleFunc("/summary/", summary)
host := fmt.Sprintf("localhost:%s", strconv.Itoa(opts.Port))
log.Fatal(http.ListenAndServe(host, nil))
}
// create new job monitor
func create(w http.ResponseWriter, r *http.Request) {
type idResponse struct {
ID string `json:"id"`
}
if r.Method == "GET" {
rand.Seed(time.Now().UTC().UnixNano())
h := sha256.New()
randomInt := rand.Intn(10000000)
h.Write([]byte(fmt.Sprintf("%d", randomInt)))
id := fmt.Sprintf("%x", h.Sum(nil)[:3])
err := initEntry(id, db, time.Now())
if err != nil {
http.Error(w, "failed to create entry", http.StatusForbidden)
}
w.Header().Set("Content-Type", "application/json")
idJSON, err := json.Marshal(idResponse{ID: id})
if err != nil {
http.Error(w, "failed to create entry", http.StatusForbidden)
}
w.Write(idJSON)
}
}
// getID extracts a job id and returns an error if invalid url
func getID(label string, r *http.Request) (string, error) {
regex := fmt.Sprintf("^/(%s)/([a-zA-Z0-9]+)$", label)
var validPath = regexp.MustCompile(regex)
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
return "", fmt.Errorf("could not extract id from path %s", r.URL.Path)
}
return m[2], nil
}
// run some things
func run(w http.ResponseWriter, r *http.Request) {
id, err := getID("run", r)
if err != nil {
fmt.Println("error")
}
setStatus(id, "running", time.Now(), db)
}
// complete
func complete(w http.ResponseWriter, r *http.Request) {
id, err := getID("complete", r)
if err != nil {
fmt.Println("error")
}
setStatus(id, "complete", time.Now(), db)
}
// clear
func clear(w http.ResponseWriter, r *http.Request) {
id, err := getID("clear", r)
if err != nil {
fmt.Println("error")
}
deleteBucket(id, db)
}
// status returns the status of the job
func status(w http.ResponseWriter, r *http.Request) {
id, err := getID("status", r)
status, err := getStatus(id, db)
statusJSON, err := json.Marshal(status)
if err != nil {
fmt.Println("Error retrieving history")
}
w.Header().Set("Content-Type", "application/json")
w.Write(statusJSON)
}
// history returns the full history of the job
func history(w http.ResponseWriter, r *http.Request) {
id, err := getID("history", r)
history, err := getHistory(id, db)
historyJSON, err := json.Marshal(history)
if err != nil {
fmt.Println("Error retrieving history")
}
w.Header().Set("Content-Type", "application/json")
w.Write(historyJSON)
}
// summary returns the summary of the job
func summary(w http.ResponseWriter, r *http.Request) {
id, err := getID("summary", r)
summary, err := getSummary(id, db)
summaryJSON, err := json.Marshal(summary)
if err != nil {
fmt.Println("Error retrieving history")
}
w.Header().Set("Content-Type", "application/json")
w.Write(summaryJSON)
}