英文:
How to Create and Render Basic Template in Golang?
问题
如何在Golang中创建和渲染基本模板?
- 创建登录表单
- 将其保存在UserLogin结构中(datastore.put和datastore.get)
type UserLogin struct{
UserName string
PassWord string
}
我根据Google的一些文档创建了以下示例:
但是这是使用Go-app中可用的默认用户创建的。
想要使用Go创建Open Id登录表单。
如何做到这一点?
package hello
import (
"appengine"
"appengine/datastore"
"html/template"
"net/http"
)
// AdminData结构
type AdminData struct {
UserName string
UserPassword string
}
func init() {
http.HandleFunc("/", root)
http.HandleFunc("/login", login)
}
func root(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
q := datastore.NewQuery("AdminData").Limit(10)
adminsdata := make([]AdminData, 0, 10)
if _, err := q.GetAll(c, &adminsdata); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := loginTemplate.Execute(w, adminsdata); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
var loginTemplate = template.Must(template.New("book").Parse(loginTemplateHTML))
const loginTemplateHTML = `
{{range .}}
{{with .UserName}}
{{.}} 用户名:
{{else}}
一个匿名人写道:
{{end}}
{{.UserPassword}} 密码:
{{end}}
`
func login(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
g := AdminData{
UserName: r.FormValue("userName"),
UserPassword: r.FormValue("userPassword"),
}
_, err := datastore.Put(c, datastore.NewIncompleteKey(c, "AdminData", nil), &g)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
英文:
How to Create and Render Basic Template in Golang?
- Create Login Form
- Save that in UserLogin struct (datastore.put and also datastore.get)
type UserLogin struct{
UserName string
PassWord string
}
I was Created following example from some document from google:
But this was created with the default user available in Go-app.
Want to create Open Id login form with Go.
How to do this?
package hello
import (
"appengine"
"appengine/datastore"
"html/template"
"net/http"
)
//AdminData Structure
type AdminData struct {
UserName string
UserPassword string
}
func init() {
http.HandleFunc("/", root)
http.HandleFunc("/login", login)
}
func root(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
q := datastore.NewQuery("AdminData").Limit(10)
adminsdata := make([]AdminData, 0, 10)
if _, err := q.GetAll(c, &adminsdata); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := loginTemplate.Execute(w, adminsdata); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
var loginTemplate = template.Must(template.New("book").Parse(loginTemplateHTML))
const loginTemplateHTML = `
<html>
<body>
{{range .}}
{{with .UserName}}
<p><b>{{.}}</b> user name:</p>
{{else}}
<p>An anonymous person wrote:</p>
{{end}}
<pre>{{.UserPassword}}</b> password:</p></pre>
{{end}}
<form action="/login" method="post">
<div>User Name : <input type="text" name="userName" value=""> </div>
<div>Password : <input type="password" name="userPassword" value=""> </div>
<div><input type="submit" value="Login"></div>
</form>
</body>
</html>
`
func login(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
g := AdminData{
UserName : r.FormValue("userName"),enter code here
UserPassword : r.FormValue("userPassword"),
}
_, err := datastore.Put(c, datastore.NewIncompleteKey(c, "AdminData", nil), &g)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}
答案1
得分: 2
在您的Google App Engine控制台中,更改基本设置中的身份验证,以使用OpenID提供程序。
然后添加以下代码,它会检查是否有App Engine用户已登录,如果没有,他将显示Google登录页面。
u := user.Current(c)
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
}
英文:
in you google appengine console change in basic settings the auth, to openid provider
then add the following code, it checks if an appengine user is logged in, if not he will show him the google login page.
u := user.Current(c)
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论