// StartServer is a simple helper function to register any handlers // (such as the API) and start the HTTP server on the configured // address (Conf.Web.Addr). // // If Conf.Web.Prefix or Conf.Web.DeproxyHeaderFields has a length // greater than zero, it wraps its http.ServeMux with a Deproxier. // // On crash, it returns the error. func StartServer() (err error) { // Register any handlers. RegisterAPI(Conf.Web.Prefix) l.Debug("Registered API handler\n") err = RegisterTemplates() if err != nil { return } // Parse the address and create an appropriate net.Listener. The // Web.Addr will be of the form "protocol://address:port". parts := strings.Split(Conf.Web.Addr, "://") if len(parts) != 2 { return InvalidBindAddress } listener, err = net.Listen(parts[0], parts[1]) if err != nil { return } // Change permissions for unix socket to be 777, so that web servers can // write to it if parts[0] == "unix" { l.Infof("Changing permissions for %q to 777\n", parts[1]) err = os.Chmod(parts[1], 0777) if err != nil { return } } // Create a custom http.Server, so that we can have better control // over certain behaviors. s := &http.Server{} // If either the Prefix or DeproxyHeaderFields are set, then we // need to wrap the default Handler with a Deproxier. Otherwise, // we just use our Handler. if len(Conf.Web.Prefix) > 0 || len(Conf.Web.DeproxyHeaderFields) > 0 { s.Handler = &Deproxier{http.DefaultServeMux} } else { s.Handler = &Handler{http.DefaultServeMux} } // We need to set the database tile store. captcha.SetCustomStore(CAPTCHAStore{}) http.HandleFunc("/", HandleStatic) http.HandleFunc("/node/", HandleMap) http.HandleFunc("/verify/", HandleMap) http.Handle("/captcha/", captchaServer) // Start the HTTP server and return any errors if it crashes. l.Infof("Starting HTTP server on %q\n", Conf.Web.Addr) return s.Serve(listener) }
func init() { // 设置自定义存储验证码,替换默认的内存存储.必须调用此函数之前生成验证码. captcha.SetCustomStore(NewCaptchaStore(models.RedisAddress, 0, 0, -1)) initSession() err := initCache() if err != nil { beego.Error("init cache error :", err) } err = initLanguage() if err != nil { beego.Error("init language error :", err) } }
func initRedisCaptchaStore() { c := createRedisConn(":6379", 4) rs := store.NewBytesStore(c) captcha.SetCustomStore(rs) }