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 1 commit
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
44 changes: 44 additions & 0 deletions docs/simulate-large-file/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"bytes"
"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 {
bytes.NewBuffer([]byte("url must have size paramter.")).WriteTo(res)
xiaobai-world marked this conversation as resolved.
Show resolved Hide resolved
return
}

targetSize, _ := strconv.ParseInt(size[0], 0, 0)
xiaobai-world marked this conversation as resolved.
Show resolved Hide resolved

if targetSize < 0 || targetSize > 300 {
bytes.NewBuffer([]byte("url must gte 0 and lte 300.")).WriteTo(res)
xiaobai-world marked this conversation as resolved.
Show resolved Hide resolved
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++ {
bytes.NewBuffer([]byte(one_kb)).WriteTo(res)
xiaobai-world marked this conversation as resolved.
Show resolved Hide resolved
}
bytes.NewBuffer([]byte("")).WriteTo(res)
xiaobai-world marked this conversation as resolved.
Show resolved Hide resolved
})

wasmhttp.Serve(nil)

select {}
}
Binary file added docs/simulate-large-file/api.wasm
Binary file not shown.
46 changes: 46 additions & 0 deletions docs/simulate-large-file/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>

<head>
<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="download" action="#" method="post">
<div>
<input type="range" step="1" min="0" max="300" value="10" id="range" name="range"> <span id="rangeText"></span>
</div>
<div>
<input type="submit" value="download file" />
</div>
</form>
</body>

<script>
navigator.serviceWorker.register("sw.js");

function rangeHalder() {
let form = document.forms.download
let size = form.elements.range.value
form.action = `/api/create-file-by-size?size=${size}`
document.querySelector('#rangeText').innerText = `${size} Mb`
}

document.querySelector('#range').addEventListener('input', rangeHalder)
rangeHalder()
</script>

xiaobai-world marked this conversation as resolved.
Show resolved Hide resolved

</html>
25 changes: 25 additions & 0 deletions docs/simulate-large-file/node-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const http = require("http");
const fs = require("fs");
const path = require("path");

const app = http.createServer((req, res) => {
let url = req.url.replace(/\?.*$/, "");
if (url === "/") {
url = "index.html";
}
url = path.join(__dirname, url);
console.log("读取文件", url);
res.statusCode = 200;
if (/\.js$/.test(url)) {
res.setHeader("Content-Type", "text/javascript");
}
if (/\.wasm$/.test(url)) {
res.setHeader("Content-Type", "application/wasm");
}
try {
res.end(fs.readFileSync(url));
} catch (e) {
}
});

app.listen(3001);
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' })