Beispiel #1
0
// loadConfiguration loads the configuration of application
func loadConfiguration(app *AppConfig, rdis *RedisConfig, nats *NatsConfig) {
	err := envconfig.Process(ServiceName, app)
	if err != nil {
		log.Panicln(err)
	}
	err = envconfig.Process("redis", rdis)
	if err != nil {
		log.Panicln(err)
	}
	err = envconfig.Process("nats", nats)
	if err != nil {
		log.Panicln(err)
	}
	if len(os.Getenv(KeyLogly)) > 0 {
		log.Printf("Loading logly token %s \n", os.Getenv(KeyLogly))
		hook := logrusly.NewLogglyHook(os.Getenv(KeyLogly),
			app.Host,
			log.InfoLevel,
			app.Name)
		log.AddHook(hook)
	}

	log.Println("#### LOADED CONFIG #####")
	log.Printf("REDIS_URI: %s \n", rdis.URI)
	log.Printf("NATS_ENDPOINT: %s \n", nats.Endpoint)
}
Beispiel #2
0
// LoadConfig will load the configuration file from filePath, if it can't open
// the file for reading, it assumes there is no configuration file and will try to create
// one on the default path (tyk.conf in the local directory)
func loadConfig(filePath string, configStruct *Config) {
	configuration, err := ioutil.ReadFile(filePath)
	if err != nil {
		log.Error("Couldn't load configuration file")
		log.Error(err)
		log.Info("Writing a default file to ./tyk.conf")

		WriteDefaultConf(configStruct)

		log.Info("Loading default configuration...")
		loadConfig("tyk.conf", configStruct)
	} else {
		if err := json.Unmarshal(configuration, &configStruct); err != nil {
			log.Error("Couldn't unmarshal configuration")
			log.Error(err)
		}

		overrideErr := envconfig.Process(ENV_PREVIX, configStruct)
		if overrideErr != nil {
			log.Error("Failed to process environment variables after file load: ", overrideErr)
		}
	}

	if configStruct.SlaveOptions.CallTimeout == 0 {
		configStruct.SlaveOptions.CallTimeout = 30
	}
	if configStruct.SlaveOptions.PingTimeout == 0 {
		configStruct.SlaveOptions.PingTimeout = 60
	}
	GlobalRPCPingTimeout = time.Second * time.Duration(configStruct.SlaveOptions.PingTimeout)
	GlobalRPCCallTimeout = time.Second * time.Duration(configStruct.SlaveOptions.CallTimeout)
	configStruct.EventTriggers = InitGenericEventHandlers(configStruct.EventHandlers)
}
Beispiel #3
0
// WriteDefaultConf will create a default configuration file and set the storage type to "memory"
func WriteDefaultConf(configStruct *Config) {
	configStruct.ListenAddress = ""
	configStruct.ListenPort = 8080
	configStruct.Secret = "352d20ee67be67f6340b4c0605b044b7"
	configStruct.TemplatePath = "./templates"
	configStruct.TykJSPath = "./js/tyk.js"
	configStruct.MiddlewarePath = "./middleware"
	configStruct.Storage.Type = "redis"
	configStruct.AppPath = "./apps/"
	configStruct.Storage.Host = "localhost"
	configStruct.Storage.Username = ""
	configStruct.Storage.Password = ""
	configStruct.Storage.Database = 0
	configStruct.Storage.MaxIdle = 100
	configStruct.Storage.Port = 6379
	configStruct.EnableAnalytics = false
	configStruct.HealthCheck.EnableHealthChecks = true
	configStruct.HealthCheck.HealthCheckValueTimeout = 60
	configStruct.AnalyticsConfig.IgnoredIPs = make([]string, 0)
	configStruct.UseAsyncSessionWrite = false
	configStruct.HideGeneratorHeader = false
	configStruct.OauthRedirectUriSeparator = ""
	newConfig, err := json.MarshalIndent(configStruct, "", "    ")
	overrideErr := envconfig.Process(ENV_PREVIX, &configStruct)
	if overrideErr != nil {
		log.Error("Failed to process environment variables: ", overrideErr)
	}
	if err != nil {
		log.Error("Problem marshalling default configuration!")
		log.Error(err)
	} else {
		ioutil.WriteFile("tyk.conf", newConfig, 0644)
	}
}
//environment variables must be in following format
//SETTINGS_URL_CHANNEL_API
//SETTINGS_QUEUE_HOST
func (settings *envSettings) readEnvironmentVariables() error {
	err := envconfig.Process("settings", settings)
	if err != nil {
		return err
	}
	return nil
}
Beispiel #5
0
func init() {
	// Pull in the configuration
	err := envconfig.Process("buoy", &Config)
	if err != nil {
		tracelog.COMPLETED_ERROR(err, helper.MAIN_GO_ROUTINE, "Init")
	}
}
Beispiel #6
0
func NewApp(name string) (AppCtx, error) {
	specs := Specs{}
	err := envconfig.Process(name, &specs)
	if err != nil {
		return AppCtx{}, err
	}

	url := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v",
		specs.DBUser,
		specs.DBPass,
		specs.DBHost,
		specs.DBPort,
		specs.DBName,
	)

	db, err := sql.Open("mysql", url)
	if err != nil {
		return AppCtx{}, err
	}

	ctx := AppCtx{
		Specs: specs,
		DB:    db,
	}

	return ctx, nil
}
Beispiel #7
0
func LoadConfig() (*Config, error) {
	var config Config

	err := envconfig.Process(configPrefix, &config)

	if err != nil {
		return nil, errors.Wrap(err, "Failed to load config from envs.")
	}

	if _, err := os.Stat(configFilePath); err != nil {
		return &config, nil
	}

	configFromFile, err := loadConfigFromFile(configFilePath)

	if err != nil {
		return nil, err
	}

	for _, configName := range configNames {
		if configName == "MaxAppDeploy" {
			n, err := strconv.ParseInt(configFromFile[configName], 10, 64)

			if err != nil {
				return nil, errors.Wrapf(err, "Failed to parse %s as integer. value: %s", configName, configFromFile[configName])
			}

			reflect.ValueOf(&config).Elem().FieldByName(configName).SetInt(n)
		} else {
			reflect.ValueOf(&config).Elem().FieldByName(configName).SetString(configFromFile[configName])
		}
	}

	return &config, nil
}
Beispiel #8
0
func configure() {
	err := envconfig.Process(appName, &config)
	if err != nil {
		log.Println("Errof: %v\n", err.Error())
	}

	log.Println("Initial generation of static files...")
	generateStaticFiles()

	log.Println("Initializing image watchdog...")
	watcher, err = fsnotify.NewWatcher()
	if err != nil {
		log.Printf("Error: %v\n", err.Error())
	}

	err = watcher.Watch("src/assets/photos")
	if err != nil {
		log.Printf("Error: %v\n", err.Error())
	}

	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				if ev.IsCreate() {
					log.Println("Regenerating static files...")
					generateStaticFiles()
				}
			}
		}
	}()

	log.Println("Configuration complete...")
}
Beispiel #9
0
func (pureInfoService) Host() (server.HostResponse, error) {
	var errResp string
	var hostSpec HostSpec

	err := envconfig.Process("infosvc", &hostSpec)
	if err != nil {
		errResp = err.Error()
	}

	var ips []string

	addrs, _ := net.InterfaceAddrs()
	for _, addr := range addrs {
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				ips = append(ips, ipnet.IP.String())
			}
		}
	}

	myHostName, err := os.Hostname()
	if err != nil {
		errResp = err.Error()
	}

	var myOS = fmt.Sprintf("%s %s", hostSpec.OS, hostSpec.Arch)
	return server.HostResponse{Name: myHostName, OS: myOS, Addrs: ips, Err: errResp}, nil
}
Beispiel #10
0
// Startup brings the manager to a running state.
func Startup(sessionID string) error {
	// If the system has already been started ignore the call.
	if singleton.sessions != nil {
		return nil
	}

	log.Started(sessionID, "Startup")

	// Pull in the configuration.
	var config mongoConfiguration
	if err := envconfig.Process("mgo", &config); err != nil {
		log.CompletedError(err, sessionID, "Startup")
		return err
	}

	// Create the Mongo Manager.
	singleton = mongoManager{
		sessions: make(map[string]mongoSession),
	}

	// Create the strong session.
	if err := CreateSession(sessionID, "strong", MasterSession, config.Url); err != nil {
		log.CompletedError(err, sessionID, "Startup")
		return err
	}

	// Create the monotonic session.
	if err := CreateSession(sessionID, "monotonic", MonotonicSession, config.Url); err != nil {
		log.CompletedError(err, sessionID, "Startup")
		return err
	}

	log.Completed(sessionID, "Startup")
	return nil
}
Beispiel #11
0
func main() {
	log = log15.New()
	log.Info("starting server...")

	envconfig.Process("", &settings)

	schema, err := graphql.NewSchema(makeSchema())
	if err != nil {
		log.Info("error creating schema", "err", err)
		return
	}

	h := handler.New(&handler.Config{
		Schema: &schema,
		Pretty: false,
	})

	mux := http.NewServeMux()
	mux.Handle("/graphql", h)
	mux.Handle("/", http.FileServer(http.Dir("dist")))

	log.Info("Listening at " + settings.Port + "...")
	graceful.Run(":"+settings.Port, 1*time.Second, mux)
	log.Info("Exiting...")
}
Beispiel #12
0
func init() {
	var c Config

	envconfig.Process("proxym_mesos_master", &c)

	if c.Enabled {
		err := sanitizeConfig(&c)
		if err != nil {
			log.ErrorLog.Critical("Not initializing module Mesos Master: '%s'", err)
			return
		}

		lr := &leaderRegistry{
			mutex: &sync.Mutex{},
		}

		n, err := NewMesosNotifier(&c, lr)
		if err != nil {
			log.ErrorLog.Fatal(err)
		}
		manager.AddNotifier(n)

		sg := &MesosMasterServiceGenerator{
			config:         &c,
			leaderRegistry: lr,
		}
		manager.AddServiceGenerator(sg)
	}
}
Beispiel #13
0
func Get() (*Config, error) {
	var ret Config
	if err := envconfig.Process(AppName, &ret); err != nil {
		return nil, err
	}
	return &ret, nil
}
Beispiel #14
0
// InitContex initializes the app context
func InitContex() *Context {
	if _context != nil {
		panic("Context already exists")
	}

	_context = new(Context)

	if err := envconfig.Process("api", &_context.Params); err != nil {
		panic(err)
	}

	// Setup database
	session, err := mgo.Dial(_context.Params.MongoURI)
	if err != nil {
		fmt.Println("Cannot start mongo")
		panic(err)
	}
	_context.Session = session
	_context.Session.SetMode(mgo.Monotonic, true)
	_context.DB = session.DB("")

	// LogEntries
	if _context.Params.LogEntriesToken != "" {
		_context.LogEntries, err = le_go.Connect(_context.Params.LogEntriesToken)
		if err != nil {
			panic(err)
		}
	}

	return _context
}
Beispiel #15
0
// Read the configuration from the environment
// Defaults will be set for unset fields
func (config *Configuration) Read() error {
	err := envconfig.Process("", config)
	if err != nil {
		log.Fatal("Failed to load configuration, error is - " + err.Error())
		return err
	}

	if config.DownloadListFile == "" {
		config.DownloadListFile = "./list.json"
	}
	if config.WWWRoot == "" {
		config.WWWRoot = "/www"
	}
	if config.Port == 0 {
		config.Port = 80
	}
	if config.MaximumNumberRetry == 0 {
		config.MaximumNumberRetry = 3
	}
	if config.MaximumParallelDownloads == 0 {
		config.MaximumParallelDownloads = 5
	}

	log.Printf("Staging UI configuration: %+v", config)
	return nil
}
Beispiel #16
0
// Load configuration settings from the environment, apply defaults, and validate them.
func (c *Context) Load() error {
	if err := envconfig.Process("PIPE", &c.Settings); err != nil {
		return err
	}

	if c.Port == 0 {
		c.Port = 8000
	}

	if c.LogLevel == "" {
		c.LogLevel = "info"
	}

	if c.MongoURL == "" {
		c.MongoURL = "mongo"
	}

	if c.Poll == 0 {
		c.Poll = 500
	}

	if c.DockerHost == "" {
		if host := os.Getenv("DOCKER_HOST"); host != "" {
			c.DockerHost = host
		} else {
			c.DockerHost = "unix:///var/run/docker.sock"
		}
	}

	certRoot := os.Getenv("DOCKER_CERT_PATH")
	if certRoot == "" {
		certRoot = "/certificates"
	}

	if c.CACert == "" {
		c.CACert = path.Join(certRoot, "ca.pem")
	}

	if c.Cert == "" {
		c.Cert = path.Join(certRoot, "cloudpipe-cert.pem")
	}

	if c.Key == "" {
		c.Key = path.Join(certRoot, "cloudpipe-key.pem")
	}

	if c.DefaultImage == "" {
		c.DefaultImage = "cloudpipe/runner-py2"
	}

	if c.Settings.AuthService == "" {
		c.Settings.AuthService = "https://authstore:9001/v1"
	}

	if _, err := log.ParseLevel(c.LogLevel); err != nil {
		return err
	}

	return nil
}
Beispiel #17
0
// LoadBot creates a new BotAPI instance using environmental variables.
// It also makes the maps needed for the bot to run, and loads admins.
func LoadBot() *tgbotapi.BotAPI {
	var cfg Config
	err := envconfig.Process("TELEGRAM", &cfg)
	if err != nil {
		log.Fatal(err)
	}

	tg, err := tgbotapi.NewBotAPI(cfg.APIToken)
	if cfg.APIDebug {
		tg.Debug = true
	}

	if err != nil {
		log.Panic(err)
	}

	bot.Users = make(map[int]tgbotapi.User)
	bot.Admins = make(map[int]bool)
	for _, id := range strings.Split(cfg.Admins, ",") {
		i, err := strconv.Atoi(id)
		if err != nil {
			log.Panic("Admin IDs must be numbers!")
		}

		bot.Admins[i] = true
	}

	return tg
}
Beispiel #18
0
// GetConfig uses envconfig to populate and return a Config struct. Returns all envconfig errors if they occurred
func GetConfig() (*Config, error) {
	conf := new(Config)
	if err := envconfig.Process(AppName, conf); err != nil {
		return nil, err
	}
	return conf, nil
}
Beispiel #19
0
// Main entry point that starts the messaging bridge, visit service, and web
// server.
func main() {

	var s Specification
	var port string

	// Load environment variables.
	if err := envconfig.Process("icra", &s); err != nil {
		log.Fatalf("Could not read environment variables: %s", err.Error())
	}
	if port = os.Getenv("VCAP_APP_PORT"); len(port) == 0 {
		log.Printf("Warning, VCAP_APP_PORT not set. Defaulting to %+v\n", s.Port)
		port = s.Port
	}

	// Generate a client identifier.
	id := fmt.Sprintf("icra-client-%d", os.Getpid())

	// Configure the visit service and messaging bridge.
	vs := NewVisitService(s.BufferSize, s.CacheSize)
	mb := NewMessagingBridge([]string{s.Topic}, vs)

	// Start the visit service.
	go vs.Serve()

	// Connect to the message broker.
	mb.Connect(s.Broker, id)

	// Configure the web server.
	http.Handle("/ws/browsing", vs.MakeHandler())
	http.Handle("/", http.FileServer(http.Dir("static")))

	// Launch the web server.
	http.ListenAndServe(":"+port, nil)

}
Beispiel #20
0
// GetGit gets the Git config using envconfig
func GetGit(appName string) (*Git, error) {
	conf := new(Git)
	if err := envconfig.Process(appName, conf); err != nil {
		return nil, err
	}
	return conf, nil
}
func consumerServer(cmd *cobra.Command, args []string) {
	var config consumerapi.Config

	if err := envconfig.Process("CONSUMER", &config); err != nil {
		log.Fatal("Error occurred during startup:", err.Error())
	}
	log.Print(`.    .                     .
                  _        .                          .            (
                 (_)        .       .                                     .
  .        ____.--^.
   .      /:  /    |                               +           .         .
         /:  '--=--'   .                                                .
        /: __[\=='-.___          *           .
       /__|\ _~~~~~~   ~~--..__            .             .
       \   \|::::|-----.....___|~--.                                 .
        \ _\_~~~~~-----:|:::______//---...___
    .   [\  \  __  --     \       ~  \_      ~~~===------==-...____
        [============================================================-
        /         __/__   --  /__    --       /____....----''''~~~~      .
  *    /  /   ==           ____....=---='''~~~~ .
 .    /____....--=-''':~~~~                      .                .
      .       ~--~              
                     .                                               .
                          .                      .                      .`)

	log.Printf("[+] Started Consumer API on: %s:%d, with MaxWokers: %d and MaxJobs: %d\n", config.Interface, config.Port, config.MaxWorker, config.MaxJobQueue)

	consumerAPIService := consumerapi.NewService(&config)

	if err := consumerAPIService.Start(); err != nil {
		log.Fatal("Error occurred during startup:", err.Error())
	}
}
Beispiel #22
0
func init() {
	// Pull in the configuration
	err := envconfig.Process("buoy", &Config)
	if err != nil {
		tracelog.CompletedError(err, helper.MAIN_GO_ROUTINE, "Init")
	}
}
Beispiel #23
0
func init() {
	err := envconfig.Process("HELEN", &Constants)
	if err != nil {
		logrus.Fatal(err)
	}

	if Constants.SteamDevAPIKey == "" {
		logrus.Warning("Steam api key not provided, setting SteamApiMockUp to true")
	}
	if Constants.PublicAddress == "" {
		Constants.PublicAddress = "http://" + Constants.ListenAddress
	}
	if Constants.MockupAuth {
		logrus.Warning("Mockup authentication enabled.")
	}

	_, err = url.Parse(Constants.PublicAddress)
	if err != nil {
		logrus.Fatal("Couldn't parse HELEN_PUBLIC_ADDR - ", err)
	}
	_, err = url.Parse(Constants.LoginRedirectPath)
	if err != nil {
		logrus.Fatal("Couldn't parse HELEN_SERVER_REDIRECT_PATH - ", err)
	}

	if Constants.GeoIP {
		logrus.Info("GeoIP support enabled")
	}
}
Beispiel #24
0
// GetServer gets the Server config using envconfig
func GetServer(appName string) (*Server, error) {
	conf := new(Server)
	if err := envconfig.Process(appName, conf); err != nil {
		return nil, err
	}
	return conf, nil
}
Beispiel #25
0
// Creates and returns a new Manager.
func New() *Manager {
	refreshChannel := make(chan string, 10)
	quitChannel := make(chan int)

	refreshCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
		Namespace: "proxym",
		Subsystem: "refresh",
		Name:      "count",
		Help:      "Number of refreshes triggered",
	}, []string{"result"})
	prometheus.MustRegister(refreshCounter)

	var c Config
	envconfig.Process("proxym", &c)

	m := &Manager{
		Config:         &c,
		httpRouter:     pat.New(),
		refresh:        refreshChannel,
		refreshCounter: refreshCounter,
		quit:           quitChannel,
	}

	m.httpRouter.Get("/metrics", prometheus.Handler())

	return m
}
Beispiel #26
0
func Parse() (*Conf, error) {
	appConf := &Conf{}
	err := envconfig.Process("bbsgo", appConf)
	if err != nil {
		panic(err)
	}
	return appConf, nil
}
Beispiel #27
0
func main() {
	// Load the configuration.
	err := envconfig.Process("elwinar", &configuration)
	if err != nil {
		log.Fatalln("unable to read the configuration from env:", err)
	}

	// Open the database connection.
	database, err = sqlx.Connect("sqlite3", configuration.Database)
	if err != nil {
		log.Fatalln("unable to open the database:", err)
	}

	// Initialize the router.
	router := httprouter.New()

	// Add the front-office handlers.
	router.GET("/", Index)
	router.GET("/read", List)
	router.GET("/article/:slug", View)
	router.GET("/fortune", Fortune)
	router.GET("/sitemap.xml", Sitemap)

	// Add the back-office handlers.
	router.GET("/login", Login)
	router.POST("/login", Authenticate)
	router.GET("/logout", Logout)
	router.GET("/write", Write)
	router.POST("/write", Create)
	router.GET("/article/:slug/edit", Edit)
	router.POST("/article/:slug/edit", Update)
	router.GET("/article/:slug/delete", Delete)
	router.GET("/article/:slug/publish", Publish)
	router.GET("/article/:slug/unpublish", Unpublish)

	// Initialize the server middleware stack.
	stack := negroni.New()
	stack.Use(gzip.Gzip(gzip.DefaultCompression))
	stack.Use(negroni.NewRecovery())
	stack.Use(negroni.NewStatic(http.Dir(configuration.Public)))
	stack.Use(sessions.Sessions("elwinar", cookiestore.New([]byte(configuration.Secret))))
	stack.UseHandler(router)

	// Initialize the HTTP server.
	server := &graceful.Server{
		Timeout: 1 * time.Second,
		Server: &http.Server{
			Addr:    fmt.Sprintf(":%d", configuration.Port),
			Handler: stack,
		},
	}

	// Run the server.
	err = server.ListenAndServe()
	if err != nil {
		log.Fatalln("unable to run the server:", err)
	}
}
Beispiel #28
0
// LoadEnv loads all configuration from the environment
func LoadEnv() (*Config, error) {
	cfg := &Config{}

	err := envconfig.Process("newrelic", cfg)
	if err != nil {
		return nil, err
	}

	return cfg, nil
}
Beispiel #29
0
func init() {
	// set up error logger
	errlog = log.New(os.Stderr, "[ERROR]", log.Lshortfile|log.LstdFlags)
	// set standard logger to stdout
	log.SetOutput(os.Stdout)
	err := envconfig.Process("sentinel-manager", &config)
	if err != nil {
		log.Fatal(err)
	}
}
Beispiel #30
0
func main() {
	log.SetLevel(log.InfoLevel)
	var configfile string
	flag.StringVar(&configfile, "config", "./broker.json", "Broker JSON config file")
	flag.Parse()

	file, err := ioutil.ReadFile(configfile)
	if err != nil {
		log.Fatal("could not read config file: " + err.Error())
	}

	f := ConfigData{}
	err = json.Unmarshal(file, &f)
	if err != nil {
		log.Fatal("could not parse config file: " + err.Error())
	}
	var c config
	err = envconfig.Process("broker", &c)
	if err != nil {
		log.Fatal(err.Error())
	}

	// defaults to INFO
	if c.LogLevel == "DEBUG" {
		log.SetLevel(log.DebugLevel)
	}
	if c.LogLevel == "WARN" {
		log.SetLevel(log.WarnLevel)
	}
	if c.LogLevel == "ERROR" {
		log.SetLevel(log.ErrorLevel)
	}
	if c.LogLevel == "FATAL" {
		log.SetLevel(log.FatalLevel)
	}

	pubsocket, _ := zmq.NewSocket(zmq.PUB)
	repsocket, _ := zmq.NewSocket(zmq.REP)
	defer pubsocket.Close()
	defer repsocket.Close()
	pubsocket.Bind(f.PubSocket)
	repsocket.Bind(f.RepSocket)

	log.Info("listening on ZMQ sockets")

	var e envelope
	for {
		msg, _ := repsocket.Recv(0)
		json.Unmarshal([]byte(msg), &e)
		pubsocket.Send(e.Address, zmq.SNDMORE)
		pubsocket.Send(e.Content, 0)
		repsocket.Send("published", 0)
		log.Debug("published message")
	}
}