本文整理汇总了Golang中github.com/app-kit/go-appkit.Request.GetFrontend方法的典型用法代码示例。如果您正苦于以下问题:Golang Request.GetFrontend方法的具体用法?Golang Request.GetFrontend怎么用?Golang Request.GetFrontend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/app-kit/go-appkit.Request
的用法示例。
在下文中一共展示了Request.GetFrontend方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ApiCreate
// Creating a session is equivalent to logging in.
func (hooks SessionResourceHooks) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response {
userService := res.Registry().UserService()
meta := r.GetMeta()
isAnonymous, _ := meta.Bool("anonymous")
// Find user.
userIdentifier := meta.String("user")
adaptor := meta.String("adaptor")
data, _ := meta.Map("authData")
var user kit.User
if !isAnonymous {
if adaptor == "" {
return kit.NewErrorResponse("adaptor_missing", "Expected 'adaptor' in metadata.", true)
}
if data == nil {
kit.NewErrorResponse("no_or_invalid_auth_data", "Expected 'authData' dictionary in metadata.")
}
var err apperror.Error
user, err = userService.AuthenticateUser(userIdentifier, adaptor, data)
if err != nil {
return kit.NewErrorResponse(err)
}
}
session, err := userService.StartSession(user, r.GetFrontend())
if err != nil {
return kit.NewErrorResponse(err)
}
responseMeta := make(map[string]interface{})
if !isAnonymous {
userData, err := res.Backend().ModelToMap(user, true, false)
if err != nil {
return kit.NewErrorResponse("marshal_error", err)
}
responseMeta["user"] = userData
if user.GetProfile() != nil {
profileData, err := res.Backend().ModelToMap(user.GetProfile(), true, false)
if err != nil {
return kit.NewErrorResponse("marshal_error", err)
}
responseMeta["profile"] = profileData
}
}
return &kit.AppResponse{
Data: session,
Meta: responseMeta,
}
}
示例2: RequestLoggerMiddleware
func RequestLoggerMiddleware(registry kit.Registry, r kit.Request, response kit.Response) (kit.Response, bool) {
// Calculate time taken.
rawStarted, ok1 := r.GetContext().Get("startTime")
rawFinished, ok2 := r.GetContext().Get("endTime")
timeTaken := int64(-1)
if ok1 && ok2 {
started := rawStarted.(time.Time)
finished := rawFinished.(time.Time)
timeTaken = int64(finished.Sub(started) / time.Millisecond)
}
// Log the request.
method := r.GetHttpMethod()
path := r.GetPath()
if response.GetError() != nil {
registry.Logger().WithFields(logrus.Fields{
"frontend": r.GetFrontend(),
"action": "request",
"method": method,
"path": path,
"status": response.GetHttpStatus(),
"err": response.GetError(),
"milliseconds": timeTaken,
}).Errorf("%v: %v - %v - %v", response.GetHttpStatus(), method, path, response.GetError())
} else {
registry.Logger().WithFields(logrus.Fields{
"frontend": r.GetFrontend(),
"action": "request",
"method": method,
"path": path,
"status": response.GetHttpStatus(),
"milliseconds": timeTaken,
}).Debugf("%v: %v - %v", response.GetHttpStatus(), method, path)
}
return nil, false
}