rpc

package
v1.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2019 License: MIT Imports: 31 Imported by: 0

Documentation

Overview

Package rpc provide the rpc service entry and other tools

Index

Constants

This section is empty.

Variables

View Source
var (

	// Commands defines the rpc service command line entry
	Commands = []*cli.Command{
		{
			Name:      "rpc:make-cert",
			Category:  category,
			Usage:     "generate root, client and server certificates",
			UsageText: "rpc:make-cert [command options]",
			Action:    generateCertificates,
			Flags: []cli.Flag{
				&cli.StringFlag{
					Name:  "save-to",
					Usage: "the directory for saving certificates",
					Value: ".",
				},
				&cli.StringSliceFlag{
					Name:  "server-cert-ips",
					Usage: "server certificate cert save path",
					Value: cli.NewStringSlice("127.0.0.1", "0.0.0.0", "::1"),
				},
			},
		},
		{
			Name:      "rpc:start",
			Category:  category,
			Usage:     "start rpc service",
			UsageText: "rpc:start [command options]",
			Flags: []cli.Flag{
				&cli.StringFlag{
					Name:  "server-cert",
					Usage: "the path to server certificate",
				},
				&cli.StringFlag{
					Name:  "server-key",
					Usage: "the path to server certificate key",
				},
				&cli.StringFlag{
					Name:  "ca-cert",
					Usage: "the path to root certificate",
				},
				&cli.IntFlag{
					Name:  "auth-client",
					Usage: "client certificate auth type, 0: no client cert, 1: request client cert, 2: require any client cert, 3: verify client cert if given, 4: require and verify client cert",
					Value: 4,
				},
				&cli.StringFlag{
					Name:    "host",
					Aliases: []string{"H"},
					Usage:   "rpc service listen ip",
					Value:   "0.0.0.0",
				},
				&cli.Int64Flag{
					Name:    "port",
					Aliases: []string{"P"},
					Usage:   "rpc service listen port",
					Value:   10986,
				},
			},
			Action: func(ctx *cli.Context) (err error) {
				var (
					serverCert         tls.Certificate
					certPool           = x509.NewCertPool()
					rootCaContentBytes []byte
					tlsConf            *tls.Config
					listener           net.Listener
				)
				if serverCert, err = tls.LoadX509KeyPair(ctx.String("server-cert"), ctx.String("server-key")); err != nil {
					log.MustNewLogger(nil).Errorf("load server certificates failed, %s", err)
					return
				}
				if rootCaContentBytes, err = ioutil.ReadFile(ctx.String("ca-cert")); err != nil {
					log.MustNewLogger(nil).Errorf("load ca certificates failed, %s", err)
					return
				}
				if !certPool.AppendCertsFromPEM(rootCaContentBytes) {
					return errors.New("append root ca to cert pool failed")
				}
				tlsConf = &tls.Config{
					ClientAuth:   tls.ClientAuthType(ctx.Int("auth-client")),
					Certificates: []tls.Certificate{serverCert},
					ClientCAs:    certPool,
				}
				addr := fmt.Sprintf("%s:%d", ctx.String("host"), ctx.Int64("port"))
				if listener, err = net.Listen("tcp", addr); err != nil {
					return err
				}
				defer listener.Close()
				rpcServer := grpc.NewServer(
					grpc.Creds(credentials.NewTLS(tlsConf)),
					grpc.KeepaliveParams(keepalive.ServerParameters{MaxConnectionAge: 2 * time.Minute}),
					grpc.StreamInterceptor(
						grpc_middleware.ChainStreamServer(
							grpc_prometheus.StreamServerInterceptor,
							grpc_recovery.StreamServerInterceptor(),
						),
					),
					grpc.UnaryInterceptor(
						grpc_middleware.ChainUnaryServer(
							grpc_prometheus.UnaryServerInterceptor,
							grpc_recovery.UnaryServerInterceptor(),
						),
					),
				)

				service := &rpc.Server{}
				rpc.RegisterDirectoryListServer(rpcServer, service)
				rpc.RegisterTokenCreateServer(rpcServer, service)
				rpc.RegisterTokenUpdateServer(rpcServer, service)
				rpc.RegisterTokenDeleteServer(rpcServer, service)
				rpc.RegisterFileCreateServer(rpcServer, service)
				rpc.RegisterFileReadServer(rpcServer, service)
				rpc.RegisterFileUpdateServer(rpcServer, service)
				rpc.RegisterFileDeleteServer(rpcServer, service)

				go func() {
					log.MustNewLogger(nil).Infof("bigfile rpc service listening on: tcp://%s", listener.Addr().String())
					if err = rpcServer.Serve(listener); err != nil {
						log.MustNewLogger(nil).Error(err)
					}
				}()

				quit := make(chan os.Signal, 1)
				signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
				<-quit
				log.MustNewLogger(nil).Debug("Shutdown Server ...")
				rpcServer.GracefulStop()

				return
			},
			Before: func(context *cli.Context) (err error) {
				db := databases.MustNewConnection(&config.DefaultConfig.Database)
				migrate.DefaultMC.SetConnection(db)
				migrate.DefaultMC.Upgrade()
				return nil
			},
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL