使用 Go 的网络服务器,网站的根在哪里映射到文件系统? [英] With Go's webserver where does the root of the website map onto the filesystem?

查看:21
本文介绍了使用 Go 的网络服务器,网站的根在哪里映射到文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Go net/http 网络服务器的文件系统根"在哪里.它似乎不在可执行文件所在的目录中.根"是指我将用于的目录,例如 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.

推荐答案

简介

在 Go 中使用 net/http 包提供 Web 服务器功能.这不是静态文件服务器,它远不止于此.

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.

没有文件系统根"概念.提供的 Web 服务器使用 handlers 来处理 HTTP 请求映射到 URL.处理程序负责处理 HTTP 请求并设置和生成响应.可以注册处理程序,例如使用 Handle()HandleFunc() 函数.可以使用 ListenAndServe() 启动服务器功能.

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.

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

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

博客文章 Writing Web Applications 也很有帮助.

The Blog article Writing Web Applications is also helpful.

然而,提供了静态文件服务器或文件系统"功能,有一个<http 包中的 code>FileServer() 函数返回一个 Handler ,它提供静态文件.您可以指定根"文件夹来提供静态文件作为 FileServer() 的参数.

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

如果您将绝对路径传递给FileServer(),那么毫无疑问这意味着什么.如果您提供一个相对路径,它总是在当前工作目录的上下文中被解释.默认情况下,这是您启动应用程序的文件夹(您在执行 go run ... 命令或编译后的可执行二进制文件时所在的文件夹).

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

示例:

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

这将设置一个处理程序来提供来自文件夹 /tmp 映射到根 URL / 的文件.例如,对 GET 请求 "/mydoc.txt" 的响应将是 "/tmp/mydoc.txt" 静态文件.

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.

完成申请:

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

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

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"))))

这篇关于使用 Go 的网络服务器,网站的根在哪里映射到文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆