oneandone-billing-mailer/pkg/appcontext/app_context.go

95 lines
2.2 KiB
Go
Raw Permalink Normal View History

2022-08-17 01:34:18 +00:00
package appcontext
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"time"
"git.icedream.tech/icedream/oneandone-billing-mailer/pkg/backend"
"git.icedream.tech/icedream/oneandone-billing-mailer/pkg/environment"
mssahttp "git.icedream.tech/icedream/oneandone-billing-mailer/pkg/mssa/http"
)
type Entry struct {
BaseURI string `json:"baseURI"`
}
func (e *Entry) BaseURL() (*url.URL, error) {
return url.Parse(e.BaseURI)
}
type Config struct {
RefreshTokenRenewInterval time.Duration `json:"refreshTokenRenewInterval"`
}
func mustMakeURL(u string) *url.URL {
uo, err := url.Parse(u)
if err != nil {
panic(err)
}
return uo
}
var (
appcontext_uri_live = mustMakeURL("https://auth-proxy.1und1.de/appcontext/appcontext.json")
)
var instance *AppContext
func GetEnvironmentContext() (*AppContext, error) {
if instance == nil {
err := retrieveAppContextInternal()
if err != nil {
return nil, err
}
}
return instance, nil
}
type AppContext struct {
MSSA Entry `json:"MSSA"`
CentralLogin Entry `json:"CentralLogin"`
CentralLoginApp Entry `json:"CentralLoginApp"`
ChatServiceRest Entry `json:"ChatServiceRest"`
ChatServiceWebsocket Entry `json:"ChatServiceWebsocket"`
ChatServiceWebsocket2 Entry `json:"ChatServiceWebsocket2"`
DOPAS Entry `json:"DOPAS"`
Config Config `json:"Config"`
Links map[string]string `json:"Links"`
}
func retrieveAppContextInternal() error {
u := appcontext_uri_live
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("User-agent", environment.UserAgent())
backend.AddRequestHeadersForProxy(req)
client := mssahttp.GetDefaultClient()
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return errors.New("unexpected error code: " + strconv.Itoa(resp.StatusCode))
}
appContext := new(AppContext)
err = json.NewDecoder(resp.Body).Decode(appContext)
if err != nil {
return err
}
instance = appContext
return nil
}