Beispiel #1
0
func getPort() string {
	if port == "" {
		config, err := config.NewConfig("ini", "../conf/app.conf")
		if err != nil {
			return "8080"
		}
		port = config.String("httpport")
		return port
	}
	return port
}
Beispiel #2
0
func TestXML(t *testing.T) {
	f, err := os.Create("testxml.conf")
	if err != nil {
		t.Fatal(err)
	}
	_, err = f.WriteString(xmlcontext)
	if err != nil {
		f.Close()
		t.Fatal(err)
	}
	f.Close()
	defer os.Remove("testxml.conf")
	xmlconf, err := config.NewConfig("xml", "testxml.conf")
	if err != nil {
		t.Fatal(err)
	}
	if xmlconf.String("appname") != "beeapi" {
		t.Fatal("appname not equal to beeapi")
	}
	if port, err := xmlconf.Int("httpport"); err != nil || port != 8080 {
		t.Error(port)
		t.Fatal(err)
	}
	if port, err := xmlconf.Int64("mysqlport"); err != nil || port != 3600 {
		t.Error(port)
		t.Fatal(err)
	}
	if pi, err := xmlconf.Float("PI"); err != nil || pi != 3.1415976 {
		t.Error(pi)
		t.Fatal(err)
	}
	if xmlconf.String("runmode") != "dev" {
		t.Fatal("runmode not equal to dev")
	}
	if v, err := xmlconf.Bool("autorender"); err != nil || v != false {
		t.Error(v)
		t.Fatal(err)
	}
	if v, err := xmlconf.Bool("copyrequestbody"); err != nil || v != true {
		t.Error(v)
		t.Fatal(err)
	}
	if err = xmlconf.Set("name", "astaxie"); err != nil {
		t.Fatal(err)
	}
	if xmlconf.String("name") != "astaxie" {
		t.Fatal("get name error")
	}
}
Beispiel #3
0
// ParseConfig parsed default config file.
// now only support ini, next will support json.
func ParseConfig() (err error) {
	AppConfig, err = config.NewConfig(AppConfigProvider, AppConfigPath)
	if err != nil {
		AppConfig = config.NewFakeConfig()
		return err
	} else {

		if v, err := GetConfig("string", "HttpAddr"); err == nil {
			HttpAddr = v.(string)
		}

		if v, err := GetConfig("int", "HttpPort"); err == nil {
			HttpPort = v.(int)
		}

		if v, err := GetConfig("bool", "EnableHttpListen"); err == nil {
			EnableHttpListen = v.(bool)
		}

		if maxmemory, err := GetConfig("int64", "MaxMemory"); err == nil {
			MaxMemory = maxmemory.(int64)
		}

		if appname, _ := GetConfig("string", "AppName"); appname != "" {
			AppName = appname.(string)
		}

		if runmode, _ := GetConfig("string", "RunMode"); runmode != "" {
			RunMode = runmode.(string)
		}

		if autorender, err := GetConfig("bool", "AutoRender"); err == nil {
			AutoRender = autorender.(bool)
		}

		if autorecover, err := GetConfig("bool", "RecoverPanic"); err == nil {
			RecoverPanic = autorecover.(bool)
		}

		if views, _ := GetConfig("string", "ViewsPath"); views != "" {
			ViewsPath = views.(string)
		}

		if sessionon, err := GetConfig("bool", "SessionOn"); err == nil {
			SessionOn = sessionon.(bool)
		}

		if sessProvider, _ := GetConfig("string", "SessionProvider"); sessProvider != "" {
			SessionProvider = sessProvider.(string)
		}

		if sessName, _ := GetConfig("string", "SessionName"); sessName != "" {
			SessionName = sessName.(string)
		}

		if sesssavepath, _ := GetConfig("string", "SessionSavePath"); sesssavepath != "" {
			SessionSavePath = sesssavepath.(string)
		}

		if sesshashfunc, _ := GetConfig("string", "SessionHashFunc"); sesshashfunc != "" {
			SessionHashFunc = sesshashfunc.(string)
		}

		if sesshashkey, _ := GetConfig("string", "SessionHashKey"); sesshashkey != "" {
			SessionHashKey = sesshashkey.(string)
		}

		if sessMaxLifeTime, err := GetConfig("int64", "SessionGCMaxLifetime"); err == nil && sessMaxLifeTime != 0 {
			SessionGCMaxLifetime = sessMaxLifeTime.(int64)
		}

		if sesscookielifetime, err := GetConfig("int", "SessionCookieLifeTime"); err == nil && sesscookielifetime != 0 {
			SessionCookieLifeTime = sesscookielifetime.(int)
		}

		if usefcgi, err := GetConfig("bool", "UseFcgi"); err == nil {
			UseFcgi = usefcgi.(bool)
		}

		if enablegzip, err := GetConfig("bool", "EnableGzip"); err == nil {
			EnableGzip = enablegzip.(bool)
		}

		if directoryindex, err := GetConfig("bool", "DirectoryIndex"); err == nil {
			DirectoryIndex = directoryindex.(bool)
		}

		if timeout, err := GetConfig("int64", "HttpServerTimeOut"); err == nil {
			HttpServerTimeOut = timeout.(int64)
		}

		if errorsshow, err := GetConfig("bool", "ErrorsShow"); err == nil {
			ErrorsShow = errorsshow.(bool)
		}

		if copyrequestbody, err := GetConfig("bool", "CopyRequestBody"); err == nil {
			CopyRequestBody = copyrequestbody.(bool)
		}

		if xsrfkey, _ := GetConfig("string", "XSRFKEY"); xsrfkey != "" {
			XSRFKEY = xsrfkey.(string)
		}

		if enablexsrf, err := GetConfig("bool", "EnableXSRF"); err == nil {
			EnableXSRF = enablexsrf.(bool)
		}

		if expire, err := GetConfig("int", "XSRFExpire"); err == nil {
			XSRFExpire = expire.(int)
		}

		if tplleft, _ := GetConfig("string", "TemplateLeft"); tplleft != "" {
			TemplateLeft = tplleft.(string)
		}

		if tplright, _ := GetConfig("string", "TemplateRight"); tplright != "" {
			TemplateRight = tplright.(string)
		}

		if httptls, err := GetConfig("bool", "EnableHttpTLS"); err == nil {
			EnableHttpTLS = httptls.(bool)
		}

		if httpsport, err := GetConfig("int", "HttpsPort"); err == nil {
			HttpsPort = httpsport.(int)
		}

		if certfile, _ := GetConfig("string", "HttpCertFile"); certfile != "" {
			HttpCertFile = certfile.(string)
		}

		if keyfile, _ := GetConfig("string", "HttpKeyFile"); keyfile != "" {
			HttpKeyFile = keyfile.(string)
		}

		if serverName, _ := GetConfig("string", "BeegoServerName"); serverName != "" {
			BeegoServerName = serverName.(string)
		}

		if flashname, _ := GetConfig("string", "FlashName"); flashname != "" {
			FlashName = flashname.(string)
		}

		if flashseperator, _ := GetConfig("string", "FlashSeperator"); flashseperator != "" {
			FlashSeperator = flashseperator.(string)
		}

		if sd, _ := GetConfig("string", "StaticDir"); sd != "" {
			for k := range StaticDir {
				delete(StaticDir, k)
			}
			sds := strings.Fields(sd.(string))
			for _, v := range sds {
				if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 {
					StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[1]
				} else {
					StaticDir["/"+strings.TrimRight(url2fsmap[0], "/")] = url2fsmap[0]
				}
			}
		}

		if sgz, _ := GetConfig("string", "StaticExtensionsToGzip"); sgz != "" {
			extensions := strings.Split(sgz.(string), ",")
			if len(extensions) > 0 {
				StaticExtensionsToGzip = []string{}
				for _, ext := range extensions {
					if len(ext) == 0 {
						continue
					}
					extWithDot := ext
					if extWithDot[:1] != "." {
						extWithDot = "." + extWithDot
					}
					StaticExtensionsToGzip = append(StaticExtensionsToGzip, extWithDot)
				}
			}
		}

		if enableadmin, err := GetConfig("bool", "EnableAdmin"); err == nil {
			EnableAdmin = enableadmin.(bool)
		}

		if adminhttpaddr, _ := GetConfig("string", "AdminHttpAddr"); adminhttpaddr != "" {
			AdminHttpAddr = adminhttpaddr.(string)
		}

		if adminhttpport, err := GetConfig("int", "AdminHttpPort"); err == nil {
			AdminHttpPort = adminhttpport.(int)
		}

		if enabledocs, err := GetConfig("bool", "EnableDocs"); err == nil {
			EnableDocs = enabledocs.(bool)
		}
	}
	return nil
}