在Go的Web服务器中,网站的根目录映射到文件系统的哪个位置?

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

With Go's webserver where does the root of the website map onto the filesystem?

问题

Go的net/http web服务器的文件系统“root”在哪里?它似乎不在可执行文件所在的目录中。
通过“root”,我指的是我可以用于imgsrc属性的目录,没有任何路径。我不打算这样做,但如果我知道这个结构,会对我有所帮助。

英文:

Where is the filesystem "root" of a Go net/http webserver. It doesn't seem to be in the directory the executable is in.
By "root" I mean the directory I would use for, say, the src attribute of an img, without any path. I don't plan to do this, but it would help me understand the structure if I knew.

答案1

得分: 14

简介

在Go语言中,net/http 包用于提供Web服务器功能。它不仅仅是一个静态文件服务器,功能更加强大。

在该包中,没有文件系统的“根”概念。提供的Web服务器使用处理器来处理映射到URL的HTTP请求。处理器负责处理HTTP请求并设置和生成响应。可以使用Handle()HandleFunc()函数注册处理器。可以使用ListenAndServe()函数启动服务器。

阅读net/http包的文档以了解基本概念并开始使用。文档中还包含许多小例子。

博客文章编写Web应用程序也很有帮助。

静态文件服务器

然而,虽然提供了静态文件服务器或“文件系统”功能,但在http包中有一个FileServer()函数,它返回一个用于提供静态文件的处理器。可以将要提供静态文件的“根”文件夹作为FileServer()的参数进行指定。

如果向FileServer()传递一个_绝对_路径,那么它的含义是明确的。如果提供一个_相对_路径,则始终在_当前_或_工作_目录的上下文中解释。默认情况下,这是您启动应用程序的文件夹(执行go run ...命令或编译后的可执行二进制文件所在的文件夹)。

示例:

http.Handle("/", http.FileServer(http.Dir("/tmp")))

这将设置一个处理器,用于从文件夹/tmp中提供文件,并将其映射到根URL/。例如,对于GET请求"/mydoc.txt"的响应将是静态文件"/tmp/mydoc.txt"

完整的应用程序示例:

package main
import (
    "log"
    "net/http"
)
func main() {
    // 简单的静态Web服务器:
    http.Handle("/", http.FileServer(http.Dir("/tmp")))
    log.Fatal(http.ListenAndServe(":8080", nil))
}

您还可以使用StripPrefix()函数进行更复杂的映射。示例:

// 使用StripPrefix函数,在FileServer处理请求之前修改请求URL的路径,以便在磁盘上的目录(/tmp)下提供替代URL路径(/tmpfiles/):
http.Handle("/tmpfiles/",
    http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
英文:

Introduction

In Go the net/http package is used to provide Web Server functionality. This is not a static fileserver, it is much more than that.

There is no filesystem "root" concept. The provided web server uses handlers to serve HTTP requests which are mapped to URLs. A handler is responsible to process an HTTP request and to setup and generate the response. A handler can be registered e.g. with the Handle() or HandleFunc() functions. The server can be started with the ListenAndServe() function.

Read the package documentation of net/http to understand the basic concepts and to get started. It also contains many small examples.

The Blog article Writing Web Applications is also helpful.

Static File Server

However, a static file server or "filesystem" functionality is provided, there is a FileServer() function in the http package which returns a Handler which serves static files. You can specify the "root" folder to serve static files from as a parameter of FileServer().

If you pass an absolute path to FileServer(), then there is no question what that means. If you provide a relative path, it is always interpreted in the context of the current or working directory. By default this is the folder you start the application from (the folder you're in when executing the go run ... command or the compiled executable binary).

Example:

http.Handle("/", http.FileServer(http.Dir("/tmp")))

This will setup a handler to serve files from the folder /tmp mapped to the root URL /. For example the response to the GET request "/mydoc.txt" will be the "/tmp/mydoc.txt" static file.

Complete application:

package main
import (
	"log"
	"net/http"
)
func main() {
	// Simple static webserver:
	http.Handle("/", http.FileServer(http.Dir("/tmp")))
	log.Fatal(http.ListenAndServe(":8080", nil))
}

You can do more complex mapping using the StripPrefix() function. Example:

// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.Handle("/tmpfiles/",
    http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))

huangapple
  • 本文由 发表于 2015年2月26日 22:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/28745161.html
匿名

发表评论

匿名网友

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

确定