top

Package server is the bulk of the toolkit and relies on `server.Config` for any managing `Server` implementations. A server must implement the following interface:

// Server is the basic interface that defines what expect from any server.
type Server interface {
    Register(...Service) error
    Start() error
    Stop() error
}

The package offers 2 server implementations:

`SimpleServer`, which is capable of handling basic HTTP and JSON requests via 3 of the available `Service` implementations: `SimpleService`, `JSONService`, `ContextService`, `MixedService` and `MixedContextService`. A service and these implementations will be defined below.

`RPCServer`, which is capable of serving a gRPC server on one port and JSON endpoints on another. This kind of server can only handle the `RPCService` implementation.

The `Service` interface is minimal to allow for maximum flexibility:

type Service interface {
    Prefix() string

    // Middleware provides a hook for service-wide middleware
    Middleware(http.Handler) http.Handler
}

The 3 service types that are accepted and hostable on the `SimpleServer`:

type SimpleService interface {
    Service

    // router - method - func
    Endpoints() map[string]map[string]http.HandlerFunc
}

type JSONService interface {
    Service

    // Ensure that the route syntax is compatible with the router
    // implementation chosen in cfg.RouterType.
    // route - method - func
    JSONEndpoints() map[string]map[string]JSONEndpoint
    // JSONMiddleware provides a hook for service-wide middleware around JSONEndpoints.
    JSONMiddleware(JSONEndpoint) JSONEndpoint
}

type MixedService interface {
    Service

    // route - method - func
    Endpoints() map[string]map[string]http.HandlerFunc

    // Ensure that the route syntax is compatible with the router
    // implementation chosen in cfg.RouterType.
    // route - method - func
    JSONEndpoints() map[string]map[string]JSONEndpoint
    // JSONMiddleware provides a hook for service-wide middleware around JSONEndpoints.
    JSONMiddleware(JSONEndpoint) JSONEndpoint
}

type ContextService interface {
    Service

    // route - method - func
    ContextEndpoints() map[string]map[string]ContextHandlerFunc
    // ContextMiddleware provides a hook for service-wide middleware around ContextHandler
    ContextMiddleware(ContextHandler) ContextHandler
}

type MixedContextService interface {
    ContextService

    // route - method - func
    JSONEndpoints() map[string]map[string]JSONContextEndpoint
    JSONContextMiddleware(JSONContextEndpoint) JSONContextEndpoint
}

Where `JSONEndpoint`, `JSONContextEndpoint`, `ContextHandler` and `ContextHandlerFunc` are defined as:

type JSONEndpoint func(*http.Request) (int, interface{}, error)

type JSONContextEndpoint func(context.Context, *http.Request) (int, interface{}, error)

type ContextHandler interface {
    ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request)
}

type ContextHandlerFunc func(context.Context, http.ResponseWriter, *http.Request)

Also, the one service type that works with an `RPCServer`:

type RPCService interface {
    ContextService

    Service() (grpc.ServiceDesc, interface{})

    // Ensure that the route syntax is compatible with the router
    // implementation chosen in cfg.RouterType.
    // route - method - func
    JSONEndpoints() map[string]map[string]JSONContextEndpoint
    // JSONMiddleware provides a hook for service-wide middleware around JSONContextEndpoints.
    JSONMiddlware(JSONContextEndpoint) JSONContextEndpoint
}

The `Middleware(..)` functions offer each service a 'hook' to wrap each of its endpoints. This may be handy for adding additional headers or context to the request. This is also the point where other, third-party middleware could be easily be plugged in (ie. oauth, tracing, metrics, logging, etc.)

Examples

Check out the gizmo/examples/servers directory to see several reference implementations.

Imported by 48 package(s)

  1. github.com/NYTimes/gizmo/config/combined
  2. github.com/NYTimes/mock-ec2-metadata
  3. github.com/NYTimes/mock-ec2-metadata/service
  4. github.com/NYTimes/video-transcoding-api
  5. github.com/NYTimes/video-transcoding-api/config
  6. github.com/NYTimes/video-transcoding-api/service
  7. github.com/NYTimes/video-transcoding-api/swagger
  8. github.com/clawio/authentication/server
  9. github.com/clawio/data/server
  10. github.com/clawio/data/service
  11. github.com/clawio/metadata/server
  12. github.com/clawio/metadata/service
  13. github.com/coffeehc/gizmo/examples/pubsub/api-kafka-websocket-pubsub
  14. github.com/coffeehc/gizmo/examples/pubsub/api-sns-pub
  15. github.com/coffeehc/gizmo/examples/servers/json
  16. github.com/coffeehc/gizmo/examples/servers/mixed
  17. github.com/coffeehc/gizmo/examples/servers/mysql-saved-items
  18. github.com/coffeehc/gizmo/examples/servers/rpc
  19. github.com/coffeehc/gizmo/examples/servers/simple
  20. github.com/jprobinson/mock-ec2-metadata
  21. github.com/jprobinson/mock-ec2-metadata/service
  22. github.com/jprobinson/webserver/api/subway
  23. github.com/marcotroisi/gizmo/examples/pubsub/api-kafka-websocket-pubsub
  24. github.com/marcotroisi/gizmo/examples/pubsub/api-sns-pub
  25. github.com/marcotroisi/gizmo/examples/servers/json
  26. github.com/marcotroisi/gizmo/examples/servers/mixed
  27. github.com/marcotroisi/gizmo/examples/servers/mysql-saved-items
  28. github.com/marcotroisi/gizmo/examples/servers/rpc
  29. github.com/marcotroisi/gizmo/examples/servers/simple
  30. github.com/nytimes/mock-ec2-metadata
  31. github.com/nytimes/mock-ec2-metadata/service
  32. github.com/slok/khronos/service
  33. github.com/stuartweir/gizmo/examples/pubsub/api-kafka-websocket-pubsub
  34. github.com/stuartweir/gizmo/examples/pubsub/api-sns-pub
  35. github.com/stuartweir/gizmo/examples/servers/json
  36. github.com/stuartweir/gizmo/examples/servers/mixed
  37. github.com/stuartweir/gizmo/examples/servers/mysql-saved-items
  38. github.com/stuartweir/gizmo/examples/servers/rpc
  39. github.com/stuartweir/gizmo/examples/servers/simple
  40. github.com/tossp/gizmo/examples/pubsub/api-kafka-websocket-pubsub
  41. github.com/tossp/gizmo/examples/pubsub/api-sns-pub
  42. github.com/tossp/gizmo/examples/servers/json
  43. github.com/tossp/gizmo/examples/servers/mixed
  44. github.com/tossp/gizmo/examples/servers/mysql-saved-items
  45. github.com/tossp/gizmo/examples/servers/rpc
  46. github.com/tossp/gizmo/examples/servers/simple
  47. github.com/tsuru/riakapi/cmd
  48. github.com/tsuru/riakapi/service

Imported only in test by 1 package(s)

  1. github.com/clawio/authentication/service

Imports 17 package(s)

  1. github.com/Sirupsen/logrus
  2. github.com/nu7hatch/gouuid
  3. google.golang.org/grpc/metadata
  4. google.golang.org/appengine
  5. github.com/go-kit/kit/metrics
  6. github.com/gorilla/handlers
  7. github.com/gorilla/mux
  8. github.com/NYTimes/logrotate
  9. github.com/go-kit/kit/metrics/provider
  10. github.com/NYTimes/gizmo/web
  11. github.com/gorilla/context
  12. github.com/NYTimes/gizmo/config
  13. golang.org/x/net/context
  14. github.com/julienschmidt/httprouter
  15. google.golang.org/grpc
  16. github.com/NYTimes/gizmo/config/metrics
  17. github.com/prometheus/client_golang/prometheus