Пример #1
0
func TestYaml(t *testing.T) {
	f, err := os.Create("testyaml.conf")
	if err != nil {
		t.Fatal(err)
	}
	_, err = f.WriteString(yamlcontext)
	if err != nil {
		f.Close()
		t.Fatal(err)
	}
	f.Close()
	defer os.Remove("testyaml.conf")
	yamlconf, err := config.NewConfig("yaml", "testyaml.conf")
	if err != nil {
		t.Fatal(err)
	}
	if yamlconf.String("appname") != "beeapi" {
		t.Fatal("appname not equal to beeapi")
	}
	if port, err := yamlconf.Int("httpport"); err != nil || port != 8080 {
		t.Error(port)
		t.Fatal(err)
	}
	if port, err := yamlconf.Int64("mysqlport"); err != nil || port != 3600 {
		t.Error(port)
		t.Fatal(err)
	}
	if pi, err := yamlconf.Float("PI"); err != nil || pi != 3.1415976 {
		t.Error(pi)
		t.Fatal(err)
	}
	if yamlconf.String("runmode") != "dev" {
		t.Fatal("runmode not equal to dev")
	}
	if v, err := yamlconf.Bool("autorender"); err != nil || v != false {
		t.Error(v)
		t.Fatal(err)
	}
	if v, err := yamlconf.Bool("copyrequestbody"); err != nil || v != true {
		t.Error(v)
		t.Fatal(err)
	}
	if err = yamlconf.Set("name", "henrylee2cn"); err != nil {
		t.Fatal(err)
	}
	if yamlconf.String("name") != "henrylee2cn" {
		t.Fatal("get name error")
	}
}
Пример #2
0
func readConfig() Config {
	iniconf, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		log.Panicln(err)
	}
	return Config{
		AppName:   iniconf.DefaultString("appname", "thinkgo"),
		RunMode:   iniconf.DefaultString("runmode", "debug"),
		HttpAddr:  iniconf.DefaultString("httpaddr", "0.0.0.0"),
		HttpPort:  iniconf.DefaultInt("httpport", 8080),
		TplSuffex: iniconf.DefaultString("tplsuffex", ".html"),
		TplLeft:   iniconf.DefaultString("tplleft", "{{{"),
		TplRight:  iniconf.DefaultString("tplright", "}}}"),
	}
}
Пример #3
0
func getConfig() Config {
	iniconf, err := config.NewConfig("ini", "conf/app.conf")
	if err != nil {
		fmt.Println("\n  请确保在项目目录下运行,且存在配置文件 conf/app.conf")
		os.Exit(1)
	}

	var logLevel log.Level
	switch strings.ToUpper(iniconf.String("loglevel")) {
	case "TRACE":
		logLevel = log.TRACE
	case "DEBUG":
		logLevel = log.DEBUG
	case "INFO":
		logLevel = log.INFO
	case "NOTICE":
		logLevel = log.NOTICE
	case "WARN":
		logLevel = log.WARN
	case "ERROR":
		logLevel = log.ERROR
	case "FATAL":
		logLevel = log.FATAL
	case "OFF":
		logLevel = log.OFF
	default:
		logLevel = log.DEBUG
	}
	defaultModule := iniconf.DefaultString("defmodule", "home")
	return Config{
		AppName:       iniconf.DefaultString("appname", "thinkgo"),
		Debug:         iniconf.DefaultBool("debug", true),
		LogLevel:      logLevel,
		HttpAddr:      iniconf.DefaultString("httpaddr", "0.0.0.0"),
		HttpPort:      iniconf.DefaultInt("httpport", 8080),
		TplSuffix:     iniconf.DefaultString("tplsuffex", ".html"),
		TplLeft:       iniconf.DefaultString("tplleft", "{{{"),
		TplRight:      iniconf.DefaultString("tplright", "}}}"),
		DefaultModule: SnakeString(strings.Trim(defaultModule, "/")),
	}
}