Skip to content

Commit

Permalink
add tlsconfig support
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed Dec 22, 2019
1 parent 29beb4a commit 2fde950
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func EchoHandler(ctx context.Context, con net.Conn) error {
func main() {
srv := tcpserver.New(
tcpserver.Address(":8080"),
//tcpserver.TLSConfig(tlsconfig),
tcpserver.TCPHandler(EchoHandler),
)
if err := srv.Serve(context.TODO()); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/x-mod/tcpserver

go 1.13

require github.com/x-mod/routine v1.2.7
require (
github.com/x-mod/routine v1.2.7
github.com/x-mod/tlsconfig v0.0.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/x-mod/errors v0.1.6 h1:k0XsZCl/aQKPGo293ewOuYnRuxy9YVxDmUIC/b4C47s=
github.com/x-mod/errors v0.1.6/go.mod h1:sWzfaUOjYKcW4cQaF/VEUQibdxGjkD+8LnL+GJhkIs0=
github.com/x-mod/routine v1.2.7 h1:P55Bfv9YYOQDwisHyb/bjmcPw8ep+kYqSQ8WE5E/YQ8=
github.com/x-mod/routine v1.2.7/go.mod h1:FUJxsh8BYaKeT88jyhr0ubhumCMusZla3VlrrL46dcs=
github.com/x-mod/tlsconfig v0.0.1 h1:3LpCmjxPBZYuJ9mrRRxfoJRGd7n8fjtRp39b3xWRRE0=
github.com/x-mod/tlsconfig v0.0.1/go.mod h1:yiTPHfiJzNrZPOaXweRiTt7F4LWGVAqTVLP1N7UMy9g=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down
12 changes: 12 additions & 0 deletions tcpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tcpserver

import (
"context"
"crypto/tls"
"log"
"net"
"sync"
Expand All @@ -16,6 +17,7 @@ type Server struct {
address string
handler Handler
listener net.Listener
tlsc *tls.Config
wgroup sync.WaitGroup
}

Expand All @@ -37,6 +39,13 @@ func Address(addr string) ServerOpt {
}
}

//TLSConfig option
func TLSConfig(sc *tls.Config) ServerOpt {
return func(srv *Server) {
srv.tlsc = sc
}
}

//Listener option for listener
func Listener(ln net.Listener) ServerOpt {
return func(srv *Server) {
Expand Down Expand Up @@ -79,6 +88,9 @@ func (srv *Server) Serve(ctx context.Context) error {
log.Println("tcpserver serving at ", srv.network, srv.address)
srv.listener = ln
}
if srv.tlsc != nil {
srv.listener = tls.NewListener(srv.listener, srv.tlsc)
}
for {
select {
case <-ctx.Done():
Expand Down

0 comments on commit 2fde950

Please sign in to comment.