server

package
v0.0.0-...-21d6f6e Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2016 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AuthOff = func() bool { return false }
	AuthOn  = func() bool { return true }
)
View Source
var (
	HttpRequestContextKey  httpRequestKey  = 1
	HttpResponseContextKey httpResponseKey = 2
	HttpStreamerContextKey httpStreamerKey = 3
	EngineContextKey       engineKey       = 4
	ApiDiscoveryContextKey apiDiscoveryKey = 4
)
View Source
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")
)
View Source
var (
	ErrNoServiceDefined = errors.New("no-service-defined")
	ErrNoWebhookDefined = errors.New("no-webhook-defined")

	WebhookHmacHeader = "X-Gohm-Hmac"
)
View Source
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 ContentTypeForRequest(req *http.Request) string

func ContentTypeForResponse

func ContentTypeForResponse(req *http.Request) string

func ContextGetHttpRequest

func ContextGetHttpRequest(ctx context.Context) *http.Request

func ContextGetHttpResponse

func ContextGetHttpResponse(ctx context.Context) http.ResponseWriter

func GetContentType

func GetContentType(req *http.Request) *string

func GetHttpHeaders

func GetHttpHeaders(req *http.Request, m HttpHeaders) (map[string][]string, error)

func GetUrlParameter

func GetUrlParameter(req *http.Request, key string) string

func HandleError

func HandleError(ctx context.Context, code int, message string)

func JSONContentType

func JSONContentType(req *http.Request) bool

func Marshal

func Marshal(resp http.ResponseWriter, req *http.Request, value interface{}) error

func NewService

func NewService() *serviceBuilder

func ReadPublicKeyPemFile

func ReadPublicKeyPemFile(filename string) func() []byte

func RunServer

func RunServer(server *http.Server) (chan<- int, <-chan error)

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.

func Start

func Start(port interface{}, endpoint http.Handler,
	onShutdown func() error, timeout time.Duration) (chan<- int, <-chan error)

func Unmarshal

func Unmarshal(resp http.ResponseWriter, req *http.Request, value interface{}) error

Types

type ApiDiscovery

type ApiDiscovery interface {
	ApiForScope() Endpoint
	ApiForFunc(func(context.Context, http.ResponseWriter, *http.Request)) Endpoint
}

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

func (*Auth) IsAuthOn

func (this *Auth) IsAuthOn() bool

func (*Auth) IsAuthorized

func (this *Auth) IsAuthorized(scope AuthScope, req *http.Request) (bool, *auth.Token, error)

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 AuthScope

type AuthScope string

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 ApiForScope

func ApiForScope(ctx context.Context) Endpoint

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.

func (Endpoint) Equals

func (sm Endpoint) Equals(other Endpoint) bool

type EventKey

type EventKey string

type EventKeyUrlMap

type EventKeyUrlMap map[string]Webhook

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 Handler

type Handler func(auth context.Context, resp http.ResponseWriter, req *http.Request)

type HttpHeaders

type HttpHeaders map[string]string

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

type ReverseProxy struct {
	http.Handler
	// contains filtered or unexported fields
}

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 Server

type Server interface {
	ApiDiscovery
	http.Handler
	Handle(string, http.Handler)
}

type ServerEvent

type ServerEvent struct {
	Timestamp time.Time
	Key       string
	Route     string
	Body      interface{}
}

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

func ContextGetStreamer(ctx context.Context) Streamer

type UrlQueries

type UrlQueries map[string]QueryDefault

func GetUrlQueries

func GetUrlQueries(req *http.Request, m UrlQueries) (UrlQueries, error)

type Webhook

type Webhook struct {
	Url       string `json:"destination_url"`
	AuthToken string `json:"auth_token,omitempty"`
}

func (*Webhook) Send

func (hook *Webhook) Send(message interface{}, templateString string) error

type WebhookManager

type WebhookManager interface {
	Send(service string, event string, message interface{}, templateString string) error
	RegisterWebhooks(service string, ekum EventKeyUrlMap) error
	RemoveWebhooks(service string) error
}

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

Jump to

Keyboard shortcuts

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