Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnGet() event hook not firing #79

Closed
ultimateboy opened this issue Jan 3, 2017 · 2 comments
Closed

OnGet() event hook not firing #79

ultimateboy opened this issue Jan 3, 2017 · 2 comments

Comments

@ultimateboy
Copy link
Contributor

Steps to reproduce:

  1. Create a simple resource endpoint
  2. Attach OnGet and OnFind event hooks with a simple log message (see code below)
  3. Post a new simple resource (http post :8080/simple name="testing")
  4. Do a GET query on the new item (http get :8080/simple/id-from-above)

Expected Results:

See log entry for both OnGet() and OnFind() hooks.

Actual Results:

Only OnFind() is fired, not OnGet()

Sample Code:

package main

import (
	"context"
	"log"
	"net/http"

	"github.com/justinas/alice"

	mem "github.com/rs/rest-layer-mem"
	"github.com/rs/rest-layer/resource"
	"github.com/rs/rest-layer/rest"
	"github.com/rs/rest-layer/schema"
)

var (
	SimpleSchema = schema.Schema{
		Description: `A simple object`,
		Fields: schema.Fields{
			"id": schema.IDField,
			"name": {
				Required:   true,
				Filterable: true,
				Validator: &schema.String{
					MaxLen: 150,
				},
			},
		},
	}
)

type SimpleHook struct{}

func (h SimpleHook) OnFind(ctx context.Context, lookup *resource.Lookup, offset, limit int) error {
	log.Println("SimpleHook.OnFind()")
	return nil
}

func (h SimpleHook) OnGet(ctx context.Context, id interface{}) error {
	log.Println("SimpleHook.OnGet()")
	return nil
}

func main() {
	resource.LoggerLevel = resource.LogLevelDebug

	index := resource.NewIndex()

	simpleHandler := mem.NewHandler()
	simpleResource := index.Bind("simple", SimpleSchema, simpleHandler, resource.Conf{
		AllowedModes: resource.ReadWrite,
	})
	err := simpleResource.Use(SimpleHook{})
	if err != nil {
		log.Fatalf("Error protecting simple resource: %s", err)
	}

	api, err := rest.NewHandler(index)
	if err != nil {
		log.Fatalf("Invalid API configuration: %s", err)
	}

	c := alice.New()
	http.Handle("/", c.Then(api))

	log.Print("Serving API on http://localhost:8080")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}
}
@rs
Copy link
Owner

rs commented Jan 3, 2017

This is expected and somewhat badly documented:

Hook Interface Description
FindEventHandler Defines a function called when the resource is listed with or without a query. Note that hook is called for both resource and item fetch as well as prior to updates and deletes.

The rest layer always use Find because you can get an item by its id together with filters. The OnGet handler is only called when Get is explicitly called on the resource, either from one of your hooks or when checking cross resource reference.

I know it is counter intuitive.

@ultimateboy
Copy link
Contributor Author

Alright that makes sense. Thanks for the quick response, Olivier!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants