|
| 1 | +# net |
| 2 | + |
| 3 | +This library provides `net.Dial` and `net.Listen` functions |
| 4 | +for `GOOS=wasip1`. It uses the WasmEdge sockets extension to WASI |
| 5 | +preview 1. |
| 6 | + |
| 7 | +Applications built with this library are compatible with WasmEdge |
| 8 | +and [stealthrocket/wasi-go](https://github.com/stealthrocket/wasi-go). |
| 9 | + |
| 10 | +## Dialing |
| 11 | + |
| 12 | +The library will automatically configure the default HTTP transport |
| 13 | +to use the `Dial` function from this library. To make outbound HTTP |
| 14 | +connections you just need the following import somewhere: |
| 15 | + |
| 16 | +```go |
| 17 | +import _ "github.com/stealthrocket/net" |
| 18 | +``` |
| 19 | + |
| 20 | +To connect to databases, there's usually a way to pass in a custom `Dial` |
| 21 | +function. |
| 22 | + |
| 23 | +For example, to connect to MySQL: |
| 24 | + |
| 25 | +```go |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + |
| 29 | + "github.com/go-sql-driver/mysql" |
| 30 | + "github.com/stealthrocket/net" |
| 31 | +) |
| 32 | + |
| 33 | +func init() { |
| 34 | + for _, network := range []string{"tcp", "tcp4", "tcp6"} { |
| 35 | + mysql.RegisterDialContext(network, func(ctx context.Context, addr string) (net.Conn, error) { |
| 36 | + return net.Dial(network, addr) |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func main() { |
| 42 | + db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/database") |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +For example, to connect to Redis: |
| 47 | + |
| 48 | +```go |
| 49 | +import ( |
| 50 | + "github.com/redis/go-redis/v9" |
| 51 | + "github.com/stealthrocket/net" |
| 52 | +) |
| 53 | + |
| 54 | +func main() { |
| 55 | + db := redis.NewClient(&redis.Options{ |
| 56 | + Addr: "127.0.0.1:6379", |
| 57 | + Dialer: net.DialContext, |
| 58 | + }) |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Listening |
| 63 | + |
| 64 | +HTTP servers can be created like so: |
| 65 | + |
| 66 | +```go |
| 67 | +import ( |
| 68 | + "net/http" |
| 69 | + |
| 70 | + "github.com/stealthrocket/net" |
| 71 | +) |
| 72 | + |
| 73 | +func main() { |
| 74 | + listener, err := net.Listen("tcp", "localhost:8080") |
| 75 | + if err != nil { |
| 76 | + // TODO: handle listen error |
| 77 | + } |
| 78 | + server := &http.Server{ |
| 79 | + // TODO: setup HTTP server |
| 80 | + } |
| 81 | + err = server.Serve(listener) |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Name Resolution |
| 86 | + |
| 87 | +Go has a built-in name resolver that sidesteps CGO (e.g. `getaddrinfo(3)`) |
| 88 | +calls. |
| 89 | + |
| 90 | +This library will automatically configure the `net.DefaultResolver` |
| 91 | +from the standard library to use the `Dial` function from this library. |
| 92 | +You just need the following import somewhere: |
| 93 | + |
| 94 | +```go |
| 95 | +import _ "github.com/stealthrocket/net" |
| 96 | +``` |
| 97 | + |
| 98 | +You should then be able to use the lookup functions from the standard |
| 99 | +library (e.g. `net.LookupIP(host)`). |
| 100 | + |
| 101 | +Note that name resolution currently depends on the following series of CLs: |
| 102 | +https://go-review.googlesource.com/c/go/+/500576 |
0 commit comments