Browse Source

add cli handle option supported

fancl 2 years ago
parent
commit
0beddb8656
3 changed files with 39 additions and 14 deletions
  1. 33 10
      gateway/cli/server.go
  2. 5 3
      reporter.go
  3. 1 1
      service.go

+ 33 - 10
gateway/cli/server.go

@@ -24,6 +24,15 @@ const (
 
 type HandleFunc func(ctx *Context) (err error)
 
+type (
+	Options struct {
+		Usage       string
+		Description string
+	}
+
+	Option func(o *Options)
+)
+
 type Server struct {
 	seq        int32
 	ctx        context.Context
@@ -109,13 +118,17 @@ func (svr *Server) process(id int32, conn net.Conn) (err error) {
 	return
 }
 
-func (svr *Server) Handle(path string, cb HandleFunc) {
+func (svr *Server) Handle(path string, cb HandleFunc, args ...Option) {
 	var tokens []string
 	if strings.HasPrefix(path, "/") {
 		tokens = strings.Split(path[1:], "/")
 	} else {
 		tokens = utils.BreakUp(path)
 	}
+	opts := &Options{}
+	for _, o := range args {
+		o(opts)
+	}
 	svr.locker.Lock()
 	defer svr.locker.Unlock()
 	var (
@@ -149,6 +162,12 @@ func (svr *Server) Handle(path string, cb HandleFunc) {
 			p = q
 		} else {
 			q = NewExecutor(token, "", strings.Title(strings.Join(tokens, " ")))
+			if opts.Usage != "" {
+				q.usage = opts.Usage
+			}
+			if opts.Description != "" {
+				q.description = opts.Description
+			}
 			if i == length-1 {
 				q.handleFunc = cb
 			}
@@ -173,11 +192,10 @@ func (svr *Server) tcpServe(listener net.Listener) {
 	}
 }
 
-func (svr *Server) unixServe(filename string) {
+func (svr *Server) unixServe(filename string) (listener net.Listener) {
 	var (
-		err      error
-		addr     *net.UnixAddr
-		listener net.Listener
+		err  error
+		addr *net.UnixAddr
 	)
 	if addr, err = net.ResolveUnixAddr("unix", filename); err != nil {
 		return
@@ -186,14 +204,17 @@ func (svr *Server) unixServe(filename string) {
 		return
 	}
 	svr.tcpServe(listener)
-	err = listener.Close()
+	return
 }
 
 func (svr *Server) Serve(listener net.Listener) (err error) {
 	svr.Handle("help", func(ctx *Context) (err error) {
 		return ctx.Success(svr.executor.String())
 	})
-	var wg sync.WaitGroup
+	var (
+		wg           sync.WaitGroup
+		unixListener net.Listener
+	)
 	if listener != nil {
 		wg.Add(1)
 		go func() {
@@ -202,13 +223,15 @@ func (svr *Server) Serve(listener net.Listener) (err error) {
 		}()
 	}
 	if runtime.GOOS == "linux" {
-		wg.Add(1)
 		go func() {
-			svr.unixServe(path.Join(os.TempDir(), os.Getenv("MICRO_SERVICE_NAME")+".sock"))
-			wg.Done()
+			//listen unix
+			unixListener = svr.unixServe(path.Join(os.TempDir(), os.Getenv("MICRO_SERVICE_NAME")+".sock"))
 		}()
 	}
 	wg.Wait()
+	if unixListener != nil {
+		err = unixListener.Close()
+	}
 	return
 }
 

+ 5 - 3
reporter.go

@@ -41,10 +41,11 @@ type serviceInfo struct {
 	OS          string `json:"os"`
 	Cpus        int    `json:"cpus"`
 	Memory      int    `json:"memory"`
+	Port        int    `json:"port"`
 	GOVersion   string `json:"go_version"`
 }
 
-func serviceReportAndChecked(ctx context.Context, serviceName string, serviceVersion string) (state int, err error) {
+func serviceReportAndChecked(ctx context.Context, opts *Options) (state int, err error) {
 	var (
 		buf      []byte
 		req      *http.Request
@@ -55,12 +56,13 @@ func serviceReportAndChecked(ctx context.Context, serviceName string, serviceVer
 		recover()
 	}()
 	si := &serviceInfo{
-		ServiceName: serviceName,
-		Version:     serviceVersion,
+		ServiceName: opts.shortName,
+		Version:     opts.Version,
 		IP:          ip.InternalIP(),
 		OS:          runtime.GOOS,
 		Cpus:        runtime.NumCPU(),
 		GOVersion:   runtime.Version(),
+		Port:        opts.Port,
 	}
 	memState = &runtime.MemStats{}
 	runtime.ReadMemStats(memState)

+ 1 - 1
service.go

@@ -432,7 +432,7 @@ func (svr *Service) prepare() (err error) {
 	if svr.opts.EnableReport {
 		rn := rand.Int31n(60) + 30
 		svr.DeferTick(time.Minute*time.Duration(rn), func(ctx context.Context) {
-			if state, err2 := serviceReportAndChecked(svr.ctx, svr.node.Name, svr.node.Version); err2 == nil {
+			if state, err2 := serviceReportAndChecked(svr.ctx, svr.opts); err2 == nil {
 				if state == serviceStateUnavailable {
 					os.Exit(1)
 				}