Documentation
¶
Overview ¶
Package hertz contains a hertz server for go-orb.
Index ¶
- Constants
- Variables
- func GetAcceptType(ctx codecs.Map, acceptHeader string, contentType string) string
- func GetContentType(header string) (string, error)
- func New(acfg any, logger log.Logger, reg registry.Type) (orbserver.Entrypoint, error)
- func NewGRPCHandler[Tin any, Tout any](srv *Server, fHandler func(context.Context, *Tin) (*Tout, error), ...) func(c context.Context, ctx *app.RequestContext)
- func Provide(sections []string, configs types.ConfigData, logger log.Logger, ...) (orbserver.Entrypoint, error)
- func WithAddress(addr string) server.Option
- func WithAllowH2C() server.Option
- func WithDisableHTTP2() server.Option
- func WithHandlers(h ...server.RegistrationFunc) server.Option
- func WithIdleTimeout(timeout time.Duration) server.Option
- func WithInsecure() server.Option
- func WithLogLevel(level string) server.Option
- func WithLogPlugin(plugin string) server.Option
- func WithMaxConcurrentStreams(value int) server.Option
- func WithMiddleware(m string) server.Option
- func WithName(name string) server.Option
- func WithReadTimeout(timeout time.Duration) server.Option
- func WithTLS(config *tls.Config) server.Option
- func WithWriteTimeout(timeout time.Duration) server.Option
- func WriteError(ctx *app.RequestContext, err error)
- type Config
- type Server
- func (s *Server) Address() string
- func (s *Server) Enabled() bool
- func (s *Server) EntrypointID() string
- func (s *Server) Name() string
- func (s *Server) Register(register orbserver.RegistrationFunc)
- func (s *Server) Router() *server.Hertz
- func (s *Server) Start() error
- func (s *Server) Stop(ctx context.Context) error
- func (s *Server) String() string
- func (s *Server) Transport() string
- func (s *Server) Type() string
Constants ¶
const ( // DefaultAddress to use for new Hertz servers. DefaultAddress = ":0" // DefaultInsecure will create an HTTP server without TLS, for insecure connections. // Note: as a result you can only make insecure HTTP requests, and no HTTP2 // unless you set WithH2C. // // WARNING: don't use this in production, unless you really know what you are // doing. this will result in unencrypted traffic. Really, it is even advised // against using this in testing environments. DefaultInsecure = false // DefaultAllowH2C allows insecure, unencrypted traffic to HTTP2 servers. // Don't use this, see the notes at DefaultInsecure for more details. DefaultAllowH2C = false // DefaultMaxConcurrentStreams for HTTP2. DefaultMaxConcurrentStreams = 512 // DefaultHTTP2 dicates whether to also allow HTTP/2 connections. DefaultHTTP2 = true // DefaultReadTimeout see net/http pkg for more details. DefaultReadTimeout = 5 * time.Second // DefaultWriteTimeout see net/http pkg for more details. DefaultWriteTimeout = 5 * time.Second // DefaultIdleTimeout see net/http pkg for more details. DefaultIdleTimeout = 5 * time.Second // DefaultStopTimeout sets the timeout for ServerHertz.Stop(). DefaultStopTimeout = time.Second // DefaultConfigSection is the section key used in config files used to // configure the server options. DefaultConfigSection = Plugin // DefaultMaxHeaderBytes is the maximum size to parse from a client's // HTTP request headers. DefaultMaxHeaderBytes = 1024 * 64 )
const Plugin = "hertz"
Plugin is the plugin name.
Variables ¶
var ( ErrNoRouter = errors.New("no router plugin name set in config") ErrRouterNotFound = errors.New("router plugin not found, did you register it?") ErrNoMatchingCodecs = errors.New("no matching codecs found, did you register the codec plugins?") )
Errors.
var ( // ErrContentTypeNotSupported is returned when there is no matching codec. ErrContentTypeNotSupported = errors.New("content type not supported") ErrInvalidConfigType = errors.New("http server: invalid config type provided, not of type http.Config") )
Errors.
var (
ErrNotHTTPServer = errors.New("server provider is not of type *http.Server")
)
Errors.
Functions ¶
func GetAcceptType ¶
GetAcceptType parses the Accept header and checks against the available codecs to find a matching content type.
func GetContentType ¶
GetContentType parses the content type from the header value.
func NewGRPCHandler ¶
func NewGRPCHandler[Tin any, Tout any]( srv *Server, fHandler func(context.Context, *Tin) (*Tout, error), service string, method string, ) func(c context.Context, ctx *app.RequestContext)
NewGRPCHandler wraps a gRPC function with a Hertz handler.
func Provide ¶
func Provide( sections []string, configs types.ConfigData, logger log.Logger, reg registry.Type, opts ...orbserver.Option, ) (orbserver.Entrypoint, error)
Provide creates a new entrypoint for a single address. You can create multiple entrypoints for multiple addresses and ports. One entrypoint can serve a HTTP1 and HTTP2/H2C server.
func WithAddress ¶
WithAddress specifies the address to listen on. If you want to listen on all interfaces use the format ":8080" If you want to listen on a specific interface/address use the full IP.
func WithAllowH2C ¶
WithAllowH2C will allow H2C connections on the entrypoint. H2C is HTTP2 without TLS. It is not recommended to turn this on.
func WithDisableHTTP2 ¶
WithDisableHTTP2 will prevent the creation of an HTTP2 server on the entrypoint.
func WithHandlers ¶
func WithHandlers(h ...server.RegistrationFunc) server.Option
WithHandlers adds custom handlers.
func WithIdleTimeout ¶
WithIdleTimeout is the maximum amount of time to wait for the next request when keep-alives are enabled. If IdleTimeout is zero, the value of ReadTimeout is used. If both are zero, there is no timeout.
func WithInsecure ¶
WithInsecure will create the entrypoint without using TLS. Note: as a result you can only make insecure HTTP requests, and no HTTP2 unless you set WithH2C.
WARNING: don't use this in production, unless you really know what you are doing. this will result in unencrypted traffic. Really, it is even advised against using this in testing environments.
func WithLogLevel ¶
WithLogLevel changes the log level from the inherited logger.
func WithLogPlugin ¶
WithLogPlugin changes the log level from the inherited logger.
func WithMaxConcurrentStreams ¶
WithMaxConcurrentStreams sets the concurrent streams limit for HTTP2.
func WithMiddleware ¶
WithMiddleware adds a pre-registered middleware.
func WithName ¶
WithName sets the entrypoint name. The default name is in the format of 'http-<uuid>'.
Setting a custom name allows you to dynamically reference the entrypoint in the file config, and makes it easier to attribute the logs.
func WithReadTimeout ¶
WithReadTimeout sets the maximum duration for reading the entire request, including the body. A zero or negative value means there will be no timeout.
func WithWriteTimeout ¶
WithWriteTimeout sets the maximum duration before timing out writes of the response. It is reset whenever a new request's header is read. Like ReadTimeout, it does not let Handlers make decisions on a per-request basis. A zero or negative value means there will be no timeout.
func WriteError ¶
func WriteError(ctx *app.RequestContext, err error)
WriteError returns an error response to the HTTP request.
Types ¶
type Config ¶
type Config struct { server.EntrypointConfig `yaml:",inline"` // Address to listen on. // TODO(davincible): implement this, and the address method. // If no IP is provided, an interface will be selected automatically. Private // interfaces are preferred, if none are found a public interface will be used. // // If no port is provided, a random port will be selected. To listen on a // specific interface, but with a random port, you can use '<IP>:0'. Address string `json:"address" yaml:"address"` // Insecure will create an HTTP server without TLS, for insecure connections. // Note: as a result you can only make insecure HTTP1 requests, no HTTP2 // unless you set WithH2C. // // WARNING: don't use this in production, unless you really know what you are // doing. this will result in unencrypted traffic. Really, it is even advised // against using this in testing environments. Insecure bool `json:"insecure" yaml:"insecure"` // TLS config, if none is provided a self-signed certificates will be generated. // // You can load a tls config from yaml/json with the following options: // // “`yaml // rootCAFiles: // - xxx // clientCAFiles: // - xxx // clientAuth: "none" | "request" | "require" | "verify" | "require+verify" // certificates: // - certFile: xxx // keyFile: xxx // “` TLS *mtls.Config `json:"tls,omitempty" yaml:"tls,omitempty"` // H2C allows h2c connections; HTTP2 without TLS. H2C bool `json:"h2c" yaml:"h2c"` // HTTP2 dicates whether to also allow HTTP/2 connections. Defaults to true. HTTP2 bool `json:"http2" yaml:"http2"` // MaxConcurrentStreams for HTTP2. MaxConcurrentStreams int `json:"maxConcurrentStreams" yaml:"maxConcurrentStreams"` // MaxHeaderBytes is the maximum size to parse from a client's // HTTP request headers. MaxHeaderBytes int `json:"maxHeaderBytes" yaml:"maxHeaderBytes"` // ReadTimeout is the maximum duration for reading the entire // request, including the body. A zero or negative value means // there will be no timeout. ReadTimeout time.Duration `json:"readTimeout" yaml:"readTimeout"` // WriteTimeout is the maximum duration before timing out // writes of the response. It is reset whenever a new // request's header is read. Like ReadTimeout, it does not // let Handlers make decisions on a per-request basis. // A zero or negative value means there will be no timeout. WriteTimeout time.Duration `json:"writeTimeout" yaml:"writeTimeout"` // IdleTimeout is the maximum amount of time to wait for the // next request when keep-alives are enabled. If IdleTimeout // is zero, the value of ReadTimeout is used. If both are // zero, there is no timeout. IdleTimeout time.Duration `json:"idleTimeout" yaml:"idleTimeout"` // StopTimeout is the timeout for ServerHertz.Stop(). StopTimeout time.Duration `json:"stopTimeout" yaml:"stopTimeout"` // Middlewares is a list of middleware to use. Middlewares []server.MiddlewareConfig `json:"middlewares" yaml:"middlewares"` // Handlers is a list of pre-registered handlers. Handlers []string `json:"handlers" yaml:"handlers"` // Logger allows you to dynamically change the log level and plugin for a // specific entrypoint. Logger log.Config `json:"logger" yaml:"logger"` }
Config provides options to the entrypoint.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the hertz Server for go-orb.
func (*Server) EntrypointID ¶
EntrypointID returns the id (uuid) of this entrypoint in the registry.
func (*Server) Register ¶
func (s *Server) Register(register orbserver.RegistrationFunc)
Register executes a registration function on the entrypoint.