You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
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) |
|
} |
|
}
|
|
|