mirror of
https://github.com/benbusby/farside.git
synced 2025-11-07 01:02:53 -05:00
Some checks are pending
Tests / test (1.21.x, macos-latest) (push) Waiting to run
Tests / test (1.21.x, ubuntu-latest) (push) Waiting to run
Tests / test (1.21.x, windows-latest) (push) Waiting to run
Tests / test (1.22.x, macos-latest) (push) Waiting to run
Tests / test (1.22.x, ubuntu-latest) (push) Waiting to run
Tests / test (1.22.x, windows-latest) (push) Waiting to run
Tests / test (1.23.x, macos-latest) (push) Waiting to run
Tests / test (1.23.x, ubuntu-latest) (push) Waiting to run
Tests / test (1.23.x, windows-latest) (push) Waiting to run
The sourcehut link is (rightfully) blocking unauthenticated requests, so using the github link is more reliable.
87 lines
1.6 KiB
Go
87 lines
1.6 KiB
Go
package services
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
var (
|
|
ServiceList []Service
|
|
FallbackMap map[string]string
|
|
)
|
|
|
|
const (
|
|
baseRepoLink = "https://raw.githubusercontent.com/benbusby/farside/refs/heads/main/"
|
|
|
|
noCFServicesJSON = "services.json"
|
|
fullServicesJSON = "services-full.json"
|
|
)
|
|
|
|
type Service struct {
|
|
Type string `json:"type"`
|
|
TestURL string `json:"test_url,omitempty"`
|
|
Fallback string `json:"fallback,omimtempty"`
|
|
Instances []string `json:"instances"`
|
|
}
|
|
|
|
func GetServicesFileName() string {
|
|
cloudflareEnabled := false
|
|
|
|
cfEnabledVar := os.Getenv("FARSIDE_CF_ENABLED")
|
|
if len(cfEnabledVar) > 0 && cfEnabledVar == "1" {
|
|
cloudflareEnabled = true
|
|
}
|
|
|
|
serviceJSON := noCFServicesJSON
|
|
if cloudflareEnabled {
|
|
serviceJSON = fullServicesJSON
|
|
}
|
|
|
|
return serviceJSON
|
|
}
|
|
|
|
func FetchServicesFile(serviceJSON string) ([]byte, error) {
|
|
resp, err := http.Get(baseRepoLink + serviceJSON)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
bodyBytes, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = os.WriteFile(serviceJSON, bodyBytes, 0666)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return bodyBytes, nil
|
|
}
|
|
|
|
func InitializeServices() error {
|
|
serviceJSON := GetServicesFileName()
|
|
fileBytes, err := os.ReadFile(serviceJSON)
|
|
if err != nil {
|
|
fileBytes, err = FetchServicesFile(serviceJSON)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
err = json.Unmarshal(fileBytes, &ServiceList)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
FallbackMap = make(map[string]string)
|
|
for _, serviceElement := range ServiceList {
|
|
FallbackMap[serviceElement.Type] = serviceElement.Fallback
|
|
}
|
|
|
|
return nil
|
|
}
|