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

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

问题描述

Go net/http Web服务器的文件系统根"在哪里.它似乎不在可执行文件所在的目录中. 根"指的是我将用于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服务器使用 处理程序 来满足HTTP请求映射到URL.处理程序负责处理HTTP请求,并设置和生成响应.可以注册处理程序,例如使用 Handle() 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.

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

但是,提供了静态文件服务器或文件系统"功能,其中有一个> http程序包中的c8> 函数,该函数返回为静态文件提供服务的Handler.您可以指定"root"文件夹来作为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(),那么毫无疑问,这意味着什么.如果提供了 relative 路径,则始终在 current working 目录的上下文中解释该路径.默认情况下,这是您从中启动应用程序的文件夹(执行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")))

这将设置一个处理程序,以处理映射到根URL /的文件夹/tmp中的文件.例如,对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天全站免登陆