本文整理匯總了Golang中github.com/app-kit/go-appkit.Resource類的典型用法代碼示例。如果您正苦於以下問題:Golang Resource類的具體用法?Golang Resource怎麽用?Golang Resource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Resource類的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ApiCreate
func (_ FilesResource) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response {
// Verify that tmp path is set either in metadata or on model.
file := obj.(kit.File)
if file.GetTmpPath() == "" {
file.SetTmpPath(r.GetMeta().String("file"))
}
filePath := file.GetTmpPath()
if filePath == "" {
return kit.NewErrorResponse("no_tmp_path", "A tmp path must be set when creating a file", true)
}
tmpPath := getTmpPath(res)
if !strings.HasPrefix(filePath, tmpPath) && filePath[0] != '/' {
filePath = tmpPath + string(os.PathSeparator) + filePath
file.SetTmpPath(filePath)
}
// Build the file, save it to backend and persist it to the db.
err := res.Registry().FileService().BuildFile(file, r.GetUser(), true, true)
if err != nil {
kit.NewErrorResponse(err)
}
return &kit.AppResponse{
Data: file,
}
}
示例2: BeforeDelete
func (PageResource) BeforeDelete(res kit.Resource, obj kit.Model, user kit.User) apperror.Error {
// Note: other relationship deletion handling happens in models BeforeDelete() hook.
// Files have to be deleted here, since they require the fileService.
// Delete files.
fileService := res.Registry().FileService()
m2m, err := res.Backend().M2M(obj, "Files")
if err != nil {
return err
}
files, err := m2m.All()
if err != nil {
return err
}
// Delete m2m relation.
if err := m2m.Clear(); err != nil {
return err
}
for _, file := range files {
if err := fileService.Delete(file.(kit.File), user); err != nil {
return err
}
}
return nil
}
示例3: AllowDelete
func (UserResource) AllowDelete(res kit.Resource, obj kit.Model, user kit.User) bool {
if user == nil {
return false
}
if obj.(kit.UserModel).GetUserId() == user.GetId() {
return true
}
return user.HasRole("admin") || user.HasPermission(res.Collection()+".delete")
}
示例4: ApiDelete
func (SessionResourceHooks) ApiDelete(res kit.Resource, id string, r kit.Request) kit.Response {
if id != r.GetSession().GetStrId() {
return kit.NewErrorResponse("permission_denied", "Permission denied", 403)
}
if err := res.Backend().Delete(r.GetSession()); err != nil {
return kit.NewErrorResponse("db_delete_error", err, true)
}
return &kit.AppResponse{}
}
示例5: getTmpPath
func getTmpPath(res kit.Resource) string {
c := res.Registry().Config()
tmpPath := c.UPath("tmpUploadDir")
if tmpPath == "" {
tmpPath = c.TmpDir()
if tmpPath == "" {
panic("config.TmpDir() empty")
}
tmpPath = path.Join(tmpPath, "uploads")
}
return tmpPath
}
示例6: RegisterResource
func (s *Service) RegisterResource(res kit.Resource) {
if res.Backend() == nil {
if s.defaultBackend == nil {
s.registry.Logger().Panic("Registering resource without backend, but no default backend set on resources.Service")
}
s.defaultBackend.RegisterModel(res.Model())
res.SetBackend(s.defaultBackend)
}
if res.Collection() == "" {
s.registry.Logger().Panic("Registering resource without a model type")
}
s.resources[res.Collection()] = res
}
示例7: ApiCreate
func (hooks UserResourceHooks) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response {
meta := r.GetMeta()
adaptor := meta.String("adaptor")
if adaptor == "" {
return kit.NewErrorResponse("adaptor_missing", "Expected 'adaptor' in metadata.", true)
}
rawData, ok := meta.Get("authData")
if !ok {
return kit.NewErrorResponse("auth_data_missing", "Expected 'authData' in metadata.", true)
}
data, ok := rawData.(map[string]interface{})
if !ok {
return kit.NewErrorResponse("invalid_auth_data", "Invalid auth data: expected dictionary", true)
}
user := obj.(kit.User)
service := res.Registry().UserService()
// If a profile model was registered, and profile data is in meta,
// create the profile model.
if profiles := service.ProfileResource(); profiles != nil {
profile := profiles.CreateModel().(kit.UserProfile)
if rawData, ok := meta.Get("profile"); ok {
if data, ok := rawData.(map[string]interface{}); ok {
// Profile data present in meta.
// Update profile with data.
if err := res.ModelInfo().UpdateModelFromData(profile, data); err != nil {
return kit.NewErrorResponse("invalid_profile_data", "Invalid profile data.", err, true)
}
}
}
user.SetProfile(profile)
}
if err := service.CreateUser(user, adaptor, data); err != nil {
return kit.NewErrorResponse(err)
}
return &kit.AppResponse{
Data: user,
}
}
示例8: Methods
func (PageResource) Methods(res kit.Resource) []kit.Method {
publish := &methods.Method{
Name: "cms.page.publish",
Blocking: true,
Handler: func(registry kit.Registry, r kit.Request, unblock func()) kit.Response {
user := r.GetUser()
if user == nil || !user.HasRole("admin") {
return kit.NewErrorResponse("permission_denied")
}
id := utils.GetMapStringKey(r.GetData(), "id")
if id == "" {
return kit.NewErrorResponse("no_id_in_data", "Expected 'id' key in data.")
}
rawPage, err := res.Backend().FindOne("pages", id)
if err != nil {
return kit.NewErrorResponse("db_error", err)
} else if rawPage == nil {
return kit.NewErrorResponse("not_found", "The specified page id does not exist.")
}
err = res.ModelInfo().UpdateModelFromData(rawPage, map[string]interface{}{
"published": true,
"published_at": time.Now(),
})
if err != nil {
return kit.NewErrorResponse("db_error", err)
}
return &kit.AppResponse{
Data: map[string]interface{}{"success": true},
}
},
}
return []kit.Method{publish}
}
示例9: ApiFindOne
// ApiFindOne verifies the session and returns user and profile in meta if valid.
func (SessionResourceHooks) ApiFindOne(res kit.Resource, rawId string, r kit.Request) kit.Response {
if rawId == "" {
return kit.NewErrorResponse("empty_token", "Empty token")
}
userService := res.Registry().UserService()
user, session, err := userService.VerifySession(rawId)
if err != nil {
return kit.NewErrorResponse(err)
}
meta := make(map[string]interface{})
if user != nil {
userData, err := res.Backend().ModelToMap(user, true, false)
if err != nil {
return kit.NewErrorResponse("marshal_error", err)
}
meta["user"] = userData
if user.GetProfile() != nil {
profileData, err := res.Backend().ModelToMap(user.GetProfile(), true, false)
if err != nil {
return kit.NewErrorResponse("marshal_error", err)
}
meta["profile"] = profileData
}
}
return &kit.AppResponse{
Data: session,
Meta: meta,
}
}
示例10: HttpRoutes
func (hooks FilesResource) HttpRoutes(res kit.Resource) []kit.HttpRoute {
maxRunning := res.Registry().Config().UInt("files.thumbGenerator.maxRunning", 10)
maxPerIPPerMinute := res.Registry().Config().UInt("files.thumbGenerator.maxPerIPPerMinute", 100)
maxQueueSize := res.Registry().Config().UInt("files.thumbGenerator.maxQueueSize", 100)
hooks.thumbnailRateLimiter = newRateLimiter(maxRunning, maxPerIPPerMinute, maxQueueSize)
routes := make([]kit.HttpRoute, 0)
// Upload route.
uploadOptionsHandler := func(registry kit.Registry, r kit.Request) (kit.Response, bool) {
header := r.GetHttpResponseWriter().Header()
allowedOrigins := registry.Config().UString("fileHandler.allowedOrigins", "*")
header.Set("Access-Control-Allow-Origin", allowedOrigins)
header.Set("Access-Control-Allow-Methods", "OPTIONS, POST")
allowedHeaders := registry.Config().UString("accessControl.allowedHeaders")
if allowedHeaders == "" {
allowedHeaders = "Authentication, Content-Type, Content-Range, Content-Disposition"
} else {
allowedHeaders += ", Authentication, Content-Type, Content-Range, Content-Disposition"
}
header.Set("Access-Control-Allow-Headers", allowedHeaders)
return &kit.AppResponse{
HttpStatus: 200,
RawData: []byte{},
}, true
}
uploadOptionsRoute := kit.NewHttpRoute("/api/file-upload", "OPTIONS", uploadOptionsHandler)
routes = append(routes, uploadOptionsRoute)
tmpPath := getTmpPath(res)
if tmpPath == "" {
panic("Empty tmp path")
}
uploadHandler := func(registry kit.Registry, r kit.Request) (kit.Response, bool) {
if registry.Config().UBool("fileHandler.requiresAuth", false) {
if r.GetUser() == nil {
return kit.NewErrorResponse("permission_denied", ""), false
}
}
var files []string
var err apperror.Error
if err == nil {
files, err = handleUpload(registry, tmpPath, r.GetHttpRequest())
if err != nil {
return kit.NewErrorResponse(err), false
}
}
data := map[string]interface{}{
"data": files,
}
return &kit.AppResponse{Data: data}, false
}
uploadRoute := kit.NewHttpRoute("/api/file-upload", "POST", uploadHandler)
routes = append(routes, uploadRoute)
serveFileHandler := func(registry kit.Registry, r kit.Request) (kit.Response, bool) {
file, err := registry.FileService().FindOne(r.GetContext().String("id"))
if err != nil {
return kit.NewErrorResponse(err), false
}
if file == nil {
return &kit.AppResponse{
HttpStatus: 404,
RawData: []byte("File not found"),
}, false
}
reader, err := file.Reader()
if err != nil {
return kit.NewErrorResponse(err), false
}
defer reader.Close()
w := r.GetHttpResponseWriter()
err = serveFile(w, r.GetHttpRequest(), file.GetMime(), file.GetSize(), reader)
reader.Close()
if err != nil {
registry.Logger().Errorf("Error while serving file %v(%v): %v", file.GetId(), file.GetBackendId(), err)
}
return nil, true
}
serveFileRoute := kit.NewHttpRoute("/files/:id/*rest", "GET", serveFileHandler)
routes = append(routes, serveFileRoute)
serveImageHandler := func(registry kit.Registry, r kit.Request) (kit.Response, bool) {
//.........這裏部分代碼省略.........
示例11: RegisterResource
func (a *App) RegisterResource(res kit.Resource) {
if res.Backend() == nil {
if a.registry.DefaultBackend() == nil {
a.Logger().Panic("Registering resource without backend, but no default backend set.")
}
// Set backend.
res.SetBackend(a.registry.DefaultBackend())
}
if res.Registry() == nil {
res.SetRegistry(a.registry)
}
res.SetDebug(a.Debug())
// Allow a resource to register custom http routes and methods.
if res.Hooks() != nil {
// Handle http routes.
if resRoutes, ok := res.Hooks().(resources.ApiHttpRoutes); ok {
for _, route := range resRoutes.HttpRoutes(res) {
a.RegisterHttpHandler(route.Method(), route.Route(), route.Handler())
}
}
// Handle methods.
if resMethods, ok := res.Hooks().(resources.MethodsHook); ok {
for _, method := range resMethods.Methods(res) {
a.RegisterMethod(method)
}
}
}
a.registry.AddResource(res)
}
示例12: AddResource
func (d *Registry) AddResource(res kit.Resource) {
d.resources[res.Collection()] = res
}
示例13: Find
func Find(res kit.Resource, request kit.Request) (kit.Response, apperror.Error) {
collection := res.Collection()
info := res.Backend().ModelInfo(collection)
var query *db.Query
jsonQuery := request.GetContext().String("query")
if jsonQuery != "" {
var rawQuery map[string]interface{}
if err := json.Unmarshal([]byte(jsonQuery), &rawQuery); err != nil {
return nil, apperror.Wrap(err, "invalid_query_json")
}
rawQuery["collection"] = collection
// A custom query was supplied.
// Try to parse the query.
var err apperror.Error
query, err = db.ParseQuery(res.Backend(), rawQuery)
if err != nil {
return nil, apperror.Wrap(err, "invalid_query", "", false)
}
}
if query == nil {
query = res.Backend().Q(collection)
}
// Check paging parameters.
var limit, offset int
context := request.GetContext()
if context.Has("limit") {
val, err := context.Int("limit")
if err != nil {
return nil, &apperror.Err{
Public: true,
Code: "non_numeric_limit_parameter",
Message: "The get query contains a non-numeric ?limit",
}
}
limit = val
}
if context.Has("offset") {
val, err := context.Int("offset")
if err != nil {
return nil, &apperror.Err{
Public: true,
Code: "non_numeric_offset_parameter",
Message: "The get query contains a non-numeric ?offset",
}
}
offset = val
}
var page, perPage int
if context.Has("page") {
val, err := context.Int("page")
if err != nil {
return nil, &apperror.Err{
Public: true,
Code: "non_numeric_page_parameter",
Message: "The get query contains a non-numeric ?page",
}
}
page = val
}
if context.Has("per_page") {
val, err := context.Int("per_page")
if err != nil {
return nil, &apperror.Err{
Public: true,
Code: "non_numeric_per_page_parameter",
Message: "The get query contains a non-numeric ?per_page",
}
}
perPage = val
}
if perPage > 0 {
limit = perPage
}
if page > 1 {
offset = (page - 1) * limit
}
if limit > 0 {
query.Limit(int(limit)).Offset(int(offset))
}
// Add joins.
if context.Has("joins") {
parts := strings.Split(context.String("joins"), ",")
for _, name := range parts {
//.........這裏部分代碼省略.........