gateway.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdmain
  15. import (
  16. "fmt"
  17. "net"
  18. "os"
  19. "strings"
  20. "github.com/coreos/etcd/proxy/tcpproxy"
  21. "github.com/spf13/cobra"
  22. )
  23. var (
  24. gatewayListenAddr string
  25. gatewayEndpoints string
  26. )
  27. var (
  28. rootCmd = &cobra.Command{
  29. Use: "etcd",
  30. Short: "etcd server",
  31. SuggestFor: []string{"etcd"},
  32. }
  33. )
  34. func init() {
  35. rootCmd.AddCommand(newGatewayCommand())
  36. }
  37. // newGatewayCommand returns the cobra command for "gateway".
  38. func newGatewayCommand() *cobra.Command {
  39. lpc := &cobra.Command{
  40. Use: "gateway <subcommand>",
  41. Short: "gateway related command",
  42. }
  43. lpc.AddCommand(newGatewayStartCommand())
  44. return lpc
  45. }
  46. func newGatewayStartCommand() *cobra.Command {
  47. cmd := cobra.Command{
  48. Use: "start",
  49. Short: "start the gateway",
  50. Run: startGateway,
  51. }
  52. cmd.Flags().StringVar(&gatewayListenAddr, "listen-addr", "127.0.0.1:23790", "listen address")
  53. cmd.Flags().StringVar(&gatewayEndpoints, "endpoints", "127.0.0.1:2379", "comma separated etcd cluster endpoints")
  54. return &cmd
  55. }
  56. func startGateway(cmd *cobra.Command, args []string) {
  57. endpoints := strings.Split(gatewayEndpoints, ",")
  58. l, err := net.Listen("tcp", gatewayListenAddr)
  59. if err != nil {
  60. fmt.Fprintln(os.Stderr, err)
  61. os.Exit(1)
  62. }
  63. tp := tcpproxy.TCPProxy{
  64. Listener: l,
  65. Endpoints: endpoints,
  66. }
  67. tp.Run()
  68. }