-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from ultravioletrs/NOISSUE-updateserver
NOISSUE - Move HTTP and GRPC Server Implementations To Internal
- Loading branch information
Showing
120 changed files
with
23,807 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package server contains the HTTP, gRPC and CoAP server implementation. | ||
package server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package grpc contains the gRPC server implementation. | ||
package grpc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/mainflux/mainflux/logger" | ||
"github.com/ultravioletrs/agent/internal/server" | ||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
) | ||
|
||
const ( | ||
stopWaitTime = 5 * time.Second | ||
) | ||
|
||
type Server struct { | ||
server.BaseServer | ||
server *grpc.Server | ||
registerService serviceRegister | ||
} | ||
|
||
type serviceRegister func(srv *grpc.Server) | ||
|
||
var _ server.Server = (*Server)(nil) | ||
|
||
func New(ctx context.Context, cancel context.CancelFunc, name string, config server.Config, registerService serviceRegister, logger logger.Logger) server.Server { | ||
var listenFullAddress = fmt.Sprintf("%s:%s", config.Host, config.Port) | ||
|
||
return &Server{ | ||
BaseServer: server.BaseServer{ | ||
Ctx: ctx, | ||
Cancel: cancel, | ||
Name: name, | ||
Address: listenFullAddress, | ||
Config: config, | ||
Logger: logger, | ||
}, | ||
registerService: registerService, | ||
} | ||
} | ||
|
||
func (s *Server) Start() error { | ||
var errCh = make(chan error) | ||
|
||
listener, err := net.Listen("tcp", s.Address) | ||
if err != nil { | ||
return fmt.Errorf("failed to listen on port %s: %w", s.Address, err) | ||
} | ||
|
||
switch { | ||
case s.Config.CertFile != "" || s.Config.KeyFile != "": | ||
creds, err := credentials.NewServerTLSFromFile(s.Config.CertFile, s.Config.KeyFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to load auth certificates: %w", err) | ||
} | ||
s.Logger.Info(fmt.Sprintf("%s service gRPC server listening at %s with TLS cert %s and key %s", s.Name, s.Address, s.Config.CertFile, s.Config.KeyFile)) | ||
s.server = grpc.NewServer( | ||
grpc.Creds(creds), | ||
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()), | ||
) | ||
default: | ||
s.Logger.Info(fmt.Sprintf("%s service gRPC server listening at %s without TLS", s.Name, s.Address)) | ||
s.server = grpc.NewServer( | ||
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()), | ||
) | ||
} | ||
|
||
s.registerService(s.server) | ||
|
||
go func() { | ||
errCh <- s.server.Serve(listener) | ||
}() | ||
|
||
select { | ||
case <-s.Ctx.Done(): | ||
return s.Stop() | ||
case err := <-errCh: | ||
s.Cancel() | ||
|
||
return err | ||
} | ||
} | ||
|
||
func (s *Server) Stop() error { | ||
defer s.Cancel() | ||
var c = make(chan bool) | ||
go func() { | ||
defer close(c) | ||
s.server.GracefulStop() | ||
}() | ||
select { | ||
case <-c: | ||
case <-time.After(stopWaitTime): | ||
} | ||
s.Logger.Info(fmt.Sprintf("%s gRPC service shutdown at %s", s.Name, s.Address)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package http contains the HTTP server implementation. | ||
package http |
Oops, something went wrong.