-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
66 lines (57 loc) · 1.65 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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rafaelvieiras/jellyfin-exporter/pkg/config"
"github.com/rafaelvieiras/jellyfin-exporter/pkg/metrics"
)
func main() {
// Load the environment variables
err := config.LoadEnvironment(".env")
if err != nil {
log.Fatalf("Error loading environment variables: %v", err)
}
// Get the environment variables
jellyfinApiUrl := os.Getenv("JELLYFIN_API_URL")
jellyfinToken := os.Getenv("JELLYFIN_TOKEN")
metricsPath := os.Getenv("METRICS_PATH")
serverAddr := os.Getenv("SERVER_ADDR")
serverPort := os.Getenv("SERVER_PORT")
// Validate the environment variables
if jellyfinApiUrl == "" {
log.Fatal("JELLYFIN_API_URL not set")
}
if jellyfinToken == "" {
log.Fatal("JELLYFIN_TOKEN not set")
}
if metricsPath == "" {
log.Fatal("METRICS_PATH not set")
}
if serverPort == "" {
log.Fatal("SERVER_PORT not set")
}
// Collect metrics periodically
go func() {
for {
metrics.FetchConnectedClients(jellyfinApiUrl, jellyfinToken)
metrics.FetchMediaCounts(jellyfinApiUrl, jellyfinToken)
metrics.FetchStreamCounts(jellyfinApiUrl, jellyfinToken)
metrics.FetchScheduledTasks(jellyfinApiUrl, jellyfinToken)
// Wait before collecting again (ex: 15 seconds)
time.Sleep(15 * time.Second)
}
}()
// Configure the server and route
http.Handle(metricsPath, promhttp.Handler())
server := &http.Server{
Addr: serverAddr + ":" + serverPort,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
// Start the server
log.Printf("Starting server on port %s", serverPort)
log.Fatal(server.ListenAndServe())
}