0%

React+GO 搭建文件服务器(Nginx部署)

Go 路由静态文件

由于Go本身自带路由静态文件的功能,便将React工程和Hexo工程都交由Go进行静态路由

原Hexo工程自带的的NodeJs Server弃用

  • 注意:系统内部的各个server还是交由Nginx进行反向代理,所以go server依旧监听本地端口
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
// 路由指定目录下的全部文件和文件夹

var PathStatic []StaticRoot = []StaticRoot{
{Router: "/", Path: "../hexo_blog/public/"},
{Router: "/file/", Path: "../liry-web-tool/build/"},
}

func (u *StaticDir) Bind() {

for _, item := range config.PathStatic {
dirs, err := ioutil.ReadDir(item.Path)
if err != nil {
log.Println("读取静态文件夹失败:", item.Path)
continue
}

for _, fi := range dirs {
fi.Name()
if fi.IsDir() {
u.Ge.Static(item.Router+fi.Name(), item.Path+fi.Name())
} else {
u.Ge.StaticFile(item.Router+fi.Name(), item.Path+fi.Name())
}
}
u.Ge.StaticFile(item.Router, item.Path+"index.html")
}
}

启动 Go Server

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
func main() {
r := gin.Default()
r.MaxMultipartMemory = 512 << 20 // 512 MiB

(&routers.StaticDir{
Routers: &routers.Routers{Ge: r},
}).Bind()

(&routers.Upload{
Routers: &routers.Routers{Ge: r},
}).Bind()

(&routers.FileList{
Routers: &routers.Routers{Ge: r},
}).Bind()

(&routers.Download{
Routers: &routers.Routers{Ge: r},
}).Bind()

(&routers.Delete{
Routers: &routers.Routers{Ge: r},
}).Bind()

r.Run("127.0.0.1:47777") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

Go Nginx配置

Go 监听本地47777端口,对外交由Nginx

1
2
3
4
5
6
location / {
root html;
index index.html index.htm;

proxy_pass http://127.0.0.1:47777;
}

pm2 托管 go server

直接用pm2启动go的可执行程序即可,不用额外编写脚本

1
pm2 start ./xxxxx