-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
what's new: - database factory - server factory
- Loading branch information
1 parent
00bc34b
commit 9ad1170
Showing
31 changed files
with
456 additions
and
1,924 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.