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

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

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。以下是修改后的代码示例:

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

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

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

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()中拦截提供的文件。

package main

import (
	"log"
	"net/http"
	"path"
	"path/filepath"
	"strings"
)

func main() {
	mux := http.NewServeMux()

	//staticHandler := http.FileServer(http.Dir("./templates"))
	staticHandler := fileServer("./templates")

	mux.Handle("/", http.StripPrefix("/", staticHandler))
	log.Printf("listening")
	log.Fatal(http.ListenAndServe(":8080", mux))
}

// 返回自定义的文件服务器
func fileServer(root string) http.Handler {
	return &fileHandler{root}
}

// 自定义的文件服务器
type fileHandler struct {
	root string
}

func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	upath := r.URL.Path
	if !strings.HasPrefix(upath, "/") {
		upath = "/" + upath
		r.URL.Path = upath
	}
	name := filepath.Join(f.root, path.Clean(upath))
	log.Printf("fileHandler.ServeHTTP: path=%s", name)
	http.ServeFile(w, r, name)
}

希望对你有帮助!

英文:

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().

package main

import (
	"log"
	"net/http"
	"path"
	"path/filepath"
	"strings"
)

func main() {
	mux := http.NewServeMux()

	//staticHandler := http.FileServer(http.Dir("./templates"))
	staticHandler := fileServer("./templates")

	mux.Handle("/", http.StripPrefix("/", staticHandler))
	log.Printf("listening")
	log.Fatal(http.ListenAndServe(":8080", mux))
}

// returns custom file server
func fileServer(root string) http.Handler {
	return &fileHandler{root}
}

// custom file server
type fileHandler struct {
	root string
}

func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	upath := r.URL.Path
	if !strings.HasPrefix(upath, "/") {
		upath = "/" + upath
		r.URL.Path = upath
	}
	name := filepath.Join(f.root, path.Clean(upath))
	log.Printf("fileHandler.ServeHTTP: path=%s", name)
	http.ServeFile(w, r, name)
}

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:

确定