added handler for tailing game log file

This commit is contained in:
majormjr 2016-04-18 12:39:09 -04:00
parent 7c60375c13
commit 3b14731509
14 changed files with 20269 additions and 24 deletions

23
gamelog.go Normal file
View File

@ -0,0 +1,23 @@
package main
import (
"log"
"github.com/hpcloud/tail"
)
func tailLog(filename string) ([]string, error) {
result := []string{}
t, err := tail.TailFile(config.FactorioLog, tail.Config{Follow: false})
if err != nil {
log.Printf("Error tailing log %s", err)
return result, err
}
for line := range t.Lines {
result = append(result, line.Text)
}
return result, nil
}

View File

@ -14,10 +14,9 @@ func Index(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")
w.WriteHeader(http.StatusOK)
mods := listInstalledMods()
if err := json.NewEncoder(w).Encode(mods); err != nil {
log.Printf("Error in list mods: %s", err)
@ -69,3 +68,17 @@ func ListSaves(w http.ResponseWriter, r *http.Request) {
log.Printf("Error listing saves: %s", err)
}
}
func LogTail(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
logLines, err := tailLog(config.FactorioLog)
if err != nil {
log.Printf("Could not tail %s: %s", config.FactorioLog, err)
return
}
if err := json.NewEncoder(w).Encode(logLines); err != nil {
log.Printf("Error tailing logfile", err)
}
}

10
main.go
View File

@ -2,26 +2,28 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"
)
type Config struct {
FactorioDir string
ServerIP string
FactorioLog string
}
var config Config
func main() {
factorioDir := flag.String("dir", "./", "Specify location of Factorio config directory.")
factorioIP := flag.String("host", "0.0.0.0:8080", "Specify IP and port for webserver to listen on.")
flag.Parse()
config.FactorioDir = *factorioDir
fmt.Println(listInstalledMods())
config.ServerIP = *factorioIP
config.FactorioLog = config.FactorioDir + "/factorio-current.log"
router := NewRouter()
log.Fatal(http.ListenAndServe(":8080", router))
log.Fatal(http.ListenAndServe(config.ServerIP, router))
}

16
mods.go
View File

@ -2,7 +2,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
)
@ -16,6 +15,7 @@ type Mod struct {
Enabled bool `json:"enabled,string"`
}
// List mods installed in the factorio/mods directory
func listInstalledMods() []string {
modDir := config.FactorioDir + "/mods"
result := []string{}
@ -35,6 +35,8 @@ func listInstalledMods() []string {
return result
}
// Parses mod-list.json file in factorio/mods
// returns ModList struct
func parseModList() (ModList, error) {
var mods ModList
modListFile := config.FactorioDir + "/mods/mod-list.json"
@ -52,9 +54,9 @@ func parseModList() (ModList, error) {
}
return mods, nil
}
// Toggles Enabled boolean on each Mod in mod-list.json file
func (m *ModList) toggleMod(name string) error {
found := false
@ -69,16 +71,16 @@ func (m *ModList) toggleMod(name string) error {
}
}
if found == false {
err := fmt.Errorf("Mod with name %s not found", name)
return err
if found {
m.save()
log.Printf("Mod: %s was toggled", name)
}
m.save()
return nil
}
// Saves ModList object to mod-list.json file
// Overwrites old file
func (m ModList) save() error {
modListFile := config.FactorioDir + "/mods/mod-list.json"
b, _ := json.MarshalIndent(m, "", " ")

View File

@ -55,5 +55,10 @@ var routes = Routes{
"GET",
"/saves/list",
ListSaves,
}, {
"TailLog",
"GET",
"/log/tail",
LogTail,
},
}

View File

@ -5,6 +5,7 @@ import (
"log"
)
// Lists save files in factorio/saves
func listSaves() []string {
saveDir := config.FactorioDir + "/saves"
result := []string{}

16
static/js/App/App.jsx Normal file
View File

@ -0,0 +1,16 @@
import React from 'react';
import Hello from './components/hello.jsx';
import World from './components/world.jsx';
class App extends React.Component {
render() {
return(
<div>
<Hello />
<World />
</div>
)
}
}
export default App

View File

@ -1,5 +1,4 @@
import React from 'react';
import ReactDOM from 'react-dom';
class Hello extends React.Component {
render() {
@ -7,4 +6,4 @@ class Hello extends React.Component {
}
}
ReactDOM.render(<Hello />, document.getElementById('hello'));
export default Hello

View File

@ -1,11 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom';
class World extends React.Component {
render() {
return <h1>World</h1>
}
}
ReactDOM.render(<World/>, document.getElementById('world'));
export default World

View File

@ -1,2 +0,0 @@
import Hello from './components/hello.jsx';
import World from './components/world.jsx';

20181
static/js/bundle.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,8 +5,7 @@
<title>Hello React</title>
</head>
<body>
<div id="hello"></div>
<div id="world"></div>
<div id="app"></div>
<script src="bundle.js"></script>
</body>
</html>

8
static/js/index.js Normal file
View File

@ -0,0 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App/App.jsx';
ReactDOM.render(
<App />,
document.getElementById('app')
);

View File

@ -2,7 +2,7 @@ var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './App/main.js',
entry: './index.js',
output: { path: __dirname, filename: 'bundle.js' },
module: {
loaders: [