Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

93 linhas
2.6KB

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "path"
  7. "strings"
  8. )
  9. type httpEntry func(http.ResponseWriter, *http.Request)
  10. var httpEntryMap = map[string]httpEntry{
  11. apiV1Prefix: apiV1Main,
  12. apiV1WebSocket: apiV1WebSocketHandler,
  13. videoPrefix: videoMain,
  14. }
  15. func setupHTTPHandler() {
  16. for key, val := range httpEntryMap {
  17. http.HandleFunc(key, val)
  18. }
  19. log.Printf("Server started at %s:%s\n", config.Host, config.Port)
  20. log.Fatal(http.ListenAndServe(config.Host+":"+config.Port, nil))
  21. //log.Fatal(http.ListenAndServeTLS(config.Host+":"+config.Port, config.TlsCert, config.TlsKey, nil))
  22. }
  23. // FSHandler404 provides the function signature for passing to the FileServerWith404
  24. type FSHandler404 = func(w http.ResponseWriter, r *http.Request) (doDefaultFileServe bool)
  25. /*
  26. FileServerWith404 wraps the http.FileServer checking to see if the url path exists first.
  27. If the file fails to exist it calls the supplied FSHandle404 function
  28. The implementation can choose to either modify the request, e.g. change the URL path and return true to have the
  29. default FileServer handling to still take place, or return false to stop further processing, for example if you wanted
  30. to write a custom response
  31. e.g. redirects to root and continues the file serving handler chain
  32. func fileSystem404(w http.ResponseWriter, r *http.Request) (doDefaultFileServe bool) {
  33. //if not found redirect to /
  34. r.URL.Path = "/"
  35. return true
  36. }
  37. Use the same as you would with a http.FileServer e.g.
  38. r.Handle("/", http.StripPrefix("/", mw.FileServerWith404(http.Dir("./staticDir"), fileSystem404)))
  39. */
  40. func FileServerWith404(root http.FileSystem, handler404 FSHandler404) http.Handler {
  41. fs := http.FileServer(root)
  42. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  43. //make sure the url path starts with /
  44. upath := r.URL.Path
  45. if !strings.HasPrefix(upath, "/") {
  46. upath = "/" + upath
  47. r.URL.Path = upath
  48. }
  49. upath = path.Clean(upath)
  50. // attempt to open the file via the http.FileSystem
  51. f, err := root.Open(upath)
  52. if err != nil {
  53. if os.IsNotExist(err) {
  54. // call handler
  55. if handler404 != nil {
  56. doDefault := handler404(w, r)
  57. if !doDefault {
  58. return
  59. }
  60. }
  61. }
  62. }
  63. // close if successfully opened
  64. if err == nil {
  65. f.Close()
  66. }
  67. // default serve
  68. fs.ServeHTTP(w, r)
  69. })
  70. }
  71. func fileSystem404(w http.ResponseWriter, r *http.Request) (doDefaultFileServe bool) {
  72. //if not found redirect to /
  73. // r.URL.Path = "/404.html" //not working as some directorys may not be feasible.
  74. // return true
  75. http.Redirect(w, r, "/404.html", http.StatusSeeOther)
  76. return false
  77. }