GOLANG How can I load a certain html file from templates directory using http.FileServer

huangapple go评论143阅读模式
英文:

GOLANG How can I load a certain html file from templates directory using http.FileServer

问题

你可以使用http.Dir函数指定要加载的目录,然后使用http.FileServer函数创建一个处理静态文件的处理程序。如果在templates目录下有多个文件,你可以在URL中指定要加载的特定文件。例如,如果你想加载名为index.html的文件,可以在URL中添加/index.html。以下是修改后的代码示例:

  1. func main() {
  2. mux := http.NewServeMux()
  3. staticHandler := http.FileServer(http.Dir("./templates"))
  4. mux.Handle("/", http.StripPrefix("/", staticHandler))
  5. log.Fatal(http.ListenAndServe(":8080", mux))
  6. }

在浏览器中访问http://localhost:8080/index.html即可加载templates目录下的index.html文件。

英文:
  1. func main() {
  2. mux := http.NewServeMux()
  3. staticHandler := http.FileServer(http.Dir("./templates"))
  4. mux.Handle("/", http.StripPrefix("/", staticHandler))
  5. log.Fatal(http.ListenAndServe(":8080", mux))
  6. }

I want to load a html file which is in 'templates' directory.
if there are more than one files in 'templates', how can I select certain file to load?

答案1

得分: 2

你可以使用http.ServeFile()来构建自己的文件服务器。

请参考下面的示例代码。

然后,你可以在自定义的fileHandler.ServeHTTP()中拦截提供的文件。

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "path"
  6. "path/filepath"
  7. "strings"
  8. )
  9. func main() {
  10. mux := http.NewServeMux()
  11. //staticHandler := http.FileServer(http.Dir("./templates"))
  12. staticHandler := fileServer("./templates")
  13. mux.Handle("/", http.StripPrefix("/", staticHandler))
  14. log.Printf("listening")
  15. log.Fatal(http.ListenAndServe(":8080", mux))
  16. }
  17. // 返回自定义的文件服务器
  18. func fileServer(root string) http.Handler {
  19. return &fileHandler{root}
  20. }
  21. // 自定义的文件服务器
  22. type fileHandler struct {
  23. root string
  24. }
  25. func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  26. upath := r.URL.Path
  27. if !strings.HasPrefix(upath, "/") {
  28. upath = "/" + upath
  29. r.URL.Path = upath
  30. }
  31. name := filepath.Join(f.root, path.Clean(upath))
  32. log.Printf("fileHandler.ServeHTTP: path=%s", name)
  33. http.ServeFile(w, r, name)
  34. }

希望对你有帮助!

英文:

You can use http.ServeFile() to build your own file server.

See sketch below.

Then you can intercept the served files within your custom fileHandler.ServeHTTP().

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "path"
  6. "path/filepath"
  7. "strings"
  8. )
  9. func main() {
  10. mux := http.NewServeMux()
  11. //staticHandler := http.FileServer(http.Dir("./templates"))
  12. staticHandler := fileServer("./templates")
  13. mux.Handle("/", http.StripPrefix("/", staticHandler))
  14. log.Printf("listening")
  15. log.Fatal(http.ListenAndServe(":8080", mux))
  16. }
  17. // returns custom file server
  18. func fileServer(root string) http.Handler {
  19. return &fileHandler{root}
  20. }
  21. // custom file server
  22. type fileHandler struct {
  23. root string
  24. }
  25. func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  26. upath := r.URL.Path
  27. if !strings.HasPrefix(upath, "/") {
  28. upath = "/" + upath
  29. r.URL.Path = upath
  30. }
  31. name := filepath.Join(f.root, path.Clean(upath))
  32. log.Printf("fileHandler.ServeHTTP: path=%s", name)
  33. http.ServeFile(w, r, name)
  34. }

huangapple
  • 本文由 发表于 2022年4月23日 14:36:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/71977451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定