Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified docker/build-release.sh
100755 → 100644
Empty file.
Empty file modified docker/build.sh
100755 → 100644
Empty file.
Empty file modified docker/entrypoint.sh
100755 → 100644
Empty file.

Large diffs are not rendered by default.

855 changes: 855 additions & 0 deletions docs/superpowers/specs/2026-06-21-server-version-manager-design.md

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions src/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func NewRouter() *mux.Router {
Methods("GET").
Name("User management").
Handler(http.StripPrefix("/user-management", http.FileServer(http.Dir("./app/"))))
subRouter.Path("/server-version").
Methods("GET").
Name("Server version").
Handler(http.StripPrefix("/server-version", http.FileServer(http.Dir("./app/"))))
subRouter.Path("/help").
Methods("GET").
Name("Help").
Expand Down Expand Up @@ -292,6 +296,37 @@ var apiRoutes = Routes{
UpdateServerSettings,
false,
},
{
"GetCurrentVersion",
"GET",
"/server/version/current",
GetCurrentVersion,
false,
}, {
"GetAvailableVersions",
"GET",
"/server/version/available",
GetAvailableVersions,
false,
}, {
"GetFullVersionList",
"GET",
"/server/version/list",
GetFullVersionList,
false,
}, {
"InstallVersion",
"POST",
"/server/version/install",
InstallVersion,
true,
}, {
"GetInstallStatus",
"GET",
"/server/version/install-status",
GetInstallStatus,
false,
},
// Mod Portal Stuff
{
"ModPortalListAllMods",
Expand Down
129 changes: 129 additions & 0 deletions src/api/version_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package api

import (
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/OpenFactorioServerManager/factorio-server-manager/api/websocket"
"github.com/OpenFactorioServerManager/factorio-server-manager/factorio"
)

func GetCurrentVersion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")

vm := factorio.NewVersionManager()
version, err := vm.GetCurrentVersion()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"version": version})
}

func GetAvailableVersions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")

vm := factorio.NewVersionManager()
releases, err := vm.GetAvailableVersions()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(releases)
}

func GetFullVersionList(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")

vm := factorio.NewVersionManager()
releases, err := vm.GetFullVersionList()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(releases)
}

func InstallVersion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")

var data struct {
Version string `json:"version"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "invalid request body"})
return
}

status := factorio.GetInstallStatus()
if status.Installing {
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(map[string]string{"error": "installation already in progress"})
return
}

factorio.SetInstallStatus(factorio.InstallStatus{
Installing: true,
Version: data.Version,
Progress: 0,
})

go func() {
vm := factorio.NewVersionManager()
wsRoom := websocket.WebsocketHub.GetRoom("server_version")

err := vm.DownloadAndInstall(data.Version, func(percent int) {
factorio.SetInstallStatus(factorio.InstallStatus{
Installing: true,
Version: data.Version,
Progress: percent,
})
msg := fmt.Sprintf(`{"type":"download_progress","version":"%s","percent":%d}`, data.Version, percent)
wsRoom.Send(string(msg))
})

if err != nil {
log.Printf("Version install error: %v", err)
factorio.SetInstallStatus(factorio.InstallStatus{
Installing: false,
Version: data.Version,
Progress: 100,
Error: err.Error(),
})
wsRoom.Send(fmt.Sprintf(`{"type":"install_error","version":"%s","error":"%s"}`, data.Version, err.Error()))
return
}

if err := factorio.RefreshServerVersion(); err != nil {
log.Printf("Failed to refresh server version: %v", err)
}

factorio.SetInstallStatus(factorio.InstallStatus{})
wsRoom.Send(fmt.Sprintf(`{"type":"install_complete","version":"%s"}`, data.Version))
}()

w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(map[string]string{
"status": "started",
"version": data.Version,
})
}

func GetInstallStatus(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
status := factorio.GetInstallStatus()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(status)
}
Loading