added method and handler for reading config.ini file

This commit is contained in:
majormjr 2016-04-23 19:14:13 -04:00
parent 4ac1fc0568
commit a77a442513
11 changed files with 141 additions and 58 deletions

View File

@ -85,9 +85,7 @@
_reactRouter.Route,
{ path: '/', component: _App2.default },
_react2.default.createElement(_reactRouter.IndexRoute, { component: _Index2.default }),
_react2.default.createElement(_reactRouter.Route, { path: '/mods',
component: _ModsContent2.default
}),
_react2.default.createElement(_reactRouter.Route, { path: '/mods', component: _ModsContent2.default }),
_react2.default.createElement(_reactRouter.Route, { path: '/logs', component: _LogsContent2.default }),
_react2.default.createElement(_reactRouter.Route, { path: '/saves', component: _SavesContent2.default })
)
@ -26272,11 +26270,6 @@
_react2.default.createElement(
'div',
{ className: 'box-body' },
_react2.default.createElement(
'h4',
null,
'Upload Mod'
),
_react2.default.createElement(
'div',
{ className: 'table-responsive' },
@ -26526,17 +26519,18 @@
_react2.default.createElement(
'div',
{ className: 'box-body' },
_react2.default.createElement(
'h4',
null,
'Upload Mod'
),
_react2.default.createElement(
'form',
{ ref: 'uploadForm', className: 'form', encType: 'multipart/form-data' },
_react2.default.createElement(
'fieldset',
null,
_react2.default.createElement(
'span',
{ className: 'btn btn-default btn-file' },
_react2.default.createElement('input', { ref: 'file', type: 'file', name: 'modfile', id: 'modfile' })
),
_react2.default.createElement('input', { className: 'btn btn-default', ref: 'file', type: 'file', name: 'modfile', id: 'modfile' }),
_react2.default.createElement('input', { type: 'button', ref: 'button', value: 'Upload', onClick: this.uploadFile })
)
),
@ -27042,7 +27036,6 @@
}, {
key: 'removeSave',
value: function removeSave(saveName, e) {
console.log(e, saveName);
$.ajax({
url: "/api/saves/rm/" + saveName,
success: function success(data) {
@ -27072,17 +27065,39 @@
'div',
{ className: 'box-body' },
_react2.default.createElement(
'form',
{ ref: 'uploadForm', className: 'form', encType: 'multipart/form-data' },
'div',
{ className: 'box' },
_react2.default.createElement(
'fieldset',
null,
'div',
{ className: 'box-header' },
_react2.default.createElement(
'span',
{ className: 'btn btn-default btn-file' },
_react2.default.createElement('input', { ref: 'file', type: 'file', name: 'modfile', id: 'modfile' })
),
_react2.default.createElement('input', { type: 'button', ref: 'button', value: 'Upload', onClick: this.uploadFile })
'h4',
{ className: 'box-title' },
'Upload Save File'
)
),
_react2.default.createElement(
'div',
{ className: 'box-body' },
_react2.default.createElement(
'form',
{ ref: 'uploadForm', className: 'form-inline', encType: 'multipart/form-data' },
_react2.default.createElement(
'div',
{ className: 'form-group' },
_react2.default.createElement(
'label',
{ 'for': 'savefile' },
'Upload Save File...'
),
_react2.default.createElement('input', { className: 'form-control btn btn-default', ref: 'file', type: 'file', name: 'savefile', id: 'savefile' })
),
_react2.default.createElement(
'div',
{ className: 'form-group' },
_react2.default.createElement('input', { className: 'form-control btn btn-default', type: 'button', ref: 'button', value: 'Upload', onClick: this.uploadFile })
)
)
)
),
_react2.default.createElement(

39
gameconfig.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"encoding/json"
"log"
"github.com/go-ini/ini"
)
func loadConfig(filename string) ([]byte, error) {
log.Printf("Loading config file: %s", filename)
cfg, err := ini.Load(filename)
if err != nil {
log.Printf("Error loading config.ini file: %s", err)
return nil, err
}
result := map[string]map[string]string{}
sections := cfg.Sections()
sectionNames := cfg.SectionStrings()
log.Printf("Appending sections %s to JSON response", sectionNames)
for _, s := range sections {
sectionName := s.Name()
if sectionName == "DEFAULT" {
continue
}
result[sectionName] = map[string]string{}
result[sectionName] = s.KeysHash()
}
log.Printf("Encoding config.ini to JSON")
resp, err := json.Marshal(result)
if err != nil {
log.Printf("Error marshaling config.ini to JSON: %s", err)
return nil, err
}
return resp, nil
}

View File

@ -84,10 +84,8 @@ func UploadMod(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(32 << 20)
file, header, err := r.FormFile("modfile")
if err != nil {
json.NewEncoder(w).Encode(err.Error())
log.Printf("%+v", file)
log.Printf("%+v", header)
log.Printf("Error in formfile")
log.Printf("No mod filename provided for upload: %s", err)
json.NewEncoder(w).Encode("No mod file provided.")
return
}
defer file.Close()
@ -224,6 +222,8 @@ func UploadSave(w http.ResponseWriter, r *http.Request) {
}
}
// Deletes provided save
//TODO sanitize
func RemoveSave(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
@ -232,7 +232,7 @@ func RemoveSave(w http.ResponseWriter, r *http.Request) {
err := rmSave(saveName)
if err == nil {
// No error returned means save was removed
// save was removed
resp := fmt.Sprintf("Removed save: %s", saveName)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error removing save %s", err)
@ -244,7 +244,6 @@ func RemoveSave(w http.ResponseWriter, r *http.Request) {
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error removing save: %s", err)
}
return
}
}
@ -262,3 +261,22 @@ func LogTail(w http.ResponseWriter, r *http.Request) {
log.Printf("Error tailing logfile", err)
}
}
// Return JSON response of config.ini file
func LoadConfig(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
configContents, err := loadConfig(config.FactorioConfigFile)
if err != nil {
log.Printf("Could not retrieve config.ini: %s", err)
resp := "Error getting config.ini"
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error tailing logfile", err)
}
return
}
if _, err := w.Write(configContents); err != nil {
log.Printf("Error encoding config.ini response: %s", err)
}
log.Printf("Sent config.ini response")
}

20
main.go
View File

@ -7,21 +7,23 @@ import (
)
type Config struct {
FactorioDir string
FactorioSavesDir string
FactorioModsDir string
FactorioLog string
ServerIP string
ServerPort string
MaxUploadSize int64
FactorioDir string
FactorioSavesDir string
FactorioModsDir string
FactorioConfigFile string
FactorioLog string
ServerIP string
ServerPort string
MaxUploadSize int64
}
var config Config
func loadFlags() {
factorioDir := flag.String("dir", "./", "Specify location of Factorio config directory.")
factorioDir := flag.String("dir", "./", "Specify location of Factorio directory.")
factorioIP := flag.String("host", "0.0.0.0", "Specify IP for webserver to listen on.")
factorioPort := flag.String("port", "8080", "Specify a port for the server.")
factorioConfigFile := flag.String("config", "config/config.ini", "Specify location of Factorio config.ini file")
factorioMaxUpload := flag.Int64("max-upload", 100000, "Maximum filesize for uploaded files.")
flag.Parse()
@ -29,11 +31,11 @@ func loadFlags() {
config.FactorioDir = *factorioDir
config.FactorioSavesDir = config.FactorioDir + "/saves"
config.FactorioModsDir = config.FactorioDir + "/mods"
config.FactorioConfigFile = config.FactorioDir + "/" + *factorioConfigFile
config.ServerIP = *factorioIP
config.ServerPort = *factorioPort
config.FactorioLog = config.FactorioDir + "/factorio-current.log"
config.MaxUploadSize = *factorioMaxUpload
log.Printf(config.FactorioSavesDir)
}
func main() {

View File

@ -43,15 +43,17 @@ func rmMod(modName string) error {
if modName == "" {
return errors.New("No mod name provided.")
}
// Get list of installed mods
installedMods, err := listInstalledMods(config.FactorioModsDir)
if err != nil {
log.Printf("Error in remove mod list: %s", err)
return err
}
// Check if provided mod matches one thats installed else return err
for _, mod := range installedMods {
log.Printf("Checking if %s in %s", mod, modName)
if strings.Contains(mod, modName) {
log.Printf("Removing mod: %s", mod)
err := os.Remove(config.FactorioModsDir + "/" + mod)
if err != nil {
log.Printf("Error removing mod %s: %s", mod, err)

View File

@ -109,5 +109,10 @@ var apiRoutes = Routes{
"GET",
"/log/tail",
LogTail,
}, {
"LoadConfig",
"GET",
"/config/get",
LoadConfig,
},
}

1
server.go Normal file
View File

@ -0,0 +1 @@
package main

View File

@ -39,7 +39,6 @@ class InstalledMods extends React.Component {
}
render() {
return(
<div className="box">
<div className="box-header">
@ -47,11 +46,10 @@ class InstalledMods extends React.Component {
</div>
<div className="box-body">
<h4>Upload Mod</h4>
<form ref="uploadForm" className="form" encType='multipart/form-data'>
<fieldset>
<span className="btn btn-default btn-file">
<input ref="file" type="file" name="modfile" id="modfile" />
</span>
<input className="btn btn-default" ref="file" type="file" name="modfile" id="modfile" />
<input type="button" ref="button" value="Upload" onClick={this.uploadFile} />
</fieldset>

View File

@ -10,7 +10,6 @@ class ModList extends React.Component {
</div>
<div className="box-body">
<h4>Upload Mod</h4>
<div className="table-responsive">
<table className="table table-striped">
<thead>

View File

@ -32,7 +32,6 @@ class SavesList extends React.Component {
}
removeSave(saveName, e) {
console.log(e, saveName);
$.ajax({
url: "/api/saves/rm/" + saveName,
success: (data) => {
@ -48,17 +47,24 @@ class SavesList extends React.Component {
<div className="box-header">
<h3 className="box-title">Save Files</h3>
</div>
<div className="box-body">
<form ref="uploadForm" className="form" encType='multipart/form-data'>
<fieldset>
<span className="btn btn-default btn-file">
<input ref="file" type="file" name="modfile" id="modfile" />
</span>
<input type="button" ref="button" value="Upload" onClick={this.uploadFile} />
</fieldset>
</form>
<div className="box">
<div className="box-header">
<h4 className="box-title">Upload Save File</h4>
</div>
<div className="box-body">
<form ref="uploadForm" className="form-inline" encType='multipart/form-data'>
<div className="form-group">
<label for="savefile">Upload Save File...</label>
<input className="form-control btn btn-default" ref="file" type="file" name="savefile" id="savefile" />
</div>
<div className="form-group">
<input className="form-control btn btn-default" type="button" ref="button" value="Upload" onClick={this.uploadFile} />
</div>
</form>
</div>
</div>
<div className="table-responsive">
<table className="table table-striped">

View File

@ -11,9 +11,7 @@ ReactDOM.render((
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRoute component={Index}/>
<Route path="/mods"
component={ModsContent}
/>
<Route path="/mods" component={ModsContent}/>
<Route path="/logs" component={LogsContent}/>
<Route path="/saves" component={SavesContent}/>
</Route>