脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - 在go文件服务器加入http.StripPrefix的用途介绍

在go文件服务器加入http.StripPrefix的用途介绍

2021-03-07 00:52yyyzhhhhh Golang

这篇文章主要介绍了在go文件服务器加入http.StripPrefix的用途介绍,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

例子:

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

当访问localhost:xxxx/tmpfiles时,会路由到fileserver进行处理

当访问URL为/tmpfiles/example.txt时,fileserver会将/tmp与URL进行拼接,得到/tmp/tmpfiles/example.txt,而实际上example.txt的地址是/tmp/example.txt,因此这样将访问不到相应的文件,返回404 NOT FOUND。

因此解决方案就是把URL中的/tmpfiles/去掉,而http.StripPrefix做的就是这个。

补充:go语言实现一个简单的文件服务器 http.FileServer

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
 "flag"
 "fmt"
 "github.com/julienschmidt/httprouter"
 "log"
 "net/http"
 "strings"
 "time"
)
func main() {
 root := flag.String("p", "", "file server root directory")
 flag.Parse()
 if len(*root) == 0 {
 log.Fatalln("file server root directory not set")
 }
 if !strings.HasPrefix(*root, "/") {
 log.Fatalln("file server root directory not begin with '/'")
 }
 if !strings.HasSuffix(*root, "/") {
 log.Fatalln("file server root directory not end with '/'")
 }
 p, h := NewFileHandle(*root)
 r := httprouter.New()
 r.GET(p, LogHandle(h))
 log.Fatalln(http.ListenAndServe(":8080", r))
}
func NewFileHandle(path string) (string, httprouter.Handle) {
 return fmt.Sprintf("%s*files", path), func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
 http.StripPrefix(path, http.FileServer(http.Dir(path))).ServeHTTP(w, r)
 }
}
func LogHandle(handle httprouter.Handle) httprouter.Handle {
 return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
 now := time.Now()
 handle(w, r, p)
 log.Printf("%s %s %s done in %v", r.RemoteAddr, r.Method, r.URL.Path, time.Since(now))
 }
}

准备测试文件

在go文件服务器加入http.StripPrefix的用途介绍

编译运行

在go文件服务器加入http.StripPrefix的用途介绍

用浏览器访问

在go文件服务器加入http.StripPrefix的用途介绍

在go文件服务器加入http.StripPrefix的用途介绍

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/a13602955218/article/details/106692668

延伸 · 阅读

精彩推荐
  • GolangGolang通脉之数据类型详情

    Golang通脉之数据类型详情

    这篇文章主要介绍了Golang通脉之数据类型,在编程语言中标识符就是定义的具有某种意义的词,比如变量名、常量名、函数名等等,Go语言中标识符允许由...

    4272021-11-24
  • GolangGolang中Bit数组的实现方式

    Golang中Bit数组的实现方式

    这篇文章主要介绍了Golang中Bit数组的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    天易独尊11682021-06-09
  • Golanggolang的httpserver优雅重启方法详解

    golang的httpserver优雅重启方法详解

    这篇文章主要给大家介绍了关于golang的httpserver优雅重启的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    helight2992020-05-14
  • Golanggo日志系统logrus显示文件和行号的操作

    go日志系统logrus显示文件和行号的操作

    这篇文章主要介绍了go日志系统logrus显示文件和行号的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    SmallQinYan12302021-02-02
  • Golanggolang如何使用struct的tag属性的详细介绍

    golang如何使用struct的tag属性的详细介绍

    这篇文章主要介绍了golang如何使用struct的tag属性的详细介绍,从例子说起,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看...

    Go语言中文网11352020-05-21
  • Golanggolang 通过ssh代理连接mysql的操作

    golang 通过ssh代理连接mysql的操作

    这篇文章主要介绍了golang 通过ssh代理连接mysql的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    a165861639710342021-03-08
  • Golanggolang json.Marshal 特殊html字符被转义的解决方法

    golang json.Marshal 特殊html字符被转义的解决方法

    今天小编就为大家分享一篇golang json.Marshal 特殊html字符被转义的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    李浩的life12792020-05-27
  • Golanggo语言制作端口扫描器

    go语言制作端口扫描器

    本文给大家分享的是使用go语言编写的TCP端口扫描器,可以选择IP范围,扫描的端口,以及多线程,有需要的小伙伴可以参考下。 ...

    脚本之家3642020-04-25