servers

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: MIT Imports: 21 Imported by: 0

README

servers

GoDev Build Status Coverage Status Go Report Card Deepsource.io Card

HTTP, HTTPs, gRPC, UNIX servers with configs and context

Quick-start

package main

import (
  "bytes"
  "context"
  "fmt"
  "http"
  "log"
  "sync"

  "google.golang.org/grpc"
  "github.com/go-x-pkg/servers"
)

func main() {
  var s servers.Servers

  config := `- kind: [unix, http]
  addr: /run/acme/acme.sock
- kind: inet
  host: 0.0.0.0
  port: 8000
- kind: [inet, grpc]
  host: 0.0.0.0
  port: 8443
  tls:
    enable: true
    cert-file: /etc/acme/tls.cert
    key-file: /etc/acme/tls.key
  grpc:
    # e.g. grpcurl
    reflection: true`

  if err := yaml.Unmarshal([]byte(config), &ss); err != nil {
    log.Fatalf("error unmarshal: %s", err)
  }

  listeners, err := ss.Listen()
  if len(errs) != 0 {
    listeners.Close()

    log.Fatalf("error listen: %#v", errs)
  }

  http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
    log.Printf("[<] %#v %#v", w, r)
  })

  ctx := context.Background()
  ctx, cancel := context.WithCancel(ctx)
  defer cancel()

  wg := sync.WaitGroup()

  wg.Add(1)
  go func() {
    defer wg.Done()

    err := listeners.ServeHTTP(
      http.DefaultServerMux,

      servers.Context(ctx),
    )
  }()

  wg.Add(1)
  go func() {
    defer wg.Done()

    err := listeners.ServeGRPC(
      func (*grpc.Server) {
        # register gRPC servers here
      },

      servers.Context(ctx),
    )
  }()

  wg.Wait()
}

Config example

- kind: [unix, http]
  addr: /run/acme/acme.sock

- kind: inet
  host: 0.0.0.0
  port: 8000

- kind: [inet, grpc]
  host: 0.0.0.0
  port: 8443
  tls:
    enable: true
    cert-file: /etc/acme/tls.cert
    key-file: /etc/acme/tls.key
  grpc:
    # e.g. grpcurl
    reflection: true

Documentation

Index

Constants

View Source
const (
	KindEmpty Kind = 0

	KindINET = 1 << (iota - 1)
	KindUNIX

	KindHTTP
	KindGRPC
)

Variables

View Source
var (
	ErrUnmarshalUnknownKind = errors.New("unknown server-kind, no server associated with kind")

	ErrTLSCertFileNotExists       = errors.New("tls cert-file doesn't exists")
	ErrTLSCertFilePathNotProvided = errors.New("tls cert-file path is not provided")

	ErrTLSKeyFileNotExists       = errors.New("tls key-file doesn't exists")
	ErrTLSKeyFilePathNotProvided = errors.New("tls key-file path is not provided")

	ErrUnixSocketParentDirNotExists = errors.New("unix socket parent dir doesn't exists")
	ErrUnixSocketPathNotProvided    = errors.New("tls key-file path is not provided")

	ErrGotBothInetAndUnix = errors.New("provided server is both unix and inet")
)
View Source
var ErrKindINETAndUNIX = errors.New("got both INET and UNIX kind")

Functions

This section is empty.

Types

type Arg

type Arg func(*args)

func Context

func Context(v context.Context) Arg

func FnLog

func FnLog(v log.FnT) Arg

func FnShutdownTimeout

func FnShutdownTimeout(v func() time.Duration) Arg

type Kind

type Kind uint8

func NewKindFromString

func NewKindFromString(v string) Kind

func NewKindFromStringSlice added in v0.1.0

func NewKindFromStringSlice(vv []string) Kind

func (Kind) Has added in v0.1.0

func (knd Kind) Has(v Kind) bool

func (Kind) IsEmpty added in v0.1.0

func (knd Kind) IsEmpty() bool

func (Kind) IsSingle added in v0.1.0

func (knd Kind) IsSingle() bool

IsSingle is power of two algorithm.

func (Kind) MarshalJSON

func (knd Kind) MarshalJSON() ([]byte, error)

func (Kind) MarshalYAML

func (knd Kind) MarshalYAML() (interface{}, error)

func (Kind) NewServer added in v0.1.0

func (knd Kind) NewServer() Server

func (*Kind) Set added in v0.1.0

func (knd *Kind) Set(v Kind)

func (Kind) String

func (knd Kind) String() string

func (Kind) StringTrySingle added in v0.1.0

func (knd Kind) StringTrySingle() string

func (Kind) ToStringSlice added in v0.1.0

func (knd Kind) ToStringSlice() (vv []string)

func (*Kind) UnSet added in v0.1.0

func (knd *Kind) UnSet(v Kind)

func (*Kind) UnmarshalJSON

func (knd *Kind) UnmarshalJSON(data []byte) error

func (*Kind) UnmarshalYAML

func (knd *Kind) UnmarshalYAML(unmarshal func(interface{}) error) error

type Server added in v0.1.0

type Server interface {
	// contains filtered or unexported methods
}

type ServerBase added in v0.1.0

type ServerBase struct {
	WithKind    `json:",inline" yaml:",inline" bson:",inline"`
	WithNetwork `json:",inline" yaml:",inline" bson:",inline"`

	GRPC struct {
		Reflection bool `yaml:"reflection"`
	} `yaml:"grpc"`

	HTTP struct {
		ReadHeaderTimeout time.Duration `yaml:"readHeaderTimeout"`
	} `yaml:"http"`

	Pprof struct {
		Enable bool   `yaml:"enable"`
		Prefix string `yaml:"prefix"`
	} `yaml:"pprof"`
}

func (*ServerBase) Dump added in v0.4.0

func (s *ServerBase) Dump(ctx *dumpctx.Ctx, w io.Writer)

func (*ServerBase) Network added in v0.2.0

func (s *ServerBase) Network() string

type ServerINET added in v0.1.0

type ServerINET struct {
	ServerBase `json:",inline" yaml:",inline" bson:",inline"`

	Host string `yaml:"host"`
	Port int    `yaml:"port"`

	TLS struct {
		Enable   bool   `yaml:"enable"`
		CertFile string `yaml:"certFile"`
		KeyFile  string `yaml:"keyFile"`
	} `yaml:"tls"`
}

func (*ServerINET) Addr added in v0.1.0

func (s *ServerINET) Addr() string

func (*ServerINET) Base added in v0.4.0

func (s *ServerINET) Base() *ServerBase

func (*ServerINET) Dump added in v0.1.0

func (s *ServerINET) Dump(ctx *dumpctx.Ctx, w io.Writer)

type ServerListener added in v0.1.0

type ServerListener struct {
	Server
	net.Listener
}

func (*ServerListener) Addr added in v0.1.0

func (sl *ServerListener) Addr() string

type ServerUNIX added in v0.1.0

type ServerUNIX struct {
	ServerBase `json:",inline" yaml:",inline" bson:",inline"`

	Address        string      `yaml:"addr"`
	SocketFileMode os.FileMode `yaml:"socketFileMode"`
}

func (*ServerUNIX) Addr added in v0.1.0

func (s *ServerUNIX) Addr() string

func (*ServerUNIX) Base added in v0.4.0

func (s *ServerUNIX) Base() *ServerBase

func (*ServerUNIX) Dump added in v0.1.0

func (s *ServerUNIX) Dump(ctx *dumpctx.Ctx, w io.Writer)

type ServerWrapped added in v0.1.0

type ServerWrapped struct {
	Server `json:",inline" yaml:",inline" bson:",inline"`
}

ServerWrapped is struct wrapped typed server. Server + kind to unmarshal and build typed json/yaml/bson. see https://stackoverflow.com/a/35584188/723095.

func (*ServerWrapped) MarshalJSON added in v0.1.0

func (sw *ServerWrapped) MarshalJSON() ([]byte, error)

func (*ServerWrapped) MarshalYAML added in v0.1.0

func (sw *ServerWrapped) MarshalYAML() (interface{}, error)

func (*ServerWrapped) UnmarshalJSON added in v0.1.0

func (sw *ServerWrapped) UnmarshalJSON(data []byte) error

func (*ServerWrapped) UnmarshalYAML added in v0.1.0

func (sw *ServerWrapped) UnmarshalYAML(unmarshal func(interface{}) error) error

type Servers added in v0.1.0

type Servers []*ServerWrapped

func (*Servers) Close added in v0.1.1

func (ss *Servers) Close() []error

func (Servers) Defaultize added in v0.1.0

func (ss Servers) Defaultize(inetHost string, inetPort int, unixAddr string) (err error)

func (Servers) Dump added in v0.1.0

func (ss Servers) Dump(ctx *dumpctx.Ctx, w io.Writer)

func (Servers) ForEach added in v0.1.0

func (ss Servers) ForEach(cb iterCb) bool

func (Servers) IntoIter added in v0.1.0

func (ss Servers) IntoIter() iterator

func (*Servers) Listen added in v0.1.1

func (ss *Servers) Listen(fnArgs ...Arg) (Servers, []error)

func (*Servers) PushINETIfNotExists added in v0.1.0

func (ss *Servers) PushINETIfNotExists(host string, port int, kind Kind)

func (*Servers) PushUnixIfNotExists added in v0.1.0

func (ss *Servers) PushUnixIfNotExists(addr string, kind Kind)

func (*Servers) ServeGRPC added in v0.2.0

func (ss *Servers) ServeGRPC(fnNewServer func(opts ...grpc.ServerOption) *grpc.Server, fnArgs ...Arg) error

func (*Servers) ServeHTTP added in v0.1.1

func (ss *Servers) ServeHTTP(fnNewServer func(Server) http.Handler, fnArgs ...Arg) error

func (*Servers) SetPortToFirstINET added in v0.1.0

func (ss *Servers) SetPortToFirstINET(port int)

func (Servers) Validate added in v0.1.0

func (ss Servers) Validate() error

type WithKind added in v0.1.0

type WithKind struct {
	Knd Kind `json:"kind" yaml:"kind" bson:"kind"`
}

func (*WithKind) Kind added in v0.1.0

func (wk *WithKind) Kind() Kind

type WithNetwork added in v0.2.0

type WithNetwork struct {
	Net string `json:"network" yaml:"network" bson:"network"`
}

Jump to

Keyboard shortcuts

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