Skip to content

Commit

Permalink
refactor(agent): improve agent.go structure and documentation
Browse files Browse the repository at this point in the history
- Restructure code to enhance readability and maintainability
- Add comprehensive comments to clarify interface and method purposes
- Introduce new constructor functions for Agent interface:
  - NewAgent: supports both HTTP and gRPC
  - NewAgentWithGRPC: supports only gRPC
  - NewAgentWithHTTP: supports only HTTP
- Remove unused const declarations
  • Loading branch information
godcong committed Dec 24, 2024
1 parent 46a1b0f commit b73cdcc
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,62 @@ import (
"google.golang.org/grpc"
)

const (
ApiVersionV1 = "/api/v1"
DefaultPrefix = "/api"
DefaultVersion = "v1"
)
// ApiVersionV1 defines the version number of the API
const ApiVersionV1 = "/api/v1"

// DefaultPrefix defines the default API prefix
const DefaultPrefix = "/api"

// DefaultVersion defines the default API version
const DefaultVersion = "v1"

// Agent is an interface that combines the HTTPAgent and GRPCAgent interfaces
type Agent interface {
HTTPAgent
GRPCAgent
}

// HTTPAgent is an interface that defines the basic methods of an HTTP proxy
type HTTPAgent interface {
// URI returns the URI of the HTTP service
URI() string
// HTTPServer returns an instance of the HTTP server
HTTPServer() *http.Server
// Route returns an instance of the HTTP router
Route() *http.Router
}

// GRPCAgent is an interface that defines the basic methods of a gRPC proxy
type GRPCAgent interface {
// Server returns an instance of the gRPC server
Server() *grpc.Server
// RegisterService registers a gRPC service
RegisterService(desc *grpc.ServiceDesc, impl interface{})
}

// agent is an implementation of the Agent interface
type agent struct {
GRPCAgent
HTTPAgent
}

// NewAgent creates a new Agent instance that supports both HTTP and gRPC
func NewAgent(server *http.Server, grpcServer *grpc.Server) Agent {
return &agent{
GRPCAgent: NewGRPC(grpcServer),
HTTPAgent: NewHTTP(server),
}
}

// NewAgentWithGRPC creates a new Agent instance that only supports gRPC
func NewAgentWithGRPC(grpcServer *grpc.Server) Agent {
return &agent{
GRPCAgent: NewGRPC(grpcServer),
HTTPAgent: UnimplementedAgent,
}
}

// NewAgentWithHTTP creates a new Agent instance that only supports HTTP
func NewAgentWithHTTP(server *http.Server) Agent {
return &agent{
GRPCAgent: UnimplementedAgent,
Expand Down

0 comments on commit b73cdcc

Please sign in to comment.