diff --git a/README.md b/README.md index ab4c54b..2269610 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ - [Hello example](https://nlepage.github.io/go-wasm-http-server/hello) ([sources](https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello)) - [Hello example with state](https://nlepage.github.io/go-wasm-http-server/hello-state) ([sources](https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-state)) - [Hello example with state and keepalive](https://nlepage.github.io/go-wasm-http-server/hello-state-keepalive) ([sources](https://github.com/nlepage/go-wasm-http-server/tree/master/docs/hello-state-keepalive)) + - [Simulate large file](https://nlepage.github.io/go-wasm-http-server/simulate-large-file) ([sources](https://github.com/nlepage/go-wasm-http-server/tree/master/docs/simulate-large-file)) - [😺 Catption generator example](https://nlepage.github.io/catption/wasm) ([sources](https://github.com/nlepage/catption/tree/wasm)) - [Random password generator web server](https://nlepage.github.io/random-password-please/) ([sources](https://github.com/nlepage/random-password-please) forked from [jbarham/random-password-please](https://github.com/jbarham/random-password-please)) diff --git a/docs/simulate-large-file/api.go b/docs/simulate-large-file/api.go new file mode 100644 index 0000000..0efc173 --- /dev/null +++ b/docs/simulate-large-file/api.go @@ -0,0 +1,50 @@ +package main + +import ( + "net/http" + "strconv" + + wasmhttp "github.com/nlepage/go-wasm-http-server" +) + +func main() { + + one_kb := "" + for i := 0; i < 1024; i++ { + one_kb += "0" + } + + http.HandleFunc("/create-file-by-size", func(res http.ResponseWriter, req *http.Request) { + vars := req.URL.Query() + size, ok := vars["size"] + if !ok { + res.WriteHeader(http.StatusBadRequest) + res.Write([]byte("URL must have size parameter.")) + return + } + + targetSize, err := strconv.ParseInt(size[0], 0, 0) + + if err != nil { + res.WriteHeader(http.StatusBadRequest) + res.Write([]byte("Size must be 0 or a positive integer.")) + return + } + + if targetSize < 0 || targetSize > 300 { + res.WriteHeader(http.StatusBadRequest) + res.Write([]byte("Size must be >= 0 and <= 300.")) + return + } + + res.Header().Set("Content-Type", "application/octet-stream") + res.Header().Set("Content-Disposition", "attachment;filename=size-file") + for i := int64(1); i <= 1024*targetSize; i++ { + res.Write([]byte(one_kb)) + } + }) + + wasmhttp.Serve(nil) + + select {} +} diff --git a/docs/simulate-large-file/api.wasm b/docs/simulate-large-file/api.wasm new file mode 100755 index 0000000..78cfd34 Binary files /dev/null and b/docs/simulate-large-file/api.wasm differ diff --git a/docs/simulate-large-file/index.html b/docs/simulate-large-file/index.html new file mode 100644 index 0000000..2618ff8 --- /dev/null +++ b/docs/simulate-large-file/index.html @@ -0,0 +1,48 @@ + + + +
+ +