feat: implement dynamic server list loading from API
Add internal/api package to fetch server list from /api/servers.json endpoint Replace hardcoded server list with dynamic API call Implement loading states, error handling, and refresh capability
This commit is contained in:
72
internal/api/server.go
Normal file
72
internal/api/server.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// package api handles launcher-specific API calls to the backend.
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/ui/screens"
|
||||
)
|
||||
|
||||
// ServerResponse represents the JSON response from /api/servers.json endpoint.
|
||||
type ServerResponse struct {
|
||||
Servers []ServerInfo `json:"servers"`
|
||||
}
|
||||
|
||||
// ServerInfo represents a single server/modpack entry from the API.
|
||||
type ServerInfo struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
IP string `json:"ip"`
|
||||
}
|
||||
|
||||
// FetchServerList retrieves and parses the server list from the backend API.
|
||||
// Returns a slice of Modpack objects suitable for UI consumption.
|
||||
func FetchServerList(serverURL string) ([]screens.Modpack, error) {
|
||||
// Create HTTP client with timeout
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
// Create request with context for cancellation
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/api/servers.json", serverURL), nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating request: %w", err)
|
||||
}
|
||||
|
||||
// Make the HTTP request
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("making request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check HTTP status code
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
// Decode JSON response
|
||||
var serverResp ServerResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&serverResp); err != nil {
|
||||
return nil, fmt.Errorf("decoding JSON: %w", err)
|
||||
}
|
||||
|
||||
// Convert API server info to UI Modpack format
|
||||
var modpacks []screens.Modpack
|
||||
for _, server := range serverResp.Servers {
|
||||
modpacks = append(modpacks, screens.Modpack{
|
||||
Slug: server.Slug,
|
||||
Name: server.Name,
|
||||
})
|
||||
}
|
||||
|
||||
return modpacks, nil
|
||||
}
|
||||
133
internal/api/server_test.go
Normal file
133
internal/api/server_test.go
Normal file
@@ -0,0 +1,133 @@
|
||||
// package api handles launcher-specific API calls to the backend.
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/ui/screens"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFetchServerList_Success(t *testing.T) {
|
||||
// Arrange - create mock server response
|
||||
serverResp := ServerResponse{
|
||||
Servers: []ServerInfo{
|
||||
{Slug: "hitech", Name: "HiTech 1.21", Version: "1.21", IP: "192.168.1.100"},
|
||||
{Slug: "vanilla", Name: "Vanilla 1.20", Version: "1.20", IP: "192.168.1.101"},
|
||||
},
|
||||
}
|
||||
|
||||
// Create test server with mock response
|
||||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/servers.json" {
|
||||
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("unexpected method: %s", r.Method)
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(serverResp)
|
||||
}))
|
||||
defer testServer.Close()
|
||||
|
||||
// Act - call FetchServerList with test server URL
|
||||
modpacks, err := FetchServerList(testServer.URL)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Assert - check results
|
||||
require.Len(t, modpacks, 2)
|
||||
require.Equal(t, screens.Modpack{Slug: "hitech", Name: "HiTech 1.21"}, modpacks[0])
|
||||
require.Equal(t, screens.Modpack{Slug: "vanilla", Name: "Vanilla 1.20"}, modpacks[1])
|
||||
}
|
||||
|
||||
func TestFetchServerList_EmptyList(t *testing.T) {
|
||||
// Arrange - create mock server with empty servers list
|
||||
serverResp := ServerResponse{Servers: []ServerInfo{}}
|
||||
|
||||
// Create test server with mock response
|
||||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/servers.json" {
|
||||
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("unexpected method: %s", r.Method)
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(serverResp)
|
||||
}))
|
||||
defer testServer.Close()
|
||||
|
||||
// Act - call FetchServerList with test server URL
|
||||
modpacks, err := FetchServerList(testServer.URL)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Assert - should return empty slice
|
||||
require.Empty(t, modpacks)
|
||||
}
|
||||
|
||||
func TestFetchServerList_NetworkError(t *testing.T) {
|
||||
// Act - call FetchServerList with invalid URL that will fail to connect
|
||||
modpacks, err := FetchServerList("http://localhost:12345/api/servers.json") // Assuming no server on this port
|
||||
|
||||
// Assert - should return error
|
||||
require.Error(t, err)
|
||||
require.Nil(t, modpacks)
|
||||
require.Contains(t, err.Error(), "making request")
|
||||
}
|
||||
|
||||
func TestFetchServerList_HTTPError(t *testing.T) {
|
||||
// Arrange - create test server that returns 500 error
|
||||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
defer testServer.Close()
|
||||
|
||||
// Act - call FetchServerList with test server URL
|
||||
modpacks, err := FetchServerList(testServer.URL)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, modpacks)
|
||||
require.Contains(t, err.Error(), "unexpected status code: 500")
|
||||
}
|
||||
|
||||
func TestFetchServerList_InvalidJSON(t *testing.T) {
|
||||
// Arrange - create test server that returns invalid JSON
|
||||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"invalid": json}`))
|
||||
}))
|
||||
defer testServer.Close()
|
||||
|
||||
// Act - call FetchServerList with test server URL
|
||||
modpacks, err := FetchServerList(testServer.URL)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, modpacks)
|
||||
require.Contains(t, err.Error(), "decoding JSON")
|
||||
}
|
||||
|
||||
func TestFetchServerList_Timeout(t *testing.T) {
|
||||
// Arrange - create test server that delays response to trigger timeout
|
||||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Sleep longer than our timeout to trigger timeout error
|
||||
time.Sleep(15 * time.Second)
|
||||
}))
|
||||
defer testServer.Close()
|
||||
|
||||
// Act - call FetchServerList with test server URL
|
||||
modpacks, err := FetchServerList(testServer.URL)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, modpacks)
|
||||
require.Contains(t, err.Error(), "context deadline exceeded")
|
||||
require.Contains(t, err.Error(), "making request")
|
||||
}
|
||||
Reference in New Issue
Block a user