rest

package module
v0.0.0-...-aa6a652 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2018 License: Apache-2.0 Imports: 6 Imported by: 23

README

Build Status GoDoc

Heketi RESTful Functions

Generic Go RESTful funcitions

Licensing

Heketi is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Documentation

Overview

Generic RESTful application functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncHttpHandler

type AsyncHttpHandler struct {
	// contains filtered or unexported fields
}

Contains information about the asynchronous operation

func (*AsyncHttpHandler) Completed

func (h *AsyncHttpHandler) Completed()

Registers that the handler has completed and no data needs to be returned

func (*AsyncHttpHandler) CompletedWithError

func (h *AsyncHttpHandler) CompletedWithError(err error)

Registers that the handler has completed with an error

func (*AsyncHttpHandler) CompletedWithLocation

func (h *AsyncHttpHandler) CompletedWithLocation(location string)

Registers that the handler has completed and has provided a location where information can be retreived

func (*AsyncHttpHandler) Url

func (h *AsyncHttpHandler) Url() string

Returns the url for the specified asynchronous handler

type AsyncHttpManager

type AsyncHttpManager struct {
	IdGen func() string
	// contains filtered or unexported fields
}

Manager of asynchronous operations

func NewAsyncHttpManager

func NewAsyncHttpManager(route string) *AsyncHttpManager

Creates a new manager

func (*AsyncHttpManager) AsyncHttpRedirectFunc

func (a *AsyncHttpManager) AsyncHttpRedirectFunc(w http.ResponseWriter,
	r *http.Request,
	handlerfunc func() (string, error))

Create an asynchronous operation handler and return the appropiate information the caller. This function will call handlerfunc() in a new go routine, then return to the caller a HTTP status 202 setting up the `Location` header to point to the new asynchronous handler.

If handlerfunc() returns failure, the asynchronous handler will return an http status of 500 and save the error string in the body. If handlerfunc() is successful and returns a location url path in "string", the asynchronous handler will return 303 (See Other) with the Location header set to the value returned in the string. If handlerfunc() is successful and returns an empty string, then the asynchronous handler will return 204 to the caller.

Example:

     package rest
		import (
			"github.com/gorilla/mux"
         "github.com/heketi/rest"
			"net/http"
			"net/http/httptest"
			"time"
		)

		// Setup asynchronous manager
		route := "/x"
		manager := rest.NewAsyncHttpManager(route)

		// Setup the route
		router := mux.NewRouter()
	 	router.HandleFunc(route+"/{id}", manager.HandlerStatus).Methods("GET")
		router.HandleFunc("/result", func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
			w.WriteHeader(http.StatusOK)
			fmt.Fprint(w, "HelloWorld")
		}).Methods("GET")

		router.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
			manager.AsyncHttpRedirectFunc(w, r, func() (string, error) {
				time.Sleep(100 * time.Millisecond)
				return "/result", nil
			})
		}).Methods("GET")

		// Setup the server
		ts := httptest.NewServer(router)
		defer ts.Close()

func (*AsyncHttpManager) AsyncHttpRedirectUsing

func (a *AsyncHttpManager) AsyncHttpRedirectUsing(w http.ResponseWriter,
	r *http.Request,
	id string,
	handlerfunc func() (string, error))

func (*AsyncHttpManager) HandlerStatus

func (a *AsyncHttpManager) HandlerStatus(w http.ResponseWriter, r *http.Request)

Handler for asynchronous operation status Register this handler with a router like Gorilla Mux

Returns the following HTTP status codes

200 Operation is still pending
404 Id requested does not exist
500 Operation finished and has failed.  Body will be filled in with the
	error in plain text.
303 Operation finished and has setup a new location to retreive data.
204 Operation finished and has no data to return

Example:

     package rest
		import (
			"github.com/gorilla/mux"
         "github.com/heketi/rest"
			"net/http"
			"net/http/httptest"
			"time"
		)

		// Setup asynchronous manager
		route := "/x"
		manager := rest.NewAsyncHttpManager(route)

		// Setup the route
		router := mux.NewRouter()
	 	router.HandleFunc(route+"/{id:[A-Fa-f0-9]+}", manager.HandlerStatus).Methods("GET")

		// Setup the server
		ts := httptest.NewServer(router)
		defer ts.Close()

func (*AsyncHttpManager) NewHandler

func (a *AsyncHttpManager) NewHandler() *AsyncHttpHandler

Use to create a new asynchronous operation handler. Only use this function if you need to do every step by hand. It is recommended to use AsyncHttpRedirectFunc() instead

func (*AsyncHttpManager) NewHandlerWithId

func (a *AsyncHttpManager) NewHandlerWithId(id string) *AsyncHttpHandler

NewHandlerWithId constructs and returns an AsyncHttpHandler with the given ID. Compare to NewHandler() which automatically generates its own ID.

func (*AsyncHttpManager) NewId

func (a *AsyncHttpManager) NewId() string

NewId returns a new string id for a handler. This string is not preserved internally and must be passed to another function to be used.

type Route

type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

This route style comes from the tutorial on http://thenewstack.io/make-a-restful-json-api-go/

type Routes

type Routes []Route

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL