I am very new to Golang and trying to create a Golang REST API that can be shared via WASM and used in a React app. I am looking for someone who can provide a sample example to help me.
AFAIK, WASM reduces the need for a interacting with a common web server - just loading? More similar to an old client-server solution. Meaning all communication is done direct from WASM client to a wide open API? You may restrict access to the API from the WASM (client) by some token IMO.
I was interested in WASM as well, but my vision is that the API should be completely locked from internet access. Only accessed via localhost or internal IP.
So my interpretation was that WASM is both modern as web app and old fashion as a desktop application at the same time. In order to make the API as “safe boxed” as possible, I did chose a traditional SPA/PWA web app with internal IP access from the web server.
Not answer to your question, but a foundation for how to think when you build the API?
To create a Golang REST API for use in a React app via WASM, build a simple Go HTTP server, compile it to main.wasm
using GOOS=js GOARCH=wasm go build
, and serve it with wasm_exec.js
via a basic HTML file. In React, use axios
to fetch data from the WASM API’s endpoint (/message
in the example). Remember to handle CORS and error handling, and serve the WASM files correctly for production.