-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (55 loc) · 1.38 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"
"net/http"
"github.com/alecthomas/kingpin"
"github.com/caarlos0/go-solarman"
"github.com/caarlos0/solarman-exporter/collector"
"github.com/caarlos0/solarman-exporter/config"
"github.com/charmbracelet/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// nolint: gochecknoglobals
var (
bind = kingpin.Flag("bind", "addr to bind the server").
Short('b').
Default(":9230").
String()
version = "main"
)
func main() {
kingpin.Version("solarman-exporter version " + version)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Info("starting solarman-exporter", "version", version)
cfg := config.Must()
client, err := solarman.New(
cfg.AppID,
cfg.AppSecret,
cfg.Email,
cfg.Password,
)
if err != nil {
log.Fatal("error creating client", "err", err)
}
prometheus.MustRegister(collector.CurrentCollector(client, cfg.InverterSN))
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(
w, `
<html>
<head><title>Solarman Exporter</title></head>
<body>
<h1>Solarman Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>
`,
)
})
log.Info("listening", "addr", *bind)
if err := http.ListenAndServe(*bind, nil); err != nil {
log.Fatal("error starting server", "err", err)
}
}