func FromFile(filename string) (*Config, error) { p, err := properties.LoadFile(filename, properties.UTF8) if err != nil { return nil, err } return FromProperties(p) }
func loadProperties(path string) (p *properties.Properties, err error) { if path == "" { return properties.NewProperties(), nil } if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") { return properties.LoadURL(path) } return properties.LoadFile(path, properties.UTF8) }
func Load(filename string) (*Config, error) { if filename == "" { return fromProperties(properties.NewProperties()) } p, err := properties.LoadFile(filename, properties.UTF8) if err != nil { return nil, err } return fromProperties(p) }
func main() { flag.Parse() log.Printf("loading configuration from [%s]", *propertiesFile) var err error if props, err = properties.LoadFile(*propertiesFile, properties.UTF8); err != nil { log.Fatalf("[soysos][error] Unable to read properties:%v\n", err) } addr := props.MustGet("http.server.host") + ":" + props.MustGet("http.server.port") basePath := "http://" + addr SwaggerPath = props.GetString("swagger.path", "") cmap := make(map[string]CatFact) cmap["Lion"] = CatFact{"", "Lion", "Lions have sharp teef :D"} cat := CatResource{cmap} root := RootResource{SwaggerPath} user := UserResource{} wsContainer := restful.NewContainer() wsContainer.Filter(GlobalLogging) wsContainer.Filter(enableCORS) restful.TraceLogger(log.New(os.Stdout, "[restful] ", log.LstdFlags|log.Lshortfile)) cat.Register(wsContainer) root.Register(wsContainer) user.Register(wsContainer) config := swagger.Config{ WebServices: wsContainer.RegisteredWebServices(), WebServicesUrl: basePath, ApiPath: "/apidocs.json", SwaggerPath: SwaggerPath, SwaggerFilePath: props.GetString("swagger.file.path", "")} swagger.RegisterSwaggerService(config, wsContainer) cors := restful.CrossOriginResourceSharing{ AllowedHeaders: []string{"Accept", "Authorization"}, AllowedMethods: []string{"GET"}, CookiesAllowed: false, Container: wsContainer} //wsContainer.Filter(wsContainer.OPTIONSFilter) wsContainer.Filter(cors.Filter) server := &http.Server{Addr: addr, Handler: wsContainer} log.Printf("start listening on %s", basePath) log.Fatal(server.ListenAndServe()) }
func init() { // get configuration flag.Parse() Hostname = *HostnameFlag fmt.Printf("PropertiesFile is %s\n", *PropertiesFile) var err error if Props, err = properties.LoadFile(*PropertiesFile, properties.UTF8); err != nil { fmt.Printf("[error] Unable to read properties:%v\n", err) } // set log configuration // Log as JSON instead of the default ASCII formatter. switch Props.GetString("logrus.formatter", "") { case "text": logrus.SetFormatter(&logrus.TextFormatter{}) case "json": logrus.SetFormatter(&logrus.JSONFormatter{}) default: logrus.SetFormatter(&logrus.TextFormatter{}) } // Use the Airbrake hook to report errors that have Error severity or above to // an exception tracker. You can create custom hooks, see the Hooks section. // log.AddHook(airbrake.NewHook("https://example.com", "xyz", "development")) // Output to stderr instead of stdout, could also be a file. // logrus.SetOutput(f) logFile := Props.GetString("logrus.file", "/var/log/cluster_mgmt.log") f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777) if err != nil { fmt.Printf("error opening file %v", err) f, err = os.OpenFile("cluster_mgmt.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777) if err != nil { fmt.Printf("still failed to open log file cluster_mgmt.log %v", err) } } logrus.SetOutput(f) // Only log the warning severity or above. level, err := logrus.ParseLevel(Props.GetString("logrus.level", "info")) if err != nil { fmt.Printf("parse log level err is %v\n", err) fmt.Printf("using default level is %v \n", logrus.InfoLevel) level = logrus.InfoLevel } logrus.SetLevel(level) }
func init() { // get configuration flag.Parse() fmt.Printf("propertiesFile is %s\n", *PROPERTIESFILE) var err error if PROPS, err = properties.LoadFile(*PROPERTIESFILE, properties.UTF8); err != nil { fmt.Printf("[error] Unable to read properties:%v\n", err) } // set log configuration // Log as JSON instead of the default ASCII formatter. switch PROPS.GetString("logrus.formatter", "") { case "text": logrus.SetFormatter(&logrus.TextFormatter{}) case "json": logrus.SetFormatter(&logrus.JSONFormatter{}) default: logrus.SetFormatter(&logrus.TextFormatter{}) } // Output to stderr instead of stdout, could also be a file. logFile := PROPS.GetString("logrus.file", "/var/log/linker_userMgmt.log") f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777) if err != nil { fmt.Println("error opening file %v", err) f, err = os.OpenFile("linker_userMgmt.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0777) if err != nil { fmt.Println("still failed to open log file linker_userMgmt.log %v", err) } } logrus.SetOutput(f) // Only log the warning severity or above. level, err := logrus.ParseLevel(PROPS.GetString("logrus.level", "info")) if err != nil { fmt.Printf("parse log level err is %v\n", err) fmt.Printf("using default level is %v \n", logrus.InfoLevel) level = logrus.InfoLevel } logrus.SetLevel(level) }
func Load(args, environ []string) (cfg *Config, err error) { var props *properties.Properties cmdline, path, version, err := parse(args) switch { case err != nil: return nil, err case version: return nil, nil case path != "": switch { case strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://"): props, err = properties.LoadURL(path) case path != "": props, err = properties.LoadFile(path, properties.UTF8) } if err != nil { return nil, err } } envprefix := []string{"FABIO_", ""} return load(cmdline, environ, envprefix, props) }
// 程序入口点 func main() { //通过二进制文件名,加载同名配置文件 filePath, _ := exec.LookPath(os.Args[0]) fileName := filepath.Base(filePath) idx := strings.LastIndex(fileName, ".") fileName = fileName[0:idx] flag.Set("config", fileName+".properties") flag.Parse() // Load configurations from a file info("loading configuration from [%s]", *propertiesFile) var err error if props, err = properties.LoadFile(*propertiesFile, properties.UTF8); err != nil { log.Fatalf("[mora][error] Unable to read properties:%v\n", err) } // Swagger configuration SwaggerPath = props.GetString("swagger.path", "") MoraIcon = filepath.Join(SwaggerPath, "images/mora.ico") // New, shared session manager sessMng := session.NewSessionManager(props.FilterPrefix("mongod.")) defer sessMng.CloseAll() // accept and respond in JSON unless told otherwise restful.DefaultRequestContentType(restful.MIME_JSON) restful.DefaultResponseContentType(restful.MIME_JSON) // gzip if accepted restful.DefaultContainer.EnableContentEncoding(true) // faster router restful.DefaultContainer.Router(restful.CurlyRouter{}) // no need to access body more than once restful.SetCacheReadEntity(false) // 获取配置文件中是否允许cors的变量值 apiCors := props.GetBool("http.server.cors", false) // 注册API(Documents API) documents.Register(sessMng, restful.DefaultContainer, apiCors) // 如果启用了统计类API(Statistics API) if ok := props.GetBool("mora.statistics.enable", false); ok { statistics.Register(sessMng, restful.DefaultContainer) } // 要监听的服务器IP:端口 addr := props.MustGet("http.server.host") + ":" + props.MustGet("http.server.port") basePath := "http://" + addr // Register Swagger UI swagger.InstallSwaggerService(swagger.Config{ WebServices: restful.RegisteredWebServices(), WebServicesUrl: basePath, ApiPath: "/apidocs.json", SwaggerPath: SwaggerPath, SwaggerFilePath: props.GetString("swagger.file.path", ""), }) // If swagger is not on `/` redirect to it if SwaggerPath != "/" { http.HandleFunc("/", index) } // Serve favicon.ico http.HandleFunc("/favion.ico", icon) info("ready to serve on %s", basePath) log.Fatal(http.ListenAndServe(addr, nil)) }