Documentation ¶
Overview ¶
Package sherpa exports your Go functions as fully documented sherpa web API's.
Sherpa is similar to JSON-RPC, but discoverable and self-documenting. Read more at https://www.ueber.net/who/mjl/sherpa/.
Use sherpa.NewHandler to export Go functions using a http.Handler. An example of how to use NewHandler can be found in https://github.com/mjl-/sherpaweb/
Index ¶
Constants ¶
const ( SherpaBadResponse = "sherpa:badResponse" // Bad response from server, e.g. JSON response body could not be parsed. SherpaHTTPError = "sherpa:http" // Unexpected http response status code from server. SherpaNoAPI = "sherpa:noAPI" // No API was found at this URL. )
Errors generated by clients
const ( SherpaBadRequest = "sherpa:badRequest" // Error parsing JSON request body. SherpaBadParams = "sherpa:badParams" // Wrong number of parameters in function call. )
Errors generated by servers
const (
SherpaBadFunction = "sherpa:badFunction" // Function does not exist at server.
)
Errors generated by both clients and servers
const SherpaVersion = 1
SherpaVersion is the version of the Sherpa protocol this package implements. Sherpa is at version 1.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶
func NewHandler(path string, version string, api interface{}, doc *sherpadoc.Section, opts *HandlerOpts) (http.Handler, error)
NewHandler returns a new http.Handler that serves all Sherpa API-related requests.
Path is the path this API is available at.
Version should be a semantic version.
API should by a struct. It represents the root section. All methods of a section are exported as sherpa functions. All fields must be other sections (structs) whose methods are also exported. recursively. Method names must start with an uppercase character to be exported, but their exported names start with a lowercase character by default (but see HandlerOpts.AdjustFunctionNames).
Doc is documentation for the top-level sherpa section, as generated by sherpadoc.
Opts allows further configuration of the handler.
Methods on the exported sections are exported as Sherpa functions. If the first parameter of a method is a context.Context, the context from the HTTP request is passed. This lets you abort work if the HTTP request underlying the function call disappears.
Parameters and return values for exported functions are automatically converted from/to JSON. If the last element of a return value (if any) is an error, that error field is taken to indicate whether the call succeeded. Exported functions can also panic with an *Error or *InternalServerError to indicate a failed function call. Returning an error with a Code starting with "server" indicates an implementation error, which will be logged through the collector.
Variadic functions can be called, but in the call (from the client), the variadic parameters must be passed in as an array.
This handler strips "path" from the request.
Types ¶
type Collector ¶ added in v0.2.0
type Collector interface { ProtocolError() // Invalid request at protocol-level, e.g. wrong mimetype or request body. BadFunction() // Function does not exist. JavaScript() // Sherpa.js is requested. JSON() // Sherpa.json is requested. // Call of function, how long it took, and in case of failure, the error code. FunctionCall(name string, durationSec float64, errorCode string) }
Collector facilitates collection of metrics. Functions are called by the library as such events or errors occur. See https://github.com/irias/sherpa-prometheus-collector for an implementation for prometheus.
type Error ¶
Error returned by a function called through a sherpa API. Message is a human-readable error message. Code is optional, it can be used to handle errors programmatically.
type HandlerOpts ¶ added in v0.6.0
type HandlerOpts struct { // Holds functions for collecting metrics about function calls and other incoming // HTTP requests. May be nil. Collector Collector // If enabled, incoming sherpa function calls will ignore unrecognized fields in // struct parameters, instead of failing. LaxParameterParsing bool // If empty, only the first character of function names are lower cased. For // "lowerWord", the first string of capitals is lowercased, for "none", the // function name is left as is. AdjustFunctionNames string // Don't send any CORS headers, and respond to OPTIONS requests with 405 "bad // method". NoCORS bool }
HandlerOpts are options for creating a new handler.
type Int64s ¶ added in v0.6.0
type Int64s int64
Int64s is an int64 that can be read as either a JSON string or JSON number, to be used in sherpa function parameters for compatibility with JavaScript. For struct fields, use the "json:,string" struct tag instead.
func (*Int64s) MarshalJSON ¶ added in v0.6.0
MarshalJSON returns a JSON-string-encoding of the int64.
func (*Int64s) UnmarshalJSON ¶ added in v0.6.0
UnmarshalJSON parses JSON into the int64. Both a string encoding as a number encoding are allowed. JavaScript clients must use the string encoding because the number encoding loses precision at 1<<53.
type InternalServerError ¶ added in v0.0.3
InternalServerError is an error that propagates as an HTTP internal server error (HTTP status 500), instead of returning a regular HTTP status 200 OK with the error message in the response body. Useful for making Sherpa endpoints that can be monitored by simple HTTP monitoring tools.
func (*InternalServerError) Error ¶ added in v0.0.3
func (e *InternalServerError) Error() string
type JSON ¶ added in v0.6.0
type JSON struct { ID string `json:"id"` Title string `json:"title"` Functions []string `json:"functions"` BaseURL string `json:"baseurl"` Version string `json:"version"` SherpaVersion int `json:"sherpaVersion"` SherpadocVersion int `json:"sherpadocVersion"` }
JSON holds all fields for a request to sherpa.json.
type Raw ¶ added in v0.6.5
type Raw []byte
Raw signals a raw JSON response. If a handler panics with this type, the raw bytes are sent (with regular response headers). Can be used to skip the json encoding from the handler, eg for caching, or when you read a properly formatted JSON document from a file or database. By using panic to signal a raw JSON response, the return types stay intact for sherpadoc to generate documentation from.
type Uint64s ¶ added in v0.6.0
type Uint64s uint64
Uint64s is an uint64 that can be read as either a JSON string or JSON number, to be used in sherpa function parameters for compatibility with JavaScript. For struct fields, use the "json:,string" struct tag instead.
func (*Uint64s) MarshalJSON ¶ added in v0.6.0
MarshalJSON returns a JSON-string-encoding of the uint64.
func (*Uint64s) UnmarshalJSON ¶ added in v0.6.0
UnmarshalJSON parses JSON into the uint64. Both a string encoding as a number encoding are allowed. JavaScript clients must use the string encoding because the number encoding loses precision at 1<<53.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
sherpaclient
Sherpaclient calls Sherpa API functions and prints Sherpa API documentation from the command-line.
|
Sherpaclient calls Sherpa API functions and prints Sherpa API documentation from the command-line. |