Example #1
0
func main() {
	var configFile string
	var ca string
	var showVersion bool
	var shouldUpdate bool
	var checkUpdate bool

	flag.StringVar(&configFile, "c", "", "Configuration file.")
	flag.StringVar(&ca, "ca", "", "Root CA certificate.")
	flag.BoolVar(&showVersion, "version", false, "Show version information.")
	flag.BoolVar(&shouldUpdate, "update", false, "Update the binary.")
	flag.BoolVar(&checkUpdate, "checkupdate", false, "Check for updates.")
	flag.Parse()

	printHelp := func() {
		fmt.Fprint(os.Stderr, usage)
	}

	if showVersion {
		if !version.PrintVersionInformation(os.Stdout) {
			os.Exit(1)
		}
		return
	}

	if checkUpdate {
		updates.CheckForUpdates(BinaryBaseName)
		return
	}

	if shouldUpdate {
		updates.UpdateApp(BinaryBaseName)
		return
	}

	if configFile == "" {
		fmt.Fprintln(os.Stderr, "fatal: flag: missing -c option")
		fmt.Fprintln(os.Stderr, "")
		printHelp()
		os.Exit(1)
	}

	env := &environment{}

	if ca != "" {
		var config tls.Config
		pem, err := ioutil.ReadFile(ca)
		if err != nil {
			fmt.Fprintf(os.Stderr, "fatal: read %v: %v\n", ca, err)
			os.Exit(1)
		}

		config.RootCAs = x509.NewCertPool()
		if !config.RootCAs.AppendCertsFromPEM(pem) {
			log.Println("warning: x509: could not use PEM in", ca)
		}

		env.Server.Session.Client = &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &config,
			},
		}
	}

	// Parse configuration file.
	fh, err := os.Open(configFile)
	if err != nil {
		log.Fatal(err)
	}
	defer fh.Close()

	if err := env.Configuration.ParseConfiguration(fh); err != nil {
		fmt.Fprintln(os.Stderr, "fatal: could not parse configuration file",
			configFile+":", err)
		os.Exit(1)
	}

	// Configure server behaviour
	env.Server.Configuration = env.Configuration

	for {
		// Register the client.
		if initialized, err, recoverable := register(env); err != nil {
			if !recoverable {
				log.Fatal("Could not register:", err)
			} else {
				log.Println("Could not register:", err)
			}
		} else {
			// Registration was successful, constantly update now.
			log.Println("Successfully registered:", initialized)
			started := make(map[string]widgets.Widget)
			for {
				if err := update(env, initialized, started); err != nil {
					log.Println("Could not update widgets:", err)
					break
				}
			}
		}

		// Don't overload
		t := 5 * time.Second
		log.Println("Reregistering in", t)
		<-time.After(t)
	}
}
Example #2
0
func main() {
	var configFile string
	var listenAddr string
	var debug bool
	var showVersion bool
	var shouldUpdate bool
	var checkUpdate bool

	flag.StringVar(&configFile, "c", "", "Configuration file.")
	flag.StringVar(&listenAddr, "listen", ":8050", "Listen address.")
	flag.BoolVar(&debug, "debug", false, "Debug gin router.")
	flag.BoolVar(&showVersion, "version", false, "Show version information.")
	flag.BoolVar(&shouldUpdate, "update", false, "Update the binary.")
	flag.BoolVar(&checkUpdate, "checkupdate", false, "Check for updates.")
	flag.Parse()

	printHelp := func() {
		fmt.Fprint(os.Stderr, usage)
	}

	if showVersion {
		if !version.PrintVersionInformation(os.Stdout) {
			os.Exit(1)
		}
		return
	}

	if checkUpdate {
		updates.CheckForUpdates(BinaryBaseName)
		return
	}

	if shouldUpdate {
		updates.UpdateApp(BinaryBaseName)
		return
	}

	if configFile == "" {
		fmt.Fprintln(os.Stderr, "fatal: flag: missing -c option")
		fmt.Fprintln(os.Stderr, "")
		printHelp()
		os.Exit(1)
	}

	// Setup configuration
	config := server.Configuration{}
	if err := config.ParseConfiguration(configFile); err != nil {
		fmt.Fprintln(os.Stderr, "fatal: could not parse configuration file ",
			configFile+":", err)
		os.Exit(1)
	}

	if !debug {
		gin.SetMode(gin.ReleaseMode)
	}
	router := gin.Default()

	// Setup user authenticatuer
	userAuth := server.UserAuthenticator{
		Configuration: config,
	}

	// Create a server
	serv := server.NewServer(config)

	// Install static api
	staticApi := static.Static{
		UserAuthenticator: userAuth,
	}
	if err := staticApi.Install(router); err != nil {
		log.Println("We could not serve the static files. If not already")
		log.Println("done, set the HTML_ROOT environment variable to the")
		log.Println("of the static files.")
		log.Fatal("fatal: could not serve static files:", err)
	}

	// Install rest api
	restApi := api.API{
		Configuration:     config,
		Server:            serv,
		UserAuthenticator: userAuth,
		Scheduler:         scheduler.New(config),
	}
	if err := restApi.Install(router); err != nil {
		log.Fatal("fatal: could not serve API:", err)
	}

	router.Run(listenAddr)
}