2024-10-21 21:03:22 +02:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.espin.casa/albert/cml04-falcon-system/internal/types"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const TimeOut time.Duration = time.Second * 2
|
|
|
|
|
|
|
|
type DBConfig struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type NATSconfig struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
func Nats(conf *NATSconfig) (*nats.Conn, error) {
|
|
|
|
// NATS connect
|
|
|
|
nc, err := nats.Connect(fmt.Sprintf("nats://%s:%d", conf.Host, conf.Port))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DataBase(conf *DBConfig) (*gorm.DB, error) {
|
|
|
|
// create dsn string
|
|
|
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Europe/Madrid",
|
|
|
|
conf.Host,
|
|
|
|
conf.Username,
|
|
|
|
conf.Password,
|
|
|
|
conf.Name,
|
|
|
|
conf.Port,
|
|
|
|
)
|
|
|
|
// create open database connection
|
|
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service interface {
|
|
|
|
Close(ctx context.Context) error
|
|
|
|
StoreLabelData(ctx context.Context, data *types.Label) error
|
|
|
|
PublishNewLabelData(ctx context.Context, id string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
db *gorm.DB
|
|
|
|
nc *nats.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements Service.
|
|
|
|
func (s *service) Close(ctx context.Context) error {
|
|
|
|
if err := s.nc.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.nc.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublishNewLabelData implements Service.
|
|
|
|
func (s *service) PublishNewLabelData(ctx context.Context, id string) error {
|
2024-10-23 14:26:40 +02:00
|
|
|
_, err := s.nc.Request("svc.new.label", []byte(id), TimeOut)
|
2024-10-21 21:03:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreLabelData implements Service.
|
|
|
|
func (s *service) StoreLabelData(ctx context.Context, data *types.Label) error {
|
|
|
|
return s.db.WithContext(ctx).Save(data).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func AutoMigrate(db *gorm.DB) error {
|
|
|
|
return db.AutoMigrate(&types.Label{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(dbConf *DBConfig, natsConf *NATSconfig) (Service, error) {
|
|
|
|
// create data base
|
|
|
|
db, err := DataBase(dbConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// auto migrate
|
|
|
|
if err := AutoMigrate(db); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// create nc connection
|
|
|
|
nc, err := Nats(natsConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &service{
|
|
|
|
db: db,
|
|
|
|
nc: nc,
|
|
|
|
}, nil
|
|
|
|
}
|