Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pieceowater committed Oct 1, 2024
1 parent 0d82ea2 commit 2a6daca
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 68 deletions.
File renamed without changes.
95 changes: 49 additions & 46 deletions gossiper.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
package gossiper

import (
bootstrap "github.com/pieceowater-dev/lotof.lib.gossiper/internal/bootstrap"
config "github.com/pieceowater-dev/lotof.lib.gossiper/internal/config"
network "github.com/pieceowater-dev/lotof.lib.gossiper/internal/consume/mqp"
environment "github.com/pieceowater-dev/lotof.lib.gossiper/internal/environment"
tools "github.com/pieceowater-dev/lotof.lib.gossiper/internal/tools"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/boot"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/conf"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/env"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/infra"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/tools"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/tools/formats/errors"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/tools/formats/filter"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/tools/formats/pagination"
t "github.com/pieceowater-dev/lotof.lib.gossiper/types"
)

// ENVIRONMENT
/* ENVIRONMENT */

// Env is an alias for the environment.Env
type Env = environment.Env
type Env = env.Env

// EnvVars is a pointer alias for the &environment.EnvVars
var EnvVars = &environment.EnvVars
var EnvVars = &env.EnvVars

// NETWORK
/* NETWORK */

// AMQP is an alias for the network.AMQP
type AMQP = network.AMQP
type AMQP = infra.AMQP

// AMQMessage is an alias for the network.DefaultMessage
type AMQMessage = network.DefaultMessage
// AMQMessage is an alias for the infra.DefaultMessage
type AMQMessage = infra.DefaultMessage

// CONFIG
/* CONFIG */

// Config is an alias for the config.Config
type Config = config.Config
type Config = conf.Config

// EnvConfig is an alias for the config.EnvConfig
type EnvConfig = config.EnvConfig
type EnvConfig = conf.EnvConfig

// QueueConfig is an alias for the config.QueueConfig
type QueueConfig = config.QueueConfig
type QueueConfig = conf.QueueConfig

// AMQPConsumerConfig is an alias for the config.AMQPConsumerConfig
type AMQPConsumerConfig = config.AMQPConsumerConfig
type AMQPConsumerConfig = conf.AMQPConsumerConfig

// AMQPConsumeConfig is an alias for the config.AMQPConsumeConfig
type AMQPConsumeConfig = config.AMQPConsumeConfig
type AMQPConsumeConfig = conf.AMQPConsumeConfig

// TOOLS
/* TOOLS */

// Tools is an alias for the tools.Tools
type Tools = tools.Tools
Expand All @@ -60,54 +63,54 @@ func LogAction(action string, data any) {
}

// NewServiceError is an alias for the Tools.NewServiceError method.
func NewServiceError(message string) *tools.ServiceError {
return tools.NewServiceError(message)
func NewServiceError(message string) *errors.ServiceError {
return errors.NewServiceError(message)
}

// Enum with aliases for predefined pagination page length
const (
TEN = tools.TEN
FIFTEEN = tools.FIFTEEN
TWENTY = tools.TWENTY
TWENTY_FIVE = tools.TWENTY_FIVE
THIRTY = tools.THIRTY
THIRTY_FIVE = tools.THIRTY_FIVE
FORTY = tools.FORTY
FORTY_FIVE = tools.FORTY_FIVE
FIFTY = tools.FIFTY
FIFTY_FIVE = tools.FIFTY_FIVE
SIXTY = tools.SIXTY
SIXTY_FIVE = tools.SIXTY_FIVE
SEVENTY = tools.SEVENTY
SEVENTY_FIVE = tools.SEVENTY_FIVE
EIGHTY = tools.EIGHTY
EIGHTY_FIVE = tools.EIGHTY_FIVE
NINETY = tools.NINETY
NINETY_FIVE = tools.NINETY_FIVE
ONE_HUNDRED = tools.ONE_HUNDRED
TEN = filter.TEN
FIFTEEN = filter.FIFTEEN
TWENTY = filter.TWENTY
TWENTY_FIVE = filter.TWENTY_FIVE
THIRTY = filter.THIRTY
THIRTY_FIVE = filter.THIRTY_FIVE
FORTY = filter.FORTY
FORTY_FIVE = filter.FORTY_FIVE
FIFTY = filter.FIFTY
FIFTY_FIVE = filter.FIFTY_FIVE
SIXTY = filter.SIXTY
SIXTY_FIVE = filter.SIXTY_FIVE
SEVENTY = filter.SEVENTY
SEVENTY_FIVE = filter.SEVENTY_FIVE
EIGHTY = filter.EIGHTY
EIGHTY_FIVE = filter.EIGHTY_FIVE
NINETY = filter.NINETY
NINETY_FIVE = filter.NINETY_FIVE
ONE_HUNDRED = filter.ONE_HUNDRED
)

// PaginatedEntity is a wrapper for tools.PaginatedEntity
type PaginatedEntity[T any] struct {
tools.PaginatedEntity[T]
pagination.PaginatedEntity[T]
}

// NewFilter creates a new DefaultFilter instance.
func NewFilter[T any]() t.DefaultFilter[T] {
return tools.NewDefaultFilter[T]()
return filter.NewDefaultFilter[T]()
}

// ToPaginated PaginatedEntity directly uses tools.PaginatedEntity
func ToPaginated[T any](items []T, count int) PaginatedEntity[T] {
return PaginatedEntity[T]{tools.ToPaginated[T](items, count)}
return PaginatedEntity[T]{pagination.ToPaginated[T](items, count)}
}

// BOOTSTRAP
/* BOOTSTRAP */

type Bootstrap = bootstrap.Bootstrap
type Bootstrap = boot.Bootstrap

// Setup is an alias for the Bootstrap.Setup method.
func Setup(cfg config.Config, startupFunc func() any, messageHandler func([]byte) any) {
func Setup(cfg conf.Config, startupFunc func() any, messageHandler func([]byte) any) {
b := Bootstrap{}
b.Setup(cfg, startupFunc, messageHandler)
}
16 changes: 8 additions & 8 deletions internal/bootstrap/setup.go → internal/boot/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package gossiper
package boot

import (
"github.com/fatih/color"
config "github.com/pieceowater-dev/lotof.lib.gossiper/internal/config"
network "github.com/pieceowater-dev/lotof.lib.gossiper/internal/consume/mqp"
environment "github.com/pieceowater-dev/lotof.lib.gossiper/internal/environment"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/conf"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/env"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/infra"
"log"
)

Expand All @@ -18,13 +18,13 @@ type Bootstrap struct {
// - cfg: the configuration structure containing environment and AMQP settings.
// - messageHandler: a callback function to handle incoming RabbitMQ messages.
// - startupFunc: a function to execute after environment initialization.
func (b *Bootstrap) Setup(cfg config.Config, startupFunc func() any, messageHandler func([]byte) any) {
func (b *Bootstrap) Setup(cfg conf.Config, startupFunc func() any, messageHandler func([]byte) any) {
color.Set(color.FgGreen)
log.SetFlags(log.LstdFlags)
log.Println("Setting up Gossiper...")

env := &environment.Env{}
env.Init(cfg.Env.Required)
envInst := &env.Env{}
envInst.Init(cfg.Env.Required)

color.Set(color.FgCyan)
log.Println("Setup complete.")
Expand All @@ -35,6 +35,6 @@ func (b *Bootstrap) Setup(cfg config.Config, startupFunc func() any, messageHand
startupFunc()
}

net := &network.AMQP{ConsumerConfig: cfg.AMQPConsumer}
net := &infra.AMQP{ConsumerConfig: cfg.AMQPConsumer}
net.SetupAMQPConsumers(messageHandler)
}
2 changes: 1 addition & 1 deletion internal/config/conf.go → internal/conf/conf.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gossiper
package conf

import "github.com/rabbitmq/amqp091-go"

Expand Down
2 changes: 1 addition & 1 deletion internal/environment/env.go → internal/env/env.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gossiper
package env

import (
"errors"
Expand Down
12 changes: 6 additions & 6 deletions internal/consume/mqp/setup.go → internal/infra/amqp/amqp.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package gossiper
package amqp

import (
"encoding/json"
config "github.com/pieceowater-dev/lotof.lib.gossiper/internal/config"
environment "github.com/pieceowater-dev/lotof.lib.gossiper/internal/environment"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/conf"
"github.com/pieceowater-dev/lotof.lib.gossiper/internal/env"
amqp "github.com/rabbitmq/amqp091-go"
"log"
"strings"
)

// AMQP holds the RabbitMQ consumer configuration
type AMQP struct {
ConsumerConfig config.AMQPConsumerConfig // Configuration for RabbitMQ consumers
ConsumerConfig conf.AMQPConsumerConfig // Configuration for RabbitMQ consumers
}

// DefaultMessage defines the default message structure for Gossiper
Expand Down Expand Up @@ -43,8 +43,8 @@ func (n *AMQP) SetupAMQPConsumers(messageHandler func([]byte) any) {
}

// Load RabbitMQ DSN from environment variables
env := environment.Env{}
dsn, err := env.Get(n.ConsumerConfig.DSNEnv)
envInst := env.Env{}
dsn, err := envInst.Get(n.ConsumerConfig.DSNEnv)
if err != nil {
panic(err) // Panic if the DSN is missing or invalid
}
Expand Down
6 changes: 6 additions & 0 deletions internal/infra/infra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package infra

import "github.com/pieceowater-dev/lotof.lib.gossiper/internal/infra/amqp"

type AMQP = amqp.AMQP
type DefaultMessage = amqp.DefaultMessage
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gossiper
package errors

// ServiceError represents a custom error used throughout the application.
type ServiceError struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gossiper
package filter

import t "github.com/pieceowater-dev/lotof.lib.gossiper/types"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gossiper
package pagination

// EntityInfo provides additional metadata for a paginated entity.
type EntityInfo struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/tools/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gossiper
package tools

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion internal/tools/satisfies.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gossiper
package tools

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion internal/tools/utils.go → internal/tools/tools.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gossiper
package tools

import (
"encoding/json"
Expand Down

0 comments on commit 2a6daca

Please sign in to comment.