feat(launcher): implement modpack manifest API endpoint
This commit is contained in:
@@ -26,6 +26,7 @@ func main() {
|
||||
serverPoller := &core.ServerPoller{Repo: serverRepo}
|
||||
|
||||
modpackHandler := &api.ModpackHandler{ModpackRepo: modpackRepo}
|
||||
launcherHandler := &api.LauncherHandler{ModpackRepo: modpackRepo}
|
||||
|
||||
// Запускаем поллер в фоновой горутине
|
||||
go serverPoller.Start(context.Background())
|
||||
@@ -69,6 +70,9 @@ func main() {
|
||||
r.Post("/join", authHandler.Join)
|
||||
r.Get("/profile/{uuid}", profileHandler.GetProfile)
|
||||
})
|
||||
r.Route("/launcher", func(r chi.Router) {
|
||||
r.Get("/modpacks/{name}/manifest", launcherHandler.GetModpackManifest)
|
||||
})
|
||||
|
||||
// --- Защищенные роуты ---
|
||||
r.Group(func(r chi.Router) {
|
||||
|
||||
37
internal/api/launcher_handler.go
Normal file
37
internal/api/launcher_handler.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"gitea.mrixs.me/minecraft-platform/backend/internal/database"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type LauncherHandler struct {
|
||||
ModpackRepo *database.ModpackRepository
|
||||
}
|
||||
|
||||
func (h *LauncherHandler) GetModpackManifest(w http.ResponseWriter, r *http.Request) {
|
||||
modpackName := chi.URLParam(r, "name")
|
||||
if modpackName == "" {
|
||||
http.Error(w, "Modpack name is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
manifest, err := h.ModpackRepo.GetModpackManifest(r.Context(), modpackName)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
http.Error(w, "Modpack not found or not active", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Failed to get modpack manifest", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(manifest)
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type ModpackRepository struct {
|
||||
DB *pgxpool.Pool // <--- ИЗМЕНЕНИЕ
|
||||
DB *pgxpool.Pool
|
||||
}
|
||||
|
||||
// CreateModpackTx создает модпак и все его файлы в одной транзакции.
|
||||
@@ -47,3 +47,37 @@ func (r *ModpackRepository) CreateModpackTx(ctx context.Context, modpack *models
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// GetModpackManifest возвращает список файлов для указанного модпака.
|
||||
func (r *ModpackRepository) GetModpackManifest(ctx context.Context, modpackName string) ([]models.ManifestEntry, error) {
|
||||
query := `
|
||||
SELECT mf.relative_path, mf.file_hash, mf.file_size
|
||||
FROM modpack_files mf
|
||||
JOIN modpacks m ON mf.modpack_id = m.id
|
||||
WHERE m.name = $1 AND m.is_active = TRUE`
|
||||
|
||||
rows, err := r.DB.Query(ctx, query, modpackName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var manifest []models.ManifestEntry
|
||||
for rows.Next() {
|
||||
var entry models.ManifestEntry
|
||||
if err := rows.Scan(&entry.Path, &entry.Hash, &entry.Size); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
manifest = append(manifest, entry)
|
||||
}
|
||||
|
||||
if len(manifest) == 0 {
|
||||
var exists bool
|
||||
err := r.DB.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM modpacks WHERE name = $1)", modpackName).Scan(&exists)
|
||||
if err != nil || !exists {
|
||||
return nil, pgx.ErrNoRows
|
||||
}
|
||||
}
|
||||
|
||||
return manifest, nil
|
||||
}
|
||||
|
||||
8
internal/models/launcher.go
Normal file
8
internal/models/launcher.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
// ManifestEntry представляет один файл в манифесте модпака.
|
||||
type ManifestEntry struct {
|
||||
Path string `json:"path"`
|
||||
Hash string `json:"hash"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
Reference in New Issue
Block a user