Documentation
¶
Index ¶
- Variables
- func ContentTypeForRequest(req *http.Request) string
- func ContentTypeForResponse(req *http.Request) string
- func ContextGetHttpRequest(ctx context.Context) *http.Request
- func ContextGetHttpResponse(ctx context.Context) http.ResponseWriter
- func GetContentType(req *http.Request) *string
- func GetHttpHeaders(req *http.Request, m HttpHeaders) (map[string][]string, error)
- func GetUrlParameter(req *http.Request, key string) string
- func HandleError(ctx context.Context, code int, message string)
- func JSONContentType(req *http.Request) bool
- func Marshal(resp http.ResponseWriter, req *http.Request, value interface{}) error
- func NewService() *serviceBuilder
- func ReadPublicKeyPemFile(filename string) func() []byte
- func RunServer(server *http.Server) (chan<- int, <-chan error)
- func Start(port interface{}, endpoint http.Handler, onShutdown func() error, ...) (chan<- int, <-chan error)
- func Unmarshal(resp http.ResponseWriter, req *http.Request, value interface{}) error
- type ApiDiscovery
- type Auth
- type AuthManager
- type AuthScope
- type Endpoint
- type EventKey
- type EventKeyUrlMap
- type FormParams
- type Handler
- type HttpHeaders
- type HttpMethod
- type QueryDefault
- type ReverseProxy
- func (this *ReverseProxy) ServeHTTP(resp http.ResponseWriter, req *http.Request)
- func (this *ReverseProxy) SetForwardHost(h string) *ReverseProxy
- func (this *ReverseProxy) SetForwardHostPort(hp string) *ReverseProxy
- func (this *ReverseProxy) SetForwardPort(p int) *ReverseProxy
- func (this *ReverseProxy) SetForwardPrefix(p string) *ReverseProxy
- func (this *ReverseProxy) SetForwardScheme(s string) *ReverseProxy
- func (this *ReverseProxy) Strip(s string) *ReverseProxy
- type Server
- type ServerEvent
- type Streamer
- type UrlQueries
- type Webhook
- type WebhookManager
- type WebhookMap
- func (this *WebhookMap) FromJSON(s []byte) error
- func (this WebhookMap) RegisterWebhooks(serviceKey string, ekum EventKeyUrlMap) error
- func (this WebhookMap) RemoveWebhooks(serviceKey string) error
- func (this WebhookMap) Send(serviceKey, eventKey string, message interface{}, templateString string) error
- func (this *WebhookMap) ToJSON() []byte
Constants ¶
This section is empty.
Variables ¶
var ( AuthOff = func() bool { return false } AuthOn = func() bool { return true } )
var ( HttpRequestContextKey httpRequestKey = 1 HttpResponseContextKey httpResponseKey = 2 HttpStreamerContextKey httpStreamerKey = 3 EngineContextKey engineKey = 4 ApiDiscoveryContextKey apiDiscoveryKey = 4 )
var ( ErrMissingInput = errors.New("err-missing-input") ErrUnknownMethod = errors.New("err-unknown-method") ErrNotSupportedUrlParameterType = errors.New("err-not-supported-url-query-param-type") ErrNoHttpHeaderSpec = errors.New("err-no-http-header-spec") ErrNoSignKey = errors.New("err-no-sign-key") ErrNoVerifyKey = errors.New("err-no-verify-key") ErrInvalidAuthToken = errors.New("err-invalid-token") ErrExpiredAuthToken = errors.New("err-token-expired") ErrNoAuthToken = errors.New("err-no-auth-token") ErrBadContentType = errors.New("err-bad-content-type") )
var ( ErrNoServiceDefined = errors.New("no-service-defined") ErrNoWebhookDefined = errors.New("no-webhook-defined") WebhookHmacHeader = "X-Gohm-Hmac" )
var ( DefaultErrorRenderer = func(resp http.ResponseWriter, req *http.Request, message string, code int) error { resp.WriteHeader(code) escaped := message if len(message) > 0 { escaped = strings.Replace(message, "\"", "'", -1) } ct := content_type_for_response(req) switch ct { case "application/json": resp.Write([]byte(fmt.Sprintf("{ \"error\": \"%s\" }", escaped))) case "application/protobuf": default: resp.Write([]byte(fmt.Sprintf("<html><body>Error: %s </body></html>", escaped))) } return nil } )
Functions ¶
func ContentTypeForRequest ¶
func ContentTypeForResponse ¶
func ContextGetHttpResponse ¶
func ContextGetHttpResponse(ctx context.Context) http.ResponseWriter
func GetContentType ¶
func GetHttpHeaders ¶
func JSONContentType ¶
func NewService ¶
func NewService() *serviceBuilder
func ReadPublicKeyPemFile ¶
func RunServer ¶
Runs the http server. This server offers more control than the standard go's default http server in that when a 'true' is sent to the stop channel, the listener is closed to force a clean shutdown. The return value is a channel that can be used to block on. An error is received if server shuts down in error; or a nil is received on a clean signalled shutdown.
Types ¶
type ApiDiscovery ¶
type Auth ¶
type Auth struct { IsAuthOnFunc func() bool GetTimeFunc func() time.Time VerifyKeyFunc func() []byte ErrorRenderFunc func(http.ResponseWriter, *http.Request, string, int) error InterceptAuthFunc func(bool, context.Context) (bool, context.Context) }
func (Auth) Init ¶
func (data Auth) Init() AuthManager
type AuthManager ¶
type AuthManager interface { IsAuthOn() bool IsAuthorized(AuthScope, *http.Request) (bool, *auth.Token, error) // contains filtered or unexported methods }
func DisableAuth ¶
func DisableAuth() AuthManager
type Endpoint ¶
type Endpoint struct { Doc string `json:"doc,omitempty"` UrlRoute string `json:"route,omitempty"` HttpHeaders HttpHeaders `json:"headers,omitempty"` HttpMethod HttpMethod `json:"method,omitempty"` HttpMethods []HttpMethod `json:"methods,omitempty"` UrlQueries UrlQueries `json:"queries,omitempty"` FormParams FormParams `json:"params,omitempty"` ContentType encoding.ContentType `json:"contentType,omitempty"` RequestBody func(*http.Request) interface{} `json:"requestBody,omitempty"` ResponseBody func(*http.Request) interface{} `json:"responseBody,omitempty"` CallbackEvent EventKey `json:"callbackEvent,omitempty"` CallbackBodyTemplate string `json:"callbackBody,omitempty"` AuthScope AuthScope `json:"scope,omitempty"` }
func ApiForFunc ¶
func ApiForScope ¶
If the scope of the caller of this function is the scope of a function bound to the Endpoint of the api, then return that Api according to the binding. Otherwise, return NotDefined.
type EventKeyUrlMap ¶
Webhook callbacks
func (*EventKeyUrlMap) FromJSON ¶
func (this *EventKeyUrlMap) FromJSON(s []byte) error
func (*EventKeyUrlMap) ToJSON ¶
func (this *EventKeyUrlMap) ToJSON() []byte
type FormParams ¶
type FormParams UrlQueries
func GetPostForm ¶
func GetPostForm(req *http.Request, m FormParams) (FormParams, error)
type HttpHeaders ¶
type HttpMethod ¶
type HttpMethod string
var ( NotDefined = Endpoint{} AuthScopeNone = AuthScope("*") HEAD HttpMethod = HttpMethod("HEAD") PATCH HttpMethod = HttpMethod("PATCH") GET HttpMethod = HttpMethod("GET") POST HttpMethod = HttpMethod("POST") PUT HttpMethod = HttpMethod("PUT") DELETE HttpMethod = HttpMethod("DELETE") MULTIPART HttpMethod = HttpMethod("POST") )
type QueryDefault ¶
type QueryDefault interface{}
type ReverseProxy ¶
Simple, single-host reverse proxy. This assumes that the backend information can be somehow determined and that there are some simple url stripping that takes place. See the test case for example usage to build a trivial reverse proxy that can optionally support token authentication.
func NewReverseProxy ¶
func NewReverseProxy() *ReverseProxy
func (*ReverseProxy) ServeHTTP ¶
func (this *ReverseProxy) ServeHTTP(resp http.ResponseWriter, req *http.Request)
func (*ReverseProxy) SetForwardHost ¶
func (this *ReverseProxy) SetForwardHost(h string) *ReverseProxy
func (*ReverseProxy) SetForwardHostPort ¶
func (this *ReverseProxy) SetForwardHostPort(hp string) *ReverseProxy
func (*ReverseProxy) SetForwardPort ¶
func (this *ReverseProxy) SetForwardPort(p int) *ReverseProxy
func (*ReverseProxy) SetForwardPrefix ¶
func (this *ReverseProxy) SetForwardPrefix(p string) *ReverseProxy
func (*ReverseProxy) SetForwardScheme ¶
func (this *ReverseProxy) SetForwardScheme(s string) *ReverseProxy
func (*ReverseProxy) Strip ¶
func (this *ReverseProxy) Strip(s string) *ReverseProxy
type ServerEvent ¶
type Streamer ¶
type Streamer interface { EventChannel() chan<- *ServerEvent StreamChannel(contentType, eventType, key string) (*sseChannel, bool) MergeHttpStream(w http.ResponseWriter, r *http.Request, contentType, eventType, key string, src <-chan interface{}) error DirectHttpStream(http.ResponseWriter, *http.Request) (chan<- interface{}, error) }
func ContextGetStreamer ¶
type UrlQueries ¶
type UrlQueries map[string]QueryDefault
func GetUrlQueries ¶
func GetUrlQueries(req *http.Request, m UrlQueries) (UrlQueries, error)
type Webhook ¶
type WebhookManager ¶
type WebhookMap ¶
type WebhookMap map[string]EventKeyUrlMap
func (*WebhookMap) FromJSON ¶
func (this *WebhookMap) FromJSON(s []byte) error
func (WebhookMap) RegisterWebhooks ¶
func (this WebhookMap) RegisterWebhooks(serviceKey string, ekum EventKeyUrlMap) error
func (WebhookMap) RemoveWebhooks ¶
func (this WebhookMap) RemoveWebhooks(serviceKey string) error
func (WebhookMap) Send ¶
func (this WebhookMap) Send(serviceKey, eventKey string, message interface{}, templateString string) error
Default in-memory implementation
func (*WebhookMap) ToJSON ¶
func (this *WebhookMap) ToJSON() []byte