使用golang webserver,网站的根目录映射到文件系统上? [英] With golang webserver where does the root of the website map onto the filesystem?

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

问题描述

golang web服务器的文件系统root在哪里。它似乎不在可执行文件所在的目录中。
root我的意思是我将使用的目录,例如img的src属性,没有任何路径。我不打算这样做,但是如果我知道的话,它可以帮助我理解结构。

解决方案



转到 net / http 软件包用于提供Web服务器功能。这不是一个静态文件服务器,它远不止于此。



没有文件系统的root概念。提供的Web服务器使用 处理程序 来提供HTTP请求被映射到URL。处理程序负责处理HTTP请求并设置并生成响应。处理程序可以被登记,例如使用 Handle() HandleFunc() 功能。服务器可以使用 ListenAndServe() 功能。

阅读 net / http 了解基本概念并开始使用。它还包含许多小例子。 博客文章编写Web应用程序也是有帮助的。

静态文件服务器



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



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



示例:

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

这将设置一个处理程序,映射到根URL / 的文件夹 / tmp 。例如,对GET请求/ mydoc.txt的响应将是/ tmp / mydoc.txt
$ b

完整的应用程序:

  package main 
导入(
log
net / http

func main(){
//简单静态网络服务器:
log。致命(http.ListenAndServe(:8080,
http.Handle(/,http.FileServer(http.Dir(/ tmp)))))
}

您可以使用 StripPrefix() 函数。示例:

  //在一个可选URL下为磁盘(/ tmp)提供一个目录
// path( / tmpfiles /),使用StripPrefix修改请求
// FileServer发现它之前的URL路径:
http.Handle(/ tmpfiles /,
http.StripPrefix(/ tmpfiles /,http.FileServer(http.Dir(/ tmp))))


Where is the filesystem "root" of a golang 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

解决方案

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:
    log.Fatal(http.ListenAndServe(":8080",
            http.Handle("/", http.FileServer(http.Dir("/tmp")))))
}

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

这篇关于使用golang webserver,网站的根目录映射到文件系统上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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