/** * Handles a request from WS/HTTP */ func HandleRequestMessage(inMsg models.BaseMessage) { // Create Queue message from original msg := models.QueueMessage{ BaseMessage: inMsg, Sender: inMsg.Source, } // Lookup message type switch msg.Target { // Request for data, direct to basic module case "data": msg.Receiver = "modules.basic" // TODO: Add more options here // Unknown target default: fmt.Printf("Unknown message target: %s", msg.Target) return } // Call sender handler q.SendMessage(msg) }
/** * Handles logic for a message coming from a Worker */ func handleWorkerMessage(msg models.QueueMessage) { // Lookup worker sender switch msg.Sender { // Coming from DB Worker case "workers.db": // Lookup Target switch msg.Target { // Data request target case "data": // Lookup Action switch msg.Action { // DB action case "index", "post", "get", "update", "delete": // TODO: Logic here for needed steps. for now considering one-off tasks msg.Receiver = msg.Source // TODO: More Worker receivers here // Unknown action default: fmt.Printf("Invalid action '%s' for Basic Module", msg.Action) return } // Unknown target default: fmt.Printf("Invalid message target '%s' for Basic Module: ", msg.Target) return } // Unknown target default: fmt.Printf("Invalid message source '%s' for Basic Module: ", msg.Sender) return } // Set the sender to current module msg.Sender = ID // Send the message to queue q.SendMessage(msg) }
/** * Handles a received message from DB Queue, sending relevant info to connected client */ func HandleQueueMessage(msg models.QueueMessage) { // Validations // TODO: Abstract this please if msg.Target == "" || msg.Target != "data" { fmt.Println("Invalid or missing target defined in message!") return } if msg.Code == "" { fmt.Println("No entity/code defined in message!") return } if msg.Action == "" { fmt.Println("No action defined in message!") return } if msg.Data == nil { fmt.Println("No data provided in message!") return } // Variables for query result var result interface{} var err error // Check what type of entity we're dealing with switch msg.Code { case "game": err, result = dbModels.Games{}.HandleMessage(msg.BaseMessage) default: fmt.Printf("Unknown entity: %s\n", msg.Code) return } // Error from query if err != nil { fmt.Println(err.Error()) return } // Parse result from relevant interface model to bytes data, err := json.Marshal(result) if err != nil { fmt.Println(err.Error()) return } // Parse previous bytes to generic map var outData = make(map[string]interface{}) if err := json.Unmarshal(data, &outData); err != nil { fmt.Println(err.Error()) return } // Build base response message and init final one msg.Receiver = msg.Sender msg.Sender = ID msg.Data = outData // Send it q.SendMessage(msg) }