1 Star 1 Fork 0

湖底观景 / GolangTraining

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
handlers.go 2.35 KB
一键复制 编辑 原始数据 按行查看 历史
GoesToEleven 提交于 2016-04-20 17:45 . changes dir structure
package chat
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"io"
"net/http"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/channel"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/user"
)
// API is the API
type API struct {
root string
}
func newAPI(root string) *API {
return &API{
root: root,
}
}
func (api *API) ServeHTTP(res http.ResponseWriter, req *http.Request) {
endpoint := req.URL.Path[len(api.root):]
method := req.Method
var err error
switch endpoint {
case "channels":
switch method {
case "POST":
err = api.handlePostChannel(res, req)
default:
http.Error(res, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
case "messages":
switch method {
case "POST":
err = api.handlePostMessage(res, req)
default:
http.Error(res, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
default:
http.NotFound(res, req)
return
}
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(res).Encode(err.Error())
}
}
func getClientID(email string) string {
hash := md5.New()
io.WriteString(hash, email)
sum := hash.Sum(nil)
return hex.EncodeToString(sum)
}
func saveNewConnection(ctx context.Context, clientID string) error {
key := datastore.NewKey(ctx, "connection", clientID, 0, nil)
val := struct{ Value string }{clientID}
_, err := datastore.Put(ctx, key, &val)
return err
}
func (api *API) handlePostChannel(res http.ResponseWriter, req *http.Request) error {
ctx := appengine.NewContext(req)
u := user.Current(ctx)
clientID := getClientID(u.Email)
token, err := channel.Create(ctx, clientID)
if err != nil {
return err
}
err = saveNewConnection(ctx, clientID)
if err != nil {
return err
}
return json.NewEncoder(res).Encode(token)
}
func (api *API) handlePostMessage(res http.ResponseWriter, req *http.Request) error {
ctx := appengine.NewContext(req)
var message struct{ Text string }
err := json.NewDecoder(req.Body).Decode(&message)
if err != nil {
return err
}
query := datastore.NewQuery("connection")
it := query.Run(ctx)
for {
var conn struct{ Value string }
_, err := it.Next(&conn)
if err == datastore.Done {
break
} else if err != nil {
return err
}
err = channel.SendJSON(ctx, conn.Value, message)
if err != nil {
return err
}
}
return nil
}
1
https://gitee.com/zhangjianGood/GolangTraining.git
git@gitee.com:zhangjianGood/GolangTraining.git
zhangjianGood
GolangTraining
GolangTraining
afa19f5c43f3

搜索帮助

53164aa7 5694891 3bd8fe86 5694891