added handlers for listing saves and installed mods

This commit is contained in:
majormjr
2016-04-17 20:18:16 -04:00
parent cd0ffdd631
commit 7c60375c13
6 changed files with 58 additions and 2 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
node_modules/
factorio-mod-manager
factorio-server-manager
Binary file not shown.
+23 -1
View File
@@ -13,7 +13,7 @@ func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello world")
}
func ListMods(w http.ResponseWriter, r *http.Request) {
func ListInstalledMods(w http.ResponseWriter, r *http.Request) {
mods := listInstalledMods()
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
@@ -45,5 +45,27 @@ func ToggleMod(w http.ResponseWriter, r *http.Request) {
if err := json.NewEncoder(w).Encode(m); err != nil {
log.Printf("Error in toggle mod: %s", err)
}
}
func ListMods(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
m, err := parseModList()
if err != nil {
log.Printf("Could not parse mod list: %s", err)
}
if err := json.NewEncoder(w).Encode(m); err != nil {
log.Printf("Error listing mods: %s", err)
}
}
func ListSaves(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
saves := listSaves()
if err := json.NewEncoder(w).Encode(saves); err != nil {
log.Printf("Error listing saves: %s", err)
}
}
+1
View File
@@ -23,6 +23,7 @@ func listInstalledMods() []string {
files, err := ioutil.ReadDir(modDir)
if err != nil {
log.Printf("Error listing installed mods")
return result
}
for _, f := range files {
if f.Name() == "mod-list.json" {
+10
View File
@@ -35,6 +35,11 @@ var routes = Routes{
"GET",
"/",
Index,
}, {
"ListInstalledMods",
"GET",
"/mods/list/installed",
ListInstalledMods,
}, {
"ListMods",
"GET",
@@ -45,5 +50,10 @@ var routes = Routes{
"GET",
"/mods/toggle/{mod}",
ToggleMod,
}, {
"ListSaves",
"GET",
"/saves/list",
ListSaves,
},
}
+23
View File
@@ -0,0 +1,23 @@
package main
import (
"io/ioutil"
"log"
)
func listSaves() []string {
saveDir := config.FactorioDir + "/saves"
result := []string{}
files, err := ioutil.ReadDir(saveDir)
if err != nil {
log.Printf("Error listing save directory: %s", err)
return result
}
for _, f := range files {
result = append(result, f.Name())
}
return result
}