mirror of
https://github.com/benbusby/farside.git
synced 2025-03-14 11:16:34 -04: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
Services like searxng don't need to have instance checks performed since the nightly cron task filters out the instances already.
93 lines
1.7 KiB
Go
93 lines
1.7 KiB
Go
package services
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
var (
|
|
ServiceList []Service
|
|
FallbackMap map[string]string
|
|
)
|
|
|
|
const (
|
|
baseRepoLink = "https://git.sr.ht/~benbusby/farside/blob/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 ingestServicesList(servicesBytes []byte) error {
|
|
err := json.Unmarshal(servicesBytes, &ServiceList)
|
|
return err
|
|
}
|
|
|
|
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 = ingestServicesList(fileBytes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
FallbackMap = make(map[string]string)
|
|
for _, serviceElement := range ServiceList {
|
|
FallbackMap[serviceElement.Type] = serviceElement.Fallback
|
|
}
|
|
|
|
return nil
|
|
}
|