diff --git a/client/main.go b/client/main.go index c8b017b..2741457 100644 --- a/client/main.go +++ b/client/main.go @@ -12,6 +12,7 @@ import ( "github.com/findy-network/findy-common-go/rpc" "github.com/golang/glog" "github.com/lainio/err2" + _ "github.com/lainio/err2/assert" // we want an --asserter flag "github.com/lainio/err2/try" "google.golang.org/grpc" ) @@ -20,19 +21,19 @@ var ( user = flag.String("user", "", "test user name") serverAddr = flag.String("addr", "localhost", "agency host gRPC address") port = flag.Int("port", 50051, "agency host gRPC port") - tls = flag.Bool("tls", true, "use TLS and cert files") + noTLS = flag.Bool("no-tls", false, "do NOT use TLS and cert files (hard coded)") ) func main() { - err2.SetTracers(os.Stderr) + os.Args = append(os.Args, + "-logtostderr", + ) + glog.CopyStandardLogTo("ERROR") // for err2 binging - defer err2.Catch(err2.Err(func(err error) { - glog.Error(err) - })) - flag.Parse() + //defer err2.Catch(err2.ToStderr) // TODO: nex err2 version will have + defer err2.Catch() - // we want this for glog, this is just a tester, not a real world service - try.To(flag.Set("logtostderr", "true")) + flag.Parse() conn := try.To1(newClient(*user, fmt.Sprintf("%s:%d", *serverAddr, *port))) defer conn.Close() @@ -54,7 +55,7 @@ func newClient(user, addr string) (conn *grpc.ClientConn, err error) { var pki *rpc.PKI jwtStr := "" - if *tls { + if !*noTLS { pki = rpc.LoadPKIWithServerName("./cert", addr) jwtStr = jwt.BuildJWT(user) } @@ -64,7 +65,7 @@ func newClient(user, addr string) (conn *grpc.ClientConn, err error) { JWT: jwtStr, Addr: addr, - Insecure: !*tls, + Insecure: *noTLS, })) return } diff --git a/http/http.go b/http/http.go index 58cf892..1b4bcc4 100644 --- a/http/http.go +++ b/http/http.go @@ -2,6 +2,7 @@ package http import ( "context" + "fmt" nethttp "net/http" "os" "os/signal" @@ -33,9 +34,11 @@ func Run(s *nethttp.Server, a ...string) <-chan os.Signal { } startHTTPS := len(a) == 2 glog.V(3).Infof("startHTTPS: %v, length a: %v", startHTTPS, len(a)) + shutdownCh := make(chan os.Signal, 1) go func() { defer err2.Catch(err2.Err(func(err error) { - glog.Error(err) + fmt.Fprintln(os.Stderr, err) + shutdownCh <- syscall.SIGALRM })) if startHTTPS { @@ -51,7 +54,6 @@ func Run(s *nethttp.Server, a ...string) <-chan os.Signal { } }() - shutdownCh := make(chan os.Signal, 1) signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM) return shutdownCh diff --git a/integration/grpc_test.go b/integration/grpc_test.go index 90162d0..f8d025c 100644 --- a/integration/grpc_test.go +++ b/integration/grpc_test.go @@ -66,12 +66,10 @@ func setUp() { } func tearDown() { - err := conn.Close() - try.To(err) // just dump information out, we are inside a test + try.To(conn.Close()) server.GracefulStop() - err = insecureConn.Close() - try.To(err) // just dump information out, we are inside a test + try.To(insecureConn.Close()) insecureServer.GracefulStop() } @@ -134,6 +132,7 @@ func newClient(user, addr string) (conn *grpc.ClientConn, err error) { return } +// runServer starts a gRPC server in own goroutine. func runServer() { pki := rpc.LoadPKI("../cert") glog.V(1).Infof("starting gRPC server with\ncrt:\t%s\nkey:\t%s\nclient:\t%s", diff --git a/rpc/server.go b/rpc/server.go index c5faef7..e7e846e 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -57,7 +57,7 @@ func Server(cfg *ServerCfg) (s *grpc.Server, err error) { } if cfg.NoAuthorization { - glog.V(1).Infoln("no jwt authorization") + glog.V(1).Infoln("no authorization") opts = append(opts, grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( grpc_recovery.UnaryServerInterceptor(), @@ -68,6 +68,7 @@ func Server(cfg *ServerCfg) (s *grpc.Server, err error) { )), ) } else { + glog.V(1).Infoln("jwt token validity checked") opts = append(opts, grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( grpc_auth.UnaryServerInterceptor(jwt.CheckTokenValidity), @@ -99,6 +100,9 @@ func Serve(cfg *ServerCfg) { try.To(s.Serve(lis)) } +// PrepareServe builds gRPC server that allows graceful shutdown. You must start +// it by yourself with Serve() that blocks which typically means that you need a +// goroutine for that. func PrepareServe(cfg *ServerCfg) (s *grpc.Server, lis net.Listener, err error) { defer err2.Handle(&err) diff --git a/server/main.go b/server/main.go index 28449fd..acd838a 100644 --- a/server/main.go +++ b/server/main.go @@ -5,13 +5,14 @@ import ( "errors" "flag" "fmt" + "os" ops "github.com/findy-network/findy-common-go/grpc/ops/v1" "github.com/findy-network/findy-common-go/jwt" "github.com/findy-network/findy-common-go/rpc" "github.com/golang/glog" "github.com/lainio/err2" - "github.com/lainio/err2/try" + _ "github.com/lainio/err2/assert" // we want an --asserter flag "google.golang.org/grpc" ) @@ -19,25 +20,28 @@ var ( user = flag.String("user", "findy-root", "test user name") serverAddr = flag.String("addr", "localhost", "agency host gRPC address") port = flag.Int("port", 50051, "agency host gRPC port") - tls = flag.Bool("tls", true, "use TLS and cert files") + noTLS = flag.Bool("no-tls", false, "do NOT use TLS and cert files (hard coded)") ) func main() { + os.Args = append(os.Args, + "-logtostderr", + ) + glog.CopyStandardLogTo("ERROR") // for err2 binging + + //defer err2.Catch(err2.ToStderr) // TODO: nex err2 version will have defer err2.Catch() flag.Parse() - // whe want this for glog, this is just a tester, not a real world service - try.To(flag.Set("logtostderr", "true")) - var pki *rpc.PKI - if *tls { + if !*noTLS { pki = rpc.LoadPKI("./cert") glog.V(3).Infof("starting gRPC server with\ncrt:\t%s\nkey:\t%s\nclient:\t%s", pki.Server.CertFile, pki.Server.KeyFile, pki.Client.CertFile) } rpc.Serve(&rpc.ServerCfg{ - NoAuthorization: !*tls, + NoAuthorization: *noTLS, Port: *port, PKI: pki, @@ -58,7 +62,7 @@ func (d devOpsServer) Enter(ctx context.Context, cmd *ops.Cmd) (cr *ops.CmdRetur defer err2.Handle(&err) glog.V(1).Info("enter Enter()") - if *tls { + if !*noTLS { user := jwt.User(ctx) if user != d.Root { diff --git a/utils/utils.go b/utils/utils.go index a03e1b7..67ca212 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -9,6 +9,13 @@ import ( ) func ParseLoggingArgs(s string) { + if s == "" { + return + } + + os.Args = append(os.Args, + "-logtostderr", // todo: should be the first if we want to change this + ) args := make([]string, 1, 12) args[0] = os.Args[0] args = append(args, strings.Split(s, " ")...)