-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
69 lines (58 loc) · 2.16 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
package main
import (
"flag"
"log"
"net/http"
"os"
"time"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
addr := flag.String("addr", ":10000", "service addr")
influxdbURL := flag.String("influxdb.url", getenv("INFLUXDB_URL", "http://localhost:8086"), "influxdb url")
influxdbToken := flag.String("influxdb.token", getenv("INFLUXDB_TOKEN", ""), "influxdb token")
influxdbBucket := flag.String("influxdb.bucket", getenv("INFLUXDB_BUCKET", ""), "influxdb bucket")
influxdbTimeout := flag.Duration("influxdb.timeout", mustParseDuration(getenv("INFLUXDB_TIMEOUT", "15m")), "influxdb client timeout")
rabbitmqURL := flag.String("rabbitmq.url", getenv("RABBITMQ_URL", ""), "rabbitmq url")
streamHeartbeatDuration := flag.Duration("stream.heartbeat-duration", mustParseDuration(getenv("STREAM_HEARTBEAT_DURATION", "15s")), "stream heartbeat duration")
flag.Parse()
log.Printf("connecting to influxdb at %s", *influxdbURL)
client := influxdb2.NewClient(*influxdbURL, *influxdbToken)
defer client.Close()
// TODO figure out reasonable timeout on potentially large result sets
client.Options().HTTPClient().Timeout = *influxdbTimeout
querySvc := NewService(&ServiceConfig{
Backend: &InfluxBackend{
Client: client,
Org: "waggle",
Bucket: *influxdbBucket,
},
})
streamSvc := &StreamService{
RabbitMQURL: *rabbitmqURL,
HeartbeatDuration: *streamHeartbeatDuration,
}
// NOTE temporarily redirecting to sage docs. can change to something better later.
http.Handle("/", http.RedirectHandler("https://docs.waggle-edge.ai/docs/tutorials/accessing-data", http.StatusTemporaryRedirect))
http.Handle("/metrics", promhttp.Handler())
http.Handle("/api/v1/query", querySvc)
http.Handle("/api/v0/stream", streamSvc)
log.Printf("service listening on %s", *addr)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal(err)
}
}
func getenv(key string, fallback string) string {
if s, ok := os.LookupEnv(key); ok {
return s
}
return fallback
}
func mustParseDuration(s string) time.Duration {
d, err := time.ParseDuration(s)
if err != nil {
panic(err)
}
return d
}