mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2025-03-11 01:29:11 +00:00
♻️
This commit is contained in:
parent
79db7567a5
commit
04f6573b78
@ -9,14 +9,18 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
async function hello() {
|
async function hello() {
|
||||||
res = await fetch('api/hello', {
|
const name = document.querySelector("#name").value
|
||||||
|
|
||||||
|
const res = await fetch('api/hello', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ name: document.querySelector("#name").value })
|
body: JSON.stringify({ name })
|
||||||
})
|
})
|
||||||
|
|
||||||
const { message } = await res.json()
|
const { message } = await res.json()
|
||||||
|
|
||||||
alert(message)
|
alert(message)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -4,12 +4,13 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
wasmhttp "github.com/nlepage/go-wasm-http-server"
|
wasmhttp "github.com/nlepage/go-wasm-http-server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var no = 1
|
var counter int32
|
||||||
|
|
||||||
http.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) {
|
http.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) {
|
||||||
params := make(map[string]string)
|
params := make(map[string]string)
|
||||||
@ -19,12 +20,10 @@ func main() {
|
|||||||
|
|
||||||
res.Header().Add("Content-Type", "application/json")
|
res.Header().Add("Content-Type", "application/json")
|
||||||
if err := json.NewEncoder(res).Encode(map[string]string{
|
if err := json.NewEncoder(res).Encode(map[string]string{
|
||||||
"message": fmt.Sprintf("Hello %s! (request n°%d)", params["name"], no),
|
"message": fmt.Sprintf("Hello %s! (request %d)", params["name"], atomic.AddInt32(&counter, 1)),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
no++
|
|
||||||
})
|
})
|
||||||
|
|
||||||
wasmhttp.Serve(nil)
|
wasmhttp.Serve(nil)
|
||||||
|
Binary file not shown.
@ -6,14 +6,18 @@
|
|||||||
navigator.serviceWorker.register('sw.js')
|
navigator.serviceWorker.register('sw.js')
|
||||||
|
|
||||||
async function hello() {
|
async function hello() {
|
||||||
res = await fetch('api/hello', {
|
const name = document.querySelector("#name").value
|
||||||
|
|
||||||
|
const res = await fetch('api/hello', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ name: document.querySelector("#name").value })
|
body: JSON.stringify({ name })
|
||||||
})
|
})
|
||||||
|
|
||||||
const { message } = await res.json()
|
const { message } = await res.json()
|
||||||
|
|
||||||
alert(message)
|
alert(message)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -6,14 +6,18 @@
|
|||||||
navigator.serviceWorker.register('sw.js')
|
navigator.serviceWorker.register('sw.js')
|
||||||
|
|
||||||
async function hello() {
|
async function hello() {
|
||||||
res = await fetch('api/hello', {
|
const name = document.querySelector("#name").value
|
||||||
|
|
||||||
|
const res = await fetch('api/hello', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ name: document.querySelector("#name").value })
|
body: JSON.stringify({ name })
|
||||||
})
|
})
|
||||||
|
|
||||||
const { message } = await res.json()
|
const { message } = await res.json()
|
||||||
|
|
||||||
alert(message)
|
alert(message)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -21,6 +21,17 @@ var _ js.Wrapper = ResponseRecorder{}
|
|||||||
// JSValue builds and returns the equivalent JS Response (implementing js.Wrapper)
|
// JSValue builds and returns the equivalent JS Response (implementing js.Wrapper)
|
||||||
func (rr ResponseRecorder) JSValue() js.Value {
|
func (rr ResponseRecorder) JSValue() js.Value {
|
||||||
var res = rr.Result()
|
var res = rr.Result()
|
||||||
|
|
||||||
|
var body js.Value = js.Undefined()
|
||||||
|
if res.ContentLength != 0 {
|
||||||
|
var b, err = ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
body = js.Global().Get("Uint8Array").New(len(b))
|
||||||
|
js.CopyBytesToJS(body, b)
|
||||||
|
}
|
||||||
|
|
||||||
var init = make(map[string]interface{})
|
var init = make(map[string]interface{})
|
||||||
|
|
||||||
if res.StatusCode != 0 {
|
if res.StatusCode != 0 {
|
||||||
@ -35,15 +46,5 @@ func (rr ResponseRecorder) JSValue() js.Value {
|
|||||||
init["headers"] = headers
|
init["headers"] = headers
|
||||||
}
|
}
|
||||||
|
|
||||||
var body js.Value = js.Undefined()
|
|
||||||
if res.ContentLength != 0 {
|
|
||||||
var b, err = ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
body = js.Global().Get("Uint8Array").New(len(b))
|
|
||||||
js.CopyBytesToJS(body, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
return js.Global().Get("Response").New(body, init)
|
return js.Global().Get("Response").New(body, init)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user