backend.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package naffg
  2. import (
  3. "net/http"
  4. "path/filepath"
  5. )
  6. // Mux returns the backend mutex for user to register their own handlers
  7. func (app *Application) Mux() *http.ServeMux {
  8. return app.backendMutex
  9. }
  10. func (app *Application) embedFsPrefixMiddleware(fsHandler http.Handler) http.Handler {
  11. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  12. if r.URL.Path == "/" {
  13. http.Redirect(w, r, "/index.html", http.StatusFound)
  14. return
  15. }
  16. if app.options.UiResPreix != "" {
  17. r.URL.Path = filepath.Join(app.options.UiResPreix, r.URL.Path)
  18. //Force override of mime type if html, js or css
  19. if filepath.Ext(r.URL.Path) == ".html" {
  20. w.Header().Set("Content-Type", "text/html")
  21. } else if filepath.Ext(r.URL.Path) == ".js" {
  22. w.Header().Set("Content-Type", "application/javascript")
  23. } else if filepath.Ext(r.URL.Path) == ".css" {
  24. w.Header().Set("Content-Type", "text/css")
  25. }
  26. if app.options.Debug {
  27. println("Serving: ", r.URL.Path)
  28. }
  29. http.ServeFile(w, r, r.URL.Path)
  30. return
  31. }
  32. fsHandler.ServeHTTP(w, r)
  33. })
  34. }