Files
next.orly.dev/pkg/wasm/hello/server.go
mleku 24eef5b5a8
Some checks failed
Go / build (push) Has been cancelled
Go / release (push) Has been cancelled
fix CORS headers and a wasm experiment
2025-11-14 19:15:50 +00:00

49 lines
1.2 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"net/http"
"path/filepath"
)
func main() {
port := flag.Int("port", 8080, "Port to serve on")
dir := flag.String("dir", ".", "Directory to serve files from")
flag.Parse()
// Create file server
fs := http.FileServer(http.Dir(*dir))
// Wrap with MIME type handler for WASM files
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Set correct MIME type for WebAssembly files
if filepath.Ext(r.URL.Path) == ".wasm" {
w.Header().Set("Content-Type", "application/wasm")
}
// Set CORS headers to allow cross-origin requests (useful for development)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
// Handle OPTIONS preflight requests
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
fs.ServeHTTP(w, r)
})
addr := fmt.Sprintf(":%d", *port)
log.Printf("Starting WASM server on http://localhost%s", addr)
log.Printf("Serving files from: %s", *dir)
log.Printf("\nOpen http://localhost%s/ in your browser", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatal(err)
}
}