updating plex json api parts
This commit is contained in:
57
main.go
57
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
|
||||
type serverSettings struct {
|
||||
port int
|
||||
jsonPort int
|
||||
plexAddr string
|
||||
plexToken string
|
||||
plexTimeout int
|
||||
@@ -33,6 +35,10 @@ func setupServerSettings() serverSettings {
|
||||
if err != nil {
|
||||
log.Fatal("Unable to parse port string to int, failing....")
|
||||
}
|
||||
jsonPort, err := strconv.Atoi(envGetter("JSONPORT", "9546"))
|
||||
if err != nil {
|
||||
log.Fatal("Unable to parse jsonapi port string to int, failing....")
|
||||
}
|
||||
plexToken, value := os.LookupEnv("PLEX_TOKEN")
|
||||
if !value {
|
||||
log.Fatal("No plex token provided, failing....")
|
||||
@@ -48,6 +54,7 @@ func setupServerSettings() serverSettings {
|
||||
|
||||
return serverSettings{
|
||||
port: port,
|
||||
jsonPort: jsonPort,
|
||||
plexAddr: envGetter("PLEX_ADDR", "http://localhost:32400"),
|
||||
plexToken: plexToken,
|
||||
plexTimeout: plexTimeout,
|
||||
@@ -56,9 +63,42 @@ func setupServerSettings() serverSettings {
|
||||
}
|
||||
}
|
||||
|
||||
func setupJSONAPI(plexStatsChannel chan *plexStats, settings serverSettings) {
|
||||
|
||||
// Create a new router
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// Create a handler for the `/plex-stats` endpoint
|
||||
mux.HandleFunc("/plex-stats", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Create a new plexStats struct
|
||||
plexStats := <-plexStatsChannel
|
||||
|
||||
// Marshal the plexStats struct to JSON
|
||||
jsonBody, err := json.Marshal(plexStats)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Set the content type to JSON
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// Write the JSON body to the response
|
||||
w.Write(jsonBody)
|
||||
})
|
||||
|
||||
// Start the server
|
||||
fmt.Println("Starting JSON API Server.....")
|
||||
err := http.ListenAndServe(fmt.Sprintf(":%d", settings.jsonPort), mux)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to start JSON Server on port: ", settings.jsonPort, " with err: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
os.Setenv("PORT", "9545")
|
||||
os.Setenv("JSONPORT", "9546")
|
||||
os.Setenv("PLEX_ADDR", "https://plex.derajnet.duckdns.org:32400")
|
||||
os.Setenv("PLEX_TOKEN", "5ezJu5cjnhoAbPJRKngs")
|
||||
os.Setenv("PLEX_TIMEOUT", "10")
|
||||
@@ -75,16 +115,21 @@ func main() {
|
||||
prometheus.MustRegister(plexTranscodeSessions)
|
||||
prometheus.MustRegister(plexAllSessions)
|
||||
|
||||
plexStatsChannel := make(chan *plexStats)
|
||||
|
||||
go setupJSONAPI(plexStatsChannel, settings)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Second * 60)
|
||||
time.Sleep(time.Second * 5)
|
||||
fmt.Println("Collecting info...")
|
||||
stats := pc.gatherAllStats()
|
||||
plexActiveSessions.Set(stats.currentSessions)
|
||||
plexNumMovies.Set(stats.numMovies)
|
||||
plexNumTVShows.Set(stats.numTV)
|
||||
plexTranscodeSessions.Set(stats.numTranscodes)
|
||||
plexAllSessions.Set(stats.numAllSessions)
|
||||
plexStatsChannel <- stats
|
||||
plexActiveSessions.Set(stats.CurrentSessions)
|
||||
plexNumMovies.Set(stats.NumMovies)
|
||||
plexNumTVShows.Set(stats.NumTV)
|
||||
plexTranscodeSessions.Set(stats.NumTranscodes)
|
||||
plexAllSessions.Set(stats.NumAllSessions)
|
||||
}
|
||||
}()
|
||||
|
||||
|
Reference in New Issue
Block a user