servers

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 17 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"
  "fmt"
  "http"
  "log"

  "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
  host: 0.0.0.0
  port: 8443`

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

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

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

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

  done, errs := listeners.IntoIter().FilterHTTP().ServeHTTP(
    http.DefaultServerMux,
    servers.Context(context.Background()),
  )

  select {
  case <-done:
  case err := <-errsChan:
    log.Printf("error listen: %#v", errs)
  }
}

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")
)
View Source
var ErrKindINETAndUNIX = errors.New("got both INET and UNIX kind")

Functions

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

Types

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

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"`
}

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:"cert-file"`
		KeyFile  string `yaml:"key-file"`
	} `yaml:"tls"`
}

func (*ServerINET) Addr added in v0.1.0

func (s *ServerINET) Addr() string

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:"socket-file-mode"`
}

func (*ServerUNIX) Addr added in v0.1.0

func (s *ServerUNIX) Addr() string

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"`
}

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) 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) PushINETIfNotExists added in v0.1.0

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

func (*Servers) PushUnixIfNotExists added in v0.1.0

func (ss *Servers) PushUnixIfNotExists(addr string)

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

Jump to

Keyboard shortcuts

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