-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (61 loc) · 1.34 KB
/
main.go
File metadata and controls
71 lines (61 loc) · 1.34 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
package main
import (
"fmt"
"github.com/alecthomas/kingpin/v2"
"github.com/gorilla/mux"
"github.com/prometheus/common/version"
"net/http"
"time"
)
const (
usage = `
/start : start sending 20 log/s for 1 minute
/stop : stop sending logs
/* : give this usage
`
)
type app struct {
end time.Time
}
func (a *app) start(w http.ResponseWriter, _ *http.Request) {
a.end = time.Now().Add(1 * time.Minute)
w.WriteHeader(200)
}
func (a *app) stop(w http.ResponseWriter, _ *http.Request) {
a.end = time.Now()
w.WriteHeader(200)
}
func (a *app) defaultHandler(w http.ResponseWriter, _ *http.Request) {
// nolint:errcheck
w.Write([]byte(usage))
w.WriteHeader(200)
}
func (a *app) bg() {
for {
if time.Now().After(a.end) {
time.Sleep(1 * time.Second)
continue
}
for i := 0; i < 20; i++ {
fmt.Printf("{\"message\": \"here is some log %d\"}\n", i)
}
time.Sleep(1 * time.Second)
}
}
func main() {
kingpin.Version(version.Print("standard-app"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
a := &app{
end: time.Now(),
}
go a.bg()
router := mux.NewRouter()
router.HandleFunc("/start", a.start)
router.HandleFunc("/stop", a.stop)
router.NotFoundHandler = http.HandlerFunc(a.defaultHandler)
router.HandleFunc("/", a.stop)
fmt.Printf("listening on :8080\n")
fmt.Printf("%s", usage)
panic(http.ListenAndServe(":8080", router))
}