示例#1
0
// Redirect back to a return URL, if specified; otherwise, redirect to
// defaultURL.
func ReturnRedirect(req *http.Request, statusCode int, defaultURL string) {
	ak := opts.VariantSecretKey("redirect")
	r := req.FormValue("r")

	if r != "" && webac.VerifyFor("redirect/"+r, req.FormValue("rac"), ak) {
		if _, err := url.Parse(r); err == nil {
			defaultURL = r
		}
	}

	miscctx.RedirectTo(req, statusCode, defaultURL)
}
示例#2
0
// Redirect to a given URL with the given status code, such that the user agent
// can eventually be redirected back to the current URL, unless a return URL
// has already been provided in the current request, in which case that return
// URL is used.
func RedirectWithReturn(req *http.Request, statusCode int, targetURL string) {
	ak := opts.VariantSecretKey("redirect")
	ustr := req.URL.String()

	r, rac := req.FormValue("r"), req.FormValue("rac")
	if r == "" || !webac.VerifyFor("redirect/"+r, rac, ak) {
		r = ustr
		rac = webac.NewFor("redirect/"+r, ak)
	}

	tgt, err := req.URL.Parse(targetURL)
	if err == nil {
		q := tgt.Query()
		q.Set("r", r)
		q.Set("rac", rac)
		tgt.RawQuery = q.Encode()
		targetURL = tgt.String()
	}

	miscctx.RedirectTo(req, statusCode, targetURL)
}
示例#3
0
func (cfg *Config) init() error {
	if cfg.inited {
		return nil
	}

	var err error
	if redisAddressFlag.Value() != "" {
		cfg.rpool.Dial = func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", redisAddressFlag.Value())
			if err != nil {
				return nil, err
			}

			if redisPasswordFlag.Value() != "" {
				if _, err := c.Do("AUTH", redisPasswordFlag.Value()); err != nil {
					c.Close()
					return nil, err
				}
			}

			return c, nil
		}
		cfg.rpool.MaxIdle = 2
	}

	cfg.statusChan = make(chan string, 8)
	cfg.criterion = health.NewCriterion("web.ok", false)
	if cfg.HTTPServer.Server == nil {
		cfg.HTTPServer.Server = &http.Server{}
	}
	if cfg.HTTPServer.Addr == "" {
		cfg.HTTPServer.Addr = bindFlag.Value()
	}
	if cfg.SessionConfig == nil {
		cfg.SessionConfig = &session.Config{}
	}
	if cfg.SessionConfig.CookieName == "" {
		cfg.SessionConfig.CookieName = "s"
	}
	if len(cfg.SessionConfig.SecretKey) == 0 {
		cfg.SessionConfig.SecretKey = opts.VariantSecretKey("cookie-secret-key")
	}
	if cfg.SessionConfig.Store == nil {
		var redisStore storage.Store
		if redisAddressFlag.Value() != "" {
			if redisPrefixFlag.Value() == "" {
				return fmt.Errorf("must specify a redis prefix")
			}
			redisStore, err = redissession.New(redissession.Config{
				Prefix: redisPrefixFlag.Value() + "s/",
				GetConn: func() (redis.Conn, error) {
					c := cfg.rpool.Get()
					if c == nil {
						return nil, fmt.Errorf("cannot get redis")
					}

					return c, nil
				},
			})
			if err != nil {
				return err
			}
		}

		cfg.SessionConfig.Store, err = memorysession.New(memorysession.Config{
			FallbackStore: redisStore,
		})
		if err != nil {
			return err
		}
	}

	draw2d.SetFontFolder(filepath.Join(opts.BaseDir, "assets/fonts"))

	err = tpl.LoadTemplates(filepath.Join(opts.BaseDir, "tpl"))
	if err != nil {
		return err
	}

	bstaticName := "bstatic"
	if !opts.DevMode {
		bstaticName = "bstatic.rel"
	}

	assetmgr.Default, err = assetmgr.New(assetmgr.Config{
		Path: filepath.Join(opts.BaseDir, bstaticName),
	})
	if err != nil {
		return err
	}

	Router.HandleFunc("/{page}", Front_GET).Methods("GET")
	Router.HandleFunc("/", Front_GET).Methods("GET")

	cfg.inited = true
	return nil
}