Documentation ¶
Index ¶
- Variables
- type DeleteRoute
- type DeleteRouteRoutePath
- type GetAllRoute
- type GetAllRoutePath
- type GetRoute
- type GetRoutePath
- type HttpError
- type Loggable
- type Middleware
- type Parser
- type PostRoute
- type PostRouteRoutePath
- type QueryParams
- type RawRoute
- type RawRouteRoutePath
- type RouteMachine
- func (rm *RouteMachine) AddMiddleware(next Middleware) error
- func (rm *RouteMachine) AddRouteSet(routeSet *RouteSet) error
- func (rm *RouteMachine) SetIdleTimeout(timeout time.Duration) *RouteMachine
- func (rm *RouteMachine) SetReadHeaderTimeout(timeout time.Duration) *RouteMachine
- func (rm *RouteMachine) SetReadTimeout(timeout time.Duration) *RouteMachine
- func (rm *RouteMachine) Start() error
- func (rm *RouteMachine) Stop() error
- type RouteSet
- type UpdateRoute
- type UpdateRouteRoutePath
- type UrlParams
- type WithLogger
Constants ¶
This section is empty.
Variables ¶
var ( ErrAddressNotSet = errors.New("address not set") ErrRouteSetNotPresent = errors.New("missing routesets") ErrNilRouteSetIsNotAllowed = errors.New("empty route set is not supported") ErrNilMiddlewareIsNotAllowed = errors.New("nil middleware is not supported") )
var ( ErrGetRouteIsNil = errors.New("get route is nil") ErrGetAllRouteIsNil = errors.New("get all route is nil") ErrPostRouteIsNil = errors.New("post route is nil") ErrUpdateRouteIsNil = errors.New("update route is nil") ErrDeleteRouteIsNil = errors.New("delete route is nil") )
var (
ErrHttpResponseWriterNotSet = errors.New("http response writer is not set")
)
Functions ¶
This section is empty.
Types ¶
type DeleteRoute ¶
type DeleteRoute interface { // Delete represents the method that contains the business logic for deleting a resource. // // Example // type Model struct { // Name string `json:"name,omitempty"` // URL string `json:"url,omitempty"` // } // // type MyType struct { // Model // } // // func (m *MyType) Delete(requestData interface{}) *HttpError { // // do something // fmt.Printf("%+v\n", m.Model) // return nil // } Delete(requestData interface{}) *HttpError }
DeleteRoute provides the interface that must be implemented to create a Delete endpoint.
type DeleteRouteRoutePath ¶
type DeleteRouteRoutePath interface { // DeleteRoutePath represents an optional method that can be set to define a custom path for the delete route. // // Example: // type MyType struct {} // // func (m *MyType) DeleteRoutePath() string { // return "/delete" // } DeleteRoutePath() string }
DeleteRouteRoutePath defines an optional child interface that is used to customize route path.
type GetAllRoute ¶
type GetAllRoute interface { // GetAll represents the method that contains the business logic for receiving all resources. // // Example // type Model struct { // Name string `json:"name,omitempty"` // URL string `json:"url,omitempty"` // } // // type MyType struct { // Model // } // // func (m *MyType) GetAll(requestData interface{}) ([]interface{}, *HttpError) { // // do something // return []interface{}{ // Model{ // Name: "example", // URL: "example.url", // }, // }, nil // } GetAll(requestData interface{}) ([]interface{}, *HttpError) }
GetAllRoute provides the interface that must be implemented to create a Get All endpoint.
type GetAllRoutePath ¶
type GetAllRoutePath interface { // GetAllRoutePath represents an optional method that can be set to define a custom path for the get all route. // // Example: // type MyType struct {} // // func (m *MyType) GetAllRoutePath() string { // return "/get-all" // } GetAllRoutePath() string }
GetAllRoutePath defines an optional child interface that is used to customize route path.
type GetRoute ¶
type GetRoute interface { // Get represents the method that contains the business logic for receiving a resource. // // Example // type Model struct { // Name string `json:"name,omitempty"` // URL string `json:"url,omitempty"` // } // // type MyType struct { // Model // } // // func (m *MyType) Get(requestData interface{}) (interface{}, *HttpError) { // // do something // return Model{ // Name: "example", // URL: "example.url", // }, nil // } Get(requestData interface{}) (interface{}, *HttpError) }
GetRoute provides the interface that must be implemented to create a Get endpoint.
type GetRoutePath ¶
type GetRoutePath interface { // GetRoutePath represents an optional method that can be set to define a custom path for the get route. // // Example: // type MyType struct {} // // func (m *MyType) GetRoutePath() string { // return "/get-all" // } GetRoutePath() string }
GetRoutePath defines an optional child interface that is used to customize route path.
type Loggable ¶
type Loggable interface { Trace(format string, v ...interface{}) Debug(format string, v ...interface{}) Info(format string, v ...interface{}) Warn(format string, v ...interface{}) Error(format string, v ...interface{}) Fatal(format string, v ...interface{}) }
Loggable defines the interface to pass a logger to the RouteMachine. The logger can be used later by implementing the WithLogger interface.
type Middleware ¶
type Middleware interface { // Middleware represents the function that must be implemented to assign a new Middleware to the RouteMachine. // In most cases the implementation is similar to: // // Example: // func (m *MyType) Middleware(http.Handler) http.Handler { // return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // // do something // next.ServeHTTP(w, r) // }) // } Middleware(http.Handler) http.Handler }
Middleware defines an interface that is used to inject a middleware
type Parser ¶
type Parser interface { // Unmarshal parses the encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an error. // // Example: // func (m *JsonParser) Unmarshal(data []byte, v interface{}) error { // return json.Unmarshal(data, v) // } Unmarshal(data []byte, v interface{}) error // Marshal returns the encoded data as byte slice. // // Exaple: // func (m *JsonParser) Marshal(v interface{}) ([]byte, error) { // return json.Marshal(v) // } Marshal(v interface{}) ([]byte, error) // MimeType returns the associated mime type in string representation. // A list of available MIME types can be found at: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types // // Example: // func (m *JsonParser) MimeType() string { // return "application/json" // } MimeType() string }
Parser provides the interface that must be implemented to marshal and unmarshal the data sent during http request and http responses.
type PostRoute ¶
type PostRoute interface { // Post represents the method that contains the business logic for creating a resource. // // Example // type Model struct { // Name string `json:"name,omitempty"` // URL string `json:"url,omitempty"` // } // // type MyType struct { // Model // } // // func (m *MyType) Post(requestData interface{}) *HttpError { // // do something // fmt.Printf("%+v\n", requestData) // return nil // } Post(requestData interface{}) *HttpError }
PostRoute provides the interface that must be implemented to create a Post endpoint.
type PostRouteRoutePath ¶
type PostRouteRoutePath interface { // PostRoutePath represents an optional method that can be set to define a custom path for the post route. // // Example: // type MyType struct {} // // func (m *MyType) PostRoutePath() string { // return "/post" // } PostRoutePath() string }
PostRouteRoutePath defines an optional child interface that is used to customize route path.
type QueryParams ¶ added in v1.1.0
type RawRoute ¶ added in v1.0.2
type RawRoute interface { // Raw represents the method that does nothing for you. // Any logic must be handled by the user itself. // This type can be used when a functionality is missing by this framework. Raw(w http.ResponseWriter, r *http.Request) // HttpMethods must returns a slice of http methods procroute should use to register the route. // Example: // type MyType struct {} // // func (m *MyType) HttpMethods() string { // return []string{"GET","OPTIONS"} // } HttpMethods() []string }
RawRoute provides the interface that must be implemented to create a Raw endpoint.
type RawRouteRoutePath ¶ added in v1.0.2
type RawRouteRoutePath interface { // RawRoutePath represents an optional method that can be set to define a custom path for the raw route. // // Example: // type MyType struct {} // // func (m *MyType) RawRoutePath() string { // return "/raw" // } RawRoutePath() string }
RawRouteRoutePath defines an optional child interface that is used to customize route path.
type RouteMachine ¶
type RouteMachine struct {
// contains filtered or unexported fields
}
RouteMachine represents the manager type used to create and operate endpoints.
func NewRouteMachine ¶
func NewRouteMachine(addr string, port uint16, basePath string, loggable Loggable) *RouteMachine
NewRouteMachine is a constructor that creates a route machine based on the settings passed as parameters. If the port or loggable is not set correctly, you will get errors during execution.
func (*RouteMachine) AddMiddleware ¶
func (rm *RouteMachine) AddMiddleware(next Middleware) error
AddMiddleware injects a middleware just before an endpoint is touched.
func (*RouteMachine) AddRouteSet ¶
func (rm *RouteMachine) AddRouteSet(routeSet *RouteSet) error
AddRouteSet provides a method to register a new RouteSet within the route machine
func (*RouteMachine) SetIdleTimeout ¶
func (rm *RouteMachine) SetIdleTimeout(timeout time.Duration) *RouteMachine
SetIdleTimeout provides a method that changes the idle timeout within the http server
func (*RouteMachine) SetReadHeaderTimeout ¶
func (rm *RouteMachine) SetReadHeaderTimeout(timeout time.Duration) *RouteMachine
SetReadHeaderTimeout provides a method that changes the read header timeout within the http server
func (*RouteMachine) SetReadTimeout ¶
func (rm *RouteMachine) SetReadTimeout(timeout time.Duration) *RouteMachine
SetReadTimeout provides a method that changes the read timeout within the http server
func (*RouteMachine) Start ¶
func (rm *RouteMachine) Start() error
Start provides a method that starts a go routine with the http server
Possible errors:
- ErrAddressNotSet
- ErrRouteSetNotPresent
- socket related errors
func (*RouteMachine) Stop ¶
func (rm *RouteMachine) Stop() error
Stop delegates the stop signal to http.server.Shutdown
type RouteSet ¶
type RouteSet struct {
// contains filtered or unexported fields
}
RouteSet defines a structure that is used to create an endpoint set based on the base path
func NewRouteSet ¶
NewRouteSet defines a new route set that is used to genereate http endpoints
func (*RouteSet) AddRoutes ¶
AddRoutes provides a method that adds routes to the route set. When calling AddRoutes, ensure that your types do not overwrite each other.
Bad example:
type Model struct { Name string `json:"name,omitempty"` URL string `json:"url,omitempty"` } type MyType struct { Model } func (m *MyType) Type() interface{} { return &m.Model } func (m *MyType) Get() (interface{}, *HttpError) { // do something return Model{ Name: "example", URL: "example.url", }, nil } type MyType2 struct { Model } func (m *MyType2) Type() interface{} { return &m.Model } func (m *MyType2) Get() (interface{}, *HttpError) { // do something return Model{ Name: "example", URL: "example.url", }, nil } rs.AddRoutes(&MyType{}, &MyType2{})
type UpdateRoute ¶
type UpdateRoute interface { // Update represents the method that contains the business logic for updating a resource. // // Example // type Model struct { // Name string `json:"name,omitempty"` // URL string `json:"url,omitempty"` // } // // type MyType struct { // Model // } // // func (m *MyType) Update(requestData interface{}) *HttpError { // // do something // fmt.Printf("%+v\n", m.Model) // return nil // } Update(requestData interface{}) *HttpError }
UpdateRoute provides the interface that must be implemented to create an Update endpoint.
type UpdateRouteRoutePath ¶
type UpdateRouteRoutePath interface { // UpdateRoutePath represents an optional method that can be set to define a custom path for the update route. // // Example: // type MyType struct {} // // func (m *MyType) UpdateRoutePath() string { // return "/update" // } UpdateRoutePath() string }
UpdateRouteRoutePath defines an optional child interface that is used to customize route path.
type UrlParams ¶
type UrlParams interface { // SetUrlParam represents a method to pass url params that can be used later to identify resources. // // Example: // type MyType struct { // urlParams map[string]string // } // // func (m *MyType) SetUrlParams(args map[string]string) { // m.urlParams = args // } SetUrlParams(args map[string]string) }
UrlParams represents an interface that must be implemented if the route works with url parameters.
type WithLogger ¶
type WithLogger interface { // WithLogger represents the function that must be implemented to inject the Loggable object into the concrete implementation. // In most cases the implementation is similar to: // // Example: // func (m *MyType) WithLogger(lggbl Loggable) { // m.logger = lggbl // } WithLogger(Loggable) }
WithLogger provides an optional interface that is used to attach the logger to the route or middleware