mirror of
https://github.com/OpenFactorioServerManager/factorio-server-manager.git
synced 2025-01-16 04:54:52 +02:00
add http handlers
This commit is contained in:
parent
034629f28d
commit
cd0ffdd631
BIN
factorio-server-manager
Executable file
BIN
factorio-server-manager
Executable file
Binary file not shown.
49
handlers.go
Normal file
49
handlers.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func Index(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "hello world")
|
||||
}
|
||||
|
||||
func ListMods(w http.ResponseWriter, r *http.Request) {
|
||||
mods := listInstalledMods()
|
||||
|
||||
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
if err := json.NewEncoder(w).Encode(mods); err != nil {
|
||||
log.Printf("Error in list mods: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func ToggleMod(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
||||
|
||||
vars := mux.Vars(r)
|
||||
modName := vars["mod"]
|
||||
|
||||
m, err := parseModList()
|
||||
if err != nil {
|
||||
log.Printf("Could parse mod list: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = m.toggleMod(modName)
|
||||
if err != nil {
|
||||
log.Printf("Could not toggle mod: %s error: %s", modName, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := json.NewEncoder(w).Encode(m); err != nil {
|
||||
log.Printf("Error in toggle mod: %s", err)
|
||||
}
|
||||
|
||||
}
|
86
main.go
86
main.go
@ -1,95 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ModList struct {
|
||||
Mods []Mod `json:"mods"`
|
||||
}
|
||||
|
||||
type Mod struct {
|
||||
Name string `json:"name"`
|
||||
Enabled bool `json:"enabled,string"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
FactorioDir string
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
func listInstalledMods() []string {
|
||||
modDir := config.FactorioDir + "/mods"
|
||||
result := []string{}
|
||||
|
||||
files, err := ioutil.ReadDir(modDir)
|
||||
if err != nil {
|
||||
log.Printf("Error listing installed mods")
|
||||
}
|
||||
for _, f := range files {
|
||||
if f.Name() == "mod-list.json" {
|
||||
continue
|
||||
}
|
||||
result = append(result, f.Name())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func parseModList() (ModList, error) {
|
||||
var mods ModList
|
||||
modListFile := config.FactorioDir + "/mods/mod-list.json"
|
||||
|
||||
modList, err := ioutil.ReadFile(modListFile)
|
||||
if err != nil {
|
||||
log.Printf("Error reading mod-list.json file: %s", err)
|
||||
return mods, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(modList, &mods)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing mod-list.json JSON: %s", err)
|
||||
return mods, err
|
||||
}
|
||||
|
||||
return mods, nil
|
||||
|
||||
}
|
||||
|
||||
func (m *ModList) toggleMod(name string) error {
|
||||
fmt.Println(m)
|
||||
for i := range m.Mods {
|
||||
if m.Mods[i].Name == name {
|
||||
if m.Mods[i].Enabled == true {
|
||||
m.Mods[i].Enabled = false
|
||||
} else {
|
||||
m.Mods[i].Enabled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m.save()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m ModList) save() error {
|
||||
modListFile := config.FactorioDir + "/mods/mod-list.json"
|
||||
b, _ := json.MarshalIndent(m, "", " ")
|
||||
|
||||
err := ioutil.WriteFile(modListFile, b, 0644)
|
||||
if err != nil {
|
||||
log.Printf("Error writing to mod-list.json file: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
factorioDir := flag.String("dir", "./", "Specify location of Factorio config directory.")
|
||||
flag.Parse()
|
||||
@ -98,8 +21,7 @@ func main() {
|
||||
|
||||
fmt.Println(listInstalledMods())
|
||||
|
||||
//m, err := parseModList()
|
||||
//if err != nil {
|
||||
// log.Printf("Error")
|
||||
//}
|
||||
router := NewRouter()
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", router))
|
||||
}
|
||||
|
91
mods.go
Normal file
91
mods.go
Normal file
@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
type ModList struct {
|
||||
Mods []Mod `json:"mods"`
|
||||
}
|
||||
|
||||
type Mod struct {
|
||||
Name string `json:"name"`
|
||||
Enabled bool `json:"enabled,string"`
|
||||
}
|
||||
|
||||
func listInstalledMods() []string {
|
||||
modDir := config.FactorioDir + "/mods"
|
||||
result := []string{}
|
||||
|
||||
files, err := ioutil.ReadDir(modDir)
|
||||
if err != nil {
|
||||
log.Printf("Error listing installed mods")
|
||||
}
|
||||
for _, f := range files {
|
||||
if f.Name() == "mod-list.json" {
|
||||
continue
|
||||
}
|
||||
result = append(result, f.Name())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func parseModList() (ModList, error) {
|
||||
var mods ModList
|
||||
modListFile := config.FactorioDir + "/mods/mod-list.json"
|
||||
|
||||
modList, err := ioutil.ReadFile(modListFile)
|
||||
if err != nil {
|
||||
log.Printf("Error reading mod-list.json file: %s", err)
|
||||
return mods, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(modList, &mods)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing mod-list.json JSON: %s", err)
|
||||
return mods, err
|
||||
}
|
||||
|
||||
return mods, nil
|
||||
|
||||
}
|
||||
|
||||
func (m *ModList) toggleMod(name string) error {
|
||||
found := false
|
||||
|
||||
for i := range m.Mods {
|
||||
if m.Mods[i].Name == name {
|
||||
found = true
|
||||
if m.Mods[i].Enabled == true {
|
||||
m.Mods[i].Enabled = false
|
||||
} else {
|
||||
m.Mods[i].Enabled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if found == false {
|
||||
err := fmt.Errorf("Mod with name %s not found", name)
|
||||
return err
|
||||
}
|
||||
|
||||
m.save()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m ModList) save() error {
|
||||
modListFile := config.FactorioDir + "/mods/mod-list.json"
|
||||
b, _ := json.MarshalIndent(m, "", " ")
|
||||
|
||||
err := ioutil.WriteFile(modListFile, b, 0644)
|
||||
if err != nil {
|
||||
log.Printf("Error writing to mod-list.json file: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
49
routes.go
Normal file
49
routes.go
Normal file
@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
Name string
|
||||
Method string
|
||||
Pattern string
|
||||
HandlerFunc http.HandlerFunc
|
||||
}
|
||||
|
||||
type Routes []Route
|
||||
|
||||
func NewRouter() *mux.Router {
|
||||
router := mux.NewRouter().StrictSlash(true)
|
||||
|
||||
for _, route := range routes {
|
||||
router.
|
||||
Methods(route.Method).
|
||||
Path(route.Pattern).
|
||||
Name(route.Name).
|
||||
Handler(route.HandlerFunc)
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
var routes = Routes{
|
||||
Route{
|
||||
"Index",
|
||||
"GET",
|
||||
"/",
|
||||
Index,
|
||||
}, {
|
||||
"ListMods",
|
||||
"GET",
|
||||
"/mods/list",
|
||||
ListMods,
|
||||
}, {
|
||||
"ToggleMod",
|
||||
"GET",
|
||||
"/mods/toggle/{mod}",
|
||||
ToggleMod,
|
||||
},
|
||||
}
|
Loading…
Reference in New Issue
Block a user