Пример #1
0
// StartService starts a server.Service after first creating the router (and its dependencies), the webserver.
func StartService() *service.Service {
	//TODO StartService could return an error in case it fails to start

	accessManager := CreateAccessManager()
	messageStore := CreateMessageStore()
	kvStore := CreateKVStore()

	var cl *cluster.Cluster
	var err error

	if *Config.Cluster.NodeID > 0 {
		exitIfInvalidClusterParams(*Config.Cluster.NodeID, *Config.Cluster.NodePort, *Config.Cluster.Remotes)
		logger.Info("Starting in cluster-mode")
		cl, err = cluster.New(&cluster.Config{
			ID:      *Config.Cluster.NodeID,
			Port:    *Config.Cluster.NodePort,
			Remotes: *Config.Cluster.Remotes,
		})
		if err != nil {
			logger.WithField("err", err).Fatal("Module could not be started (cluster)")
		}
	} else {
		logger.Info("Starting in standalone-mode")
	}

	r := router.New(accessManager, messageStore, kvStore, cl)
	websrv := webserver.New(*Config.HttpListen)

	srv := service.New(r, websrv).
		HealthEndpoint(*Config.HealthEndpoint).
		MetricsEndpoint(*Config.MetricsEndpoint)

	srv.RegisterModules(0, 6, kvStore, messageStore)
	srv.RegisterModules(4, 3, CreateModules(r)...)

	if err = srv.Start(); err != nil {
		logger.WithField("error", err.Error()).Error("errors occurred while starting service")
		if err = srv.Stop(); err != nil {
			logger.WithField("error", err.Error()).Error("errors occurred when stopping service after it failed to start")
		}
		return nil
	}

	return srv
}
Пример #2
0
func StartService() *server.Service {
	accessManager := auth.NewAllowAllAccessManager(true)
	messageStore := CreateMessageStore()
	kvStore := CreateKVStore()

	var c *cluster.Cluster
	var err error
	if *config.Cluster.NodeID > 0 {
		exitIfInvalidClusterParams(*config.Cluster.NodeID, *config.Cluster.NodePort, *config.Cluster.Remotes)
		logger.Info("Starting in cluster-mode")
		c, err = cluster.New(&cluster.Config{
			ID:      *config.Cluster.NodeID,
			Port:    *config.Cluster.NodePort,
			Remotes: *config.Cluster.Remotes,
		})
		if err != nil {
			logger.WithField("err", err).Fatal("Service could not be started (cluster)")
		}
	} else {
		logger.Info("Starting in standalone-mode")
	}

	router := server.NewRouter(accessManager, messageStore, kvStore, c)
	webserver := webserver.New(*config.HttpListen)

	service := server.NewService(router, webserver).
		HealthEndpoint(*config.HealthEndpoint).
		MetricsEndpoint(*config.Metrics.Endpoint)

	service.RegisterModules(CreateModules(router)...)

	if err = service.Start(); err != nil {
		if err = service.Stop(); err != nil {
			logger.WithField("err", err).Error("Error when stopping service after Start() failed")
		}
		logger.WithField("err", err).Fatal("Service could not be started")
	}

	return service
}