本文整理匯總了Golang中common.Context類的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Context類的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: allCSS
func allCSS(c common.Context) {
c.SetContentType("text/css; charset=UTF-8", true)
renderText(c, cssTemplates, "bootstrap.min.css")
renderText(c, cssTemplates, "bootstrap-theme.min.css")
renderText(c, cssTemplates, "bootstrap-multiselect.css")
renderText(c, cssTemplates, "common.css")
}
示例2: allJS
func allJS(c common.Context) {
c.SetContentType("application/javascript; charset=UTF-8", true)
renderText(c, jsTemplates, "jquery-2.0.3.min.js")
renderText(c, jsTemplates, "underscore-min.js")
renderText(c, jsTemplates, "backbone-min.js")
renderText(c, jsTemplates, "bootstrap.min.js")
renderText(c, jsTemplates, "bootstrap-multiselect.js")
renderText(c, jsTemplates, "viz.js")
renderText(c, jsTemplates, "tinycolor.js")
render_Templates(c)
for _, templ := range jsModelTemplates.Templates() {
if err := templ.Execute(c.Resp, c); err != nil {
panic(err)
}
}
for _, templ := range jsCollectionTemplates.Templates() {
if err := templ.Execute(c.Resp, c); err != nil {
panic(err)
}
}
for _, templ := range jsViewTemplates.Templates() {
if err := templ.Execute(c.Resp, c); err != nil {
panic(err)
}
}
renderText(c, jsTemplates, "app.js")
}
示例3: deleteAI
func deleteAI(c common.Context) {
if c.Authenticated() {
if ai := models.GetAIById(c, common.MustDecodeKey(c.Vars["ai_id"])); ai != nil && ai.Owner == c.User.Email {
ai.Delete(c)
}
}
}
示例4: getAIErrors
func getAIErrors(c common.Context) {
if c.Authenticated() {
if ai := models.GetAIById(c, common.MustDecodeKey(c.Vars["ai_id"])); ai != nil && ai.Owner == c.User.Email {
c.RenderJSON(ai.GetErrors(c))
}
}
}
示例5: createGame
func createGame(c common.Context) {
if c.Authenticated() {
var game models.Game
aiCommon.MustDecodeJSON(c.Req.Body, &game)
if len(game.Players) > 0 {
c.RenderJSON(game.Save(c))
}
}
}
示例6: createAI
func createAI(c common.Context) {
if c.Authenticated() {
var ai models.AI
aiCommon.MustDecodeJSON(c.Req.Body, &ai)
if ai.Name != "" && ai.URL != "" {
ai.Owner = c.User.Email
ai.Id = nil
c.RenderJSON(ai.Save(c))
}
}
}
示例7: handler
func handler(f func(c common.Context)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c := common.Context{
Context: appengine.NewContext(r),
Req: r,
Resp: w,
Vars: mux.Vars(r),
}
c.User = user.Current(c)
c.Version = appengine.VersionID(c.Context)
f(c)
}
}
示例8: AddError
func (self *AI) AddError(c common.Context, turnId *datastore.Key, err error) {
_, e := datastore.Put(c, datastore.NewKey(c, AIErrorKind, "", 0, self.Id), &AIError{
CreatedAt: time.Now(),
Turn: turnId,
ErrorBytes: []byte(err.Error()),
ErrorDetail1Bytes: []byte(fmt.Sprintf("%+v", err)),
ErrorDetail2Bytes: []byte(fmt.Sprintf("%#v", err)),
})
if e != nil {
c.Errorf("Got %+v when trying to save a new error!", e)
}
common.MemDel(c, aiErrorsKeyByParent(self.Id))
}
示例9: nextTurn
func nextTurn(cont appengine.Context, id *datastore.Key, playerNames []string) {
con := common.Context{Context: cont}
self := getGameById(con, id)
self.PlayerNames = playerNames
if self.Length > maxGameDuration {
self.State = StateFinished
self.Save(con)
con.Infof("Ended %v due to timeout", self.Id)
return
}
errorSavers := []func(){}
if err := common.Transaction(con, func(c common.Context) (err error) {
lastTurn := GetLatestTurnByParent(c, self.Id)
responses := make(chan orderResponse, len(self.Players))
for _, playerId := range self.Players {
orderResp := orderResponse{
DatastorePlayerId: playerId,
StatePlayerId: state.PlayerId(playerId.Encode()),
}
if foundAi := GetAIById(c, playerId); foundAi != nil {
go func() {
// Always deliver the order response
defer func() {
responses <- orderResp
}()
// create a request
orderRequest := ai.OrderRequest{
Me: orderResp.StatePlayerId,
State: lastTurn.State,
GameId: state.GameId(self.Id.Encode()),
}
// encode it into a body, and remember its string representation
sendBody := &bytes.Buffer{}
aiCommon.MustEncodeJSON(sendBody, orderRequest)
sendBodyString := sendBody.String()
// get a client
client := urlfetch.Client(c)
// send the request to the ai
req, err := http.NewRequest("POST", foundAi.URL, sendBody)
var resp *http.Response
if err == nil {
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
resp, err = client.Do(req)
}
recvBody := &bytes.Buffer{}
recvBodyString := ""
if err == nil {
// check what we received
_, err = io.Copy(recvBody, resp.Body)
recvBodyString = recvBody.String()
}
// if we have no other errors, but we got a non-200
if err == nil && resp.StatusCode != 200 {
err = orderError{
Request: req,
RequestBody: sendBodyString,
Response: resp,
ResponseBody: recvBodyString,
}
}
// lets try to unserialize
if err == nil {
err = json.Unmarshal(recvBody.Bytes(), &orderResp.Orders)
}
// store the error, if any
if err != nil {
orderResp.Error = err
}
}()
} else {
responses <- orderResp
}
}
orderMap := map[state.PlayerId]state.Orders{}
for _, _ = range self.Players {
// wait for the responses
orderResp := <-responses
// store it
orderMap[orderResp.StatePlayerId] = orderResp.Orders
// if we got an error
if orderResp.Error != nil {
// make sure to save it later
errorSavers = append(errorSavers, func() {
if ai := GetAIById(con, orderResp.DatastorePlayerId); ai != nil {
ai.AddError(con, lastTurn.Id, orderResp.Error)
}
})
}
}
// execute the orders
newTurn, winner := lastTurn.Next(c, orderMap)
// save the new turn
newTurn.Save(c, self.Id)
//.........這裏部分代碼省略.........
示例10: getGame
func getGame(c common.Context) {
c.RenderJSON(models.GetGameById(c, common.MustDecodeKey(c.Vars["game_id"])))
}
示例11: getGames
func getGames(c common.Context) {
limit := aiCommon.TryParseInt(c.Req.URL.Query().Get("limit"), 10)
offset := aiCommon.TryParseInt(c.Req.URL.Query().Get("offset"), 0)
c.RenderJSON(models.GetGamePage(c, offset, limit))
}
示例12: getAIs
func getAIs(c common.Context) {
c.RenderJSON(models.GetAllAIs(c))
}
示例13: getUser
func getUser(c common.Context) {
c.RenderJSON(c.User)
}
示例14: index
func index(c common.Context) {
c.SetContentType("text/html; charset=UTF-8", false)
renderText(c, htmlTemplates, "index.html")
}