@@ -44,58 +44,72 @@ func NewServer(cnf *config.Service) *cobra.Command {
4444 }
4545 return v .Unmarshal (cnf )
4646 },
47+ }
48+
49+ flags := command .PersistentFlags ()
50+ flags .StringVarP (& path , "config" , "c" , "config.toml" , "path to config file" )
51+ command .AddCommand (
52+ Run (& cnf .Server ),
53+ )
54+ return & command
55+ }
4756
57+ func Run (cnf * config.Server ) * cobra.Command {
58+ command := cobra.Command {
59+ Use : "run" ,
60+ Short : "run the server" ,
61+ Long : "Start listening required protocols." ,
4862 RunE : func (cmd * cobra.Command , args []string ) error {
49- if cnf .Server . Twirp .IsEnabled () {
63+ if cnf .Twirp .IsEnabled () {
5064 twirp := server .Twirp ()
5165 mux := http .NewServeMux ()
5266 mux .Handle (twirp .PathPrefix (), twirp )
5367 go func () {
54- cmd .Println ("twirp server starts listening" , cnf .Server . Twirp .BaseURL ())
68+ cmd .Println ("twirp server starts listening" , cnf .Twirp .BaseURL ())
5569 cmd .Println ("twirp server status:" , http .ListenAndServe (
56- cnf .Server . Twirp .Address ,
70+ cnf .Twirp .Address ,
5771 mux ,
5872 ))
5973 }()
6074 }
61- if cnf .Server . GRPC .IsEnabled () {
62- listener , err := net .Listen ("tcp" , cnf .Server . GRPC .Address )
75+ if cnf .GRPC .IsEnabled () {
76+ listener , err := net .Listen ("tcp" , cnf .GRPC .Address )
6377 if err != nil {
6478 return err
6579 }
6680 srv := grpc .NewServer ()
6781 v1 .RegisterGreeterServiceServer (srv , new (server.GRPC ))
6882 go func () {
69- cmd .Println ("grpc server starts listening" , "tcp://" + cnf .Server . GRPC .Address )
83+ cmd .Println ("grpc server starts listening" , "tcp://" + cnf .GRPC .Address )
7084 cmd .Println ("grpc server status:" , srv .Serve (listener ))
7185 }()
72- if cnf .Server . Gateway .IsEnabled () {
86+ if cnf .Gateway .IsEnabled () {
7387 mux := runtime .NewServeMux ()
7488 if err := v1 .RegisterGreeterServiceHandlerFromEndpoint (
7589 cmd .Context (),
7690 mux ,
77- cnf .Server . GRPC .Address ,
91+ cnf .GRPC .Address ,
7892 []grpc.DialOption {
7993 grpc .WithTransportCredentials (insecure .NewCredentials ()),
8094 },
8195 ); err != nil {
8296 return err
8397 }
8498 go func () {
85- cmd .Println ("gateway starts listening" , cnf .Server . Gateway .BaseURL ())
99+ cmd .Println ("gateway starts listening" , cnf .Gateway .BaseURL ())
86100 cmd .Println ("gateway status:" , http .ListenAndServe (
87- cnf .Server . Gateway .Address ,
101+ cnf .Gateway .Address ,
88102 mux ,
89103 ))
90104 }()
91105 }
92106 }
93- if cnf .Server . Profile .IsEnabled () {
107+ if cnf .Profile .IsEnabled () {
94108 mux := http .DefaultServeMux
95109 go func () {
96- cmd .Println ("profiler starts listening" , cnf .Server . Profile .BaseURL ())
110+ cmd .Println ("profiler starts listening" , cnf .Profile .BaseURL ())
97111 cmd .Println ("profiler status:" , http .ListenAndServe (
98- cnf .Server . Profile .Address ,
112+ cnf .Profile .Address ,
99113 mux ,
100114 ))
101115 }()
@@ -105,11 +119,11 @@ func NewServer(cnf *config.Service) *cobra.Command {
105119 path , handler := v1connect .NewGreeterServiceHandler (new (server.Connect ))
106120 mux .Handle (path , handler )
107121 srv := & http.Server {
108- Addr : cnf .Server . Connect .Address ,
122+ Addr : cnf .Connect .Address ,
109123 Handler : h2c .NewHandler (mux , new (http2.Server )),
110124 }
111125 go func () {
112- cmd .Println ("rpc server starts listening" , cnf .Server . Connect .BaseURL ())
126+ cmd .Println ("rpc server starts listening" , cnf .Connect .BaseURL ())
113127 cmd .Println ("rpc server status:" , srv .ListenAndServe ())
114128 }()
115129 err := sync .Termination ().Wait (cmd .Context ())
@@ -120,10 +134,7 @@ func NewServer(cnf *config.Service) *cobra.Command {
120134 return err
121135 },
122136 }
123- flags := command .PersistentFlags ()
124- flags .StringVarP (& path , "config" , "c" , "config.toml" , "path to config file" )
125- flags .StringVar (& cnf .Server .Connect .Address , "host" , "" , "remote rpc host" )
126-
127- command .AddCommand ( /* add related commands */ )
137+ flags := command .Flags ()
138+ flags .StringVar (& cnf .Connect .Address , "host" , "" , "remote rpc host" )
128139 return & command
129140}
0 commit comments