summaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-09-25 14:15:16 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-10-06 18:25:04 +0200
commit97ca1f277dc8b6973d6fa67add5593a9c395ce60 (patch)
tree761649d7771e8699a67567476c17fb2fa0e28e57 /main.go
parent12a20d0905b0d3e7e0f4c9ec8ee619f683256d71 (diff)
Added webapp Dashboard + logic to interact with server.
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'main.go')
-rw-r--r--main.go67
1 files changed, 12 insertions, 55 deletions
diff --git a/main.go b/main.go
index 32083bb..b0ec7ca 100644
--- a/main.go
+++ b/main.go
@@ -3,16 +3,11 @@
package main
import (
- "fmt"
- "log"
"os"
- "time"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/iotbzh/xds-agent/lib/agent"
- "github.com/iotbzh/xds-agent/lib/syncthing"
- "github.com/iotbzh/xds-agent/lib/webserver"
"github.com/iotbzh/xds-agent/lib/xdsconfig"
)
@@ -39,62 +34,18 @@ func xdsAgent(cliCtx *cli.Context) error {
var err error
// Create Agent context
- ctx := agent.NewAgent(cliCtx)
+ ctxAgent := agent.NewAgent(cliCtx)
// Load config
- ctx.Config, err = xdsconfig.Init(cliCtx, ctx.Log)
+ ctxAgent.Config, err = xdsconfig.Init(cliCtx, ctxAgent.Log)
if err != nil {
return cli.NewExitError(err, 2)
}
- // Start local instance of Syncthing and Syncthing-notify
- ctx.SThg = st.NewSyncThing(ctx.Config, ctx.Log)
+ // Run Agent (main loop)
+ errCode, err := ctxAgent.Run()
- ctx.Log.Infof("Starting Syncthing...")
- ctx.SThgCmd, err = ctx.SThg.Start()
- if err != nil {
- return cli.NewExitError(err, 2)
- }
- fmt.Printf("Syncthing started (PID %d)\n", ctx.SThgCmd.Process.Pid)
-
- ctx.Log.Infof("Starting Syncthing-inotify...")
- ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
- if err != nil {
- return cli.NewExitError(err, 2)
- }
- fmt.Printf("Syncthing-inotify started (PID %d)\n", ctx.SThgInotCmd.Process.Pid)
-
- // Establish connection with local Syncthing (retry if connection fail)
- time.Sleep(3 * time.Second)
- maxRetry := 30
- retry := maxRetry
- for retry > 0 {
- if err := ctx.SThg.Connect(); err == nil {
- break
- }
- ctx.Log.Infof("Establishing connection to Syncthing (retry %d/%d)", retry, maxRetry)
- time.Sleep(time.Second)
- retry--
- }
- if err != nil || retry == 0 {
- return cli.NewExitError(err, 2)
- }
-
- // Retrieve Syncthing config
- id, err := ctx.SThg.IDGet()
- if err != nil {
- return cli.NewExitError(err, 2)
- }
- ctx.Log.Infof("Local Syncthing ID: %s", id)
-
- // Create and start Web Server
- ctx.WWWServer = webserver.New(ctx.Config, ctx.Log)
- if err = ctx.WWWServer.Serve(); err != nil {
- log.Println(err)
- return cli.NewExitError(err, 3)
- }
-
- return cli.NewExitError("Program exited ", 4)
+ return cli.NewExitError(err, errCode)
}
// main
@@ -126,7 +77,13 @@ func main() {
Name: "log, l",
Value: "error",
Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
- EnvVar: "LOG_LEVEL",
+ EnvVar: "XDS_LOGLEVEL",
+ },
+ cli.StringFlag{
+ Name: "logfile",
+ Value: "stdout",
+ Usage: "filename where logs will be redirected (default stdout)\n\t",
+ EnvVar: "XDS_LOGFILE",
},
}