Beispiel #1
0
func ExamplePing() {

	// Add our service that implements the rest.Routable interface to our
	// endpoint. We can also add simple lambda functions to our endpoint.
	rest.AddService(new(PingService))
	rest.AddRoute("/simple", "POST", func(tick int) int { return tick })

	// The endpoint is started like any other http.Server.
	go rest.ListenAndServe(":12345", nil)

	// The rest package also provides a way to query a REST endpoint by
	// incrementally building a REST request. The body of the query can be set
	// via the SetBody() function which will serialize the object to JSON using
	// the encoding.json package.
	simpleResp := rest.NewRequest("http://localhost:12345", "POST").
		SetPath("/simple").
		SetBody(123).
		Send()

	// The rest.Response object contains the result of the REST request and can
	// be used to deserialize the JSON body into the provided object using the
	// encoding.json package.
	var tick int
	if err := simpleResp.GetBody(&tick); err != nil {
		panic("Whoops!")
	}
	fmt.Println("ping-simple:", tick)

	// The REST client can also be customized by manually creating a rest.Client
	// which can then be used to create REST requests. rest.Client embdeds an
	// http.Client struct which can be used to customize the HTTP requests.
	client := &rest.Client{Host: "http://localhost:12345", Root: "/ping"}
	clientResp := client.NewRequest("PUT").
		SetPath("%d", 321).
		Send()

	if err := clientResp.GetBody(&tick); err != nil {
		panic("Whoops!")
	}
	fmt.Println("ping-client:", tick)

	// Output:
	// ping-simple: 123
	// ping-client: 321
}
Beispiel #2
0
// NewFilterREST creates a new REST enabled Filter chained printer at the
// specified path. The def parameter should be either FilterIn or FilterOut.
func NewFilterREST(path string, def int) *FilterREST {
	filter := &FilterREST{Filter: NewFilter(def), PathPrefix: path}
	rest.AddService(filter)
	return filter
}
Beispiel #3
0
// NewRESTHandler creates a new REST interface and registers it with the default
// gorest Mux.
func NewRESTHandler(path string) *RESTHandler {
	handler := &RESTHandler{PathPrefix: path}
	rest.AddService(handler)
	return handler
}
Beispiel #4
0
// NewRingREST creates a new REST enabled Ring printer at the specified path
// with the given size. If path is empty then DefaultPathREST will be used
// instead.
func NewRingREST(path string, size int) *RingREST {
	ring := &RingREST{Ring: NewRing(size), PathPrefix: path}
	rest.AddService(ring)
	return ring
}