mirror of
https://github.com/netscrawler/changeAPI.git
synced 2025-05-06 15:29:53 +00:00
commit5b11756af6
Author: netscrawler <mariarthyjamesoo@gmail.com> Date: Thu Jul 4 00:44:15 2024 +0300 v0.1 commitc99f477e35
Author: netscrawler <mariarthyjamesoo@gmail.com> Date: Sun Jun 30 20:44:53 2024 +0300 v0.0.6
64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package grpcapp
|
|
|
|
import (
|
|
convertgrpc "converter/internal/grpc/cnvrt"
|
|
"converter/internal/services/converter"
|
|
"fmt"
|
|
"google.golang.org/grpc"
|
|
"log/slog"
|
|
"net"
|
|
)
|
|
|
|
type App struct {
|
|
log *slog.Logger
|
|
gRPCServer *grpc.Server
|
|
port int
|
|
}
|
|
|
|
func New(
|
|
log *slog.Logger,
|
|
port int) *App {
|
|
gRPCServer := grpc.NewServer()
|
|
convert := converter.New(log)
|
|
convertgrpc.Register(gRPCServer, convert)
|
|
|
|
return &App{
|
|
log: log,
|
|
gRPCServer: gRPCServer,
|
|
port: port,
|
|
}
|
|
|
|
}
|
|
|
|
func (a *App) MustRun() {
|
|
if err := a.Run(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (a *App) Run() error {
|
|
const op = "grpcapp.Run"
|
|
log := a.log.With(
|
|
slog.String("op", op),
|
|
slog.Int("port", a.port))
|
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf(":%d", a.port))
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
log.Info("gRPC server is running", slog.String("addr", l.Addr().String()))
|
|
|
|
if err := a.gRPCServer.Serve(l); err != nil {
|
|
return fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *App) Stop() {
|
|
const op = "grpcapp.Stop"
|
|
|
|
a.log.With(slog.String("op", op)).
|
|
Info("stopping gRPC server", slog.Int("port", a.port))
|
|
a.gRPCServer.GracefulStop()
|
|
}
|