|
| 1 | +# Go HTTP Password Checker |
| 2 | + |
| 3 | +This repository contains a WebAssembly Component compiled using [TinyGo][tinygo], which: |
| 4 | + |
| 5 | +- Implements a [`wasi:http`][wasi-http]-compliant HTTP handler |
| 6 | +- Uses the [`httpserver` provider][httpserver-provider] to serve requests |
| 7 | +- Can be declaratively provisioned with [`wadm`][wadm] |
| 8 | + |
| 9 | +[wasi-http]: https://github.com/WebAssembly/wasi-http |
| 10 | +[httpserver-provider]: https://github.com/wasmCloud/wasmCloud/tree/main/crates/provider-http-server |
| 11 | +[wadm]: https://github.com/wasmCloud/wadm |
| 12 | +[tinygo]: https://tinygo.org/getting-started/install/ |
| 13 | +[wash]: https://wasmcloud.com/docs/ecosystem/wash/ |
| 14 | +[wasm-tools]: https://github.com/bytecodealliance/wasm-tools#installation |
| 15 | + |
| 16 | +# Dependencies |
| 17 | + |
| 18 | +Before starting, ensure that you have the following installed in addition to the Go toolchain: |
| 19 | + |
| 20 | +- The [TinyGo toolchain][tinygo] |
| 21 | +- [`wash`, the WAsmcloud SHell][wash] installed. |
| 22 | +- [`wasm-tools`][wasm-tools] for Go bindings |
| 23 | + |
| 24 | +## Quickstart |
| 25 | + |
| 26 | +To get started developing this repository quickly, clone the repo and run `wash dev`: |
| 27 | + |
| 28 | +```console |
| 29 | +wash dev |
| 30 | +``` |
| 31 | + |
| 32 | +`wash dev` does many things for you: |
| 33 | + |
| 34 | +- Starts the [wasmCloud host][wasmcloud-host] that can run your WebAssembly component |
| 35 | +- Builds this project |
| 36 | +- Builds a declarative WADM manifest consisting of: |
| 37 | + - Your locally built component |
| 38 | + - A [HTTP server provider][httpserver-provider] which will receive requests from the outside world |
| 39 | + (on port 8000 by default) |
| 40 | + - Necessary links between providers and your component so your component can handle web traffic |
| 41 | +- Deploys the built manifest (i.e all dependencies to run this application) locally |
| 42 | +- Watches your code for changes and re-deploys when necessary. |
| 43 | + |
| 44 | +[wasmcloud-host]: https://wasmcloud.com/docs/concepts/hosts |
| 45 | + |
| 46 | +## Send a request to the running component |
| 47 | + |
| 48 | +Once `wash dev` is serving your component, to send a request to the running component (via the HTTP |
| 49 | +server provider) |
| 50 | + |
| 51 | +### Available Endpoints |
| 52 | + |
| 53 | +#### GET / |
| 54 | + |
| 55 | +Returns a list of available endpoints and their descriptions. |
| 56 | + |
| 57 | +```console |
| 58 | +curl http://localhost:8000/ |
| 59 | + /error - return a 500 error |
| 60 | + /form - echo the fields of a POST request |
| 61 | + /headers - echo your user agent back as a server side header |
| 62 | + /post - echo the body of a POST request |
| 63 | +``` |
| 64 | + |
| 65 | +#### GET /error |
| 66 | + |
| 67 | +Returns a 500 Internal Server Error. |
| 68 | + |
| 69 | +```console |
| 70 | +curl -v http://localhost:8000/error |
| 71 | +* Host localhost:8000 was resolved. |
| 72 | +* IPv6: ::1 |
| 73 | +* IPv4: 127.0.0.1 |
| 74 | +* Trying [::1]:8000... |
| 75 | +* connect to ::1 port 8000 from ::1 port 51390 failed: Connection refused |
| 76 | +* Trying 127.0.0.1:8000... |
| 77 | +* Connected to localhost (127.0.0.1) port 8000 |
| 78 | +> GET /error HTTP/1.1 |
| 79 | +> Host: localhost:8000 |
| 80 | +> User-Agent: curl/8.7.1 |
| 81 | +> Accept: */* |
| 82 | +> |
| 83 | +* Request completely sent off |
| 84 | +< HTTP/1.1 500 Internal Server Error |
| 85 | +< content-type: text/plain; charset=utf-8 |
| 86 | +< x-content-type-options: nosniff |
| 87 | +< vary: origin, access-control-request-method, access-control-request-headers |
| 88 | +< access-control-allow-origin: * |
| 89 | +< access-control-expose-headers: * |
| 90 | +< connection: close |
| 91 | +< transfer-encoding: chunked |
| 92 | +< date: Thu, 19 Dec 2024 23:52:34 GMT |
| 93 | +< |
| 94 | +Something went wrong |
| 95 | +* Closing connection |
| 96 | +``` |
| 97 | + |
| 98 | +#### GET /headers |
| 99 | + |
| 100 | +Returns your User-Agent in the response headers. |
| 101 | + |
| 102 | +```console |
| 103 | +curl -v http://localhost:8000/headers |
| 104 | +* Host localhost:8000 was resolved. |
| 105 | +* IPv6: ::1 |
| 106 | +* IPv4: 127.0.0.1 |
| 107 | +* Trying [::1]:8000... |
| 108 | +* connect to ::1 port 8000 from ::1 port 51499 failed: Connection refused |
| 109 | +* Trying 127.0.0.1:8000... |
| 110 | +* Connected to localhost (127.0.0.1) port 8000 |
| 111 | +> GET /headers HTTP/1.1 |
| 112 | +> Host: localhost:8000 |
| 113 | +> User-Agent: curl/8.7.1 |
| 114 | +> Accept: */* |
| 115 | +> |
| 116 | +* Request completely sent off |
| 117 | +< HTTP/1.1 200 OK |
| 118 | +< x-your-user-agent: curl/8.7.1 |
| 119 | +< vary: origin, access-control-request-method, access-control-request-headers |
| 120 | +< access-control-allow-origin: * |
| 121 | +< access-control-expose-headers: * |
| 122 | +< connection: close |
| 123 | +< transfer-encoding: chunked |
| 124 | +< date: Thu, 19 Dec 2024 23:53:49 GMT |
| 125 | +< |
| 126 | +* Closing connection |
| 127 | +Check headers! |
| 128 | +``` |
| 129 | + |
| 130 | +#### POST /form |
| 131 | + |
| 132 | +Echoes back form data from a POST request. |
| 133 | + |
| 134 | +```console |
| 135 | +curl -X POST -d "field1=value1&field2=value2" http://localhost:8000/form |
| 136 | +field2: value2 |
| 137 | +field1: value1 |
| 138 | +``` |
| 139 | + |
| 140 | +#### POST /post |
| 141 | + |
| 142 | +Echoes back the entire body of a POST request. |
| 143 | + |
| 144 | +```console |
| 145 | +curl -X POST -d "Hello World" http://localhost:8000/post |
| 146 | +Hello World |
| 147 | +``` |
| 148 | + |
| 149 | +## Adding Capabilities |
| 150 | + |
| 151 | +To learn how to extend this example with additional capabilities, see the [Adding |
| 152 | +Capabilities](https://wasmcloud.com/docs/tour/adding-capabilities?lang=rust) section of the |
| 153 | +wasmCloud documentation. |
| 154 | + |
| 155 | +# Issues/ FAQ |
| 156 | + |
| 157 | +<summary> |
| 158 | +<description> |
| 159 | + |
| 160 | +## `curl` produces a "failed to invoke" error |
| 161 | + |
| 162 | +</description> |
| 163 | + |
| 164 | +If `curl`ing produces |
| 165 | + |
| 166 | +``` |
| 167 | +➜ curl localhost:8000 |
| 168 | +failed to invoke `wrpc:http/incoming-handler.handle`: failed to invoke `wrpc:http/[email protected]`: failed to shutdown synchronous parameter channel: not connected% |
| 169 | +``` |
| 170 | + |
| 171 | +You *may* need to just wait a little bit -- the HTTP server takes a second or two to start up. |
| 172 | + |
| 173 | +If the issue *persists*, you *may* have a lingering HTTP server provider running on your system. You |
| 174 | +can use `pgrep` to find it: |
| 175 | + |
| 176 | +```console |
| 177 | +❯ pgrep -la ghcr_io |
| 178 | +4007604 /tmp/wasmcloudcache/NBCBQOZPJXTJEZDV2VNY32KGEMTLFVP2XJRZJ5FWEJJOXESJXXR2RO46/ghcr_io_wasmcloud_http_server_0_23_1 |
| 179 | +``` |
| 180 | + |
| 181 | +</summary> |
0 commit comments