reworked update mod inside modpack and moved to correct file && made function to read struct from request body

This commit is contained in:
knoxfighter 2020-09-06 02:12:39 +02:00
parent 7b7c017341
commit 9a8650af16
3 changed files with 137 additions and 184 deletions

View File

@ -2,7 +2,6 @@ package main
import (
"archive/zip"
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/mux"
@ -81,19 +80,11 @@ func ModPackCreateHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
body, err := ReadRequestBody(w, r, &resp)
if err != nil {
return
}
var modPackStruct struct {
Name string `json:"name"`
}
err = json.Unmarshal(body, &modPackStruct)
ReadFromRequestBody(w, r, &resp, &modPackStruct)
if err != nil {
resp = fmt.Sprintf("Error unmarshalling modPack request JSON: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusBadRequest)
return
}
@ -260,19 +251,11 @@ func ModPackToggleModHandler(w http.ResponseWriter, r *http.Request) {
return
}
body, err := ReadRequestBody(w, r, &resp)
if err != nil {
return
}
var modPackStruct struct {
ModName string `json:"modName"`
ModName string `json:"name"`
}
err = json.Unmarshal(body, &modPackStruct)
ReadFromRequestBody(w, r, &resp, &modPackStruct)
if err != nil {
resp = fmt.Sprintf("Error unmarshalling modPack struct JSON: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusBadRequest)
return
}
@ -284,3 +267,90 @@ func ModPackToggleModHandler(w http.ResponseWriter, r *http.Request) {
return
}
}
func ModPackDeleteModHandler(w http.ResponseWriter, r *http.Request) {
var err error
var resp interface{}
defer func() {
WriteResponse(w, resp)
}()
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
err, packMap, packName := ReadModPackRequest(w, r, &resp)
if err != nil {
return
}
var modPackStruct struct {
name string `json:"name"`
}
err = ReadFromRequestBody(w, r, &resp, &modPackStruct)
if err != nil {
return
}
err = packMap[packName].Mods.deleteMod(modPackStruct.name)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
resp = fmt.Sprintf("Error deleting mod {%s} in modpack {%s}: %s", modPackStruct.name, packName, err)
log.Println(resp)
return
}
resp = true
}
func ModPackUpdateModHandler(w http.ResponseWriter, r *http.Request) {
var err error
var resp interface{}
defer func() {
WriteResponse(w, resp)
}()
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
//Get Data out of the request
var modPackStruct struct {
modName string `json:"modName"`
downloadUrl string `json:"downloadUrl"`
filename string `json:"filename"`
}
err = ReadFromRequestBody(w, r, &resp, &modPackStruct)
if err != nil {
return
}
err, packMap, packName := ReadModPackRequest(w, r, &resp)
if err != nil {
return
}
err = packMap[packName].Mods.updateMod(modPackStruct.modName, modPackStruct.downloadUrl, modPackStruct.filename)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
resp = fmt.Sprintf("Error updating mod {%s} in modpack {%s}: %s", modPackStruct.modName, packName, err)
log.Println(resp)
return
}
installedMods := packMap[packName].Mods.listInstalledMods().ModsResult
var found = false
for _, mod := range installedMods {
if mod.Name == modPackStruct.modName {
resp = mod
found = true
return
}
}
if !found {
resp = fmt.Sprintf(`Could not find mod %s`, modPackStruct.modName)
log.Println(resp)
w.WriteHeader(http.StatusNotFound)
return
}
}

View File

@ -3,7 +3,6 @@ package main
import (
"archive/zip"
"encoding/json"
"errors"
"fmt"
"github.com/mroote/factorio-server-manager/lockfile"
"io"
@ -42,6 +41,23 @@ func CreateNewMods(w http.ResponseWriter, resp *interface{}) (mods Mods, err err
return
}
func ReadFromRequestBody(w http.ResponseWriter, r *http.Request, resp *interface{}, data interface{}) (err error) {
//Get Data out of the request
body, err := ReadRequestBody(w, r, resp)
if err != nil {
return
}
err = json.Unmarshal(body, data)
if err != nil {
*resp = fmt.Sprintf("Error unmarshalling requested struct JSON: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusBadRequest)
return
}
return
}
// Returns JSON response of all mods installed in factorio/mods
func ListInstalledModsHandler(w http.ResponseWriter, r *http.Request) {
var err error
@ -71,20 +87,12 @@ func ModToggleHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
//Get Data out of the request
body, err := ReadRequestBody(w, r, &resp)
if err != nil {
return
var data struct {
Name string `json:"name"`
}
var mod struct {
Name string `json:"modName"`
}
err = json.Unmarshal(body, &mod)
err = ReadFromRequestBody(w, r, &resp, &data)
if err != nil {
resp = fmt.Sprintf("Error unmarshalling modName JSON: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusBadRequest)
return
}
@ -93,7 +101,7 @@ func ModToggleHandler(w http.ResponseWriter, r *http.Request) {
return
}
err, resp = mods.ModSimpleList.toggleMod(mod.Name)
err, resp = mods.ModSimpleList.toggleMod(data.Name)
if err != nil {
resp = fmt.Sprintf("Error in toggling mod in simple list: %s", err)
log.Println(resp)
@ -112,20 +120,13 @@ func ModDeleteHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
// Get Data out of the request
body, err := ReadRequestBody(w, r, &resp)
if err != nil {
return
var data struct {
Name string `json:"name"`
}
var mod struct {
Name string `json:"modName"`
}
err = json.Unmarshal(body, &mod)
// Get Data out of the request
err = ReadFromRequestBody(w, r, &resp, &data)
if err != nil {
resp = fmt.Sprintf("Error unmarshalling modName JSON: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusBadRequest)
return
}
@ -134,15 +135,15 @@ func ModDeleteHandler(w http.ResponseWriter, r *http.Request) {
return
}
mods.deleteMod(mod.Name)
mods.deleteMod(data.Name)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
resp = fmt.Sprintf("Error in deleting mod {%s}: %s", mod.Name, err)
w.WriteHeader(http.StatusInternalServerError)
resp = fmt.Sprintf("Error in deleting mod {%s}: %s", data.Name, err)
log.Println(resp)
return
}
resp = mod.Name
resp = data.Name
}
func ModDeleteAllHandler(w http.ResponseWriter, r *http.Request) {
@ -178,21 +179,14 @@ func ModUpdateHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
//Get Data out of the request
body, err := ReadRequestBody(w, r, &resp)
if err != nil {
return
}
var modData struct {
Name string `json:"modName"`
DownloadUrl string `json:"downloadUrl"`
Filename string `json:"fileName"`
}
err = json.Unmarshal(body, &modData)
err = ReadFromRequestBody(w, r, &resp, &modData)
if err != nil {
resp = fmt.Sprintf("Error unmarshalling modName JSON: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusBadRequest)
return
}
@ -209,12 +203,21 @@ func ModUpdateHandler(w http.ResponseWriter, r *http.Request) {
}
installedMods := mods.listInstalledMods().ModsResult
var found = false
for _, mod := range installedMods {
if mod.Name == modData.Name {
resp = mod
found = true
return
}
}
if !found {
resp = fmt.Sprintf(`Could not find mod %s`, modData.Name)
log.Println(resp)
w.WriteHeader(http.StatusNotFound)
return
}
}
func ModUploadHandler(w http.ResponseWriter, r *http.Request) {
@ -239,7 +242,7 @@ func ModUploadHandler(w http.ResponseWriter, r *http.Request) {
}
if err != nil {
w.WriteHeader(500)
w.WriteHeader(http.StatusInternalServerError)
resp.Data = fmt.Sprintf("Error in uploadMod, listing mods wasn't successful: %s", err)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error in uploadMod, listing mods wasn't successful: %s", err)
@ -327,19 +330,11 @@ func LoadModsFromSaveHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
//Get Data out of the request
body, err := ReadRequestBody(w, r, &resp)
if err != nil {
return
}
var saveFileStruct struct {
Name string `json:"saveFile"`
}
err = json.Unmarshal(body, &saveFileStruct)
err = ReadFromRequestBody(w, r, &resp, &saveFileStruct)
if err != nil {
resp = fmt.Sprintf("Error unmarshalling saveFile JSON: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusBadRequest)
return
}
@ -365,116 +360,3 @@ func LoadModsFromSaveHandler(w http.ResponseWriter, r *http.Request) {
resp = header
}
func ModPackDeleteModHandler(w http.ResponseWriter, r *http.Request) {
var err error
var resp interface{}
defer func() {
WriteResponse(w, resp)
}()
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
body, err := ReadRequestBody(w, r, &resp)
if err != nil {
return
}
var modPackStruct struct {
modName string `json:"modName"`
modPack string `json:"modPack"`
}
err = json.Unmarshal(body, &modPackStruct)
if err != nil {
resp = fmt.Sprintf("Error unmarshalling saveFile JSON: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusBadRequest)
return
}
modPackMap, err := CreateNewModPackMap(w, &resp)
if err != nil {
return
}
if modPackMap.checkModPackExists(modPackStruct.modPack) {
err = modPackMap[modPackStruct.modPack].Mods.deleteMod(modPackStruct.modName)
} else {
err = errors.New("ModPack " + modPackStruct.modPack + " does not exist")
resp = fmt.Sprintf("Error loading modpack file: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusNotFound)
return
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
resp = fmt.Sprintf("Error deleting mod {%s} in modpack {%s}: %s", modPackStruct.modName, modPackStruct.modPack, err)
log.Println(resp)
return
}
resp = true
}
func ModPackUpdateModHandler(w http.ResponseWriter, r *http.Request) {
var err error
var resp interface{}
defer func() {
WriteResponse(w, resp)
}()
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
//Get Data out of the request
body, err := ReadRequestBody(w, r, &resp)
if err != nil {
return
}
var modPackStruct struct {
modName string `json:"modName"`
downloadUrl string `json:"downloadUrl"`
filename string `json:"filename"`
modPack string `json:"modPack"`
}
err = json.Unmarshal(body, &modPackStruct)
if err != nil {
resp = fmt.Sprintf("Error unmarshalling modPack JSON: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusBadRequest)
return
}
modPackMap, err := CreateNewModPackMap(w, &resp)
if err != nil {
return
}
if modPackMap.checkModPackExists(modPackStruct.modPack) {
err = modPackMap[modPackStruct.modPack].Mods.updateMod(modPackStruct.modName, modPackStruct.downloadUrl, modPackStruct.filename)
} else {
err = errors.New("ModPack " + modPackStruct.modPack + " does not exist")
resp = fmt.Sprintf("Error loading modpack file: %s", err)
log.Println(resp)
w.WriteHeader(http.StatusNotFound)
return
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
resp = fmt.Sprintf("Error updating mod {%s} in modpack {%s}: %s", modPackStruct.modName, modPackStruct.modPack, err)
log.Println(resp)
return
}
installedMods := modPackMap[modPackStruct.modPack].Mods.listInstalledMods().ModsResult
for _, mod := range installedMods {
if mod.Name == modPackStruct.modName {
resp = mod
break
}
}
}

View File

@ -366,17 +366,18 @@ var apiRoutes = Routes{
}, {
"ModPackToggleMod",
"POST",
"/mods/packs/{modpack}/toggle",
"/mods/packs/{modpack}/mod/toggle",
ModPackToggleModHandler,
}, {
"ModPackDeleteMod",
"POST",
"/mods/packs/mod/delete",
"/mods/packs/{modpack}/mod/delete",
ModPackDeleteModHandler,
}, {
},
{
"ModPackUpdateMod",
"POST",
"/mods/packs/mod/update",
"/mods/packs/{modpack}/mod/update",
ModPackUpdateModHandler,
},
}