2020-12-03 11:42:25 +01:00
|
|
|
// nolint:gomnd,exhaustivestruct
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.xsfx.dev/xsteadfastx/logginghandler"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2020-12-09 09:12:00 +01:00
|
|
|
"github.com/spf13/viper"
|
2020-12-03 11:42:25 +01:00
|
|
|
)
|
|
|
|
|
2020-12-09 08:38:00 +01:00
|
|
|
// Logic Jellyfin URL.
|
2020-12-03 11:42:25 +01:00
|
|
|
type Logic struct {
|
2020-12-09 09:12:00 +01:00
|
|
|
InternalURL string
|
|
|
|
ExternalURL string
|
2020-12-03 11:42:25 +01:00
|
|
|
}
|
|
|
|
|
2020-12-09 08:38:00 +01:00
|
|
|
// PublicInfo Chromecast configuration Payload.
|
2020-12-08 12:46:52 +01:00
|
|
|
type PublicInfo struct {
|
|
|
|
ID string
|
|
|
|
LocalAddress string
|
|
|
|
OperatingSystem string
|
|
|
|
ProductName string
|
|
|
|
ServerName string
|
|
|
|
StartupWizardCompleted bool
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
2020-12-03 11:42:25 +01:00
|
|
|
func (l Logic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logger := logginghandler.Logger(r)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // nolint:gomnd
|
|
|
|
defer cancel()
|
|
|
|
|
2020-12-09 09:12:00 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/System/Info/Public", l.InternalURL), nil)
|
2020-12-03 11:42:25 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.Error().Msg(err.Error())
|
|
|
|
http.Error(w, "could not create request jellyfin", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error().Msg(err.Error())
|
|
|
|
http.Error(w, "could not access jellyfin", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error().Msg(err.Error())
|
|
|
|
http.Error(w, "could not read body", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-08 12:46:52 +01:00
|
|
|
var jd PublicInfo
|
2020-12-03 11:42:25 +01:00
|
|
|
if err := json.Unmarshal(body, &jd); err != nil {
|
|
|
|
logger.Error().Msg(err.Error())
|
|
|
|
http.Error(w, "could not unmarshal body", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2020-12-09 08:38:00 +01:00
|
|
|
|
2020-12-09 09:12:00 +01:00
|
|
|
jd.LocalAddress = l.ExternalURL
|
2020-12-09 08:38:00 +01:00
|
|
|
|
2020-12-03 11:42:25 +01:00
|
|
|
nd, err := json.Marshal(jd)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error().Msg(err.Error())
|
|
|
|
http.Error(w, "could not marshal body", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(nd); err != nil {
|
|
|
|
logger.Error().Msg(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
log.Logger = log.Logger.With().Caller().Logger()
|
|
|
|
|
2020-12-09 09:12:00 +01:00
|
|
|
viper.SetEnvPrefix("jellyfixer")
|
|
|
|
|
|
|
|
if err := viper.BindEnv("internal_url"); err != nil {
|
|
|
|
log.Fatal().Msg(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := viper.BindEnv("external_url"); err != nil {
|
|
|
|
log.Fatal().Msg(err.Error())
|
2020-12-03 11:42:25 +01:00
|
|
|
}
|
|
|
|
|
2020-12-09 09:12:00 +01:00
|
|
|
if len(os.Args) == 2 {
|
|
|
|
viper.SetConfigFile(os.Args[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
if viper.GetString("internal_url") == "" {
|
|
|
|
log.Fatal().Msg("needs key 'internal_url'")
|
|
|
|
}
|
|
|
|
|
|
|
|
if viper.GetString("external_url") == "" {
|
|
|
|
log.Fatal().Msg("needs key 'external_url'")
|
|
|
|
}
|
|
|
|
|
|
|
|
l := Logic{
|
|
|
|
InternalURL: viper.GetString("internal_url"),
|
|
|
|
ExternalURL: viper.GetString("external_url"),
|
|
|
|
}
|
2020-12-03 11:42:25 +01:00
|
|
|
handler := logginghandler.Handler(l)
|
2020-12-08 12:46:52 +01:00
|
|
|
|
2020-12-03 11:42:25 +01:00
|
|
|
http.Handle("/", handler)
|
|
|
|
log.Info().Msg("starting server...")
|
|
|
|
log.Fatal().Msg(http.ListenAndServe("0.0.0.0:8088", nil).Error())
|
|
|
|
}
|