Example #1
0
func main() {
	// Parse arguments
	listenPtr := flag.String("listen", "localhost:8000", "host and port to listen on")
	configPtr := flag.String("config", "config.json", "config file")
	sentryPtr := flag.String("sentry", "", "Sentry DSN")
	fluentPtr := flag.String("fluent", "localhost:24220", "td-agent host and port")
	flag.Parse()

	// setup sentry
	dsn := os.Getenv("SENTRY_DSN")
	if len(*sentryPtr) > 0 {
		dsn = *sentryPtr
	}

	if len(dsn) > 0 {
		fmt.Println("Enabling sentry: ", dsn)
		raven.SetDSN(dsn)
	}

	// load config
	tmp, err := config.LoadConfigFile(*configPtr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Couldn't load config '%s': %s\n", *configPtr, err)
		os.Exit(1)
	}

	bouncerConfig = tmp

	setupConfigReload(*configPtr)
	logger := setupFluentd(*fluentPtr)

	fmt.Println("Listening on", *listenPtr, "and logging to", *fluentPtr, "pid", os.Getpid())
	err = http.ListenAndServe(*listenPtr, setupRouter(getConfig, logger))

	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(74) // io error
	}
}
Example #2
0
func setupConfigReload(filename string) {
	go func() {
		reload := make(chan os.Signal, 1)
		signal.Notify(reload, syscall.SIGUSR2)

		for {
			<-reload
			tmp, err := config.LoadConfigFile(filename)

			if err != nil {
				fmt.Fprintln(os.Stderr, "Error reloading config", err)
				return
			}

			configLock.RLock()
			*bouncerConfig = *tmp
			configLock.RUnlock()

			fmt.Println("Reloaded config:", filename)
		}
	}()
}