server

package module
v0.0.0-...-e3ac997 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 26 Imported by: 0

README

flamingo.me/server

WIP / Open for Discussion / API not final nor even thought through...

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GrpcServletErrorLoggingServerOptions

func GrpcServletErrorLoggingServerOptions() []grpc.ServerOption

func Run

func Run(ctx context.Context, servlets ...Servlet)

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:

func RunWithOpentelemetry

func RunWithOpentelemetry(ctx context.Context, resource *resource.Resource, jaegerEndpoint string, servlets ...Servlet)

Types

type Listener

type Listener func() (net.Listener, error)

Listener function return either a net.Listener or an error, for use with e.g. grpcServers

func TcpListener

func TcpListener(addr string) Listener

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

func GrpcServerServlet(listener Listener, server *grpc.Server) Servlet

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

func HttpHealthcheckServlet(addr string) Servlet

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

func HttpServerServlet(server *http.Server) Servlet

HttpServerServlet provides a way to run an HTTP server as a servlet

func HttpServlet

func HttpServlet(addr string, mux http.Handler) Servlet

HttpServlet configures and runs a http server with the provided handler/mux

func ServletsServlet

func ServletsServlet(servlets ...Servlet) Servlet

ServletsServlet runs all provided servlets

func SlowServlet

func SlowServlet(initializer func() Servlet) Servlet

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.

Jump to

Keyboard shortcuts

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