gohttpserver实现上传zip自动解压,删除文件夹,添加自定义接口 - Go语言中文社区

gohttpserver实现上传zip自动解压,删除文件夹,添加自定义接口


gohttpserver

Build Status

Demo site

  • 目标: 做最好的HTTP文件服务器
  • 功能: 人性化的UI体验,文件的上传支持,安卓和苹果安装包的二维码直接生成。

Binaries源码可以从 这里下载

Requirements

Tested with go-1.10, go-1.11

Features

  1. 支持QRCode代码生成
  2. 面包屑路径快速变化
  3. 所有资产打包成独立的二进制文件
  4. 不同的文件类型不同的图标
  5. 支持显示或隐藏隐藏文件
  6. 上传支持(令牌或会话验证)
  7. README。md预览
  8. HTTP基本验证
  9. 目录更改时部分重新加载页面
  10. 当目录下只有一个目录时,path将把两个目录合并在一起
  11. 目录zip下载
  12. 苹果ipa自动生成.plist文件,qrcode可被iphone识别(要求https)
  13. Plist代理
  14. 下载计数统计
  15. 歌珥启用
  16. 离线下载
  17. 代码文件预览
  18. 编辑文件支持
  19. 全局文件搜索
  20. 隐藏工作’下载’和’ qrcode '在小屏幕
  21. 主题选择支持
  22. 可以在Nginx后面工作
  23. .ghs。yml支持(比如.htaccess)
  24. 计算md5和sha
  25. 文件夹上传
  26. 支持按大小或修改时间排序
  27. 在索引页中添加版本信息
  28. 添加api ’ /-/info/some.(apk|ipa) '获取详细信息
  29. 添加api ’ /-/apk/info/一些。apk '获取android包信息
  30. 自动标签版本
  31. 自定义标题支持
  32. 支持从conf文件设置
  33. 快速复制下载链接
  34. 显示文件夹大小
  35. 创建文件夹
  36. 按alt键时跳过删除确认
  37. 上传时支持解压缩(表单:unzip=true)

Installation

go get -v github.com/codeskyblue/gohttpserver
cd $GOPATH/src/github.com/codeskyblue/gohttpserver
go build && ./gohttpserver

或者,从 github 发布包下载。

Usage

用8000端口监听所有接口,并启用文件上传

./gohttpserver -r ./ --port 8000 --upload

命令 gohttpserver --help 获取更多用例.

Docker Usage

分享当前目录

docker run -it --rm -p 8000:8000 -v $PWD:/app/public --name gohttpserver codeskyblue/gohttpserver

使用http basic auth共享当前目录

docker run -it --rm -p 8000:8000 -v $PWD:/app/public --name gohttpserver 
  codeskyblue/gohttpserver 
  --auth-type http --auth-http username:password

使用openid auth共享当前目录。(仅限网易公司工作)

docker run -it --rm -p 8000:8000 -v $PWD:/app/public --name gohttpserver 
  codeskyblue/gohttpserver 
  --auth-type openid

要自己构建映像,请将PWD更改为这个repo的根。

cd gohttpserver/
docker build -t codeskyblue/gohttpserver -f docker/Dockerfile .

身份验证选项

header value
X-Auth-Request-Email userId
X-Auth-Request-Fullname user’s display name(urlencoded)
X-Auth-Request-User user’s nickname (mostly email prefix)
  • 允许上传

    $ gohttpserver --upload
    
  • 启用删除和创建文件夹

    $ gohttpserver --delete
    

高级用法

通过创建’ .ghs添加访问规则。子目录下的yml '文件。一个例子:

---
upload: false
delete: false
users:
- email: "codeskyblue@codeskyblue.com"
  delete: true
  upload: true
  token: 4567gf8asydhf293r23r

在本例中,如果启用了openid auth,并且用户“codeskyblue@codeskyblue.com”已经登录,那么他/她可以删除/上传位于’ .ghs目录下的文件。yml文件出口。
’ token '用于上传。

see upload with curl

例如,在下面的目录层次结构中,用户可以在目录’ foo ‘中删除/上传文件,但在目录’ bar '中不能这样做。

root -
  |-- foo
  |    |-- .ghs.yml
  |    `-- world.txt 
  `-- bar
       `-- hello.txt

用户可以使用 --conf指定配置文件名, see example config.yml.

要指定哪些文件是隐藏的,哪些文件是可见的,请将以下行添加到 .ghs.yml

accessTables:
- regex: block.file
  allow: false
- regex: visual.file
  allow: true

ipa plist代理

这用于启用https的服务器。默认使用< https://plistproxy.herokuapp.com/plist >

./gohttpserver --plistproxy=https://someproxyhost.com/

测试代理是否工作:

$ http POST https://someproxyhost.com/plist < app.plist
{
	"key": "18f99211"
}
$ http GET https://someproxyhost.com/plist/18f99211
# show the app.plist content

如果运行在nginx服务器后面的ghs配置了https。plistproxy将自动禁用。

CURL上传

例如,上传一个名为foo.txt到目录somedir

$ curl -F file=@foo.txt localhost:8000/somedir
{"destination":"somedir/foo.txt","success":true}
# upload with token
$ curl -F file=@foo.txt -F token=12312jlkjafs localhost:8000/somedir
{"destination":"somedir/foo.txt","success":true}

# upload and change filename
$ curl -F file=@foo.txt -F filename=hi.txt localhost:8000/somedir
{"destination":"somedir/hi.txt","success":true}

上传zip文件并解压(解压完成后,zip文件将被删除)

$ curl -F file=@pkg.zip -F unzip=true localhost:8000/somedir
{"success": true}

Note: /:*<>| are not allowed in filenames.

Deploy with nginx

建议配置,假设您的gohttpserver监听 127.0.0.1:8200

server {
  listen 80;
  server_name your-domain-name.com;

  location / {
    proxy_pass http://127.0.0.1:8200; # here need to change
    proxy_redirect off;
    proxy_set_header  Host    $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;

    client_max_body_size 0; # disable upload limit
  }
}

当使用nginx时,gohttpserver应该带 --xheaders 启动.

Refs: http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

FAQ

How the query is formated

The search query follows common format rules just like Google. Keywords are seperated with space(s), keywords with prefix - will be excluded in search results.

  1. hello world means must contains hello and world
  2. hello -world means must contains hello but not contains world

Developer Guide

Depdencies are managed by govendor

  1. Build develop version. assets directory must exists
go build
./gohttpserver
  1. Build single binary release
go generate .
go build -tags vfs

Theme are defined in assets/themes directory. Now only two themes are available, “black” and “green”.

Reference Web sites

Go Libraries

History

The old version is hosted at https://github.com/codeskyblue/gohttp

LICENSE

This project is licensed under MIT.


支持上传zip的自动解压

执行拉取代码,执行install,并build项目后,
/root/go/src/github.com/codeskyblue/gohttpserver/assets/js/dropzone.js的第1382行加入如下代码

if(files[i].name.endsWith(".zip")){
          formData.append('unzip','true');
      }

支持文件夹删除

执行拉取代码,在/root/go/src/github.com/codeskyblue/gohttpserver/httpstaticserver.go 文件中,
在import中添加新依赖 os/exec

  import(
        ·
        ·
        ·
      "os/exec"
        ·
        ·
        ·
  )

找到hDelete方法,将err := os.remove(filepath.Join(s.Root,path))替换如下代码:

  cmd := exec.Command("rm","-rf",filepath.Join(s.Root, path))
  err := cmd.Run()

ok~

添加自定义的接口

/root/go/src/github.com/codeskyblue/gohttpserver/httpstaticserver.go 文件中,
第85行 添加m.HandleFunc("/-/ambari/restart", s.ambariRestart)
–tips 第一个参数是路径 第二个参数是方法

func NewHTTPStaticServer(root string) *HTTPStaticServer {
  if root == "" {
  	root = "./"
  }
  root = filepath.ToSlash(root)
  if !strings.HasSuffix(root, "/") {
  	root = root + "/"
  }
  log.Printf("root path: %sn", root)
  m := mux.NewRouter()
  s := &HTTPStaticServer{
  	Root:  root,
  	Theme: "black",
  	m:     m,
  }

  go func() {
  	time.Sleep(1 * time.Second)
  	for {
  		startTime := time.Now()
  		log.Println("Started making search index")
  		s.makeIndex()
  		log.Printf("Completed search index in %v", time.Since(startTime))
  		//time.Sleep(time.Second * 1)
  		time.Sleep(time.Minute * 10)
  	}
  }()
  // 重启ambari
  m.HandleFunc("/-/ambari/restart", s.ambariRestart)
  // routers for Apple *.ipa
  m.HandleFunc("/-/ipa/plist/{path:.*}", s.hPlist)
  m.HandleFunc("/-/ipa/link/{path:.*}", s.hIpaLink)

  m.HandleFunc("/{path:.*}", s.hIndex).Methods("GET", "HEAD")
  m.HandleFunc("/{path:.*}", s.hUploadOrMkdir).Methods("POST")
  m.HandleFunc("/{path:.*}", s.hDelete).Methods("DELETE")
  
  
  return s
}

然后添加对应的方法:

func (s *HTTPStaticServer) ambariRestart(w http.ResponseWriter, req *http.Request) {
	// only can delete file now
	cmd := exec.Command("ambari-server","restart")
	err := cmd.Run()
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	w.Write([]byte("Success"))
}
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_16773855/article/details/88870153
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢