service

package
v0.0.32 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2021 License: BSD-3-Clause Imports: 30 Imported by: 0

Documentation

Overview

Package service provides standardized command and HTTP setup by smartly composing the other cmdutil packages based on environment variables.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GRPC

GRPC returns a standard GRPC server for the provided handler. Router-bypass and TLS config are inferred from the environment.

Currently only supports running in router-bypass mode, unlike HTTP.

func HTTP

func HTTP(l logrus.FieldLogger, m metrics.Provider, h http.Handler, opts ...func(*httpOptions)) cmdutil.Server

HTTP returns a standard HTTP server for the provided handler. Port, TLS, and router-bypass config are inferred from the environment.

func SkipEnforceHTTPS

func SkipEnforceHTTPS() func(*httpOptions)

SkipEnforceHTTPS allows services to opt-out of SSL enforcement required for productionization. It should only be used in environments where SSL is not available.

func WithHTTPServerHook added in v0.0.17

func WithHTTPServerHook(fn func(*http.Server)) func(*httpOptions)

WithHTTPServerHook allows services to provide a function to adjust settings on any HTTP server before after the defaults are applied but before the server is started.

Example
package main

import (
	"io"
	"net/http"
	"time"

	"github.com/heroku/x/cmdutil/service"
)

func main() {
	var cfg struct {
		Hello string `env:"HELLO,default=hello"`
	}
	svc := service.New(&cfg)

	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, _ = io.WriteString(w, cfg.Hello)
	})

	configureHTTP := func(s *http.Server) {
		s.ReadTimeout = 10 * time.Second
	}

	svc.Add(service.HTTP(svc.Logger, svc.MetricsProvider, handler,
		service.WithHTTPServerHook(configureHTTP),
	))

	svc.Run()
}
Output:

func WithTLSConfig

func WithTLSConfig(tlscfg *tls.Config) func(*httpOptions)

WithTLSConfig allows services to use a specific TLS configuration instead of the default one constructed from environment variables.

Types

type OptionFunc

type OptionFunc func(*options)

OptionFunc is a function that modifies internal service options.

func CustomMetricsSuffix

func CustomMetricsSuffix(s string) OptionFunc

CustomMetricsSuffix to be added to metrics recorded by the MetricsProvider instead of inferring it from $DYNO.

func EnableOpenCensusTracing added in v0.0.12

func EnableOpenCensusTracing() OptionFunc

EnableOpenCensusTracing on the Service by registering and starting an open census agent exporter.

func SkipMetricsSuffix

func SkipMetricsSuffix() OptionFunc

SkipMetricsSuffix prevents the Service from suffixing the process type to metric names recorded by the MetricsProvider. The default suffix is determined from $DYNO.

type Standard

type Standard struct {
	App             string
	Deploy          string
	Logger          logrus.FieldLogger
	MetricsProvider xmetrics.Provider
	// contains filtered or unexported fields
}

Standard is a standard service.

func New

func New(appConfig interface{}, ofs ...OptionFunc) *Standard

New Standard Service with logging, rollbar, metrics, debugging, common signal handling, and possibly more.

If appConfig is non-nil, envdecode.MustStrictDecode will be called on it to ensure that it is processed.

func (*Standard) Add

func (s *Standard) Add(svs ...cmdutil.Server)

Add adds cmdutil.Servers to be managed.

func (*Standard) Run

func (s *Standard) Run()

Run runs all standard and Added cmdutil.Servers.

If a panic is encountered, it is reported to Rollbar.

If the error returned by oklog/run.Run is non-nil, it is logged with s.Logger.Fatal.

Jump to

Keyboard shortcuts

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