github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/internal/appapi/interfaces.go (about)

     1  package appapi
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"net/http"
     7  
     8  	"github.com/Asutorufa/yuhaiin/pkg/components/config"
     9  	"github.com/Asutorufa/yuhaiin/pkg/components/tools"
    10  	"github.com/Asutorufa/yuhaiin/pkg/log"
    11  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
    12  	"github.com/Asutorufa/yuhaiin/pkg/node"
    13  	gc "github.com/Asutorufa/yuhaiin/pkg/protos/config/grpc"
    14  	gn "github.com/Asutorufa/yuhaiin/pkg/protos/node/grpc"
    15  	gs "github.com/Asutorufa/yuhaiin/pkg/protos/statistic/grpc"
    16  	gt "github.com/Asutorufa/yuhaiin/pkg/protos/tools"
    17  	"github.com/Asutorufa/yuhaiin/pkg/sysproxy"
    18  	"go.etcd.io/bbolt"
    19  	"google.golang.org/grpc"
    20  )
    21  
    22  type Components struct {
    23  	Mux *http.ServeMux
    24  	*Start
    25  	HttpListener net.Listener
    26  	Node         *node.Nodes
    27  	Tools        *tools.Tools
    28  	Subscribe    gn.SubscribeServer
    29  	Connections  gs.ConnectionsServer
    30  	Tag          gn.TagServer
    31  	DB           *bbolt.DB
    32  }
    33  
    34  func (app *Components) RegisterGrpcService() {
    35  	so := app.Start
    36  	if so.GRPCServer == nil {
    37  		return
    38  	}
    39  
    40  	so.GRPCServer.RegisterService(&gc.ConfigService_ServiceDesc, so.Setting)
    41  	so.GRPCServer.RegisterService(&gn.Node_ServiceDesc, app.Node)
    42  	so.GRPCServer.RegisterService(&gn.Subscribe_ServiceDesc, app.Subscribe)
    43  	so.GRPCServer.RegisterService(&gs.Connections_ServiceDesc, app.Connections)
    44  	so.GRPCServer.RegisterService(&gn.Tag_ServiceDesc, app.Tag)
    45  	so.GRPCServer.RegisterService(&gt.Tools_ServiceDesc, app.Tools)
    46  }
    47  func (a *Components) Close() error {
    48  	for _, v := range a.Start.closers {
    49  		v.Close()
    50  	}
    51  
    52  	log.Close()
    53  
    54  	sysproxy.Unset()
    55  
    56  	return nil
    57  }
    58  
    59  type Start struct {
    60  	ConfigPath string
    61  	Host       string
    62  	Setting    config.Setting
    63  
    64  	ProcessDumper netapi.ProcessDumper
    65  	GRPCServer    *grpc.Server
    66  	closers       []io.Closer
    67  }
    68  
    69  func (a *Start) AddCloser(name string, z io.Closer) {
    70  	a.closers = append(a.closers, &moduleCloser{z, name})
    71  }
    72  
    73  type moduleCloser struct {
    74  	io.Closer
    75  	name string
    76  }
    77  
    78  func (m *moduleCloser) Close() error {
    79  	log.Info("close", "module", m.name)
    80  	defer log.Info("closed", "module", m.name)
    81  	return m.Closer.Close()
    82  }