Skip to content

Commit 59db7f9

Browse files
committed
added handler for creating mod packs and listing mod packs
1 parent a18b564 commit 59db7f9

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

src/handlers.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"net/http"
1010
"os"
11+
"path/filepath"
1112
"strconv"
1213

1314
"github.com/gorilla/mux"
@@ -18,6 +19,11 @@ type JSONResponse struct {
1819
Data interface{} `json:"data,string"`
1920
}
2021

22+
type ModPack struct {
23+
Mods []string `json:"mods"`
24+
Title string `json:"title"`
25+
}
26+
2127
// Returns JSON response of all mods installed in factorio/mods
2228
func ListInstalledMods(w http.ResponseWriter, r *http.Request) {
2329
var err error
@@ -201,6 +207,76 @@ func DownloadMod(w http.ResponseWriter, r *http.Request) {
201207
http.ServeFile(w, r, modFile)
202208
}
203209

210+
func CreateModPackHandler(w http.ResponseWriter, r *http.Request) {
211+
var err error
212+
resp := JSONResponse{
213+
Success: false,
214+
}
215+
216+
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
217+
218+
switch r.Method {
219+
case "GET":
220+
resp.Data = "Unsupported method"
221+
resp.Success = false
222+
if err = json.NewEncoder(w).Encode(resp); err != nil {
223+
log.Printf("Get request to modpack handler: %s", err)
224+
}
225+
case "POST":
226+
var mods ModPack
227+
body, err := ioutil.ReadAll(r.Body)
228+
if err != nil {
229+
log.Printf("Error in create mod pack handler body: %s", err)
230+
return
231+
}
232+
233+
log.Printf("Creating modpack with contents: %v", string(body))
234+
235+
err = json.Unmarshal(body, &mods)
236+
if err != nil {
237+
log.Printf("Error unmarshaling server settings JSON: %s", err)
238+
return
239+
}
240+
241+
err = createModPack(mods.Title, mods.Mods...)
242+
if err != nil {
243+
log.Printf("Error in creating modpack handler: %s", err)
244+
return
245+
}
246+
247+
resp.Success = true
248+
resp.Data = fmt.Sprintf("Created modpack: %s", mods.Title)
249+
if err := json.NewEncoder(w).Encode(resp); err != nil {
250+
log.Printf("Error listing saves: %s", err)
251+
}
252+
}
253+
}
254+
255+
func ListModPacks(w http.ResponseWriter, r *http.Request) {
256+
var err error
257+
resp := JSONResponse{
258+
Success: false,
259+
}
260+
261+
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
262+
263+
resp.Data, err = listModPacks(filepath.Join(config.FactorioDir, "modpacks"))
264+
if err != nil {
265+
resp.Success = false
266+
resp.Data = fmt.Sprintf("Error listing modpack files: %s", err)
267+
if err := json.NewEncoder(w).Encode(resp); err != nil {
268+
log.Printf("Error listing modpacks: %s", err)
269+
}
270+
return
271+
}
272+
273+
resp.Success = true
274+
275+
if err := json.NewEncoder(w).Encode(resp); err != nil {
276+
log.Printf("Error listing saves: %s", err)
277+
}
278+
}
279+
204280
// Lists all save files in the factorio/saves directory
205281
func ListSaves(w http.ResponseWriter, r *http.Request) {
206282
var err error

src/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func main() {
9191

9292
router := NewRouter()
9393
createModPackDir()
94-
createModPack("test", "advanced-logistics-system_0.2.7.zip", "YARM_0.7.15.zip")
94+
9595
fmt.Printf("Starting server on: %s:%s", config.ServerIP, config.ServerPort)
9696
log.Fatal(http.ListenAndServe(config.ServerIP+":"+config.ServerPort, router))
9797
}

src/mods.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ func rmMod(modName string) error {
6262
log.Printf("Error removing mod %s: %s", mod, err)
6363
return err
6464
}
65-
log.Printf("Removed mod: %s", mod)
6665
removed = true
66+
log.Printf("Removed mod: %s", mod)
6767
}
6868
}
6969

@@ -132,6 +132,21 @@ func createModPack(title string, mods ...string) error {
132132
return nil
133133
}
134134

135+
func listModPacks(modDir string) ([]string, error) {
136+
result := []string{}
137+
138+
files, err := ioutil.ReadDir(modDir)
139+
if err != nil {
140+
log.Printf("Error listing modpacks: %s", err)
141+
return result, err
142+
}
143+
for _, f := range files {
144+
result = append(result, f.Name())
145+
}
146+
147+
return result, nil
148+
}
149+
135150
// Parses mod-list.json file in factorio/mods
136151
// returns ModList struct
137152
func parseModList() (ModList, error) {

src/routes.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,10 @@ var apiRoutes = Routes{
199199
"POST",
200200
"/user/remove",
201201
RemoveUser,
202+
}, {
203+
"ListModPacks",
204+
"GET",
205+
"/mods/packs/list",
206+
ListModPacks,
202207
},
203208
}

0 commit comments

Comments
 (0)