A web blog and personal homepage engine written in Go.
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.
 
 

30 lines
706 B

  1. package responses
  2. import (
  3. "mime"
  4. "net/http"
  5. "path/filepath"
  6. "git.kirsle.net/apps/gophertype/pkg/bundled"
  7. "git.kirsle.net/apps/gophertype/pkg/console"
  8. "git.kirsle.net/apps/gophertype/pkg/settings"
  9. )
  10. // SendFile sends a file from bindata or the user root.
  11. func SendFile(w http.ResponseWriter, r *http.Request, path string) {
  12. mimeType := mime.TypeByExtension(filepath.Ext(path))
  13. if mimeType == "" {
  14. mimeType = "text/plain"
  15. }
  16. w.Header().Set("Content-Type", mimeType)
  17. if b, err := bundled.Asset(path); err == nil {
  18. w.Write(b)
  19. return
  20. }
  21. console.Debug("SendFile: http.ServeFile(%s)", filepath.Join(settings.UserRoot, path))
  22. http.ServeFile(w, r, filepath.Join(settings.UserRoot, path))
  23. }