From cb51ae31e759b866dd7b194066f49338b8e13fc4 Mon Sep 17 00:00:00 2001 From: Lorenzo Venerandi Date: Sat, 14 Dec 2024 01:12:59 +0100 Subject: [PATCH] added destination topic to config --- bindings.wadge.go | 41 ++++++++++++ messaging.go | 79 ++++++++++++---------- wadm.yaml | 162 ++++++++++++++++++++++++---------------------- wasmcloud.lock | 9 +++ 4 files changed, 177 insertions(+), 114 deletions(-) diff --git a/bindings.wadge.go b/bindings.wadge.go index 40541b6..64367eb 100644 --- a/bindings.wadge.go +++ b/bindings.wadge.go @@ -5,6 +5,7 @@ package main import ( + gitea_rebus_ninja__lore__go___nats___client__gen__wasi__config__runtime "gitea.rebus.ninja/lore/go-nats-client/gen/wasi/config/runtime" gitea_rebus_ninja__lore__go___nats___client__gen__wasmcloud__messaging__consumer "gitea.rebus.ninja/lore/go-nats-client/gen/wasmcloud/messaging/consumer" gitea_rebus_ninja__lore__go___nats___client__gen__wasmcloud__messaging__types "gitea.rebus.ninja/lore/go-nats-client/gen/wasmcloud/messaging/types" github_com__bytecodealliance__wasm___tools___go__cm "github.com/bytecodealliance/wasm-tools-go/cm" @@ -17,6 +18,46 @@ const _ string = runtime.Compiler var _ unsafe.Pointer +//go:linkname wasmimport_Get gitea.rebus.ninja/lore/go-nats-client/gen/wasi/config/runtime.wasmimport_Get +func wasmimport_Get(key0 *uint8, key1 uint32, result *github_com__bytecodealliance__wasm___tools___go__cm.Result[gitea_rebus_ninja__lore__go___nats___client__gen__wasi__config__runtime.OptionStringShape, github_com__bytecodealliance__wasm___tools___go__cm.Option[string], gitea_rebus_ninja__lore__go___nats___client__gen__wasi__config__runtime.ConfigError]) { + var __p runtime.Pinner + defer __p.Unpin() + if __err := wadge.WithCurrentInstance(func(__instance *wadge.Instance) error { + return __instance.Call("wasi:config/runtime@0.2.0-draft", "get", func() unsafe.Pointer { + ptr := unsafe.Pointer(key0) + __p.Pin(ptr) + return ptr + }(), func() unsafe.Pointer { + ptr := unsafe.Pointer(&key1) + __p.Pin(ptr) + return ptr + }(), func() unsafe.Pointer { + ptr := unsafe.Pointer(result) + __p.Pin(ptr) + return ptr + }()) + }); __err != nil { + wadge.CurrentErrorHandler()(__err) + } + return +} + +//go:linkname wasmimport_GetAll gitea.rebus.ninja/lore/go-nats-client/gen/wasi/config/runtime.wasmimport_GetAll +func wasmimport_GetAll(result *github_com__bytecodealliance__wasm___tools___go__cm.Result[gitea_rebus_ninja__lore__go___nats___client__gen__wasi__config__runtime.ConfigErrorShape, github_com__bytecodealliance__wasm___tools___go__cm.List[[2]string], gitea_rebus_ninja__lore__go___nats___client__gen__wasi__config__runtime.ConfigError]) { + var __p runtime.Pinner + defer __p.Unpin() + if __err := wadge.WithCurrentInstance(func(__instance *wadge.Instance) error { + return __instance.Call("wasi:config/runtime@0.2.0-draft", "get-all", func() unsafe.Pointer { + ptr := unsafe.Pointer(result) + __p.Pin(ptr) + return ptr + }()) + }); __err != nil { + wadge.CurrentErrorHandler()(__err) + } + return +} + //go:linkname wasmimport_Log gitea.rebus.ninja/lore/go-nats-client/gen/wasi/logging/logging.wasmimport_Log func wasmimport_Log(level0 uint32, context0 *uint8, context1 uint32, message0 *uint8, message1 uint32) { var __p runtime.Pinner diff --git a/messaging.go b/messaging.go index 4a18230..a7d318a 100644 --- a/messaging.go +++ b/messaging.go @@ -1,35 +1,44 @@ -package main - -import ( - "github.com/bytecodealliance/wasm-tools-go/cm" - "gitea.rebus.ninja/lore/go-nats-client/gen/wasmcloud/messaging/consumer" - "gitea.rebus.ninja/lore/go-nats-client/gen/wasmcloud/messaging/types" - logger "gitea.rebus.ninja/lore/go-nats-client/gen/wasi/logging/logging" -) - -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, -} - -func handleMessage(msg types.BrokerMessage) cm.Result[string, struct{}, string] { - - logger.Log(logger.LevelInfo,"MessageHandler", "Received message on subject" + msg.Subject) - - reply := types.BrokerMessage{ - Subject: msg.Subject + ".reply", - Body: msg.Body, - 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{}{}) -} +package main + +import ( + "github.com/bytecodealliance/wasm-tools-go/cm" + "gitea.rebus.ninja/lore/go-nats-client/gen/wasmcloud/messaging/consumer" + "gitea.rebus.ninja/lore/go-nats-client/gen/wasmcloud/messaging/types" + logger "gitea.rebus.ninja/lore/go-nats-client/gen/wasi/logging/logging" + //config "gitea.rebus.ninja/lore/go-nats-client/gen/wasi/config/runtime" +) + +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, +} + +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 := msg.Subject + ".reply" + + // TASK + + + // Send reply + reply := types.BrokerMessage{ + Subject: dest_topic, + Body: msg.Body, + 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{}{}) +} diff --git a/wadm.yaml b/wadm.yaml index 09d7365..478724b 100644 --- a/wadm.yaml +++ b/wadm.yaml @@ -1,79 +1,83 @@ -apiVersion: core.oam.dev/v1beta1 -kind: Application -metadata: - name: tinygo-hello-world - annotations: - description: 'HTTP hello world demo in Golang (TinyGo), using the WebAssembly Component Model and WebAssembly Interfaces Types (WIT)' - wasmcloud.dev/authors: wasmCloud team - wasmcloud.dev/source-url: https://github.com/wasmCloud/wasmCloud/blob/main/examples/golang/components/http-hello-world/wadm.yaml - wasmcloud.dev/readme-md-url: https://github.com/wasmCloud/wasmCloud/blob/main/examples/golang/components/http-hello-world/README.md - wasmcloud.dev/homepage: https://github.com/wasmCloud/wasmCloud/tree/main/examples/golang/components/http-hello-world - wasmcloud.dev/categories: | - http,outgoing-http,http-server,tinygo,golang,example -spec: - components: - - name: echo - type: component - properties: - # To use the locally compiled code in this folder, use the line below instead after running `wash build`: - # image: file://./build/echo_messaging_s.wasm - image: gitea.rebus.ninja/lore/go-nats-client:1.0.1 - id: echo - traits: - # Govern the spread/scheduling of the component - - type: spreadscaler - properties: - instances: 1 - spread: - - name: edge - weight: 100 - requirements: - host-type: edge - # Establish a unidirectional link to the messaging capability provider (powered by NATS), - # so the `echo` component can make use of messaging interface functionality - # (i.e. making interacting with the messaging system, in this case NATS) - - type: link - properties: - target: nats - namespace: wasmcloud - package: messaging - interfaces: [consumer] - - # Add a capability provider that implements `wasmcloud:messaging` using NATS - - name: nats - type: capability - properties: - image: ghcr.io/wasmcloud/messaging-nats:0.23.1 - ## To configure OTEL integration for this provider specifically, uncomment the lines below - # config: - # - name: otel - # properties: - # otel_exporter_otlp_endpoint: "http://all-in-one:4318" - # otel_exporter_otlp_traces_endpoint: "http://traces-backend/v1/traces" - # otel_exporter_otlp_metrics_endpoint: "http://metrics-backend/v1/metrics" - # otel_exporter_otlp_logs_endpoint: "http://logs-backend/v1/logs" - traits: - # Since the `nats` capability provider calls an component to handle messages - # coming over subscriptions, this provider needs a unidirectional link to the - # component that wil be called. - # - # Here we link the `nats` provider (the "source"), to the `echo` component (the "target"), - # so that so the provider can deliver messages to the component (by invoking the wasmcloud:messaging/handler interface) . - - type: link - properties: - target: echo - namespace: wasmcloud - package: messaging - interfaces: [handler] - source_config: - - name: simple-subscription - properties: - subscriptions: wasmcloud.echo - - type: spreadscaler - properties: - instances: 1 - spread: - - name: edge - weight: 100 - requirements: - host-type: edge +apiVersion: core.oam.dev/v1beta1 +kind: Application +metadata: + name: tinygo-hello-world + annotations: + description: 'HTTP hello world demo in Golang (TinyGo), using the WebAssembly Component Model and WebAssembly Interfaces Types (WIT)' + wasmcloud.dev/authors: wasmCloud team + wasmcloud.dev/source-url: https://github.com/wasmCloud/wasmCloud/blob/main/examples/golang/components/http-hello-world/wadm.yaml + wasmcloud.dev/readme-md-url: https://github.com/wasmCloud/wasmCloud/blob/main/examples/golang/components/http-hello-world/README.md + wasmcloud.dev/homepage: https://github.com/wasmCloud/wasmCloud/tree/main/examples/golang/components/http-hello-world + wasmcloud.dev/categories: | + http,outgoing-http,http-server,tinygo,golang,example +spec: + components: + - name: echo + type: component + properties: + # To use the locally compiled code in this folder, use the line below instead after running `wash build`: + # image: file://./build/echo_messaging_s.wasm + image: gitea.rebus.ninja/lore/go-nats-client:1.0.1 + id: echo + config: + - name: nats-topic + properties: + dest-topic: wasmcloud.echo.reply + traits: + # Govern the spread/scheduling of the component + - type: spreadscaler + properties: + instances: 1 + spread: + - name: edge + weight: 100 + requirements: + host-type: edge + # Establish a unidirectional link to the messaging capability provider (powered by NATS), + # so the `echo` component can make use of messaging interface functionality + # (i.e. making interacting with the messaging system, in this case NATS) + - type: link + properties: + target: nats + namespace: wasmcloud + package: messaging + interfaces: [consumer] + + # Add a capability provider that implements `wasmcloud:messaging` using NATS + - name: nats + type: capability + properties: + image: ghcr.io/wasmcloud/messaging-nats:0.23.1 + ## To configure OTEL integration for this provider specifically, uncomment the lines below + # config: + # - name: otel + # properties: + # otel_exporter_otlp_endpoint: "http://all-in-one:4318" + # otel_exporter_otlp_traces_endpoint: "http://traces-backend/v1/traces" + # otel_exporter_otlp_metrics_endpoint: "http://metrics-backend/v1/metrics" + # otel_exporter_otlp_logs_endpoint: "http://logs-backend/v1/logs" + traits: + # Since the `nats` capability provider calls an component to handle messages + # coming over subscriptions, this provider needs a unidirectional link to the + # component that wil be called. + # + # Here we link the `nats` provider (the "source"), to the `echo` component (the "target"), + # so that so the provider can deliver messages to the component (by invoking the wasmcloud:messaging/handler interface) . + - type: link + properties: + target: echo + namespace: wasmcloud + package: messaging + interfaces: [handler] + source_config: + - name: simple-subscription + properties: + subscriptions: wasmcloud.echo + - type: spreadscaler + properties: + instances: 1 + spread: + - name: edge + weight: 100 + requirements: + host-type: edge diff --git a/wasmcloud.lock b/wasmcloud.lock index fcf8406..42660f2 100644 --- a/wasmcloud.lock +++ b/wasmcloud.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 1 +[[packages]] +name = "wasi:config" +registry = "wasi.dev" + +[[packages.versions]] +requirement = "=0.2.0-draft" +version = "0.2.0-draft" +digest = "sha256:aa2d36d0843999edad80a13bf22f4529277f7b6012429f8a5d1f9499f3793c1a" + [[packages]] name = "wasi:logging" registry = "wasi.dev"