func Test_MemcacheCacher(t *testing.T) {
	Convey("Test memcache cache adapter", t, func() {
		opt := cache.Options{
			Adapter:       "memcache",
			AdapterConfig: "127.0.0.1:9090",
		}

		Convey("Basic operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))

			t.Get("/", new(CacheAction))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})

		Convey("Increase and decrease operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))
			t.Get("/", new(Cache2Action))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})
	})
}
func Test_PostgresCacher(t *testing.T) {
	Convey("Test postgres cache adapter", t, func() {

		Convey("Basic operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))

			t.Get("/", new(CacheAction))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})

		Convey("Increase and decrease operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))

			t.Get("/", new(Cache2Action))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})
	})
}
func Test_RedisCacher(t *testing.T) {
	Convey("Test redis cache adapter", t, func() {
		opt := cache.Options{
			Adapter:       "redis",
			AdapterConfig: "addr=:6379,prefix=cache:",
		}

		Convey("Basic operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))
			t.Get("/", new(CacheAction))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})

		Convey("Increase and decrease operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))
			t.Get("/", new(Cache2Action))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})
	})
}
func Test_LedisCacher(t *testing.T) {
	Convey("Test ledis cache adapter", t, func() {
		opt := cache.Options{
			Adapter:       "ledis",
			AdapterConfig: "data_dir=./tmp.db",
			Interval:      1,
		}

		Convey("Basic operations", func() {
			t := tango.New()
			t.Use(cache.New(opt))

			t.Get("/", new(CacheAction))

			resp := httptest.NewRecorder()
			req, err := http.NewRequest("GET", "/", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)

			t.Get("/id", new(Cache2Action))

			resp = httptest.NewRecorder()
			req, err = http.NewRequest("GET", "/id", nil)
			So(err, ShouldBeNil)
			t.ServeHTTP(resp, req)
		})
	})
}
Exemple #5
0
// LoadConfig loads configuration file.
func LoadConfig() *goconfig.ConfigFile {
	var err error

	if fh, _ := os.OpenFile(AppConfPath, os.O_RDONLY|os.O_CREATE, 0600); fh != nil {
		fh.Close()
	}

	// Load configuration, set app version and log level.
	Cfg, err = goconfig.LoadConfigFile(GlobalConfPath)

	if Cfg == nil {
		Cfg, err = goconfig.LoadConfigFile(AppConfPath)
		if err != nil {
			fmt.Println("Fail to load configuration file: " + err.Error())
			os.Exit(2)
		}

	} else {
		Cfg.AppendFiles(AppConfPath)
	}

	Cfg.BlockMode = false

	// set time zone of wetalk system
	TimeZone = Cfg.MustValue("app", "time_zone", "UTC")
	if _, err := time.LoadLocation(TimeZone); err == nil {
		os.Setenv("TZ", TimeZone)
	} else {
		fmt.Println("Wrong time_zone: " + TimeZone + " " + err.Error())
		os.Exit(2)
	}

	// Trim 4th part.
	AppVer = strings.Join(strings.Split(APP_VER, ".")[:3], ".")

	IsProMode = Cfg.MustValue("app", "run_mode") == "pro"

	// cache system
	Captcha = captcha.New(captcha.Options{
		Caches: cache.New(cache.Options{
			Adapter:  "memory",
			Interval: 360,
		}),
	})
	Captcha.FieldIdName = "CaptchaId"
	Captcha.FieldCaptchaName = "Captcha"

	// session settings
	SessionProvider = Cfg.MustValue("session", "session_provider", "file")
	SessionSavePath = Cfg.MustValue("session", "session_path", "sessions")
	SessionName = Cfg.MustValue("session", "session_name", "wetalk_sess")
	SessionCookieLifeTime = Cfg.MustInt("session", "session_life_time", 0)
	SessionGCMaxLifetime = Cfg.MustInt64("session", "session_gc_time", 86400)

	EnableXSRF = true
	// xsrf token expire time
	XSRFExpire = 86400 * 365

	DriverName = Cfg.MustValue("orm", "driver_name", "mysql")
	DataSource = Cfg.MustValue("orm", "data_source", "root:@/wetalk?charset=utf8")
	MaxIdle = Cfg.MustInt("orm", "max_idle_conn", 30)
	MaxOpen = Cfg.MustInt("orm", "max_open_conn", 50)
	DebugLog = Cfg.MustBool("orm", "debug_log", false)

	//set logger
	os.MkdirAll("./logs", os.ModePerm)
	f, err := os.Create("logs/wego.log")
	if err != nil {
		log.Panic("create log file failed:", err)
	}

	w := io.MultiWriter(f, os.Stdout)
	log.SetOutput(w)
	Log = log.Std

	if IsProMode {
		log.SetOutputLevel(log.Linfo)
	} else {
		log.SetOutputLevel(log.Ldebug)
	}

	reloadConfig()

	social.DefaultAppUrl = AppUrl

	// OAuth
	var clientId, secret string

	clientId = Cfg.MustValue("oauth", "github_client_id", "your_client_id")
	secret = Cfg.MustValue("oauth", "github_client_secret", "your_client_secret")
	GithubAuth = apps.NewGithub(clientId, secret)

	clientId = Cfg.MustValue("oauth", "google_client_id", "your_client_id")
	secret = Cfg.MustValue("oauth", "google_client_secret", "your_client_secret")
	GoogleAuth = apps.NewGoogle(clientId, secret)

	err = social.RegisterProvider(GithubAuth)
	if err != nil {
		log.Error(err)
	}
	err = social.RegisterProvider(GoogleAuth)
	if err != nil {
		log.Error(err)
	}

	settingLocales()
	settingCompress()

	configWatcher()

	//Qiniu
	ACCESS_KEY = QiniuAccessKey
	SECRET_KEY = QiniuSecurityKey

	return Cfg
}