No Description

serve.go 824B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "regexp"
  6. )
  7. /*
  8. build this file once:
  9. $ go build serve.go
  10. build the release version with:
  11. $ npm run build:release
  12. serve everything from /dist with:
  13. $ ./serve
  14. */
  15. var wasmFile = regexp.MustCompile("\\.wasm$")
  16. func maxAgeHandler(seconds int, h http.Handler) http.Handler {
  17. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  18. w.Header().Add("Cache-Control", fmt.Sprintf("max-age=%d, public, must-revalidate, proxy-revalidate", seconds))
  19. ruri := r.RequestURI
  20. if wasmFile.MatchString(ruri) {
  21. w.Header().Set("Content-Type", "application/wasm")
  22. }
  23. h.ServeHTTP(w, r)
  24. })
  25. }
  26. func main() {
  27. fs := http.FileServer(http.Dir("dist"))
  28. http.Handle("/", maxAgeHandler(0, fs))
  29. fmt.Printf("Serving localhost:3000\n")
  30. http.ListenAndServe(":3000", nil)
  31. }