func (this *Server) Run(withRandomPort ...bool) { this.Martini.Use(cors.Allow(&cors.Options{ AllowAllOrigins: true, AllowMethods: []string{"PUT", "PATCH", "DELETE", "GET", "OPTIONS", "POST"}, AllowCredentials: true, })) this.Martini.Use(render.Renderer()) this.Martini.Group("/eureka", func(r martini.Router) { for _, request := range this.getRequestsEureka() { request.SetRoutes(r) } }) this.registerJobsRoutes() if len(withRandomPort) > 0 && withRandomPort[0] { listener, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { loggerServer.Severe("Error when getting a free random port: %v", err.Error()) } host := listener.Addr().String() listener.Close() this.Martini.RunOnAddr(host) } else if config.GetConfig().Host != "" { this.Martini.RunOnAddr(config.GetConfig().Host) } else { this.Martini.Run() } }
func (this *Server) registerJobsRoutes() { if config.GetConfig().Admin.UserName == "" { this.Martini.Group("/jobs", func(r martini.Router) { jobs_request.CreateJobsRoutes(r) }) } else { this.Martini.Group("/jobs", func(r martini.Router) { jobs_request.CreateJobsRoutes(r) }, auth.Basic(config.GetConfig().Admin.UserName, config.GetConfig().Admin.Password)) } }
func GetDb() (*gorm.DB, error) { if dbInstance != nil { return dbInstance, nil } var db gorm.DB db, err := gorm.Open(config.GetConfig().Db.Driver, config.GetConfig().Db.DataSource) if err != nil { return nil, err } dbInstance = &db autoMigrate() return dbInstance, nil }
func GetEurekaClient() *EurekaClient { if eurekaClient != nil { return eurekaClient } var client *EurekaClient var err error conf := config.GetConfig() if conf.Eureka.Config.CertFile == "" || conf.Eureka.Config.KeyFile == "" { client = &EurekaClient{eureka.NewClient(conf.Eureka.Machines), conf.Instance.Name} } else { eurekaOriginalClient, err := eureka.NewTLSClient( conf.Eureka.Machines, conf.Eureka.Config.CertFile, conf.Eureka.Config.KeyFile, conf.Eureka.Config.CaCertFiles, ) if err != nil { panic(err) } client = &EurekaClient{eurekaOriginalClient, conf.Instance.Name} } if err != nil { panic(err) } if conf.Eureka.Config.Consistency != "" { client.Config.Consistency = conf.Eureka.Config.Consistency } if conf.Eureka.Config.Timeout != 0 { client.Config.DialTimeout = time.Duration(conf.Eureka.Config.Timeout) * time.Second } eureka.SetLogger(logger.GetMinLog().GetLogger()) return client }