golang的第三方代码拉取一直是让人头疼的问题,在github
托管的代码还好,托管在其余网站上的代码总会因为你们都懂的缘由,没法访问。纵使是github,在拉取文件数量较多的库时,也是比较慢的。html
有没有比较好的解决方案呢?有的,这里给你们提供一个:gogs
+ glide
。node
官网 无需多作介绍,对标
Gitlab
便可。gogs使用golang开发,只要是go语言支持的平台它都支持,其搭建很是简单,这也是咱们使用它而不用Gitlab的缘由。mysql
glide是Go的包管理工具,对标
godep
,支持私有的Repos和Forks,这就能够和我们的gogs
一块儿愉快的玩耍了。nginx
具体使用过程,我就很少说了,你们百度一下,资料不少。下面开始讲一下搭建过程:git
gogs依赖数据库,咱们选择使用Mysql,网上安装方法不少,不赘述github
安装gogs方式不少,好比golang
咱们这里讲的是源码安装.web
前提:系统里须要先安装Go语言,若是没安装,网上教程不少sql
# 下载并安装依赖 $ go get -u github.com/gogs/gogs # 构建主程序 $ cd $GOPATH/src/github.com/gogs/gogs $ go build
接着上一步,而后输入如下指令数据库
./gogs web
这时候发现报错,缺乏文件,新建文件$GOPATH/src/github.com/gogs/gogs/custom/conf/app.ini
,内容为
APP_NAME = Gogs RUN_USER = fabric RUN_MODE = prod [database] DB_TYPE = mysql HOST = 127.0.0.1:3306 NAME = gogs USER = gogs PASSWD = 123456 SSL_MODE = disable PATH = data/gogs.db [repository] ROOT = /home/fabric/gogs-repositories [server] DOMAIN = localhost HTTP_PORT = 3000 ROOT_URL = http://localhost:3000/ DISABLE_SSH = false SSH_PORT = 22 START_SSH_SERVER = false OFFLINE_MODE = false [mailer] ENABLED = false [service] REGISTER_EMAIL_CONFIRM = false ENABLE_NOTIFY_MAIL = false DISABLE_REGISTRATION = false ENABLE_CAPTCHA = true REQUIRE_SIGNIN_VIEW = false [picture] DISABLE_GRAVATAR = false ENABLE_FEDERATED_AVATAR = false [session] PROVIDER = file [log] MODE = file LEVEL = Info ROOT_PATH = /opt/gopath/src/github.com/gogs/gogs/log [security] INSTALL_LOCK = true SECRET_KEY = UfjSHvQULpjPmJk
注意上文里的RUN_USER = fabric
,这里是运行gogs
程序的用户,默认为git
,咱们能够改成本身的默认用户,个人叫作fabric
.
再次运行./gogs web
,打开http://localhost:3000/
发现已经能够看到gogs
的安装页面了.
所有默认便可.
咱们新建好仓库后,尝试拉取代码
git clone http://localhost:3000/fabric/test.git
拉取成功!
但这不是咱们能知足的,localhost:3000
这样子太丑陋了!咱们但愿看到相似https://github.com/jinzhu/gorm.git
这样的形式.
让咱们继续行动吧!
咱们使用Nginx来进行反向代理,以提供比较优雅的域名方式访问.
老话,百度一下,你就知道哈.
我们仿造github,起名githubs
.
在/etc/nginx/sites-available
目录下新建文件githubs
,内容为
server { server_name githubs.com; listen 80; location / { proxy_pass http://127.0.0.1:3000/; } }
而后进入 /etc/nginx/sites-enabled
中,执行 ln -s ../sites-available/githubs
,以启用这个配置文件。 最后重启 nginx 就行了,Ubuntu 下是
sudo service nginx restart
而后在hosts里面添加映射
$ vim /etc/hosts 127.0.0.1 gitbar.com
git clone http://githubs.com/fabric/test.git
能够看到代码能够正常拉取了.
可是这样还不够,咱们须要的是https
方式的拉取,如今只是http
输入如下指令生成证书
$ cd /etc/nginx/ $ sudo mkdir ssl $ sudo openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /etc/nginx/ssl/githubs.key -out /etc/nginx/ssl/githubs.crt
最后一步会要求输入一些信息
Generating a 2048 bit RSA private key ............................................+++ ...................+++ writing new private key to '/etc/nginx/ssl/githubs.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]: Locality Name (eg, city) []: Organization Name (eg, company) [Internet Widgits Pty Ltd]: Organizational Unit Name (eg, section) []: Common Name (e.g. server FQDN or YOUR name) []:githubs.com Email Address []:
其中Common Name
字段是必填的,就是我们的域名githubs.com
,其他直接回车跳过.
修改以前生成的虚拟主机配置文件
$ vim /etc/nginx/sites-available/githubs
内容为:
server { listen 443 ssl; server_name www.githubs.com; ssl on; ssl_certificate /etc/nginx/ssl/githubs.crt; ssl_certificate_key /etc/nginx/ssl/githubs.key; location / { proxy_pass http://127.0.0.1:3000/; } } server { listen 80; server_name www.githubs.com; location / { proxy_pass http://127.0.0.1:3000/; } }
修改$GOPATH/src/github.com/gogs/gogs/custom/conf/app.ini
文件,修改server
字段为
[server] DOMAIN = localhot HTTP_PORT = 3000 ROOT_URL = https://githubs.com/ DISABLE_SSH = false SSH_PORT = 22 START_SSH_SERVER = false OFFLINE_MODE = false
使用https方式拉取代码,报错:
fatal: unable to access 'https://www.githubs.com/sn/test.git/': server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none
解决方案:
git config --global http.sslverify false
再次尝试拉取,发现能够拉取代码了.
咱们在拉取golang.org/x/*
这样的代码库时候,常常发现报错,主要由于被墙的缘由.
其实这些代码都在github
有托管仓库,传统的解决方式多是这样的 :
$ cd /opt/gopath/src/github.com/golang $ git clone https://github.com/golang/net.git $ git clone https://github.com/golang/sys.git $ ln -s /opt/gopath/src/github.com/golang /opt/gopath/src/golang.org/x
这样的方式很麻烦,可不能够设置映射关系,当拉取golang.org/x/net
包时候,会自动拉取github.com/golang/net
内容,并放置在golang.org/x/net
目录下呢?
有的,glide
能够帮咱们作这样的事情.让咱们先安装它.
具体安装方式网上不少,我以前也写了这个,能够参照下 go包管理工具glide使用方法
好比咱们须要拉取mg.org/x/test
包,他不幸的被墙了,可是代码托管在我们本身搭建的gogs上,即githubs.com/fabric/test
上.咱们只须要设置mirrors
便可.
命令很简单 $ glide mirror set https://mg.org/x/test https://githubs.com/fabric/test.git --vcs git
查看mirrors文件内容
$ vim ~/.glide/mirrors.yaml repos: - original: https://mg.org/x/test repo: https://githubs.com/fabric/test.git vcs: git
咱们能够用glide get
方式来拉取代码.好比咱们有这样的代码库github.com/mango/template
这个文件夹首先是不存在
$ cd $GOPATH/src/github.com $ mkdir -p mango/template $ cd mango/template $ glide init $ glide get mg.org/x/test [INFO] Loading mirrors from mirrors.yaml file [INFO] Preparing to install 1 package. [INFO] Attempting to get package mg.org/x/test [INFO] --> Gathering release information for mg.org/x/test [INFO] --> Adding mg.org/x/test to your configuration [INFO] Downloading dependencies. Please wait... [INFO] --> Fetching mg.org/x/test [INFO] Resolving imports [INFO] Downloading dependencies. Please wait... [INFO] Exporting resolved dependencies... [INFO] --> Exporting mg.org/x/test [INFO] Replacing existing vendor dependencies
代码成功拉取了!
咱们能够将经常使用的一些第三方代码库先拉取到本身部署的gogs上,而后使用mirrors
来造成映射,这样拉取下来的代码就能够放置在正确的目录里.
至此,整个流程就结束了,我们能够丝滑的拉取代码了.