Compare commits
4 Commits
issue-2-he
...
issue-4-co
| Author | SHA1 | Date | |
|---|---|---|---|
|
4c022b13ac
|
|||
|
a580df7b72
|
|||
|
0f924d8e8a
|
|||
|
2db0628a89
|
36
examples/counter/main.go
Normal file
36
examples/counter/main.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
//go:build js && wasm
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.flowmade.one/flowmade-one/iris/reactive"
|
||||||
|
"git.flowmade.one/flowmade-one/iris/ui"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Create a signal with initial value 0
|
||||||
|
count := reactive.NewSignal(0)
|
||||||
|
|
||||||
|
view := ui.NewView()
|
||||||
|
|
||||||
|
// Reactive text that updates when count changes
|
||||||
|
view.Child(ui.TextFromFunction(func() string {
|
||||||
|
return fmt.Sprintf("Count: %d", count.Get())
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Button that increments the count on click
|
||||||
|
view.Child(ui.Button(func() {
|
||||||
|
count.Set(count.Get() + 1)
|
||||||
|
}, ui.TextFromString("Increment")))
|
||||||
|
|
||||||
|
// Button that decrements the count on click
|
||||||
|
view.Child(ui.Button(func() {
|
||||||
|
count.Set(count.Get() - 1)
|
||||||
|
}, ui.TextFromString("Decrement")))
|
||||||
|
|
||||||
|
ui.NewApp(view)
|
||||||
|
|
||||||
|
select {}
|
||||||
|
}
|
||||||
120
host/README.md
Normal file
120
host/README.md
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
# Host Package
|
||||||
|
|
||||||
|
Static file server optimized for serving Iris WASM applications.
|
||||||
|
|
||||||
|
## Basic Setup
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.flowmade.one/flowmade-one/iris/host"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
server := host.New("public", "index.html")
|
||||||
|
log.Println("Server running on http://localhost:8080")
|
||||||
|
log.Fatal(http.ListenAndServe(":8080", server))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Directory Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
myapp/
|
||||||
|
├── server.go # Server code (above)
|
||||||
|
└── public/
|
||||||
|
├── index.html # Entry point
|
||||||
|
├── app.wasm # Compiled WASM binary
|
||||||
|
└── wasm_exec.js # Go WASM runtime
|
||||||
|
```
|
||||||
|
|
||||||
|
## Serving Static Files
|
||||||
|
|
||||||
|
The server serves files from the specified public directory. Any request path maps directly to files:
|
||||||
|
|
||||||
|
- `/` → `public/index.html`
|
||||||
|
- `/app.wasm` → `public/app.wasm`
|
||||||
|
- `/styles.css` → `public/styles.css`
|
||||||
|
|
||||||
|
**SPA Fallback**: Unknown paths and directories fall back to `index.html`, enabling client-side routing.
|
||||||
|
|
||||||
|
## WASM MIME Types
|
||||||
|
|
||||||
|
The server automatically sets correct MIME types for all common file types:
|
||||||
|
|
||||||
|
| Extension | MIME Type |
|
||||||
|
|-----------|-----------|
|
||||||
|
| `.wasm` | `application/wasm` |
|
||||||
|
| `.html` | `text/html; charset=utf-8` |
|
||||||
|
| `.js` | `application/javascript` |
|
||||||
|
| `.css` | `text/css` |
|
||||||
|
| `.json` | `application/json` |
|
||||||
|
| `.svg` | `image/svg+xml` |
|
||||||
|
|
||||||
|
This ensures browsers load WASM files correctly without manual configuration.
|
||||||
|
|
||||||
|
## Compression
|
||||||
|
|
||||||
|
Gzip compression is automatically applied to compressible content types when the client supports it:
|
||||||
|
|
||||||
|
- HTML, CSS, JavaScript
|
||||||
|
- JSON
|
||||||
|
- WASM binaries
|
||||||
|
- SVG images
|
||||||
|
|
||||||
|
Binary assets (PNG, JPEG, etc.) are served uncompressed since they're already compressed.
|
||||||
|
|
||||||
|
## Development vs Production
|
||||||
|
|
||||||
|
### Development
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
server := host.New("public", "index.html")
|
||||||
|
http.ListenAndServe(":8080", server)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Run with:
|
||||||
|
```bash
|
||||||
|
go run server.go
|
||||||
|
```
|
||||||
|
|
||||||
|
### Production
|
||||||
|
|
||||||
|
For production, compile the server and run as a binary:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go build -o server ./server.go
|
||||||
|
./server
|
||||||
|
```
|
||||||
|
|
||||||
|
Consider adding:
|
||||||
|
- TLS termination (via reverse proxy or `http.ListenAndServeTLS`)
|
||||||
|
- Environment-based port configuration
|
||||||
|
- Graceful shutdown handling
|
||||||
|
|
||||||
|
Example with TLS:
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
server := host.New("public", "index.html")
|
||||||
|
log.Fatal(http.ListenAndServeTLS(":443", "cert.pem", "key.pem", server))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Example with reverse proxy (nginx):
|
||||||
|
```nginx
|
||||||
|
server {
|
||||||
|
listen 443 ssl;
|
||||||
|
server_name example.com;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://localhost:8080;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user