micro

package module
v2.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2021 License: MIT Imports: 21 Imported by: 1

README

micro

GoDoc Build Status codecov Release

Microservice prototype with gRPC + Restful + Redoc + Prometheus.

Require GO version >= v1.13

Documentation

Index

Constants

This section is empty.

Variables

InterruptSignals are the default interrupt signals to catch

Functions

func DefaultHTTPHandler

func DefaultHTTPHandler(mux *runtime.ServeMux) http.Handler

DefaultHTTPHandler is the default http handler which does nothing

Types

type AnnotatorFunc

type AnnotatorFunc func(context.Context, *http.Request) metadata.MD

AnnotatorFunc is the annotator function is for injecting meta data from http request into gRPC context

type HTTPHandlerFunc

type HTTPHandlerFunc func(*runtime.ServeMux) http.Handler

HTTPHandlerFunc is the http middleware handler function

type Logger

type Logger interface {
	Printf(string, ...interface{})
}

Logger is logger interface

type LoggerFunc

type LoggerFunc func(string, ...interface{})

LoggerFunc is a bridge between Logger and any third party logger

func (LoggerFunc) Printf

func (f LoggerFunc) Printf(msg string, args ...interface{})

Printf implements Logger interface

type Option

type Option func(s *Service)

Option is service functional option

See this post about the "functional options" pattern: http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

func Annotator

func Annotator(annotator AnnotatorFunc) Option

Annotator returns an Option to append an annotator

func ErrorHandler

func ErrorHandler(errorHandler runtime.ErrorHandlerFunc) Option

ErrorHandler returns an Option to set the errorHandler

func GRPCDialOption

func GRPCDialOption(dialOption grpc.DialOption) Option

GRPCDialOption returns an Option to append a gRPC dial option

func GRPCServerOption

func GRPCServerOption(serverOption grpc.ServerOption) Option

GRPCServerOption returns an Option to append a gRPC server option

func HTTPHandler

func HTTPHandler(httpHandler HTTPHandlerFunc) Option

HTTPHandler returns an Option to set the httpHandler

func InterruptSignal

func InterruptSignal(signal os.Signal) Option

InterruptSignal returns an Option to append a interrupt signal

func MuxOption

func MuxOption(muxOption runtime.ServeMuxOption) Option

MuxOption returns an Option to append a mux option

func PreShutdownDelay

func PreShutdownDelay(timeout time.Duration) Option

PreShutdownDelay returns an Option to set the time waiting for running goroutines to finish their jobs before the shutdown starts

func Redoc

func Redoc(redoc *RedocOpts) Option

Redoc returns an Option to set the Redoc

func RouteOpt

func RouteOpt(route Route) Option

RouteOpt returns an Option to append a route

func ShutdownFunc

func ShutdownFunc(f func()) Option

ShutdownFunc returns an Option to register a function which will be called when server shutdown

func ShutdownTimeout

func ShutdownTimeout(timeout time.Duration) Option

ShutdownTimeout returns an Option to set the timeout before the server shutdown abruptly

func StaticDir

func StaticDir(staticDir string) Option

StaticDir returns an Option to set the staticDir

func StreamInterceptor

func StreamInterceptor(streamInterceptor grpc.StreamServerInterceptor) Option

StreamInterceptor returns an Option to append an streamInterceptor

func UnaryInterceptor

func UnaryInterceptor(unaryInterceptor grpc.UnaryServerInterceptor) Option

UnaryInterceptor returns an Option to append an unaryInterceptor

func WithHTTPServer

func WithHTTPServer(server *http.Server) Option

WithHTTPServer returns an Option to set the http server, note that the Addr and Handler will be reset in startGRPCGateway(), so you are not able to specify them

func WithLogger

func WithLogger(logger Logger) Option

WithLogger uses the provided logger

type RedocOpts

type RedocOpts struct {
	// Route is the route in http server, should include / at the beginning, default is /docs.
	// Currently it can not be root route "/", see https://github.com/grpc-ecosystem/grpc-gateway/issues/1909
	Route string
	// SpecURLs are the urls to find the spec for, format: name -> url
	SpecURLs map[string]string
	// RedocURL is the js that generates the redoc site, defaults to: https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js
	RedocURL string
	// Title is the page title, default to: API documentation
	Title string
	// Up is whether to boot up the redoc endpoints
	Up bool
}

RedocOpts is configures for redoc

func (*RedocOpts) AddSpec

func (redoc *RedocOpts) AddSpec(name, url string) *RedocOpts

AddSpec adds a spec url with name

func (*RedocOpts) EnsureDefaults added in v2.1.3

func (redoc *RedocOpts) EnsureDefaults()

EnsureDefaults sets default redoc options

func (*RedocOpts) Serve

func (redoc *RedocOpts) Serve(w http.ResponseWriter, r *http.Request, pathParams map[string]string)

Serve is the HandlerFunc for Redoc

type ReverseProxyFunc

type ReverseProxyFunc func(ctx context.Context, mux *runtime.ServeMux, grpcHostAndPort string, opts []grpc.DialOption) error

ReverseProxyFunc is the callback that the caller should implement to steps to reverse-proxy the HTTP/1 requests to gRPC

type Route

type Route struct {
	Method  string
	Path    string
	Handler runtime.HandlerFunc
}

Route represents the route for mux

type Service

type Service struct {
	GRPCServer *grpc.Server
	HTTPServer *http.Server
	// contains filtered or unexported fields
}

Service represents the microservice

func NewService

func NewService(opts ...Option) *Service

NewService creates a new microservice

func (*Service) AddRoutes

func (s *Service) AddRoutes(routes ...Route)

AddRoutes adds additional routes

func (*Service) Getpid

func (s *Service) Getpid() int

Getpid gets the process id of server

func (*Service) HasRoute added in v2.1.2

func (s *Service) HasRoute(route Route) bool

HasRoute checks if a route already exists

func (*Service) ServeFile added in v2.1.0

func (s *Service) ServeFile(w http.ResponseWriter, r *http.Request, pathParams map[string]string)

ServeFile serves a file

func (*Service) Start

func (s *Service) Start(httpPort uint, grpcPort uint, reverseProxyFunc ReverseProxyFunc) error

Start starts the microservice with listening on the ports

func (*Service) Stop

func (s *Service) Stop()

Stop stops the microservice gracefully

Jump to

Keyboard shortcuts

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