使用gogs和glide來輕鬆拉取golang第三方庫

golang的第三方代碼拉取一直是讓人頭疼的問題,在github託管的代碼還好,託管在其餘網站上的代碼總會因爲你們都懂的緣由,沒法訪問。縱使是github,在拉取文件數量較多的庫時,也是比較慢的。html

有沒有比較好的解決方案呢?有的,這裏給你們提供一個:gogs + glidenode

gogs簡介

gogs 官網 無需多作介紹,對標Gitlab便可。gogs使用golang開發,只要是go語言支持的平臺它都支持,其搭建很是簡單,這也是咱們使用它而不用Gitlab的緣由。mysql

glide 簡介

glide glide是Go的包管理工具,對標godep,支持私有的Repos和Forks,這就能夠和我們的gogs一塊兒愉快的玩耍了。nginx

具體使用過程,我就很少說了,你們百度一下,資料不少。下面開始講一下搭建過程:git

安裝Mysql

gogs依賴數據庫,咱們選擇使用Mysql,網上安裝方法不少,不贅述github

安裝gogs

安裝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反向代理

咱們使用Nginx來進行反向代理,以提供比較優雅的域名方式訪問.

安裝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

配置https

生成證書

輸入如下指令生成證書

$ 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,其他直接回車跳過.

修改nginx虛擬主機配置

修改以前生成的虛擬主機配置文件

$ 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/;
    }
}

修改gogs配置

修改$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

解決方案:

跳過http的ssl證書驗證

git config --global http.sslverify false

再次嘗試拉取,發現能夠拉取代碼了.

使用glide來設置代碼映射

咱們在拉取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能夠幫咱們作這樣的事情.讓咱們先安裝它.

安裝glide

具體安裝方式網上不少,我以前也寫了這個,能夠參照下 go包管理工具glide使用方法

設置mirrors

好比咱們須要拉取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來造成映射,這樣拉取下來的代碼就能夠放置在正確的目錄裏.

至此,整個流程就結束了,我們能夠絲滑的拉取代碼了.

相關文章
相關標籤/搜索