-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
63 lines (53 loc) · 1.39 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
package main
import (
"fmt"
"log"
"net/http"
"platform-cost-report/cloud"
"runtime"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/robfig/cron/v3"
)
func main() {
log.Printf("OS: %s\nArchitecture: %s\n", runtime.GOOS, runtime.GOARCH)
scheduler := cron.New()
// First exposed metrics on init
reg, err := cloud.AWSMetrics()
if err != nil {
panic(err)
}
_, err = scheduler.AddFunc("@every 12h", func() {
reg, err = cloud.AWSMetrics()
fmt.Println("AWS metrics updated")
if err != nil {
fmt.Println("Error: %w", err)
}
})
if err != nil {
panic(err)
}
scheduler.Start()
http.HandleFunc("/updatePricing", func(writter http.ResponseWriter, reader *http.Request) {
reg, err = cloud.AWSMetrics()
if err != nil {
fmt.Println("Error: %w", err)
writter.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(writter, "{\"error\":\"%v\"}", err)
return
}
writter.WriteHeader(http.StatusOK)
fmt.Fprintf(writter, "{\"message\":\"Pricing updated\"}")
})
http.HandleFunc("/health", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
fmt.Fprintf(rw, "{\"message\":\"OK\"}")
})
http.HandleFunc("/metrics", func(rw http.ResponseWriter, r *http.Request) {
handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
handler.ServeHTTP(rw, r)
})
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}