Compare commits
1 Commits
e927fff02f
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| e1371e226a |
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")
|
||||||
|
}
|
||||||
@@ -28,27 +28,54 @@ func MainScreen(
|
|||||||
serverList []Modpack,
|
serverList []Modpack,
|
||||||
onPlay func(),
|
onPlay func(),
|
||||||
onSettings func(),
|
onSettings func(),
|
||||||
|
onServerListRefresh func(),
|
||||||
|
isServerListLoading bool,
|
||||||
|
serverListError error,
|
||||||
) fyne.CanvasObject {
|
) fyne.CanvasObject {
|
||||||
|
|
||||||
// ── Sidebar (Left) ──────────────────────────────────────
|
// ── Sidebar (Left) ──────────────────────────────────────
|
||||||
serverLabels := make([]*widget.Label, len(serverList))
|
var sidebar fyne.CanvasObject
|
||||||
serverContainer := container.NewVBox()
|
if isServerListLoading {
|
||||||
|
// Show loading state
|
||||||
|
loadingLabel := widget.NewLabelWithStyle("Loading server list...", fyne.TextAlignCenter, fyne.TextStyle{})
|
||||||
|
sidebar = container.NewVScroll(container.NewVBox(loadingLabel))
|
||||||
|
} else if len(serverList) == 0 && serverListError == nil {
|
||||||
|
// Show empty state (no error, just empty list)
|
||||||
|
emptyLabel := widget.NewLabelWithStyle("No servers available", fyne.TextAlignCenter, fyne.TextStyle{})
|
||||||
|
sidebar = container.NewVScroll(container.NewVBox(emptyLabel))
|
||||||
|
} else if serverListError != nil {
|
||||||
|
// Show error state
|
||||||
|
errorLabel := widget.NewLabelWithStyle("Failed to load server list", fyne.TextAlignCenter, fyne.TextStyle{})
|
||||||
|
detailLabel := widget.NewLabel(serverListError.Error())
|
||||||
|
detailLabel.Wrapping = fyne.TextWrapWord
|
||||||
|
retryBtn := components.SettingsButton(onServerListRefresh)
|
||||||
|
retryBtn.Text = "Retry"
|
||||||
|
sidebar = container.NewVScroll(container.NewVBox(
|
||||||
|
errorLabel,
|
||||||
|
detailLabel,
|
||||||
|
retryBtn,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
// Show normal server list
|
||||||
|
serverLabels := make([]*widget.Label, len(serverList))
|
||||||
|
serverContainer := container.NewVBox()
|
||||||
|
|
||||||
for i, sp := range serverList {
|
for i, sp := range serverList {
|
||||||
label := widget.NewLabel(sp.Name)
|
label := widget.NewLabel(sp.Name)
|
||||||
serverLabels[i] = label
|
serverLabels[i] = label
|
||||||
index := i
|
index := i
|
||||||
card := components.ServerCard(sp.Name, index == 0, func() {
|
card := components.ServerCard(sp.Name, index == 0, func() {
|
||||||
for j, l := range serverLabels {
|
for j, l := range serverLabels {
|
||||||
l.TextStyle.Bold = (j == index)
|
l.TextStyle.Bold = (j == index)
|
||||||
l.Refresh()
|
l.Refresh()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
serverContainer.Add(card)
|
serverContainer.Add(card)
|
||||||
|
}
|
||||||
|
|
||||||
|
sidebar = container.NewVScroll(serverContainer)
|
||||||
}
|
}
|
||||||
|
|
||||||
sidebar := container.NewVScroll(serverContainer)
|
|
||||||
|
|
||||||
// ── Center (background + info) ──────────────────────────
|
// ── Center (background + info) ──────────────────────────
|
||||||
bg := widget.NewRichTextFromMarkdown(
|
bg := widget.NewRichTextFromMarkdown(
|
||||||
"# Welcome to MrixsCraft\n\n" +
|
"# Welcome to MrixsCraft\n\n" +
|
||||||
@@ -93,9 +120,14 @@ func MainScreen(
|
|||||||
playBtn := components.PlayButton(onPlay)
|
playBtn := components.PlayButton(onPlay)
|
||||||
settingsBtn := components.SettingsButton(onSettings)
|
settingsBtn := components.SettingsButton(onSettings)
|
||||||
|
|
||||||
|
// Refresh button for server list
|
||||||
|
refreshBtn := components.SettingsButton(onServerListRefresh)
|
||||||
|
|
||||||
bottomRight := container.NewHBox(
|
bottomRight := container.NewHBox(
|
||||||
settingsBtn,
|
settingsBtn,
|
||||||
widget.NewSeparator(),
|
widget.NewSeparator(),
|
||||||
|
refreshBtn,
|
||||||
|
widget.NewSeparator(),
|
||||||
playBtn,
|
playBtn,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
10
internal/ui/screens/screens_test.go
Normal file
10
internal/ui/screens/screens_test.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
// package screens implements application screens (login, main menu, settings).
|
||||||
|
package screens
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// Dummy test to ensure package compiles
|
||||||
|
func TestScreensPackage_Compiles(t *testing.T) {
|
||||||
|
// This test ensures the screens package compiles without errors
|
||||||
|
// We're not testing the actual UI output as that requires a GUI environment
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
"fyne.io/fyne/v2/app"
|
"fyne.io/fyne/v2/app"
|
||||||
|
|
||||||
|
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/api"
|
||||||
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/auth"
|
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/auth"
|
||||||
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/config"
|
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/config"
|
||||||
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/ui/screens"
|
"gitea.mrixs.me/Mrixs/MrixsCraft-launcher/internal/ui/screens"
|
||||||
@@ -20,12 +21,13 @@ func Launch(client *auth.Client, session *auth.Session, settings config.Settings
|
|||||||
w.Resize(fyne.NewSize(float32(settings.Width), float32(settings.Height)))
|
w.Resize(fyne.NewSize(float32(settings.Width), float32(settings.Height)))
|
||||||
w.CenterOnScreen()
|
w.CenterOnScreen()
|
||||||
|
|
||||||
// TODO: fetch from /api/servers.json
|
// State for server list loading
|
||||||
serverList := []screens.Modpack{
|
var serverList []screens.Modpack
|
||||||
{Slug: "hitech", Name: "HiTech 1.21"},
|
var isServerListLoading bool
|
||||||
{Slug: "vanilla", Name: "Vanilla 1.20"},
|
var serverListError error
|
||||||
}
|
_ = serverListError // Use variable to prevent "declared and not used" error
|
||||||
|
|
||||||
|
// Callback functions
|
||||||
onPlay := func() {
|
onPlay := func() {
|
||||||
// TODO: launch selected modpack
|
// TODO: launch selected modpack
|
||||||
}
|
}
|
||||||
@@ -38,15 +40,38 @@ func Launch(client *auth.Client, session *auth.Session, settings config.Settings
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
content := screens.MainScreen(w, client, session, serverList, onPlay, onSettings)
|
// Function to refresh server list
|
||||||
|
var refreshServerList func()
|
||||||
|
refreshServerList = func() {
|
||||||
|
isServerListLoading = true
|
||||||
|
serverListError = nil
|
||||||
|
// Update UI to show loading state
|
||||||
|
content := screens.MainScreen(w, client, session, serverList, onPlay, onSettings, refreshServerList, isServerListLoading, serverListError)
|
||||||
|
w.SetContent(content)
|
||||||
|
|
||||||
|
// Fetch server list in goroutine to avoid blocking UI
|
||||||
|
go func() {
|
||||||
|
serverList, serverListError = api.FetchServerList(settings.ServerURL)
|
||||||
|
isServerListLoading = false
|
||||||
|
// Update UI with result
|
||||||
|
content := screens.MainScreen(w, client, session, serverList, onPlay, onSettings, refreshServerList, isServerListLoading, serverListError)
|
||||||
|
w.SetContent(content)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initial load of server list
|
||||||
|
content := screens.MainScreen(w, client, session, serverList, onPlay, onSettings, refreshServerList, isServerListLoading, serverListError)
|
||||||
w.SetContent(content)
|
w.SetContent(content)
|
||||||
|
|
||||||
|
// Start fetching server list in background
|
||||||
|
go refreshServerList()
|
||||||
|
|
||||||
// If no session, show login modal after the window is rendered.
|
// If no session, show login modal after the window is rendered.
|
||||||
if session == nil {
|
if session == nil {
|
||||||
w.Show()
|
w.Show()
|
||||||
screens.LoginScreen(w, client, func(sess *auth.Session) {
|
screens.LoginScreen(w, client, func(sess *auth.Session) {
|
||||||
// Refresh UI with logged-in state.
|
// Refresh UI with logged-in state.
|
||||||
content := screens.MainScreen(w, client, sess, serverList, onPlay, onSettings)
|
content := screens.MainScreen(w, client, sess, serverList, onPlay, onSettings, refreshServerList, isServerListLoading, serverListError)
|
||||||
w.SetContent(content)
|
w.SetContent(content)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
9
internal/ui/ui_test.go
Normal file
9
internal/ui/ui_test.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
// package ui contains the Fyne GUI bootstrap and wiring.
|
||||||
|
package ui
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// Dummy test to ensure package compiles
|
||||||
|
func TestUIPackage_Compiles(t *testing.T) {
|
||||||
|
// This test ensures the ui package compiles without errors
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user