174 lines
4.7 KiB
Go
174 lines
4.7 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type updateSettingsInput struct {
|
|
IPPort *string `json:"ip_port"`
|
|
ServerParameters *string `json:"server_parameters"`
|
|
SteamBranch *string `json:"steam_branch"`
|
|
SteamUser *string `json:"steam_user"`
|
|
Platform *string `json:"platform"`
|
|
CBASettings *string `json:"cba_settings"`
|
|
AILevelPresets *string `json:"ai_level_presets"`
|
|
DifficultyPresets *string `json:"difficulty_presets"`
|
|
ActiveConfig *string `json:"active_config"`
|
|
ActiveModlist *string `json:"active_modlist"`
|
|
}
|
|
|
|
func (h *Handler) GetSettings(c *gin.Context) {
|
|
s, err := h.settings.Load()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, s)
|
|
}
|
|
|
|
func (h *Handler) UpdateSettings(c *gin.Context) {
|
|
var input updateSettingsInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
s, err := h.settings.Load()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if input.IPPort != nil { s.IPPort = *input.IPPort }
|
|
if input.ServerParameters != nil { s.ServerParameters = *input.ServerParameters }
|
|
if input.SteamBranch != nil { s.SteamBranch = *input.SteamBranch }
|
|
if input.SteamUser != nil { s.SteamUser = *input.SteamUser }
|
|
if input.Platform != nil { s.Platform = *input.Platform }
|
|
if input.CBASettings != nil { s.CBASettings = *input.CBASettings }
|
|
if input.AILevelPresets != nil { s.AILevelPresets = *input.AILevelPresets }
|
|
if input.DifficultyPresets != nil { s.DifficultyPresets = *input.DifficultyPresets }
|
|
if input.ActiveConfig != nil { s.ActiveConfig = *input.ActiveConfig }
|
|
if input.ActiveModlist != nil { s.ActiveModlist = *input.ActiveModlist }
|
|
|
|
if err := h.process.WriteUserconfigFiles(s); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "write userconfig: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.settings.Save(s); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, s)
|
|
}
|
|
|
|
func (h *Handler) StartServer(c *gin.Context) {
|
|
if err := h.process.Start(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "starting"})
|
|
}
|
|
|
|
func (h *Handler) StopServer(c *gin.Context) {
|
|
if err := h.process.Stop(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "stopping"})
|
|
}
|
|
|
|
func (h *Handler) RestartServer(c *gin.Context) {
|
|
if err := h.process.Restart(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "restarting"})
|
|
}
|
|
|
|
func (h *Handler) ServerStatus(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"running": h.process.IsRunning(),
|
|
})
|
|
}
|
|
|
|
func (h *Handler) SteamCMDSettings(c *gin.Context) {
|
|
s, err := h.settings.Load()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"branch": s.SteamBranch,
|
|
"user": s.SteamUser,
|
|
"platform": s.Platform,
|
|
})
|
|
}
|
|
|
|
type steamcmdUpdateGameInput struct {
|
|
Branch string `json:"branch"`
|
|
User string `json:"user"`
|
|
}
|
|
|
|
func (h *Handler) SteamCMDUpdateGame(c *gin.Context) {
|
|
var input steamcmdUpdateGameInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
user := input.User
|
|
if user == "" {
|
|
user = "anonymous"
|
|
}
|
|
branch := input.Branch
|
|
if branch == "" {
|
|
branch = "stable"
|
|
}
|
|
|
|
if err := h.steamcmd.UpdateGame(branch, user); err != nil {
|
|
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "started"})
|
|
}
|
|
|
|
type steamcmdDownloadModInput struct {
|
|
ModID string `json:"mod_id"`
|
|
}
|
|
|
|
func (h *Handler) SteamCMDDownloadMod(c *gin.Context) {
|
|
var input steamcmdDownloadModInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if input.ModID == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "mod_id is required"})
|
|
return
|
|
}
|
|
|
|
if err := h.steamcmd.DownloadMod(input.ModID); err != nil {
|
|
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "started"})
|
|
}
|
|
|
|
func (h *Handler) SteamCMDStatus(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"running": h.steamcmd.IsRunning(),
|
|
})
|
|
}
|
|
|
|
func (h *Handler) ServerPaths(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"serverfile_dir": h.serverfileDir,
|
|
"mods_dir": h.modsDir,
|
|
"cfg_dir": h.cfgDir,
|
|
"profiles_dir": h.profilesDir,
|
|
})
|
|
}
|