本文整理汇总了Golang中github.com/codegangsta/martini.Context类的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: LogOne
func LogOne(res http.ResponseWriter, req *http.Request, c martini.Context, logger *log.Logger) {
start := time.Now()
c.Next()
rw := res.(martini.ResponseWriter)
status := rw.Status()
if status != 200 && status != 304 && req.URL.Path != "/ws" {
logThis(start, res, req, logger)
return
}
host, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
host = req.RemoteAddr
}
logOneLock.Lock()
if _, ok := logged[host]; ok {
logOneLock.Unlock()
return
}
logged[host] = true
logOneLock.Unlock()
logger.Printf("%s\tRequested from %s; subsequent successful requests will not be logged\n", time.Now().Format("15:04:05"), host)
}
示例2: withApp
func withApp(context martini.Context, params martini.Params, res http.ResponseWriter) {
app := appreg.Get(params["name"])
if app == nil {
http.Error(res, "No such application.", 404)
}
context.Map(app)
}
示例3: GET_home
func GET_home(c martini.Context, identity *data.Identity, render render.Render) {
if identity == nil {
c.Invoke(redirect_to("/login"))
} else {
c.Invoke(GET_profile)
}
}
示例4: inner_GET_authorize
func inner_GET_authorize(c martini.Context, sess sessions.Session, r *http.Request, ar *osin.AuthorizeRequest) bool {
var (
identity = ActiveIdentity(c)
source = current_url(r)
handler martini.Handler
)
if identity != nil {
ar.UserData = identity
sess.Delete("flow")
return true
} else {
sess.Set("flow", FlowState{
Type: AuthorizeFlow,
Source: source,
StartAt: time.Now(),
})
if provider := r.URL.Query().Get("p"); provider == "" {
handler = show_provider_chooser()
} else {
handler = redirect_to_provider(provider)
}
}
c.Invoke(handler)
return false
}
示例5: create_identity
func create_identity(c martini.Context, tx *sqlx.Tx) {
identity := &data.Identity{}
err := data.CreateIdentity(tx, identity)
if err != nil {
panic(err)
}
c.Map(identity)
}
示例6: Middleware
func Middleware(ctx martini.Context, r *http.Request, w http.ResponseWriter) {
sessionId := ensureCookie(r, w)
session := sessionStore.Get(sessionId)
ctx.Map(session)
ctx.Next()
sessionStore.Set(session)
}
示例7: SearcherMiddle
func SearcherMiddle(c martini.Context, w http.ResponseWriter, r *http.Request) {
switch r.URL.Query().Get("type") {
case "bing":
c.MapTo(BingSearcher{}, (*Searcher)(nil))
w.Header().Set("Content-Type", "application/json")
default:
c.MapTo(BingSearcher{}, (*Searcher)(nil))
w.Header().Set("Content-Type", "application/json")
}
}
示例8: ActiveIdentity
func ActiveIdentity(c martini.Context) *data.Identity {
var (
identity *data.Identity
)
c.Invoke(MayAuthenticate())
c.Invoke(func(i *data.Identity) { identity = i })
return identity
}
示例9: RequireLogin
func RequireLogin(rw http.ResponseWriter, req *http.Request,
s sessions.Session, db *sql.DB, c martini.Context) {
user := &User{}
err := db.QueryRow("select name, email from users where id=$1", s.Get("userId")).Scan(&user.Name, &user.Email)
if err != nil {
http.Redirect(rw, req, "/login", http.StatusFound)
return
}
c.Map(user)
}
示例10: ConnectDB
func ConnectDB(r *http.Request, c martini.Context) string {
host := r.FormValue("host")
port := r.FormValue("port")
client := riakpbc.NewClient([]string{
host + ":" + port,
})
c.Map(client)
return "connected"
}
示例11: must_authenticate
func must_authenticate(c martini.Context, sess sessions.Session, db *sqlx.DB, r *http.Request) {
identity := ActiveIdentity(c)
if identity != nil {
return
}
if r.Header.Get("x-interactive") == "true" {
sess.Delete("identity_id")
c.Invoke(redirect_to("/login"))
} else {
c.Invoke(forbidden())
}
}
示例12: GET_continue
func GET_continue(c martini.Context, params martini.Params) {
var (
provider = params["provider"]
handler martini.Handler
)
if provider == "" {
handler = show_provider_chooser()
} else {
handler = redirect_to_provider(provider)
}
c.Invoke(handler)
}
示例13: may_authenticate
func may_authenticate(c martini.Context, sess sessions.Session, db *sqlx.DB, r *http.Request) {
var (
interactive = true
token string
identity_id int64
identity *data.Identity
err error
)
// Attempt with Authorization header
if v := r.Header.Get("Authorization"); v != "" {
parts := strings.SplitN(v, " ", 2)
if len(parts) == 2 && strings.ToLower(parts[0]) == "bearer" {
interactive = false
token = parts[1]
}
// Attempt with access_token parameter
} else if v := r.URL.Query().Get("access_token"); v != "" {
interactive = false
token = v
// Attempt with session.identity_id
} else if id, ok := sess.Get("identity_id").(int64); ok {
interactive = true
identity_id = id
}
if token != "" {
at, err := data.GetAccessTokenWithAccessToken(db, token)
if err != nil {
panic(err)
}
identity_id = at.IdentityId
}
if identity_id > 0 {
identity, err = data.GetIdentity(db, identity_id)
if err != nil {
panic(err)
}
}
if interactive {
r.Header.Set("x-interactive", "true")
}
c.Map(identity)
}
示例14: GET_callback_AB
func GET_callback_AB(c martini.Context, sess sessions.Session) {
flow := sess.Get("flow").(FlowState)
c.Invoke(match_session_identity_with_account)
c.Invoke(match_session_identity_with_flow)
c.Invoke(update_account)
c.Invoke(redirect_to(flow.Source))
}
示例15: GET_callback_BC
func GET_callback_BC(c martini.Context, sess sessions.Session) {
flow := sess.Get("flow").(FlowState)
c.Invoke(create_identity)
c.Invoke(create_account)
c.Invoke(activate_session)
c.Invoke(redirect_to(flow.Source))
}