我不知道为什么第一个函数完美运行,第二个函数永远不会注销,我总是登录。定义的结构对应一个模板变量。
最奇怪的是,在两个不同的处理程序中,对LogoutURL的调用会生成两个不同的链接(不仅是返回URL,这两个的结构是不同的[索引页面的一个有效]以myapp-notworking-000开头。 appspot.com然后_ah / logout URL和管理页面以https://google.com/accounts和所有)开头。我在这里错过了什么? GAE为何如此复杂?
import (
"appengine"
"appengine/user"
"fmt"
"html/template"
"net/http"
)
var templates = template.Must(template.ParseGlob("static/tmpl/*"))
type AdminPage struct {
Admin_email string
LogoutUrl string
}
func init() {
http.HandleFunc("/", Index)
http.HandleFunc("/admin", Admin)
}
func Index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "text/html; charset=utf-8")
c := appengine.NewContext(r)
u := user.Current(c)
if u == nil {
url, _ := user.LoginURL(c, "/")
fmt.Fprintf(w, `<a href="%s">Sign in or register</a>`, url)
return
}
url, _ := user.LogoutURL(c, "/")
fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, u, url)
}
func Admin(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
u := user.Current(c)
an := AdminPage{"", ""}
var templ = ""
if u == nil {
url, err := user.LoginURL(c, r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusFound)
return
}
c.Debugf("User present %v", u)
an.Admin_email = u.Email
templ = "adminIndex"
url, err := user.LoginURL(c, "/admin")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
an.LogoutUrl = url
err = templates.ExecuteTemplate(w, templ, an)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
app.yaml文件-----
application: myapp-notworking-000
version: beta
runtime: go
api_version: go1
handlers:
- url: /static
static_dir: static
application_readable: true
- url: /css
static_dir: static/css
application_readable: true
- url: /js
static_dir: static/js
application_readable: true
- url: /images
static_dir: static/images
application_readable: true
- url: /fonts
static_dir: static/fonts
application_readable: true
- url: /.*
script: _go_app