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 102 package(s)

  1. github.com/NYTimes/gizmo/config/combined
  2. github.com/NYTimes/gizmo/examples/pubsub/api-kafka-websocket-pubsub/service
  3. github.com/NYTimes/gizmo/examples/pubsub/api-sns-pub/service
  4. github.com/NYTimes/gizmo/examples/servers/json
  5. github.com/NYTimes/gizmo/examples/servers/json/service
  6. github.com/NYTimes/gizmo/examples/servers/mixed
  7. github.com/NYTimes/gizmo/examples/servers/mixed/service
  8. github.com/NYTimes/gizmo/examples/servers/mysql-saved-items
  9. github.com/NYTimes/gizmo/examples/servers/mysql-saved-items/service
  10. github.com/NYTimes/gizmo/examples/servers/rpc
  11. github.com/NYTimes/gizmo/examples/servers/rpc/client
  12. github.com/NYTimes/gizmo/examples/servers/rpc/service
  13. github.com/NYTimes/gizmo/examples/servers/simple
  14. github.com/NYTimes/gizmo/examples/servers/simple/service
  15. github.com/NYTimes/mock-ec2-metadata
  16. github.com/NYTimes/mock-ec2-metadata/service
  17. github.com/NYTimes/video-transcoding-api
  18. github.com/NYTimes/video-transcoding-api/config
  19. github.com/NYTimes/video-transcoding-api/service
  20. github.com/NYTimes/video-transcoding-api/swagger
  21. github.com/clawio/authentication/server
  22. github.com/clawio/data/server
  23. github.com/clawio/data/service
  24. github.com/clawio/metadata/server
  25. github.com/clawio/metadata/service
  26. github.com/coffeehc/gizmo/examples/pubsub/api-kafka-websocket-pubsub
  27. github.com/coffeehc/gizmo/examples/pubsub/api-kafka-websocket-pubsub/service
  28. github.com/coffeehc/gizmo/examples/pubsub/api-sns-pub
  29. github.com/coffeehc/gizmo/examples/pubsub/api-sns-pub/service
  30. github.com/coffeehc/gizmo/examples/servers/json
  31. github.com/coffeehc/gizmo/examples/servers/json/service
  32. github.com/coffeehc/gizmo/examples/servers/mixed
  33. github.com/coffeehc/gizmo/examples/servers/mixed/service
  34. github.com/coffeehc/gizmo/examples/servers/mysql-saved-items
  35. github.com/coffeehc/gizmo/examples/servers/mysql-saved-items/service
  36. github.com/coffeehc/gizmo/examples/servers/rpc
  37. github.com/coffeehc/gizmo/examples/servers/rpc/client
  38. github.com/coffeehc/gizmo/examples/servers/rpc/service
  39. github.com/coffeehc/gizmo/examples/servers/simple
  40. github.com/coffeehc/gizmo/examples/servers/simple/service
  41. github.com/cristiangraz/gizmo/config/combined
  42. github.com/fsouza/ctxlogger/examples/cats
  43. github.com/jprobinson/mock-ec2-metadata
  44. github.com/jprobinson/mock-ec2-metadata/service
  45. github.com/jprobinson/webserver/api/subway
  46. github.com/lucindo/video-transcoding-api
  47. github.com/marcotroisi/gizmo/examples/pubsub/api-kafka-websocket-pubsub
  48. github.com/marcotroisi/gizmo/examples/pubsub/api-kafka-websocket-pubsub/service
  49. github.com/marcotroisi/gizmo/examples/pubsub/api-sns-pub
  50. github.com/marcotroisi/gizmo/examples/pubsub/api-sns-pub/service
  51. github.com/marcotroisi/gizmo/examples/servers/json
  52. github.com/marcotroisi/gizmo/examples/servers/json/service
  53. github.com/marcotroisi/gizmo/examples/servers/mixed
  54. github.com/marcotroisi/gizmo/examples/servers/mixed/service
  55. github.com/marcotroisi/gizmo/examples/servers/mysql-saved-items
  56. github.com/marcotroisi/gizmo/examples/servers/mysql-saved-items/service
  57. github.com/marcotroisi/gizmo/examples/servers/rpc
  58. github.com/marcotroisi/gizmo/examples/servers/rpc/client
  59. github.com/marcotroisi/gizmo/examples/servers/rpc/service
  60. github.com/marcotroisi/gizmo/examples/servers/simple
  61. github.com/marcotroisi/gizmo/examples/servers/simple/service
  62. github.com/nytimes/gizmo/examples/pubsub/api-kafka-websocket-pubsub/service
  63. github.com/nytimes/gizmo/examples/pubsub/api-sns-pub/service
  64. github.com/nytimes/gizmo/examples/servers/json/service
  65. github.com/nytimes/gizmo/examples/servers/mixed/service
  66. github.com/nytimes/gizmo/examples/servers/rpc/service
  67. github.com/nytimes/gizmo/examples/servers/simple/service
  68. github.com/nytimes/mock-ec2-metadata
  69. github.com/nytimes/mock-ec2-metadata/service
  70. github.com/slok/khronos/service
  71. github.com/stuartweir/gizmo/examples/pubsub/api-kafka-websocket-pubsub
  72. github.com/stuartweir/gizmo/examples/pubsub/api-kafka-websocket-pubsub/service
  73. github.com/stuartweir/gizmo/examples/pubsub/api-sns-pub
  74. github.com/stuartweir/gizmo/examples/pubsub/api-sns-pub/service
  75. github.com/stuartweir/gizmo/examples/servers/json
  76. github.com/stuartweir/gizmo/examples/servers/json/service
  77. github.com/stuartweir/gizmo/examples/servers/mixed
  78. github.com/stuartweir/gizmo/examples/servers/mixed/service
  79. github.com/stuartweir/gizmo/examples/servers/mysql-saved-items
  80. github.com/stuartweir/gizmo/examples/servers/mysql-saved-items/service
  81. github.com/stuartweir/gizmo/examples/servers/rpc
  82. github.com/stuartweir/gizmo/examples/servers/rpc/client
  83. github.com/stuartweir/gizmo/examples/servers/rpc/service
  84. github.com/stuartweir/gizmo/examples/servers/simple
  85. github.com/stuartweir/gizmo/examples/servers/simple/service
  86. github.com/tossp/gizmo/examples/pubsub/api-kafka-websocket-pubsub
  87. github.com/tossp/gizmo/examples/pubsub/api-kafka-websocket-pubsub/service
  88. github.com/tossp/gizmo/examples/pubsub/api-sns-pub
  89. github.com/tossp/gizmo/examples/pubsub/api-sns-pub/service
  90. github.com/tossp/gizmo/examples/servers/json
  91. github.com/tossp/gizmo/examples/servers/json/service
  92. github.com/tossp/gizmo/examples/servers/mixed
  93. github.com/tossp/gizmo/examples/servers/mixed/service
  94. github.com/tossp/gizmo/examples/servers/mysql-saved-items
  95. github.com/tossp/gizmo/examples/servers/mysql-saved-items/service
  96. github.com/tossp/gizmo/examples/servers/rpc
  97. github.com/tossp/gizmo/examples/servers/rpc/client
  98. github.com/tossp/gizmo/examples/servers/rpc/service
  99. github.com/tossp/gizmo/examples/servers/simple
  100. github.com/tossp/gizmo/examples/servers/simple/service
  101. github.com/tsuru/riakapi/cmd
  102. github.com/tsuru/riakapi/service

Imported only in test by 2 package(s)

  1. github.com/NYTimes/gizmo/server/kit
  2. 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