Esempio n. 1
0
func loadFileConf() {
	var confFile string
	// flag.StringVar(&confFile, "golink-conf", "", "golink的配置文件路径,json格式")
	// flag.Parse()

	// cmd := os.Args[1]
	flags := flag.NewFlagSet("golink-conf", flag.ContinueOnError)
	flags.StringVar(&confFile, "conf", "", "golink的配置文件路径,json格式")
	flags.Parse(os.Args[1:])

	if confFile != "" {
		conf, err := utils.LoadJsonFile(confFile)
		if err != nil {
			log.Fatalln("load conf file", confFile, "error:", err.Error())
		}
		if fc, ok := conf["DataBase"]; ok {
			dbc, ok := fc.(map[string]interface{})
			if !ok {
				log.Fatalln("conf file error: wrong DataBase Session format.")
			}
			DATABASE_Driver = dbc["DATABASE_Driver"].(string)
			DATABASE_DSN = dbc["DATABASE_DSN"].(string)
			REDIS_HOST = dbc["REDIS_HOST"].(string)
			REDIS_AUTH = dbc["REDIS_AUTH"].(string)
		}
	}
}
Esempio n. 2
0
// load the server conf.
// config file is json format.
// like this:
// {
//     "ServerConfig": {
//         "Addr": ":8888",
//         "ReadTimeout": "20ms",
//         "WriteTimeout": "20ms",
//         "MaxHeaderBytes": 110,
//         "StaticPath": "mystatic",
//         "ViewPath": "myview",
//         "Layout": "mylayout",
//         "LogLevel": 3,
//         "Debug": true
//     }
// }
func loadCmdLineConfFile(sc *ServerConfig, rt *RouteTable) {
	var confFile string
	// flag.StringVar(&confFile, "conf", "", "Specified the json format config file path")
	// flag.Parse()
	flags := flag.NewFlagSet("goku-conf", flag.ContinueOnError)
	flags.StringVar(&confFile, "conf", "", "Specified the json format config file path for goku")
	flags.Parse(os.Args[1:])
	if confFile == "" {
		return
	}

	conf, err := utils.LoadJsonFile(confFile)
	if err != nil {
		log.Fatalln("conf file ", confFile, "has error:", err)
	}

	if fsc, ok := conf["ServerConfig"]; ok {
		msc, ok := fsc.(map[string]interface{})
		if !ok {
			log.Fatalln("conf file error: wrong ServerConfig format.")
		}
		if v, ok := msc["Addr"]; ok {
			sc.Addr = v.(string)
		}
		// such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"
		if v, ok := msc["ReadTimeout"]; ok {
			sc.ReadTimeout, err = time.ParseDuration(v.(string))
			if err != nil {
				log.Fatalln("conf file error: wrong ReadTimeout format.")
			}
		}
		if v, ok := msc["WriteTimeout"]; ok {
			sc.WriteTimeout, err = time.ParseDuration(v.(string))
			if err != nil {
				log.Fatalln("conf file error: wrong WriteTimeout format.")
			}
		}
		if v, ok := msc["MaxHeaderBytes"]; ok {
			sc.MaxHeaderBytes = int(v.(float64))
		}
		if v, ok := msc["StaticPath"]; ok {
			sc.StaticPath = v.(string)
		}
		if v, ok := msc["ViewPath"]; ok {
			sc.ViewPath = v.(string)
		}
		if v, ok := msc["Layout"]; ok {
			sc.Layout = v.(string)
		}
		if v, ok := msc["LogLevel"]; ok {
			sc.LogLevel = int(v.(float64))
		}
		if v, ok := msc["Debug"]; ok {
			sc.Debug = v.(bool)
		}
	}
	if mroutes, ok := conf["Routes"]; ok {
		routes, ok := mroutes.(map[string]interface{})
		if !ok {
			log.Fatalln("conf file error: wrong Routes format.")
		}
		for _, froute := range routes {
			sroute, err := json.Marshal(froute)
			if err != nil {
				log.Fatalln("conf file error", err)
			}
			var route Route
			err = json.Unmarshal(sroute, &route)
			if err != nil {
				log.Fatalln("conf file error", err)
			}
			rt.AddRoute(&route)
		}
	}
}