github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/courier/transport_teleport/serve.go (about)

     1  package transport_teleport
     2  
     3  import (
     4  	"github.com/henrylee2cn/teleport"
     5  	"github.com/henrylee2cn/teleport/proto/pbproto"
     6  	"github.com/johnnyeven/libtools/conf"
     7  	"time"
     8  )
     9  
    10  type ServeTeleport struct {
    11  	Network            string        `conf:"env"`
    12  	IP                 string        `conf:"env"`
    13  	Port               uint16        `conf:"env"`
    14  	DefaultDialTimeout time.Duration `conf:"env"`
    15  	RedialTimes        int32         `conf:"env"`
    16  	RedialInterval     time.Duration `conf:"env"`
    17  	DefaultBodyCodec   string        `conf:"env"`
    18  	DefaultSessionAge  time.Duration `conf:"env"`
    19  	DefaultContextAge  time.Duration `conf:"env"`
    20  	SlowCometDuration  time.Duration `conf:"env"`
    21  	PrintDetail        bool          `conf:"env"`
    22  	CountTime          bool          `conf:"env"`
    23  	Plugins            []tp.Plugin   `conf:"-"`
    24  
    25  	server tp.Peer
    26  }
    27  
    28  func (s *ServeTeleport) Init() {
    29  	tp.SetDefaultProtoFunc(pbproto.NewPbProtoFunc())
    30  	s.server = tp.NewPeer(tp.PeerConfig{
    31  		Network:            s.Network,
    32  		LocalIP:            s.IP,
    33  		ListenPort:         s.Port,
    34  		DefaultDialTimeout: s.DefaultDialTimeout,
    35  		RedialTimes:        s.RedialTimes,
    36  		RedialInterval:     s.RedialInterval,
    37  		DefaultBodyCodec:   s.DefaultBodyCodec,
    38  		DefaultSessionAge:  s.DefaultSessionAge,
    39  		DefaultContextAge:  s.DefaultContextAge,
    40  		SlowCometDuration:  s.SlowCometDuration,
    41  		PrintDetail:        s.PrintDetail,
    42  		CountTime:          s.CountTime,
    43  	}, s.Plugins...)
    44  }
    45  
    46  func (s ServeTeleport) MarshalDefaults(v interface{}) {
    47  	if h, ok := v.(*ServeTeleport); ok {
    48  		if h.Port == 0 {
    49  			h.Port = 9090
    50  		}
    51  	}
    52  }
    53  
    54  func (s *ServeTeleport) DockerDefaults() conf.DockerDefaults {
    55  	return conf.DockerDefaults{
    56  		"Port": 9090,
    57  	}
    58  }
    59  
    60  func (s *ServeTeleport) Start() error {
    61  	return s.server.ListenAndServe()
    62  }
    63  
    64  func (s *ServeTeleport) Stop() error {
    65  	return s.server.Close()
    66  }
    67  
    68  func (s *ServeTeleport) RegisterCallRouter(route interface{}, plugins ...tp.Plugin) []string {
    69  	return s.server.RouteCall(route, plugins...)
    70  }
    71  
    72  func (s *ServeTeleport) RegisterPushRouter(route interface{}, plugins ...tp.Plugin) []string {
    73  	return s.server.RoutePush(route, plugins...)
    74  }