本文整理匯總了Golang中github.com/hraban/web.Context類的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Context類的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: root
func root(ctx *web.Context) string {
msg := form
if said, ok := ctx.GetSecureCookie("said"); ok {
msg = "You said " + said + "<p>" + msg
}
return msg
}
示例2: handleGetEnviron
func handleGetEnviron(ctx *web.Context) (map[string]string, error) {
if err := errorIfNotMaster(ctx); err != nil {
return nil, err
}
ctx.ContentType("json")
s := ctx.User.(*server)
return s.session.Environ(), nil
}
示例3: redirect
func redirect(ctx *web.Context, loc *url.URL) {
if _, ok := ctx.Params["noredirect"]; ok {
return
}
loc = ctx.Request.URL.ResolveReference(loc)
ctx.Header().Set("Location", loc.String())
ctx.WriteHeader(303)
fmt.Fprintf(ctx, "redirecting to %s", loc)
}
示例4: handleGetFiles
// List of files nice for tab completion
func handleGetFiles(ctx *web.Context) error {
if err := errorIfNotMaster(ctx); err != nil {
return err
}
ctx.ContentType("json")
paths, err := filepath.Glob(ctx.Params["pattern"])
if err != nil {
return err
}
if paths == nil {
paths = []string{}
}
return json.NewEncoder(ctx).Encode(paths)
}
示例5: handleGetCmdJson
func handleGetCmdJson(ctx *web.Context, idstr string) error {
id, _ := liblush.ParseCmdId(idstr)
s := ctx.User.(*server)
c := s.session.GetCommand(id)
if c == nil {
return web.WebError{404, "no such command: " + idstr}
}
md, err := metacmd{c}.Metadata()
if err != nil {
return err
}
ctx.ContentType("json")
// Don't hammer me, but don't cache for too long. This resource is not
// intended for polling, anyway. This may seem race sensitive, but that's
// because it is. Only matters in big, multi user setups with lots of
// concurrent changes, which is totally not lush's current intended use
// case. So a few race conditions here and there are no biggy (for now).
ctx.Response.Header().Set("cache-control", "max-age=3")
return json.NewEncoder(ctx).Encode(md)
}
示例6: handleGetCmdInfo
func handleGetCmdInfo(ctx *web.Context, idstr string) error {
id, _ := liblush.ParseCmdId(idstr)
s := ctx.User.(*server)
c := s.session.GetCommand(id)
if c == nil {
return web.WebError{404, "no such command: " + idstr}
}
ctx.Header().Set("content-type", "application/json")
enc := json.NewEncoder(ctx)
var info = struct {
Started, Exited *time.Time
Error string `json:",omitempty"`
}{
Started: c.Status().Started(),
Exited: c.Status().Exited(),
}
if cerr := c.Status().Err(); cerr != nil {
info.Error = cerr.Error()
}
return enc.Encode(info)
}
示例7: handleGetCmdidsJson
func handleGetCmdidsJson(ctx *web.Context) error {
s := ctx.User.(*server)
ids := s.session.GetCommandIds()
ctx.ContentType("json")
return json.NewEncoder(ctx).Encode(ids)
}
示例8: handleWsCtrl
// Websocket control connection. All connected clients are considered equal.
func handleWsCtrl(ctx *web.Context) error {
wsconn, err := websocket.Upgrade(ctx.Response, ctx.Request, nil, 1024, 1024)
if _, ok := err.(websocket.HandshakeError); ok {
// Get the secret token to include in a websocket request
ctx.ContentType("txt")
fmt.Fprint(ctx.Response, getWebsocketKey())
return nil
} else if err != nil {
return err
}
s := ctx.User.(*server)
ws := newWsClient(wsconn)
defer ws.Close()
// This is just for the incoming key, after which blocking on read is fine
err = ws.SetReadDeadline(time.Now().Add(5 * time.Second))
if err != nil {
return fmt.Errorf("Couldn't set read deadline for websocket: %v", err)
}
// First order of business: see if the client sends me the correct key
// this could be done lots of ways different, perhaps more elegant ways:
// HTTP headers, query parameters, secret path, ... BUT! This method is very
// straight-forward and easy to understand.
msg, err := ws.ReadTextMessage()
if err != nil {
return err
}
if string(msg) != getWebsocketKey() {
// This is a best effort service to help whoever tried to connect to
// this in debugging, hence no error checking.
fmt.Fprint(ws, "Invalid lush key")
return errors.New("Illegal websocket key")
}
// Alright I trust this client now, read ops are expected to block
err = ws.SetReadDeadline(time.Time{})
if err != nil {
return fmt.Errorf("Couldn't remove read deadline for websocket: %v", err)
}
// tell the client about its Id
_, err = fmt.Fprint(ws, "clientid;", ws.Id)
if err != nil {
return fmt.Errorf("Websocket write error: %v", err)
}
// Subscribe this ws client to all future control events. Will be removed
// automatically when the first Write fails (FlexibleMultiWriter).
// Therefore, no need to worry about removing: client disconnects -> next
// Write fails -> removed.
s.ctrlclients.AddWriter(ws)
// notify all other clients that a new client has connected
wseventAllclients(s, "") // pretend somebody generated this event
// TODO: keep clients updated about disconnects, too
ws.isMaster = claimMaster(ctx)
for {
msg, err := ws.ReadTextMessage()
if err != nil {
s.ctrlclients.RemoveWriter(ws)
return err
}
err = parseAndHandleWsEvent(s, ws, msg)
if err != nil {
return fmt.Errorf("error handling WS event: %v", err)
}
}
return errors.New("unreachable")
}
示例9: process
func process(ctx *web.Context) string {
ctx.ContentType("txt")
return fmt.Sprintf("%#v", ctx.Params)
}
示例10: say
func say(ctx *web.Context) {
ctx.SetSecureCookie("said", ctx.Params["said"], 3600)
ctx.Redirect(303, "/")
return
}
示例11: AuthHandler
// This function will be called prior to each web request
func AuthHandler(ctx *web.Context) error {
ctx.User = "Passed from AuthHandler"
fmt.Println(ctx.Request.Header)
return nil
}
示例12: i
func i(ctx *web.Context) {
ctx.Write([]byte("<a href=j>non-nil error"))
}