Beispiel #1
0
func Init() {
	var cfg *Config
	config.LoadJSONFile("./config.json", &cfg)
	config.SetLogOverride(cfg.Log)

	if *cfg.Log != "" {
		lf, err := logrotate.NewFile(*cfg.Log)
		if err != nil {
			Log.Fatalf("unable to access log file: %s", err)
		}
		Log.Out = lf
		Log.Formatter = &logrus.JSONFormatter{}
	} else {
		Log.Out = os.Stderr
	}

	pubsub.Log = Log

	var err error
	cfg.Metrics.Prefix = metricsNamespace()
	metrics, err = cfg.Metrics.NewProvider()
	if err != nil {
		Log.Fatal("unable to init metrics: ", err)
	}

	client = nyt.NewClient(cfg.MostPopularToken, cfg.SemanticToken)

	sub, err = aws.NewSubscriber(cfg.SQS)
	if err != nil {
		Log.Fatal("unable to init SQS: ", err)
	}
}
Beispiel #2
0
func Init() {
	flag.Parse()

	var cfg *Config
	config.LoadJSONFile("./config.json", &cfg)
	config.SetLogOverride(cfg.Log)

	if *cfg.Log != "" {
		lf, err := logrotate.NewFile(*cfg.Log)
		if err != nil {
			Log.Fatalf("unable to access log file: %s", err)
		}
		Log.Out = lf
		Log.Formatter = &logrus.JSONFormatter{}
	} else {
		Log.Out = os.Stderr
	}

	pubsub.Log = Log

	if cfg.GraphiteHost != nil {
		initMetrics(*cfg.GraphiteHost)
	}

	client = nyt.NewClient(cfg.MostPopularToken, cfg.SemanticToken)

	var err error
	sub, err = pubsub.NewSQSSubscriber(cfg.SQS)
	if err != nil {
		Log.Fatal("unable to init pb subs SQS: ", err)
	}
}
Beispiel #3
0
// Init will set up our name, logging, healthchecks and parse flags. If DefaultServer isn't set,
// this func will set it to a `SimpleServer` listening on `Config.HTTPPort`.
func Init(name string, scfg *Config) {
	// generate a unique ID for the server
	id, _ := uuid.NewV4()
	Name = name + "-" + Version + "-" + id.String()

	// if no config given, attempt to pull one from
	// the environment.
	if scfg == nil {
		// allow the default config to be overridden by CLI
		scfg = LoadConfigFromEnv()
		SetConfigOverrides(scfg)
	}

	if scfg.GOMAXPROCS != nil {
		runtime.GOMAXPROCS(*scfg.GOMAXPROCS)
	} else {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	if scfg.JSONContentType != nil {
		jsonContentType = *scfg.JSONContentType
	}

	if scfg.MaxHeaderBytes != nil {
		maxHeaderBytes = *scfg.MaxHeaderBytes
	}

	if scfg.ReadTimeout != nil {
		tReadTimeout, err := time.ParseDuration(*scfg.ReadTimeout)
		if err != nil {
			Log.Fatal("invalid server ReadTimeout: ", err)
		}
		readTimeout = tReadTimeout
	}

	if scfg.WriteTimeout != nil {
		tWriteTimeout, err := time.ParseDuration(*scfg.WriteTimeout)
		if err != nil {
			Log.Fatal("invalid server WriteTimeout: ", err)
		}
		writeTimeout = tWriteTimeout
	}

	// setup app logging
	if scfg.Log != "" {
		lf, err := logrotate.NewFile(scfg.Log)
		if err != nil {
			Log.Fatalf("unable to access log file: %s", err)
		}
		Log.Out = lf
		// json output when writing to file
		Log.Formatter = &logrus.JSONFormatter{}
	} else {
		Log.Out = os.Stderr
	}
	SetLogLevel(scfg)

	server = NewServer(scfg)
}
Beispiel #4
0
// RegisterAccessLogger will wrap a logrotate-aware Apache-style access log handler
// around the given handler if an access log location is provided by the config.
func RegisterAccessLogger(cfg *config.Server, handler http.Handler) http.Handler {
	if len(cfg.HTTPAccessLog) == 0 {
		return handler
	}

	lf, err := logrotate.NewFile(cfg.HTTPAccessLog)
	if err != nil {
		Log.Fatalf("unable to access http access log file: %s", err)
	}
	return handlers.CombinedLoggingHandler(lf, handler)
}
Beispiel #5
0
func registerRPCAccessLogger(cfg *config.Server) {
	if cfg.RPCAccessLog == "" {
		return
	}

	lf, err := logrotate.NewFile(cfg.RPCAccessLog)
	if err != nil {
		Log.Fatalf("unable to access rpc access log file: %s", err)
	}

	rpcAccessLog = logrus.New()
	rpcAccessLog.Out = lf
}
Beispiel #6
0
func registerRPCAccessLogger(cfg *Config) {
	// gRPC doesn't have a hook à la http.Handler-middleware
	// so some of this duplicates logic from config.NewAccessLogMiddleware
	if cfg.RPCAccessLog == nil {
		return
	}

	lf, err := logrotate.NewFile(*cfg.RPCAccessLog)
	if err != nil {
		Log.Fatalf("unable to access rpc access log file: %s", err)
	}

	rpcAccessLog = logrus.New()
	rpcAccessLog.Out = lf
}
Beispiel #7
0
// NewAccessLogMiddleware will wrap a logrotate-aware Apache-style access log handler
// around the given http.Handler if an access log location is provided by the config,
// or optionally send access logs to stdout.
func NewAccessLogMiddleware(logLocation *string, handler http.Handler) (http.Handler, error) {
	if logLocation == nil {
		return handler, nil
	}
	var lw io.Writer
	var err error
	switch *logLocation {
	case "stdout":
		lw = os.Stdout
	default:
		lw, err = logrotate.NewFile(*logLocation)
		if err != nil {
			return nil, err
		}
	}
	return handlers.CombinedLoggingHandler(lw, handler), nil
}
Beispiel #8
0
// Init will set up our name, logging, healthchecks and parse flags. If DefaultServer isn't set,
// this func will set it to a `SimpleServer` listening on `Config.Server.HTTPPort`.
func Init(name string, scfg *config.Server) {
	// generate a unique ID for the server
	id, _ := uuid.NewV4()
	Name = name + "-" + Version + "-" + id.String()

	// if no config given, attempt to pull one from
	// the environment.
	if scfg == nil {
		// allow the default config to be overridden by CLI
		flag.Parse()
		cfg := config.NewConfig(*config.ConfigLocationCLI)
		config.SetServerOverrides(cfg.Server)
		scfg = cfg.Server
	}

	if scfg.GOMAXPROCS != nil {
		runtime.GOMAXPROCS(*scfg.GOMAXPROCS)
	} else {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	if scfg.JSONContentType != nil {
		jsonContentType = *scfg.JSONContentType
	}

	if scfg.MaxHeaderBytes != nil {
		maxHeaderBytes = *scfg.MaxHeaderBytes
	}

	// setup app logging
	if scfg.Log != "" {
		lf, err := logrotate.NewFile(scfg.Log)
		if err != nil {
			Log.Fatalf("unable to access log file: %s", err)
		}
		Log.Out = lf
		// json output when writing to file
		Log.Formatter = &logrus.JSONFormatter{}
	} else {
		Log.Out = os.Stderr
	}
	SetLogLevel(scfg)

	server = NewServer(scfg)
}