[英]Go Golang to serve a specific html file
http.Handle("/", http.FileServer(http.Dir("static")))
Serves the html
file in static directory.在静态目录中提供
html
文件。
Is there any way in Go that we can specify the html
file to serve?在 Go 中有什么方法可以指定要提供的
html
文件吗?
Something like render_template
in Flask
类似于
Flask
render_template
I want to do something like:我想做类似的事情:
http.Handle("/hello", http.FileServer(http.Dir("static/hello.html")))
Maybe using a custom http.HandlerFunc
would be easier:也许使用自定义
http.HandlerFunc
会更容易:
Except in your case, your func would be the http.ServeFile
one, for serving just one file.除了您的情况,您的 func 将是
http.ServeFile
之一,仅用于提供一个文件。
See for instance " Go Web Applications: Serving Static Files ":参见例如“ Go Web Applications:Serving Static Files ”:
Add the following below your home handler(see below):在您的家庭处理程序下方添加以下内容
(见下文):
http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
// do NOT do this. (see below)
http.ServeFile(w, r, r.URL.Path[1:])
})
This is using the
net/http
package's ServeFile function to serve our content.这是使用
net/http
包的 ServeFile 函数来提供我们的内容。
Effectively anything that makes a request starting with the/static/
path will be handled by this function.实际上,任何以
/static/
路径开头的请求都将由该函数处理。
One thing I found I had to do in order for the request to be handled correctly was trim the leading '/' using:我发现为了正确处理请求我必须做的一件事是使用以下方法修剪前导“/”:
r.URL.Path[1:]
Actually, do not do that.实际上,不要这样做。
This won't be possible in Go 1.6, as sztanpet comments , with commit 9b67a5d :这在 Go 1.6 中是不可能的,正如sztanpet 评论的那样, 提交 9b67a5d :
If the provided file or directory name is a relative path, it is interpreted relative to the current directory and may ascend to parent directories .
如果提供的文件或目录名称是相对路径,则它相对于当前目录进行解释,并可能上升到父目录。
If the provided name is constructed from user input, it should be sanitized before callingServeFile
.如果提供的名称是根据用户输入构造的,则应在调用
ServeFile
之前对其进行清理。
As a precaution,ServeFile
will reject requests wherer.URL.Path
contains a "..
" path element.作为预防措施,
ServeFile
将拒绝r.URL.Path
包含“..
”路径元素的请求。
That will protect against the following "url":这将防止以下“网址”:
/../file
/..
/../
/../foo
/..\\foo
/file/a
/file/a..
/file/a/..
/file/a\\..
You could use http.StripPrefix
你可以使用
http.StripPrefix
Like this:像这样:
http.Handle("/hello/", http.StripPrefix("/hello/",http.FileServer(http.Dir("static"))))
Maybe I missed something here, but after a lot of confused searching, I put this together:也许我在这里遗漏了一些东西,但经过大量混乱的搜索,我把它放在一起:
... ...
func downloadHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
StoredAs := r.Form.Get("StoredAs") // file name
data, err := ioutil.ReadFile("files/"+StoredAs)
if err != nil { fmt.Fprint(w, err) }
http.ServeContent(w, r, StoredAs, time.Now(), bytes.NewReader(data))
}
... ...
Where downloadHandler is invoked as part of a simple upload and download server:其中 downloadHandler 作为简单上传和下载服务器的一部分被调用:
func main() {
http.HandleFunc("/upload", uploadHandler)
http.HandleFunc("/download", downloadHandler)
http.ListenAndServe(":3001", nil)
}
Works fine with Firefox and Chrome.适用于 Firefox 和 Chrome。 Doesn't even need a file type.
甚至不需要文件类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.