wasm-nats-stream-client/main.go

58 lines
1.8 KiB
Go
Raw Normal View History

2024-12-14 00:26:30 +00:00
//go:generate go run github.com/bytecodealliance/wasm-tools-go/cmd/wit-bindgen-go generate --world hello --out gen ./wit
package main
import (
2025-01-04 22:58:31 +00:00
logger "gitea.rebus.ninja/lore/wasm-nats-stream-client/gen/wasi/logging/logging"
"gitea.rebus.ninja/lore/wasm-nats-stream-client/gen/wasmcloud/messaging/consumer"
2024-12-14 00:26:30 +00:00
"gitea.rebus.ninja/lore/wasm-nats-stream-client/gen/wasmcloud/messaging/handler"
2025-01-04 22:58:31 +00:00
"gitea.rebus.ninja/lore/wasm-nats-stream-client/gen/wasmcloud/messaging/types"
"github.com/bytecodealliance/wasm-tools-go/cm"
2024-12-14 00:26:30 +00:00
)
2025-01-04 22:58:31 +00:00
type messagingConsumerAdapter struct {
Publish func(msg types.BrokerMessage) (result cm.Result[string, struct{}, string])
}
// NOTE(lxf): this is overridden in tests
var messagingConsumer = &messagingConsumerAdapter{
Publish: consumer.Publish,
}
2024-12-14 00:26:30 +00:00
func init() {
handler.Exports.HandleMessage = handleMessage
}
2025-01-04 22:58:31 +00:00
func handleMessage(msg types.BrokerMessage) cm.Result[string, struct{}, string] {
logger.Log(logger.LevelInfo,"MessageHandler", "Received message on subject" + msg.Subject)
// TODO implement the logic to get the destination topic from the config
// dest_topic := config.GetAll()
dest_topic := "finish"
// TASK
arg := cm.LiftString[string, *uint8, uint8](msg.Body.Data(), uint8(msg.Body.Len()))
result := exec_task(arg)
// Send reply
reply := types.BrokerMessage{
Subject: dest_topic,
Body: cm.ToList([]byte(result)),
ReplyTo: cm.None[string](),
}
res := messagingConsumer.Publish(reply)
if res.IsErr() {
logger.Log(logger.LevelError, "MessageHandler", "Failed to send reply, error: " + *res.Err())
return res
}
return cm.OK[cm.Result[string, struct{}, string]](struct{}{})
}
2024-12-14 00:26:30 +00:00
// Since we don't run this program like a CLI, the `main` function is empty. Instead,
// we call the `handleRequest` function when an HTTP request is received.
func main() {}