Documentation ¶
Index ¶
- func GrpcServletErrorLoggingServerOptions() []grpc.ServerOption
- func Run(ctx context.Context, servlets ...Servlet)
- func RunWithOpentelemetry(ctx context.Context, resource *resource.Resource, jaegerEndpoint string, ...)
- type Listener
- type Servlet
- func GrpcServerServlet(listener Listener, server *grpc.Server) Servlet
- func GrpcServlet(listener Listener, configure func(server *grpc.Server) error, ...) Servlet
- func HttpHealthcheckServlet(addr string) Servlet
- func HttpServerServlet(server *http.Server) Servlet
- func HttpServlet(addr string, mux http.Handler) Servlet
- func ServletsServlet(servlets ...Servlet) Servlet
- func SlowServlet(initializer func() Servlet) Servlet
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GrpcServletErrorLoggingServerOptions ¶
func GrpcServletErrorLoggingServerOptions() []grpc.ServerOption
func Run ¶
Run runs a all provided servlets, until either - the incoming context is cancelled - a os.Interrupt or syscall.SIGTERM signal is received - one servlets fails once one of the events happen, Run will signal all Servlets to gracefully shut down, and after a timeout force-stop all Servlets. An interrupt/term signal during shutdown stops the whole server.
Example ¶
package main import ( "net/http" "time" "flamingo.me/server" "golang.org/x/net/context" "google.golang.org/grpc" ) func main() { ctx := context.Background() http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {}) http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {}) server.Run( ctx, server.GrpcServlet(server.TcpListener(":50051"), func(server *grpc.Server) error { return nil }, nil), server.HttpServlet(":8080", nil), server.SlowServlet(func() server.Servlet { mux := http.NewServeMux() mux.HandleFunc("/mux/foo", func(w http.ResponseWriter, r *http.Request) {}) mux.HandleFunc("/mux/bar", func(w http.ResponseWriter, r *http.Request) {}) time.Sleep(1 * time.Second) // slow initalization return server.HttpServlet(":8081", mux) }), server.HttpHealthcheckServlet(":18080"), ) }
Output:
Types ¶
type Listener ¶
Listener function return either a net.Listener or an error, for use with e.g. grpcServers
func TcpListener ¶
TcpListener creates a net.Listener on a tcp port
type Servlet ¶
type Servlet func(ctx context.Context, ready chan<- struct{}, gracefulStop <-chan struct{}, errorsC chan<- error) error
Servlet is a function which is supposed to run forever and graceful stop once the gracefulStop channel is closed any return (either error or no error) let's the server graceful stop all other servlets, and finally close
func GrpcServerServlet ¶
GrpcServerServlet runs a grpc.Server instance with a given listener
func GrpcServlet ¶
func GrpcServlet(listener Listener, configure func(server *grpc.Server) error, opts func() []grpc.ServerOption) Servlet
GrpcServlet provides a setup grpc server for usage with grpc services
func HttpHealthcheckServlet ¶
HttpHealthcheckServlet provides
- /health/live with an OK response, or FAIL+412 if the context was cancelled
- /health/ready with either an OK response, or FAIL+412 once a the graceful shutdown is requested or the context was cancelled
func HttpServerServlet ¶
HttpServerServlet provides a way to run an HTTP server as a servlet
func HttpServlet ¶
HttpServlet configures and runs a http server with the provided handler/mux
func ServletsServlet ¶
ServletsServlet runs all provided servlets
func SlowServlet ¶
SlowServlet calls the initializer, which in turn returns the actual servlet required to run the service. This can be useful is initialization might need time, such as waiting for an external service to be available. The healthcheck will report a non-ready status until all servlets are running.