Skip to content

Commit

Permalink
Merge fd1dce1 into ae42c95
Browse files Browse the repository at this point in the history
  • Loading branch information
vany-egorov authored Oct 9, 2022
2 parents ae42c95 + fd1dce1 commit b285783
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 149 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
linters:
enable-all: true
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.13
- 1.17

# Required for coverage.
before_install:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func main() {
[godev-image]: https://img.shields.io/badge/go.dev-reference-5272B4?logo=go&logoColor=white
[godev-url]: https://pkg.go.dev/github.com/go-x-pkg/servers
[build-image]: https://travis-ci.com/go-x-pkg/servers.svg?branch=master
[build-url]: https://travis-ci.com/go-x-pkg/servers
[build-image]: https://app.travis-ci.com/go-x-pkg/servers.svg?branch=master
[build-url]: https://app.travis-ci.com/github/go-x-pkg/servers
[coverage-image]: https://coveralls.io/repos/github/go-x-pkg/servers/badge.svg?branch=master
[coverage-url]: https://coveralls.io/github/go-x-pkg/servers?branch=master
Expand Down
3 changes: 3 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

const (
defaultUNIXSocketFileMode os.FileMode = 0o666

// defaultPprofPrefix url prefix of pprof.
defaultPprofPrefix = "/debug/pprof"
)

var (
Expand Down
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-x-pkg/servers

go 1.19
go 1.17

require (
github.com/go-x-pkg/dumpctx v0.0.2
Expand All @@ -10,3 +10,14 @@ require (
google.golang.org/grpc v1.50.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
github.com/go-x-pkg/bufpool v0.0.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
89 changes: 2 additions & 87 deletions go.sum

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var kindText = map[Kind]string{
KindGRPC: "gRPC",
}

// is power of two algorithm
// IsSingle is power of two algorithm.
func (knd Kind) IsSingle() bool {
return !knd.IsEmpty() && (knd&(knd-1) == 0)
}
Expand All @@ -45,7 +45,9 @@ func (knd Kind) String() string {
if knd.IsEmpty() {
return "~"
}

s, _ := knd.MarshalJSON()

return string(s)
}

Expand Down Expand Up @@ -100,12 +102,15 @@ func (knd Kind) ToStringSlice() (vv []string) {
if knd.Has(KindINET) {
vv = append(vv, kindText[KindINET])
}

if knd.Has(KindUNIX) {
vv = append(vv, kindText[KindUNIX])
}

if knd.Has(KindHTTP) {
vv = append(vv, kindText[KindHTTP])
}

if knd.Has(KindGRPC) {
vv = append(vv, kindText[KindGRPC])
}
Expand Down
5 changes: 3 additions & 2 deletions listen-serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (it iterator) Listen(fnArgs ...Arg) (ss Servers, errs []error) {
return ss, errs
}

func (it iterator) ServeHTTP(handler http.Handler, fnArgs ...Arg) error {
func (it iterator) ServeHTTP(fnNewHandler func(Server) http.Handler, fnArgs ...Arg) error {
it = it.FilterListener()

cfg := args{}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (it iterator) ServeHTTP(handler http.Handler, fnArgs ...Arg) error {

server := &http.Server{
Addr: addr,
Handler: handler,
Handler: fnNewHandler(s),
}

go func() {
Expand All @@ -161,6 +161,7 @@ func (it iterator) ServeHTTP(handler http.Handler, fnArgs ...Arg) error {

if err := server.Shutdown(ctxTimeout); err != nil {
fnLog(log.Info, "server (:addr %s) shutdown failed: %s", addr, err)

return
}

Expand Down
41 changes: 40 additions & 1 deletion server-base.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package servers

import (
"fmt"
"io"

"github.com/go-x-pkg/dumpctx"
)

type WithKind struct {
Knd Kind `json:"kind" yaml:"kind" bson:"kind"`
}
Expand Down Expand Up @@ -32,6 +39,15 @@ type WithNetwork struct {
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"`

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

func (s *ServerBase) Network() string {
Expand All @@ -51,5 +67,28 @@ func (s *ServerBase) validate() error {
}

func (s *ServerBase) defaultize() error {
return s.WithKind.defaultize()
if err := s.WithKind.defaultize(); err != nil {
return err
}

if s.Pprof.Prefix == "" {
s.Pprof.Prefix = defaultPprofPrefix
}

return nil
}

func (s *ServerBase) Dump(ctx *dumpctx.Ctx, w io.Writer) {
fmt.Fprintf(w, "%sgrpc:\n", ctx.Indent())

ctx.Wrap(func() {
fmt.Fprintf(w, "%sreflection: %t\n", ctx.Indent(), s.GRPC.Reflection)
})

fmt.Fprintf(w, "%spprof:\n", ctx.Indent())

ctx.Wrap(func() {
fmt.Fprintf(w, "%senable: %t\n", ctx.Indent(), s.Pprof.Enable)
fmt.Fprintf(w, "%sprefix: %q\n", ctx.Indent(), s.Pprof.Prefix)
})
}
21 changes: 8 additions & 13 deletions server-inet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ type ServerINET struct {

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

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

func (s *ServerINET) Base() *ServerBase { return &s.ServerBase }

func (s *ServerINET) setPort(v int) { s.Port = v }

func (s *ServerINET) Addr() string {
Expand Down Expand Up @@ -66,19 +64,16 @@ func (s *ServerINET) validate() error {
func (s *ServerINET) Dump(ctx *dumpctx.Ctx, w io.Writer) {
fmt.Fprintf(w, "%shost: %s\n", ctx.Indent(), s.Host)
fmt.Fprintf(w, "%sport: %d\n", ctx.Indent(), s.Port)

if s.TLS.Enable {
fmt.Fprintf(w, "%stls:\n", ctx.Indent())

ctx.Wrap(func() {
fmt.Fprintf(w, "%senable: %t\n", ctx.Indent(), s.TLS.Enable)
fmt.Fprintf(w, "%scert-file: %s\n", ctx.Indent(), s.TLS.CertFile)
fmt.Fprintf(w, "%skey-file: %s\n", ctx.Indent(), s.TLS.KeyFile)
fmt.Fprintf(w, "%scertFile: %s\n", ctx.Indent(), s.TLS.CertFile)
fmt.Fprintf(w, "%skeyFile: %s\n", ctx.Indent(), s.TLS.KeyFile)
})
}

fmt.Fprintf(w, "%sgrpc:\n", ctx.Indent())

ctx.Wrap(func() {
fmt.Fprintf(w, "%sreflection: %t\n", ctx.Indent(), s.GRPC.Reflection)
})
s.ServerBase.Dump(ctx, w)
}
8 changes: 6 additions & 2 deletions server-unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ type ServerUNIX struct {
ServerBase `json:",inline" yaml:",inline" bson:",inline"`

Address string `yaml:"addr"`
SocketFileMode os.FileMode `yaml:"socket-file-mode"`
SocketFileMode os.FileMode `yaml:"socketFileMode"`
}

func (s *ServerUNIX) Base() *ServerBase { return &s.ServerBase }

func (s *ServerUNIX) Addr() string { return s.Address }

func (s *ServerUNIX) validate() error {
Expand Down Expand Up @@ -52,5 +54,7 @@ func (s *ServerUNIX) defaultize() error {

func (s *ServerUNIX) Dump(ctx *dumpctx.Ctx, w io.Writer) {
fmt.Fprintf(w, "%saddr: %s\n", ctx.Indent(), s.Addr())
fmt.Fprintf(w, "%ssocket-file-mode: %03o | %s\n", ctx.Indent(), s.SocketFileMode, s.SocketFileMode)
fmt.Fprintf(w, "%ssocketFileMode: %03o | %s\n", ctx.Indent(), s.SocketFileMode, s.SocketFileMode)

s.ServerBase.Dump(ctx, w)
}
2 changes: 1 addition & 1 deletion server-wrapped.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ServerWrapped struct {
}

func (sw *ServerWrapped) unmarshal(fn func(interface{}) error) error {
wk := WithKind{}
var wk WithKind

if err := fn(&wk); err != nil {
return fmt.Errorf("error unmarshal server-head-kind: %w", err)
Expand Down
24 changes: 16 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
)

type Server interface {
serverBaser
serverKinder
serverAddred
serverNetworker
}

type (
serverBaser interface{ Base() *ServerBase }
serverKinder interface{ Kind() Kind }
serverAddred interface{ Addr() string }
serverNetworker interface{ Network() string }
Expand All @@ -24,18 +26,24 @@ type (
serverDumper interface{ Dump(*dumpctx.Ctx, io.Writer) }
)

func runLogPrefix(s Server) string {
if s.Kind().IsEmpty() {
return "!!!"
} else if s.Kind().Has(KindUNIX) {
func runLogPrefix(server Server) string {
switch s := server.(type) {
case *ServerListener:
server = s.Server
case *ServerWrapped:
server = s.Server
}

switch s := server.(type) {
case *ServerUNIX:
return "UNIX"
} else if s.Kind().Has(KindINET) {
if inet, ok := s.(*ServerINET); ok && inet.TLS.Enable {
case *ServerINET:
if s.TLS.Enable {
return "TLS"
}

return "TCP"
default:
panic("undefined server")
}

return "???"
}
37 changes: 13 additions & 24 deletions servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func (it iterator) Defaultize(inetHost string, inetPort int, unixAddr string) (e
if inet.Port == 0 {
inet.Port = inetPort
}

}

return err
Expand Down Expand Up @@ -167,41 +166,31 @@ func (ss Servers) Dump(ctx *dumpctx.Ctx, w io.Writer) { ss.IntoIter().Dump(ctx,

func (ss Servers) Validate() error { return ss.IntoIter().Validate() }

func (ss *Servers) PushINETIfNotExists(host string, port int) {
func (ss *Servers) PushINETIfNotExists(host string, port int, kind Kind) {
if ss.IntoIter().FilterInet().Len() != 0 {
return
}

s := &ServerINET{
ServerBase: ServerBase{
WithKind: WithKind{
Knd: KindINET,
},
},
var server ServerINET

Host: host,
Port: port,
}
server.Knd = KindINET
server.Host = host
server.Port = port

*ss = append(*ss, serverEnsureWrapped(s))
*ss = append(*ss, serverEnsureWrapped(&server))
}

func (ss *Servers) PushUnixIfNotExists(addr string) {
func (ss *Servers) PushUnixIfNotExists(addr string, kind Kind) {
if ss.IntoIter().FilterUnix().Len() != 0 {
return
}

s := &ServerUNIX{
ServerBase: ServerBase{
WithKind: WithKind{
Knd: KindUNIX,
},
},
var server ServerUNIX

Address: addr,
}
server.Knd = KindUNIX
server.Knd.Set(kind)

*ss = append(*ss, serverEnsureWrapped(s))
*ss = append(*ss, serverEnsureWrapped(&server))
}

func (ss *Servers) SetPortToFirstINET(port int) {
Expand All @@ -216,12 +205,12 @@ func (ss *Servers) Listen(fnArgs ...Arg) (Servers, []error) {
return ss.IntoIter().Listen(fnArgs...)
}

func (ss *Servers) ServeHTTP(handler http.Handler, fnArgs ...Arg) error {
func (ss *Servers) ServeHTTP(fnNewServer func(Server) http.Handler, fnArgs ...Arg) error {
return ss.
IntoIter().
FilterHTTP().
FilterListener().
ServeHTTP(handler, fnArgs...)
ServeHTTP(fnNewServer, fnArgs...)
}

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

0 comments on commit b285783

Please sign in to comment.