Hi
I have a go app which makes a tcp connection to an ip.
here is the go program
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
fmt.Println("I am from Go app")
// Make a simple HTTP GET request
resp, err := http.Get("http://example.com")
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
os.Exit(1)
}
resp.Body.Close()
fmt.Printf("Response status: %s\n", resp.Status)
fmt.Printf("Response body: %s\n", body)
}
and I have compiled it to wasip2 target using tiny Go dev branch.
then I have wasmtime main.rs rust file which loads and instantiates and calls the main function go go app
use wasmtime::{Engine, Result, Store, Config};
use wasmtime::component::{ResourceTable, Linker, Component};
use wasmtime_wasi::{WasiCtx, WasiView, WasiCtxBuilder};
use std::sync::{Arc, Mutex};
use anyhow::{Context};
struct MyState {
ctx: WasiCtx,
table: ResourceTable,
}
impl WasiView for MyState {
fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
fn table(&mut self) -> &mut ResourceTable { &mut self.table }
}
fn main() -> Result<()> {
let mut config = Config::new();
config.wasm_component_model(true);
let engine = Engine::new(&config)?;
let mut linker = Linker::<MyState>::new(&engine);
wasmtime_wasi::add_to_linker_sync(&mut linker)?;
let wasi_ctx = WasiCtxBuilder::new().inherit_stdio().inherit_network().build();
let state = MyState {
ctx: wasi_ctx,
table: ResourceTable::new(),
};
let mut store = Store::new(&engine, state);
let module_go_app= Component::from_file(&engine, "/Users/celinesantosh/Desktop/sem_5/go_program/go_app.wasm")
.context("failed to read go_app.wasm")?;
let instance = linker.instantiate(&mut store, &module_go_app)
.context("failed to instantiate go_app")?;
let command = wasmtime_wasi::bindings::sync::Command::instantiate(
&mut store,
&module_go_app,
&linker,
)?;
let result = command
.wasi_cli_run()
.call_run(&mut store)
.context("failed to invoke `run` function");
Ok(())
}
the output prints till
I am from Go app
but the tcp connection is not success it gives error
Error: Lookup of host name 'example.com' failed: Netdev not set
I have used wasi preview 2 apis. but network calls are not success.. Is there anything extra I have to do for network configs.
I have given inherit_network() in WasiCtxBuilder object. still it doesn't work.
Have you tried calling https://docs.rs/wasmtime-wasi/23.0.1/wasmtime_wasi/struct.WasiCtxBuilder.html#method.allow_ip_name_lookup ?
yes , I just tried now
et wasi_ctx = WasiCtxBuilder::new().inherit_stdio().inherit_network().allow_ip_name_lookup(true).build();
still the same error
here is the output
Finished dev
profile [unoptimized + debuginfo] target(s) in 0.42s
Running /Users/celinesantosh/Desktop/sem_5/wasmtime_dev/target/debug/celine_app
I am from Go app
Error: Lookup of host name 'example.com' failed: Netdev not set
Golang does not yet have wasip2 support where wasi features like wasi:http
have been added. The net/http
module is part of the standard library that doesn't know about how to make wasi:http
calls. This work is underway, but I can walk you through how to build with tinygo today to get an http request working.
The easiest path to get something working today is to use the wit-bindgen CLI tool to generate go bindings. This tool is written in Rust and supports creating bindings for several languages. By generating bindings to wasi-http, you can write a go program that avoids pulling in modules that don't yet know how to target wasip2 and then build with the tinygo compiler. This works today, example here.
The very soon answer, but not really the recommend approach for folks getting started right now is to use the go native tools we've been building in the BA and golang communities. If you are on the dev
branch of tinygo, you can even start building for wasip2 by passing in -target=wasip2
. Building tinygo from source locally can be tricky, but you can go to the CI and download an artifact for your architecture. This is dependent on a new tool called wasm-tools-go
which is written in go and creates more idiomatic go bindings. Using this tool, I am seeing folks create higher-level bindings that look a lot more like what you'd expect in a go program: https://github.com/hayride-dev/wasi-tinygo/blob/main/main.go
Last updated: Nov 22 2024 at 17:03 UTC