gui_embedded.go 784 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //+build !guidev
  2. package main
  3. import (
  4. "fmt"
  5. "log"
  6. "mime"
  7. "net/http"
  8. "path/filepath"
  9. "time"
  10. "github.com/calmh/syncthing/auto"
  11. "github.com/cratonica/embed"
  12. )
  13. func embeddedStatic() interface{} {
  14. fs, err := embed.Unpack(auto.Resources)
  15. if err != nil {
  16. panic(err)
  17. }
  18. var modt = time.Now().UTC().Format(http.TimeFormat)
  19. return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
  20. file := req.URL.Path
  21. if file[0] == '/' {
  22. file = file[1:]
  23. }
  24. bs, ok := fs[file]
  25. if !ok {
  26. return
  27. }
  28. mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path))
  29. if len(mtype) != 0 {
  30. res.Header().Set("Content-Type", mtype)
  31. }
  32. res.Header().Set("Content-Size", fmt.Sprintf("%d", len(bs)))
  33. res.Header().Set("Last-Modified", modt)
  34. res.Write(bs)
  35. }
  36. }