Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows 下 tusd 编译安装全流程 #75

Closed
Dream4ever opened this issue Jul 30, 2019 · 0 comments
Closed

Windows 下 tusd 编译安装全流程 #75

Dream4ever opened this issue Jul 30, 2019 · 0 comments
Labels
Back-end Where data really come and go Server The invisible hero Software About installation ande usage

Comments

@Dream4ever
Copy link
Owner

Dream4ever commented Jul 30, 2019

项目简介

tus/tusd,这是项目官网,该项目是一个后端服务,用于接收从前端页面所上传的文件。搭配前端 JS 库 transloadit/uppy,即可实现完整的文件上传功能。

tusd 这个库的一大亮点,是可以实现文件的断点续传。

项目克隆

需已在 Windows 上安装 Go

进入 $GOPATH\src 目录,$GOPATH 通常位于 C:\Users\XXX\go。如果没有的话,就新建对应目录,路径里的 XXX 是当前用户名。

执行下面的命令,将项目克隆至当前目录中。

git clone https://github.com/tus/tusd.git

第一个坑:注意上面的克隆地址和 GitHub 官网给出的不一样,如果用的是官网给出的地址 git@github.com:tus/tusd.git,会报下面的错误:

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

安装依赖

执行下面两条命令,下载并安装 tusd 所需的依赖。

go get -u github.com/aws/aws-sdk-go/
go get -u github.com/prometheus/client_golang/prometheus

第二个坑:为保证依赖能够正常下载,请参照 解决 go get 太慢的问题 一文中所说的方法,配置 GOPROXY 参数,并在终端开启代理。

经实际测试,在配置了 GOPROXY 并在终端开启代理之后,第一个依赖 aws-sdk-go 可正常下载编译。

第三个坑:即使配置了 GOPROXY 并在终端开启代理,安装第二个依赖 prometheus 的时候还是会报错。这里并不是说所做的设置出了问题,而是因为这个依赖需要从 golang.org/x 这个网站上下载包。奇怪的是,在终端所开启的代理对 GitHub 有效,对这个网站就无效,所以才有了这个坑。

既然如此,就只能曲线救国了,按照 解决 go get 太慢的问题 一文中所说的另一个方法,去 golang 在 GitHub 上的镜像项目里下载对应的包。

比如在安装第二个依赖 prometheus 的时候,给出了下面的提示,意思就是没能下载到 sys/windows 这个包。

package golang.org/x/sys/windows: unrecognized import path "golang.org/x/sys/windows" (https fetch: Get https://golang.org/x/sys/windows?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

那就去 golang 在 GitHub 上面的网站看看,可以找到 sys/windows 这个包,先把它下载过来:

go get -u github.com/golang/sys/windows

上面的命令会把 sys/windows 这个包下载到 $GOPATH\src\github.com\golang\sys 目录下,而前面安装依赖 prometheus 的时候,所提到的包的名称是 golang.org/x/sys/windows,那就需要把刚下载过来的这个包放到提示信息中所说的目录下面。

为了省事,直接把 $GOPATH\src\github.com\golang\sys 这个文件夹,复制到 $GOPATH\src\golang.org\x 下面。

这个时候,再执行一遍下面的命令,继续安装第二个依赖。

go get -u github.com/prometheus/client_golang/prometheus

这次的安装之所以能成功,是因为 go get -u 中的 -u 参数,对于已经下载到本地的包,不会重复下载,这样前面手动把 sys/windws 这个包下载过来了,所以这次安装第二个依赖的时候,就能直接使用已经下载过来的这个包了。

编译项目

执行下面的命令,开始编译生成 tusd 对应的可执行文件。

cd $GOPATH\src\tusd
go build -o tusd.exe cmd/tusd/main.go

第四个坑:执行 go build 命令之后,会再次报错:

cmd\tusd\main.go:4:2: cannot find package "github.com/tus/tusd/cmd/tusd/cli" in any of:
        C:\Users\Administrator\go\src\tusd\vendor\github.com\tus\tusd\cmd\tusd\cli (vendor tree)
        c:\go\src\github.com\tus\tusd\cmd\tusd\cli (from $GOROOT)
        C:\Users\Administrator\go\src\github.com\tus\tusd\cmd\tusd\cli (from $GOPATH)

上网查了查,原来有人已经遇到过这个问题了,说是再手动安装一下 github.com/tus/tusd/cmd/tusd/cli 这个包就行。好吧,那就执行下面的命令,手动安装一下这个包:

go get -u github.com/tus/tusd/cmd/tusd/cli

安装完所需的包之后,再次执行下面的命令,编译代码:

go build -o tusd.exe cmd/tusd/main.go

第五个坑:这回终于能够成功编译了,不过这里的命令和官网的还是有点不一样。go build 命令的 -o 参数后面必须指定文件名,在 Unix/Linux 系统里,可执行文件是没有扩展名的,但是在 Windows 上就不一样了,通常是 .exe,而 go build 命令的 -o 参数是按照 Unix/Linux 的习惯来的,所以在 Windows 下使用 go build -o abc 这样的命令来编译的话,是不会自动加上 .exe 这个扩展名的,需要明确写上扩展名才可以。

项目运行

执行下面的命令,即可将 tusd 运行在指定的 IP 和端口上:

.\tusd.exe -host 127.0.0.1 -port 1088

参考资料

  • Are there "always on" module repositories and enterprise proxies?:列表中包含国内的几个 Go 镜像站。
  • 无法安装 golang.org/x/tools/的库:golang.org/x 或者 go.googlesource.com 上面的包无法下载的话,都可以用这个曲线救国的办法,去 GitHub 上找对应的镜像项目下过来,然后放到这个包在 golang.org/x 或者 go.googlesource.com 上的对应位置下。
  • Compile from source on MacOS:明明是这个项目里的包,却还是报错,就因为路径不对,也是够坑的。用 git clone 就是把当前项目下载到当前路径,比如本项目 tusd,就是下载到当前目录的 tusd 文件夹下。用 go get 则是把当前项目下载到相对路径下,比如本项目就是下载到 $GOPATH\src\github.com\tus\tusd 这个路径下。等等,如果我最开始不是克隆项目,而是用 go get 来下载安装这个项目的话,是不是会省事很多?大坑啊!
  • go build: Compile packages and dependencies:看了官方文档,才知道 go build-o 参数在 Windows 下,不会自动附加可执行文件的 .exe 扩展名。
@Dream4ever Dream4ever added Back-end Where data really come and go Server The invisible hero Software About installation ande usage labels Jul 30, 2019
@Dream4ever Dream4ever changed the title Windows 下 tusd 安装编译全流程 Windows 下 tusd 编译安装全流程 Jul 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Back-end Where data really come and go Server The invisible hero Software About installation ande usage
Projects
None yet
Development

No branches or pull requests

1 participant