Skip to content

Commit

Permalink
Merge pull request #360 from game1991/master
Browse files Browse the repository at this point in the history
feat: egin提供自定义listener方法
  • Loading branch information
askuy authored Aug 30, 2023
2 parents 69e079d + cffae7f commit afb4e30
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
17 changes: 16 additions & 1 deletion server/egin/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func newComponent(name string, config *Config, logger *elog.Component) *Componen
listener: nil,
routerCommentMap: make(map[string]string),
}
// 判断是否存在自定义listener
if config.listener != nil {
comp.listener = config.listener
}

if config.EmbedPath != "" {
comp.embedWrapper = &EmbedWrapper{
Expand Down Expand Up @@ -88,6 +92,18 @@ func (c *Component) Prepare() error {

// Init 初始化
func (c *Component) Init() error {
// 如果没有配置listener
if c.config.listener == nil {
if err := c.defaultListener(); err != nil {
return err
}
}

c.config.Port = c.listener.Addr().(*net.TCPAddr).Port
return nil
}

func (c *Component) defaultListener() error {
var err error
if c.config.Network == "local" {
c.listener = newLocalListener()
Expand All @@ -97,7 +113,6 @@ func (c *Component) Init() error {
c.logger.Panic("new egin server err", elog.FieldErrKind("listen err"), elog.FieldErr(err))
}
}
c.config.Port = c.listener.Addr().(*net.TCPAddr).Port
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions server/egin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"
"embed"
"fmt"
"net"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -55,6 +56,7 @@ type Config struct {
aiReqResCelPrg cel.Program
mu sync.RWMutex // mutex for EnableAccessInterceptorReq、EnableAccessInterceptorRes、AccessInterceptorReqResFilter、aiReqResCelPrg
recoveryFunc gin.RecoveryFunc // recoveryFunc 处理接口没有被 recover 的 panic,默认返回 500 并且没有任何 response body
listener net.Listener // a generic network listener 默认是net.Listen()方法生成,如果有需要自行传入可采用option方式进行替换
}

// DefaultConfig ...
Expand Down
7 changes: 7 additions & 0 deletions server/egin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package egin
import (
"crypto/tls"
"embed"
"net"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -113,3 +114,9 @@ func WithRecoveryFunc(f gin.RecoveryFunc) Option {
c.config.recoveryFunc = f
}
}

func WithListener(listener net.Listener) Option {
return func(c *Container) {
c.config.listener = listener
}
}
15 changes: 15 additions & 0 deletions server/egin/options_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package egin

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

Expand Down Expand Up @@ -92,3 +95,15 @@ func TestWithContextTimeout(t *testing.T) {
comp := DefaultContainer().Build(WithContextTimeout(timeout))
assert.Equal(t, timeout, comp.config.ContextTimeout)
}

func TestWithListener(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 处理http请求
io.WriteString(w, "Hello,Client")
}))
defer server.Close()

lister := server.Listener
comp := DefaultContainer().Build(WithListener(lister))
assert.Equal(t, lister, comp.config.listener)
}

0 comments on commit afb4e30

Please sign in to comment.