package main import ( "fmt" "git.ailur.dev/ailur/jsFetch" "io" "net/http" "strings" "syscall/js" ) func tryGet(url string) error { response, err := jsFetch.Get(url) if err != nil { return err } fmt.Println(response.StatusCode) read, err := io.ReadAll(response.Body) if err != nil { return err } fmt.Println(string(read)) return nil } func tryHead(url string) error { request, err := jsFetch.NewRequest("HEAD", url, nil) if err != nil { return err } response, err := jsFetch.Fetch.Do(request) if err != nil { return err } fmt.Println(response.StatusCode) fmt.Println(response.Header) return nil } func tryPost(url string, message string) error { response, err := jsFetch.Post(url, strings.NewReader(message)) if err != nil { return err } fmt.Println(response.StatusCode) read := make([]byte, len(message)) _, err = response.Body.Read(read) if err != nil { return err } fmt.Println(string(read)) return nil } func reportTest(fail bool, err error) { request, _ := http.NewRequest("GET", "https://localhost:8080/reportTestResults", nil) request.Header.Set("Success", fmt.Sprint(!fail)) if err != nil { request.Header.Set("Error", err.Error()) } } func main() { js.Global().Set("tryGet", js.FuncOf(func(this js.Value, p []js.Value) interface{} { go func() { err := tryGet(p[0].String()) if err != nil { panic(err) return } }() return nil })) js.Global().Set("tryHead", js.FuncOf(func(this js.Value, p []js.Value) interface{} { go func() { err := tryHead(p[0].String()) if err != nil { panic(err) return } }() return nil })) js.Global().Set("tryPost", js.FuncOf(func(this js.Value, p []js.Value) interface{} { go func() { err := tryPost(p[0].String(), p[1].String()) if err != nil { panic(err) return } }() return nil })) js.Global().Set("tryAll", js.FuncOf(func(this js.Value, p []js.Value) interface{} { go func() { err := tryGet("https://localhost:8080/hello") if err != nil { reportTest(true, err) return } err = tryHead("https://localhost:8080/hello") if err != nil { reportTest(true, err) return } err = tryPost("https://localhost:8080/hello", "Hello, world!") if err != nil { reportTest(true, err) return } reportTest(false, nil) }() return nil })) select {} }