完成Web ASM
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
//go:embed all:static/dist
|
||||
var staticFiles embed.FS
|
||||
|
||||
func main() {
|
||||
port := flag.Int("port", 8080, "server port")
|
||||
flag.Parse()
|
||||
|
||||
distFS, err := fs.Sub(staticFiles, "static/dist")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open embedded files: %v", err)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
stripped, err := fs.Sub(distFS, ".")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to sub: %v", err)
|
||||
}
|
||||
fileServer := http.FileServer(http.FS(stripped))
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, ".wasm") {
|
||||
w.Header().Set("Content-Type", "application/wasm")
|
||||
}
|
||||
fileServer.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
go func() {
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
log.Println("shutting down...")
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
log.Printf("ncmdump-web (WASM mode) starting on :%d", *port)
|
||||
addr := fmt.Sprintf(":%d", *port)
|
||||
if err := http.ListenAndServe(addr, mux); err != nil {
|
||||
log.Fatalf("server error: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user