Initial main commit.
[src/xds/xds-server.git] / main.go
1 // TODO add Doc
2 //
3 package main
4
5 import (
6         "log"
7         "os"
8
9         "github.com/Sirupsen/logrus"
10         "github.com/codegangsta/cli"
11         "github.com/iotbzh/xds-server/lib/xdsconfig"
12         "github.com/iotbzh/xds-server/lib/xdsserver"
13 )
14
15 const (
16         appName        = "xds-server"
17         appDescription = "X(cross) Development System Server is a web server that allows to remotely cross build applications."
18         appVersion     = "0.0.1"
19         appCopyright   = "Apache-2.0"
20         appUsage       = "X(cross) Development System Server"
21 )
22
23 var appAuthors = []cli.Author{
24         cli.Author{Name: "Sebastien Douheret", Email: "sebastien@iot.bzh"},
25 }
26
27 // AppVersionGitTag is the git tag id added to version string
28 // Should be set by compilation -ldflags "-X main.AppVersionGitTag=xxx"
29 var AppVersionGitTag = "unknown-dev"
30
31 // Web server main routine
32 func webServer(ctx *cli.Context) error {
33
34         // Init config
35         cfg, err := xdsconfig.Init(ctx)
36         if err != nil {
37                 return cli.NewExitError(err, 2)
38         }
39
40         // Create and start Web Server
41         svr := xdsserver.NewServer(cfg)
42         if err = svr.Serve(); err != nil {
43                 log.Println(err)
44                 return cli.NewExitError(err, 3)
45         }
46
47         return cli.NewExitError("Program exited ", 4)
48 }
49
50 // main
51 func main() {
52
53         // Create a new instance of the logger
54         log := logrus.New()
55
56         // Create a new App instance
57         app := cli.NewApp()
58         app.Name = appName
59         app.Description = appDescription
60         app.Usage = appUsage
61         app.Version = appVersion + " (" + AppVersionGitTag + ")"
62         app.Authors = appAuthors
63         app.Copyright = appCopyright
64         app.Metadata = make(map[string]interface{})
65         app.Metadata["version"] = appVersion
66         app.Metadata["git-tag"] = AppVersionGitTag
67         app.Metadata["logger"] = log
68
69         app.Flags = []cli.Flag{
70                 cli.StringFlag{
71                         Name:   "config, c",
72                         Usage:  "JSON config file to use\n\t",
73                         EnvVar: "APP_CONFIG",
74                 },
75                 cli.StringFlag{
76                         Name:   "log, l",
77                         Value:  "error",
78                         Usage:  "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
79                         EnvVar: "LOG_LEVEL",
80                 },
81         }
82
83         // only one action: Web Server
84         app.Action = webServer
85
86         app.Run(os.Args)
87 }