Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example of download large file #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
50 changes: 50 additions & 0 deletions docs/simulate-large-file/api.go
Original file line number Diff line number Diff line change
@@ -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 {}
}
Binary file added docs/simulate-large-file/api.wasm
Binary file not shown.
48 changes: 48 additions & 0 deletions docs/simulate-large-file/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>go-wasm-http-server: simulate large file</title>
<style>
form {
margin: 2em auto;
text-align: center;
line-height: 180%;
}


input[type="range"] {
width: 300px;
}
</style>
</head>

<body>
<form name="simulateFile" action="./api/create-file-by-size?size=0" method="get">
<div>
<input type="range" step="1" min="0" max="300" value="10" name="size">
<div id="sizeText"></div>
</div>
<div>
<input type="submit" value="download file" />
</div>
</form>
<script>
navigator.serviceWorker.register("sw.js");

const form = document.forms.simulateFile
const size = form.elements.size

function sizeHandler() {
form.action = `./api/create-file-by-size?size=${size.value}`
document.querySelector('#sizeText').innerText = `${size.value} Mb`
}
size.addEventListener('input', sizeHandler)
sizeHandler()
</script>
</body>



</html>
11 changes: 11 additions & 0 deletions docs/simulate-large-file/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v1.0.0/sw.js')

addEventListener('install', (event) => {
event.waitUntil(skipWaiting())
})

addEventListener('activate', event => {
event.waitUntil(clients.claim())
})

registerWasmHTTPListener('api.wasm', { base: 'api' })