代码拉取完成,页面将自动刷新
package main
import (
"encoding/json"
"github.com/dustin/go-humanize"
"github.com/julienschmidt/httprouter"
"html/template"
"net/http"
)
var tpl *template.Template
func init() {
r := httprouter.New()
http.Handle("/", r)
r.GET("/", Home)
r.GET("/form/login", Login)
r.GET("/form/signup", Signup)
r.POST("/api/checkusername", checkUserName)
r.POST("/api/createuser", createUser)
r.POST("/api/login", loginProcess)
r.POST("/api/tweet", tweetProcess)
r.GET("/api/logout", logout)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.Handle("/public/", http.StripPrefix("/public", http.FileServer(http.Dir("public/"))))
tpl = template.New("roottemplate")
tpl = tpl.Funcs(template.FuncMap{
"humanize_time": humanize.Time,
})
tpl = template.Must(tpl.ParseGlob("templates/html/*.html"))
}
func Home(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
//get tweets
tweets, err := getTweets(req, nil)
if err != nil {
// lala
}
memItem, err := getSession(req)
var sd SessionData
if err == nil {
// logged in
json.Unmarshal(memItem.Value, &sd)
sd.LoggedIn = true
}
sd.Tweets = tweets
tpl.ExecuteTemplate(res, "home.html", &sd)
}
func Login(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
serveTemplate(res, req, "login.html")
}
func Signup(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
serveTemplate(res, req, "signup.html")
}
/*
TO DO:
session
-memcache templates
- uuid in a cookie
--- https while logged in? - depends upon security required
- encrypt password on datastore?
--- never store an unencrypted password, so, resoundingly, YES
--- sha-256 fast hash value
- user memcache?
- datastore / memcache
session interface change
- change login button to logout when user logged in
post tweets
follow people
see tweets for everyone
see tweets for individual user
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。