97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
package app
|
|
|
|
import (
|
|
"flag"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"git.espin.casa/albert/cml04-gdm-int/internal/logging"
|
|
"git.espin.casa/albert/cml04-gdm-int/internal/rspdb"
|
|
"git.espin.casa/albert/cml04-gdm-int/internal/server"
|
|
"git.espin.casa/albert/cml04-gdm-int/internal/service"
|
|
"git.espin.casa/albert/cml04-gdm-int/pkg/api"
|
|
"git.espin.casa/albert/logger"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/reflection"
|
|
)
|
|
|
|
const (
|
|
NSQTopic string = "RSMIN"
|
|
)
|
|
|
|
func Run() error {
|
|
// flags
|
|
restAddress := flag.String("rest-addr", "0.0.0.0:3333", "http port")
|
|
logLevel := flag.String("level", "debug", "Log level")
|
|
flag.Parse()
|
|
// setup logger
|
|
log := logger.New(os.Stdout, *logLevel)
|
|
// log fields
|
|
logFields := logger.LogFields{
|
|
"log_level": *logLevel,
|
|
"rest_address": *restAddress,
|
|
}
|
|
// create roll shop db
|
|
rollShopDB, err := rspdb.NewRspDB(rspdb.RspDBDefaultConfig())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rollShopDB.Close()
|
|
// create service
|
|
svc := service.NewService(rollShopDB)
|
|
// create logging service
|
|
svc = logging.NewLoggingService(log, svc)
|
|
// create rest server
|
|
server := server.NewServer(svc)
|
|
// start service
|
|
s := grpc.NewServer()
|
|
// register service
|
|
api.RegisterRollDataServiceServer(s, server)
|
|
// reflection
|
|
reflection.Register(s)
|
|
// start server
|
|
// create grpc server
|
|
lis, err := net.Listen("tcp", *restAddress)
|
|
if err != nil {
|
|
log.Error("failed to listen", err, logFields)
|
|
return err
|
|
}
|
|
// start service server
|
|
go func() {
|
|
if err := s.Serve(lis); err != nil {
|
|
log.Error("failed to serve gRPC", err, logFields)
|
|
return
|
|
}
|
|
}()
|
|
// info banner
|
|
log.Info("started cml04-gdm-int service", logFields)
|
|
// wait signal to finish
|
|
signal := WaitSignal()
|
|
log.Info("signal received", logFields.Add(logger.LogFields.Add(logFields, logger.LogFields{
|
|
"signal": signal,
|
|
})))
|
|
// stop grpc service
|
|
s.GracefulStop()
|
|
return nil
|
|
}
|
|
|
|
// WaitSignal catching exit signal
|
|
func WaitSignal() os.Signal {
|
|
ch := make(chan os.Signal, 2)
|
|
signal.Notify(
|
|
ch,
|
|
syscall.SIGINT,
|
|
syscall.SIGQUIT,
|
|
syscall.SIGTERM,
|
|
)
|
|
for {
|
|
sig := <-ch
|
|
switch sig {
|
|
case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM:
|
|
return sig
|
|
}
|
|
}
|
|
}
|