You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7.8 KiB

nnet 域名配置指南(已弃用)

⚠️ 注意

当前项目已改用 GitHub 作为模块路径github.com/noahlann/nnet

本文档仅作为参考,说明如何配置自定义域名。如果您需要使用自定义域名,可以参考本文档。

一、域名配置概述

使用自定义域名(如 go.noahlan.cn)作为 Go 模块路径,这样可以:

  • 不依赖特定 Git 平台GitHub、Gitee等
  • 可以随时切换代码仓库
  • 更专业的模块路径
  • 更好的控制权

当前配置:项目使用 github.com/noahlann/nnet,无需域名配置。

二、域名 DNS 配置

2.1 配置 DNS 记录

需要在域名 DNS 中配置以下记录:

类型: CNAME 或 A
名称: go
值:   gopkg.in 或直接指向 Git 服务器 IP
TTL:  3600

2.2 Go 模块路径重定向

Go 工具会访问 https://go.noahlan.cn/nnet?go-get=1 来获取模块信息,需要返回以下 HTML

<!DOCTYPE html>
<html>
<head>
    <meta name="go-import" content="go.noahlan.cn/nnet git https://github.com/yourusername/nnet.git">
    <meta name="go-source" content="go.noahlan.cn/nnet https://github.com/yourusername/nnet https://github.com/yourusername/nnet/tree/main{/dir} https://github.com/yourusername/nnet/blob/main{/dir}/{file}#L{line}">
</head>
<body>
    <a href="https://github.com/yourusername/nnet">go.noahlan.cn/nnet</a>
</body>
</html>

2.3 实现方式

方式1使用 gopkg.in 服务(推荐,最简单)

如果使用 gopkg.in 服务,只需要:

  1. 在 GitHub/Gitee 创建仓库
  2. 在 gopkg.in 注册:https://gopkg.in
  3. 配置重定向规则

方式2自建 HTTP 服务

创建一个简单的 HTTP 服务,处理 ?go-get=1 请求:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    if r.URL.Query().Get("go-get") == "1" {
        html := `<!DOCTYPE html>
<html>
<head>
    <meta name="go-import" content="go.noahlan.cn/nnet git https://github.com/yourusername/nnet.git">
    <meta name="go-source" content="go.noahlan.cn/nnet https://github.com/yourusername/nnet https://github.com/yourusername/nnet/tree/main{/dir} https://github.com/yourusername/nnet/blob/main{/dir}/{file}#L{line}">
</head>
<body>
    <a href="https://github.com/yourusername/nnet">go.noahlan.cn/nnet</a>
</body>
</html>`
        w.Header().Set("Content-Type", "text/html")
        fmt.Fprint(w, html)
    } else {
        http.Redirect(w, r, "https://github.com/yourusername/nnet", http.StatusFound)
    }
}

func main() {
    http.HandleFunc("/nnet", handler)
    http.ListenAndServe(":80", nil)
}

方式3使用 Nginx 配置

server {
    listen 80;
    server_name go.noahlan.cn;

    location /nnet {
        if ($arg_go-get = "1") {
            return 200 '<!DOCTYPE html>
<html>
<head>
    <meta name="go-import" content="go.noahlan.cn/nnet git https://github.com/yourusername/nnet.git">
    <meta name="go-source" content="go.noahlan.cn/nnet https://github.com/yourusername/nnet https://github.com/yourusername/nnet/tree/main{/dir} https://github.com/yourusername/nnet/blob/main{/dir}/{file}#L{line}">
</head>
<body>
    <a href="https://github.com/yourusername/nnet">go.noahlan.cn/nnet</a>
</body>
</html>';
            add_header Content-Type text/html;
        }
        
        return 302 https://github.com/yourusername/nnet;
    }
}

三、多仓库支持

3.1 支持多个 Git 仓库

可以配置多个 Git 仓库作为备用源:

<meta name="go-import" content="go.noahlan.cn/nnet git https://github.com/yourusername/nnet.git">
<!-- 备用源 -->
<meta name="go-import" content="go.noahlan.cn/nnet git https://gitee.com/yourusername/nnet.git">

Go 工具会按顺序尝试这些源。

3.2 动态选择仓库

可以根据用户位置或网络条件动态选择仓库:

func handler(w http.ResponseWriter, r *http.Request) {
    // 检测用户IP或User-Agent
    // 国内用户 -> Gitee
    // 国外用户 -> GitHub
    
    var gitURL string
    if isChinaIP(r.RemoteAddr) {
        gitURL = "https://gitee.com/yourusername/nnet.git"
    } else {
        gitURL = "https://github.com/yourusername/nnet.git"
    }
    
    html := fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
    <meta name="go-import" content="go.noahlan.cn/nnet git %s">
</head>
<body>
    <a href="%s">go.noahlan.cn/nnet</a>
</body>
</html>`, gitURL, gitURL)
    
    w.Header().Set("Content-Type", "text/html")
    fmt.Fprint(w, html)
}

四、Go 环境配置

4.1 配置 GOPROXY

# 设置 Go 代理(支持国内镜像)
go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct

# 或者使用官方代理
go env -w GOPROXY=https://proxy.golang.org,direct

4.2 配置 GOSUMDB

# 使用官方校验和数据库
go env -w GOSUMDB=sum.golang.org

# 或者禁用(不推荐,除非是私有仓库)
# go env -w GOSUMDB=off

4.3 验证配置

# 测试模块路径
go list -m go.noahlan.cn/nnet

# 或者直接下载
go get go.noahlan.cn/nnet

五、版本标签

5.1 创建版本标签

# 创建标签
git tag -a v1.0.0 -m "Release v1.0.0"

# 推送到所有仓库
git push origin v1.0.0
git push gitee v1.0.0
git push private v1.0.0

5.2 使用版本

# 使用特定版本
go get go.noahlan.cn/nnet@v1.0.0

# 使用最新版本
go get go.noahlan.cn/nnet@latest

# 使用最新主版本
go get go.noahlan.cn/nnet@v1

六、测试域名配置

6.1 测试 HTTP 响应

# 测试 go-get 请求
curl "https://go.noahlan.cn/nnet?go-get=1"

# 应该返回包含 go-import meta 标签的 HTML

6.2 测试 Go 模块获取

# 在新的项目中测试
mkdir test-project
cd test-project
go mod init test

# 尝试获取模块
go get go.noahlan.cn/nnet

# 检查 go.mod
cat go.mod

6.3 验证模块信息

# 查看模块信息
go list -m -versions go.noahlan.cn/nnet

# 查看模块详情
go list -m -json go.noahlan.cn/nnet

七、常见问题

7.1 Go 工具无法找到模块

问题go get go.noahlan.cn/nnet 失败

解决方案

  1. 检查 DNS 配置是否正确
  2. 检查 HTTP 服务是否返回正确的 meta 标签
  3. 检查 Git 仓库 URL 是否正确
  4. 使用 -v 参数查看详细错误:go get -v go.noahlan.cn/nnet

7.2 模块路径不匹配

问题go.mod 中的模块路径与导入路径不匹配

解决方案

  1. 确保 go.mod 中使用 go.noahlan.cn/nnet
  2. 确保代码中的导入也使用 go.noahlan.cn/nnet
  3. 重新运行 go mod tidy

7.3 私有仓库访问

如果 Git 仓库是私有的,需要配置 Git 凭据:

# 配置 Git 凭据
git config --global credential.helper store

# 或者在 URL 中包含 token
# https://username:token@github.com/yourusername/nnet.git

八、最佳实践

8.1 仓库同步

确保所有仓库GitHub、Gitee、私有库的代码和标签保持同步

# 使用提供的脚本
./scripts/push-all.sh

8.2 版本管理

  • 使用语义化版本Semantic Versioning
  • 每个版本创建对应的 Git 标签
  • 在主仓库GitHub发布 Release

8.3 文档更新

  • 在 README 中说明模块路径
  • 更新所有示例代码使用新路径
  • 在文档中说明域名配置

九、配置检查清单

  • DNS 记录已配置go.noahlan.cn 指向正确服务器)
  • HTTP 服务已配置(返回正确的 go-import meta 标签)
  • Git 仓库已创建GitHub、Gitee等
  • go.mod 文件已更新(使用 go.noahlan.cn/nnet
  • 版本标签已创建并推送
  • 测试模块获取成功
  • 文档已更新

十、参考资源


文档版本: v1.0
最后更新: 2024
域名: go.noahlan.cn