Skip to content

Commit

Permalink
v2.0.
Browse files Browse the repository at this point in the history
what's new:
- database factory
- server factory
  • Loading branch information
pieceowater committed Nov 30, 2024
1 parent 00bc34b commit 9ad1170
Show file tree
Hide file tree
Showing 31 changed files with 456 additions and 1,924 deletions.
102 changes: 19 additions & 83 deletions cmd/example/app.go
Original file line number Diff line number Diff line change
@@ -1,94 +1,30 @@
package main

import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/pieceowater-dev/lotof.lib.gossiper"
"gorm.io/gorm"
"log"
"google.golang.org/grpc"
)

// HandleMessage processes incoming RabbitMQ messages.
// It receives an AMQMessage, logs the pattern, and returns a response.
// This is where you can add custom logic to route or process messages.
func HandleMessage(msg gossiper.AMQMessage) any {
// Log the received message's pattern
log.Printf("Received message: %s", msg.Pattern)
return "OK" // Return a response; modify this as needed
}

//type SomeData struct {
// gorm.Model
// ID int `json:"id"`
// Data json.RawMessage `json:"data"`
//}

func main() {
// Define the Gossiper configuration for RabbitMQ and PostgreSQL
conf := gossiper.Config{
Env: gossiper.EnvConfig{
Required: []string{"RABBITMQ_DSN", "DATABASE_DSN"}, // Specify required environment variables for RabbitMQ and PostgreSQL
},
AMQPConsumer: gossiper.AMQPConsumerConfig{
DSNEnv: "RABBITMQ_DSN", // Environment variable for RabbitMQ DSN
Queues: []gossiper.QueueConfig{
{
Name: "template_queue", // Queue name from which messages will be consumed
Durable: true, // Set queue as persistent (survives RabbitMQ restarts)
},
},
Consume: []gossiper.AMQPConsumeConfig{
{
Queue: "template_queue", // Queue name to consume from
Consumer: "example_consumer", // Unique consumer tag for the connection
AutoAck: true, // Automatically acknowledge receipt of messages
},
},
},
Database: gossiper.DatabaseConfig{
PG: gossiper.DBPGConfig{
EnvPostgresDBDSN: "DATABASE_DSN", // Environment variable key for PostgreSQL DSN
AutoMigrate: true, // Enable auto-migration of models
Models: []any{
// Your models go here
// &yourModel{}, // Example: Define the models that will be auto-migrated
},
},
ClickHouse: gossiper.DBClickHouseConfig{
EnvClickHouseDBDSN: "CLICKHOUSE_DSN",
AutoMigrate: true,
Models: []any{
//&SomeData{},
},
GORMConfig: &gorm.Config{},
},
},
}
// Создаём менеджер серверов
serverManager := gossiper.NewServerManager()

// Initialize the Gossiper application and setup RabbitMQ consumers and PostgreSQL connection
// Pass a handler function to process each message that is consumed
app := gossiper.Bootstrap{}
app.Setup(
conf,
func() any {
// Custom startup logic to execute after initialization (if needed)
log.Println("Custom Setup here")
return nil
},
func(msg []byte) any {
var customMessage gossiper.AMQMessage

// Attempt to unmarshal the received message into a custom structure
err := json.Unmarshal(msg, &customMessage)
if err != nil {
log.Println("Failed to unmarshal custom message:", err)
return nil // Return nil in case of unmarshalling failure
}
// Инициализация gRPC сервера
grpcInitRoute := func(server *grpc.Server) {
// Пример: добавить маршруты
}
serverManager.AddServer(gossiper.NewGRPCServ("50051", grpc.NewServer(), grpcInitRoute))

// Delegate message processing to the HandleMessage function
return HandleMessage(customMessage)
},
)
// Инициализация REST сервера
restInitRoute := func(router *gin.Engine) {
router.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{"status": "ok"})
})
}
serverManager.AddServer(gossiper.NewRESTServ("8080", gin.Default(), restInitRoute))

// Log that the application has started successfully
log.Println("Application started")
// Запуск всех серверов
serverManager.StartAll()
defer serverManager.StopAll()
}
52 changes: 25 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,47 @@ module github.com/pieceowater-dev/lotof.lib.gossiper
go 1.23.0

require (
github.com/fatih/color v1.17.0
github.com/go-playground/validator/v10 v10.22.1
github.com/joho/godotenv v1.5.1
github.com/rabbitmq/amqp091-go v1.10.0
github.com/rs/zerolog v1.33.0
github.com/sirupsen/logrus v1.9.3
gorm.io/driver/clickhouse v0.6.1
github.com/gin-gonic/gin v1.10.0
google.golang.org/grpc v1.68.0
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.12
)

require (
github.com/ClickHouse/ch-go v0.61.5 // indirect
github.com/ClickHouse/clickhouse-go/v2 v2.23.2 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/paulmach/orb v0.11.1 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.6.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 9ad1170

Please sign in to comment.