Merge pull request #10 from syumai/add-env

add Getenv helper function
This commit is contained in:
syumai 2022-08-02 23:53:55 +09:00 committed by GitHub
commit d8de7fb030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 110 additions and 1 deletions

View File

@ -22,7 +22,7 @@
- [ ] Options for KV methods
* [ ] Cache API
* [ ] Durable Objects
* [ ] environment variables (WIP)
* [x] Environment variables
## Installation

8
env.go Normal file
View File

@ -0,0 +1,8 @@
package workers
// Getenv gets an environment variable set to global object.
// * https://developers.cloudflare.com/workers/platform/environment-variables/
// * Technically, this function is just an alias for js.Global().Get(env_name).
func Getenv(name string) string {
return global.Get(name).String()
}

1
examples/env/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dist

12
examples/env/Makefile vendored Normal file
View 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
View 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
View 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
View File

15
examples/env/main.go vendored Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"fmt"
"net/http"
"github.com/syumai/workers"
)
func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "MY_ENV: %s", workers.Getenv("MY_ENV"))
})
workers.Serve(handler)
}

20
examples/env/worker.mjs vendored Normal file
View 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
View 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'