Add API KEY support to allow CORS requests.
[src/xds/xds-agent.git] / main.go
1 // TODO add Doc
2 //
3 package main
4
5 import (
6         "fmt"
7         "log"
8         "os"
9         "time"
10
11         "github.com/Sirupsen/logrus"
12         "github.com/codegangsta/cli"
13         "github.com/iotbzh/xds-agent/lib/agent"
14         "github.com/iotbzh/xds-agent/lib/syncthing"
15         "github.com/iotbzh/xds-agent/lib/webserver"
16         "github.com/iotbzh/xds-agent/lib/xdsconfig"
17 )
18
19 const (
20         appName        = "xds-agent"
21         appDescription = "X(cross) Development System Agent is a web server that allows to remotely cross build applications."
22         appCopyright   = "Apache-2.0"
23         appUsage       = "X(cross) Development System Agent"
24 )
25
26 var appAuthors = []cli.Author{
27         cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
28 }
29
30 // AppVersion is the version of this application
31 var AppVersion = "?.?.?"
32
33 // AppSubVersion is the git tag id added to version string
34 // Should be set by compilation -ldflags "-X main.AppSubVersion=xxx"
35 var AppSubVersion = "unknown-dev"
36
37 // xdsAgent main routine
38 func xdsAgent(cliCtx *cli.Context) error {
39         var err error
40
41         // Create Agent context
42         ctx := agent.NewAgent(cliCtx)
43
44         // Load config
45         ctx.Config, err = xdsconfig.Init(cliCtx, ctx.Log)
46         if err != nil {
47                 return cli.NewExitError(err, 2)
48         }
49
50         // Start local instance of Syncthing and Syncthing-notify
51         ctx.SThg = st.NewSyncThing(ctx.Config, ctx.Log)
52
53         ctx.Log.Infof("Starting Syncthing...")
54         ctx.SThgCmd, err = ctx.SThg.Start()
55         if err != nil {
56                 return cli.NewExitError(err, 2)
57         }
58         fmt.Printf("Syncthing started (PID %d)\n", ctx.SThgCmd.Process.Pid)
59
60         ctx.Log.Infof("Starting Syncthing-inotify...")
61         ctx.SThgInotCmd, err = ctx.SThg.StartInotify()
62         if err != nil {
63                 return cli.NewExitError(err, 2)
64         }
65         fmt.Printf("Syncthing-inotify started (PID %d)\n", ctx.SThgInotCmd.Process.Pid)
66
67         // Establish connection with local Syncthing (retry if connection fail)
68         time.Sleep(3 * time.Second)
69         retry := 10
70         for retry > 0 {
71                 if err := ctx.SThg.Connect(); err == nil {
72                         break
73                 }
74                 ctx.Log.Infof("Establishing connection to Syncthing (retry %d/10)", retry)
75                 time.Sleep(time.Second)
76                 retry--
77         }
78         if err != nil || retry == 0 {
79                 return cli.NewExitError(err, 2)
80         }
81
82         // Retrieve Syncthing config
83         id, err := ctx.SThg.IDGet()
84         if err != nil {
85                 return cli.NewExitError(err, 2)
86         }
87         ctx.Log.Infof("Local Syncthing ID: %s", id)
88
89         // Create and start Web Server
90         ctx.WWWServer = webserver.New(ctx.Config, ctx.Log)
91         if err = ctx.WWWServer.Serve(); err != nil {
92                 log.Println(err)
93                 return cli.NewExitError(err, 3)
94         }
95
96         return cli.NewExitError("Program exited ", 4)
97 }
98
99 // main
100 func main() {
101
102         // Create a new instance of the logger
103         log := logrus.New()
104
105         // Create a new App instance
106         app := cli.NewApp()
107         app.Name = appName
108         app.Description = appDescription
109         app.Usage = appUsage
110         app.Version = AppVersion + " (" + AppSubVersion + ")"
111         app.Authors = appAuthors
112         app.Copyright = appCopyright
113         app.Metadata = make(map[string]interface{})
114         app.Metadata["version"] = AppVersion
115         app.Metadata["git-tag"] = AppSubVersion
116         app.Metadata["logger"] = log
117
118         app.Flags = []cli.Flag{
119                 cli.StringFlag{
120                         Name:   "config, c",
121                         Usage:  "JSON config file to use\n\t",
122                         EnvVar: "XDS_CONFIGFILE",
123                 },
124                 cli.StringFlag{
125                         Name:   "log, l",
126                         Value:  "error",
127                         Usage:  "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
128                         EnvVar: "LOG_LEVEL",
129                 },
130         }
131
132         // only one action
133         app.Action = xdsAgent
134
135         app.Run(os.Args)
136 }