Initial commit
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"api-service/pkg/logger"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -108,6 +109,64 @@ func (b *Broadcaster) BroadcastMessage(messageType string, data interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// BroadcastQris godoc
|
||||
// @Summary Broadcast a QRIS-related WebSocket message
|
||||
// @Description Creates and broadcasts a WebSocket message with the specified type and data for QRIS operations
|
||||
// @Tags WebSocket QRIS
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param messageType path string true "Type of the QRIS message to broadcast"
|
||||
// @Param data body interface{} true "QRIS data payload for the message"
|
||||
// @Success 200 {object} map[string]string "QRIS message successfully queued for broadcast"
|
||||
// @Failure 500 {object} map[string]string "Failed to queue QRIS message (queue full)"
|
||||
// @Router /api/v1/ws/broadcast/qris [post]
|
||||
func (b *Broadcaster) BroadcastQris(messageType string, data interface{}) {
|
||||
msg := NewWebSocketMessage(MessageType(messageType), data, "", "")
|
||||
|
||||
select {
|
||||
case b.hub.messageQueue <- msg:
|
||||
default:
|
||||
// Antrian penuh, abaikan pesan
|
||||
logger.Error("Message queue full, dropping message")
|
||||
}
|
||||
|
||||
// Show posdevice if present
|
||||
// if m, ok := data.(map[string]interface{}); ok {
|
||||
// fmt.Println("BroadcastQris called with IP display: ", m["posdevice"])
|
||||
// }
|
||||
|
||||
//tabel m_deviceqris
|
||||
//kolom posdevice dari nama lokasi jadi ip display
|
||||
//kolom ip dari ip simrs
|
||||
}
|
||||
|
||||
// BroadcastCheck godoc
|
||||
// @Summary Broadcast a WebSocket message
|
||||
// @Description Creates and broadcasts a WebSocket message with the specified type and data
|
||||
// @Tags WebSocket QRIS
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param messageType path string true "Type of the message to broadcast"
|
||||
// @Param data body interface{} true "Data payload for the message"
|
||||
// @Success 200 {object} map[string]string "Message successfully queued for broadcast"
|
||||
// @Failure 500 {object} map[string]string "Failed to queue message (queue full)"
|
||||
// @Router /api/v1/ws/broadcast/check [post]
|
||||
func (b *Broadcaster) BroadcastCheck(messageType string, data interface{}) {
|
||||
msg := NewWebSocketMessage(MessageType(messageType), data, "", "")
|
||||
|
||||
select {
|
||||
case b.hub.messageQueue <- msg:
|
||||
default:
|
||||
// Antrian penuh, abaikan pesan
|
||||
logger.Error("Message queue full, dropping message")
|
||||
}
|
||||
|
||||
// Show posdevice if present
|
||||
// if m, ok := data.(map[string]interface{}); ok {
|
||||
// fmt.Println("BroadcastCheck called with IP display: ", m["posdevice"])
|
||||
// }
|
||||
}
|
||||
|
||||
// BroadcastToRoom mengirim pesan ke ruangan tertentu
|
||||
func (b *Broadcaster) BroadcastToRoom(room string, messageType string, data interface{}) {
|
||||
msg := NewWebSocketMessage(
|
||||
|
||||
@@ -40,7 +40,7 @@ func (h *DatabaseHandler) handleDatabaseInsert(client *Client, message WebSocket
|
||||
return nil
|
||||
}
|
||||
|
||||
table, ok := data["table"].(string)
|
||||
table, ok := data["m_deviceqris"].(string)
|
||||
if !ok || table == "" {
|
||||
client.sendErrorResponse("Invalid table name", "table is required")
|
||||
return nil
|
||||
@@ -55,7 +55,7 @@ func (h *DatabaseHandler) handleDatabaseInsert(client *Client, message WebSocket
|
||||
// Perform actual database insert
|
||||
if h.hub.dbService != nil {
|
||||
// Get database connection
|
||||
db, err := h.hub.GetDatabaseConnection("postgres_satudata")
|
||||
db, err := h.hub.GetDatabaseConnection("simrs_backup")
|
||||
if err != nil {
|
||||
client.sendErrorResponse("Database connection error", err.Error())
|
||||
return nil
|
||||
@@ -110,14 +110,14 @@ func (h *DatabaseHandler) handleDatabaseQuery(client *Client, message WebSocketM
|
||||
return nil
|
||||
}
|
||||
|
||||
table, ok := data["table"].(string)
|
||||
table, ok := data["m_deviceqris"].(string)
|
||||
if !ok || table == "" {
|
||||
client.sendErrorResponse("Invalid table name", "table is required")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Execute query
|
||||
results, err := h.hub.ExecuteDatabaseQuery("postgres_satudata", fmt.Sprintf("SELECT * FROM %s LIMIT 100", table))
|
||||
results, err := h.hub.ExecuteDatabaseQuery("simrs_backup", fmt.Sprintf("SELECT * FROM %s LIMIT 100", table))
|
||||
if err != nil {
|
||||
client.sendErrorResponse("Database query error", err.Error())
|
||||
return nil
|
||||
@@ -142,7 +142,7 @@ func (h *DatabaseHandler) handleDatabaseCustomQuery(client *Client, message WebS
|
||||
|
||||
database, ok := data["database"].(string)
|
||||
if !ok || database == "" {
|
||||
database = "postgres_satudata"
|
||||
database = "simrs_backup"
|
||||
}
|
||||
|
||||
query, ok := data["query"].(string)
|
||||
|
||||
@@ -64,7 +64,7 @@ type ActivityLog struct {
|
||||
// DatabaseService mendefinisikan interface untuk layanan database
|
||||
type DatabaseService interface {
|
||||
Health() map[string]interface{}
|
||||
ListDBs() []string
|
||||
//ListDBs() []string
|
||||
ListenForChanges(ctx context.Context, dbName string, channels []string, callback func(channel, payload string)) error
|
||||
NotifyChange(dbName, channel, payload string) error
|
||||
GetDB(name string) (*sql.DB, error)
|
||||
@@ -345,9 +345,9 @@ func (h *Hub) GetStats() map[string]interface{} {
|
||||
// setupDatabaseListeners sets up database change listeners for real-time updates
|
||||
func (h *Hub) setupDatabaseListeners() {
|
||||
// Listen for changes on retribusi table
|
||||
channels := []string{"retribusi_changes", "data_changes"}
|
||||
channels := []string{"data_changes"}
|
||||
|
||||
err := h.dbService.ListenForChanges(h.ctx, "postgres_satudata", channels, func(channel, payload string) {
|
||||
err := h.dbService.ListenForChanges(h.ctx, "simrs_backup", channels, func(channel, payload string) {
|
||||
h.handleDatabaseChange(channel, payload)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user