mirror of
https://github.com/syumai/workers.git
synced 2025-03-10 17:29:11 +00:00
add env example
This commit is contained in:
parent
83de13dcd1
commit
70d197e504
1
examples/env/.gitignore
vendored
Normal file
1
examples/env/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
dist
|
12
examples/env/Makefile
vendored
Normal file
12
examples/env/Makefile
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
.PHONY: dev
|
||||
dev:
|
||||
wrangler dev
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
mkdir -p dist
|
||||
tinygo build -o ./dist/app.wasm -target wasm ./...
|
||||
|
||||
.PHONY: publish
|
||||
publish:
|
||||
wrangler publish
|
32
examples/env/README.md
vendored
Normal file
32
examples/env/README.md
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
# env
|
||||
|
||||
* This app returns a message set as `MY_ENV` in wrangler.toml.
|
||||
|
||||
## Demo
|
||||
|
||||
* https://env.syumai.workers.dev/
|
||||
|
||||
## Development
|
||||
|
||||
### Requirements
|
||||
|
||||
This project requires these tools to be installed globally.
|
||||
|
||||
* wrangler
|
||||
* tinygo
|
||||
|
||||
### Commands
|
||||
|
||||
```
|
||||
make dev # run dev server
|
||||
make build # build Go Wasm binary
|
||||
make publish # publish worker
|
||||
```
|
||||
|
||||
## Author
|
||||
|
||||
syumai
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
7
examples/env/go.mod
vendored
Normal file
7
examples/env/go.mod
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/syumai/workers/examples/env
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/syumai/workers v0.0.0
|
||||
|
||||
replace github.com/syumai/workers => ../../
|
0
examples/env/go.sum
vendored
Normal file
0
examples/env/go.sum
vendored
Normal file
17
examples/env/main.go
vendored
Normal file
17
examples/env/main.go
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/syumai/workers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
envVal := js.Global().Get("MY_ENV")
|
||||
fmt.Fprintf(w, "MY_ENV: %s", envVal)
|
||||
})
|
||||
workers.Serve(handler)
|
||||
}
|
20
examples/env/worker.mjs
vendored
Normal file
20
examples/env/worker.mjs
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
import "../assets/polyfill_performance.js";
|
||||
import "../assets/wasm_exec.js";
|
||||
import mod from "./dist/app.wasm";
|
||||
|
||||
const go = new Go();
|
||||
|
||||
const load = WebAssembly.instantiate(mod, go.importObject).then((instance) => {
|
||||
go.run(instance);
|
||||
return instance;
|
||||
});
|
||||
|
||||
async function processRequest(event) {
|
||||
const req = event.request;
|
||||
await load;
|
||||
return handleRequest(req);
|
||||
}
|
||||
|
||||
addEventListener("fetch", (event) => {
|
||||
event.respondWith(processRequest(event));
|
||||
})
|
14
examples/env/wrangler.toml
vendored
Normal file
14
examples/env/wrangler.toml
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
name = "env"
|
||||
main = "./worker.mjs"
|
||||
compatibility_date = "2022-05-13"
|
||||
compatibility_flags = [
|
||||
"streams_enable_constructors"
|
||||
]
|
||||
|
||||
[build]
|
||||
command = "make build"
|
||||
|
||||
# declare environment variables and assign values
|
||||
# https://developers.cloudflare.com/workers/platform/environment-variables/
|
||||
[vars]
|
||||
MY_ENV = 'my env value'
|
Loading…
x
Reference in New Issue
Block a user