diff --git a/web/controllers/login.go b/web/controllers/login.go index 0fc1d1f..86805ee 100755 --- a/web/controllers/login.go +++ b/web/controllers/login.go @@ -5,6 +5,7 @@ import ( "github.com/astaxie/beego/utils/captcha" "math/rand" "net" + "strings" "sync" "time" @@ -27,10 +28,22 @@ type record struct { lastLoginTime time.Time } -func init() { +// InitCaptcha registers the captcha image route and the create_captcha template +// function. The image URL prefix must include web_base_url, otherwise the login +// page would request the captcha from the site root and fail behind a reverse +// proxy that only forwards the web_base_url sub path. +// +// It must be called after beego.LoadAppConfig, so it cannot live in init(): +// package init() runs before main() loads conf/nps.conf, where web_base_url +// would still be empty. +func InitCaptcha() { // use beego cache system store the captcha data store := cache.NewMemoryCache() - cpt = captcha.NewWithFilter("/captcha/", store) + base := strings.TrimSuffix(beego.AppConfig.String("web_base_url"), "/") + if base != "" && !strings.HasPrefix(base, "/") { + base = "/" + base + } + cpt = captcha.NewWithFilter(base+"/captcha/", store) } func (self *LoginController) Index() { diff --git a/web/routers/router.go b/web/routers/router.go index e4add32..961a552 100755 --- a/web/routers/router.go +++ b/web/routers/router.go @@ -6,6 +6,10 @@ import ( ) func Init() { + // The captcha image path and its filter are built from web_base_url, so it + // has to be registered here (after the config is loaded) instead of in an + // init() function. + controllers.InitCaptcha() web_base_url := beego.AppConfig.String("web_base_url") if len(web_base_url) > 0 { ns := beego.NewNamespace(web_base_url,