• 使用gogs和glide来轻松拉取golang第三方库


    golang的第三方代码拉取一直是让人头疼的问题,在github托管的代码还好,托管在其他网站上的代码总会由于大家都懂的原因,无法访问。纵使是github,在拉取文件数量较多的库时,也是比较慢的。

    有没有比较好的解决方案呢?有的,这里给大家提供一个:gogs + glide

    gogs简介

    gogs
    官网
    无需多做介绍,对标Gitlab即可。gogs使用golang开发,只要是go语言支持的平台它都支持,其搭建非常简单,这也是我们使用它而不用Gitlab的原因。

    glide 简介

    glide
    glide是Go的包管理工具,对标godep,支持私有的Repos和Forks,这就可以和咱们的gogs一起愉快的玩耍了。

    具体使用过程,我就不多说了,大家百度一下,资料很多。下面开始讲一下搭建过程:

    安装Mysql

    gogs依赖数据库,我们选择使用Mysql,网上安装方法很多,不赘述

    安装gogs

    安装gogs方式很多,比如

    • 源码安装
    • 二进制安装
    • 包安装

    我们这里讲的是源码安装.

    前提:系统里需要先安装Go语言,如果没安装,网上教程很多

    从源码安装

    # 下载并安装依赖
    $ 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来形成映射,这样拉取下来的代码就可以放置在正确的目录里.

    至此,整个流程就结束了,咱们可以丝滑的拉取代码了.

  • 相关阅读:
    Linux命令行和Shell脚本编程
    Excel自动触发时间
    dom4j读取xml文件 简单例子
    GSM & Foxit Reader
    ThreadPoolExecutor 线程池
    Linux /var/log
    delphi开发技巧
    利用ScktSrvr打造多功能Socket服务器
    delphi中Windows消息大全使用详解
    自己构造注入点方便入侵
  • 原文地址:https://www.cnblogs.com/laolieren/p/pull_golang_vendor_with_gogs_and_glide.html
Copyright © 2020-2023  润新知