feat: 更新构建配置,添加构建信息打印功能并重构 Makefile
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
name: Build TGExporter
|
||||
run-name: ${{ gitea.actor }} Build TGExporter
|
||||
name: Build Application
|
||||
run-name: ${{ gitea.actor }} Build Application
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
|
||||
25
templates/project/Makefile.raw → templates/project/Makefile.tpl
Executable file → Normal file
25
templates/project/Makefile.raw → templates/project/Makefile.tpl
Executable file → Normal file
@@ -1,7 +1,8 @@
|
||||
buildAt=`date +%Y/%m/%d-%H:%M:%S`
|
||||
gitHash=`git rev-parse HEAD`
|
||||
version=`git rev-parse --abbrev-ref HEAD | grep -v HEAD || git describe --exact-match HEAD || git rev-parse HEAD` ## todo: use current release git tag
|
||||
flags="-X 'atom/utils.Version=${version}' -X 'atom/utils.BuildAt=${buildAt}' -X 'atom/utils.GitHash=${gitHash}'"
|
||||
version=`git rev-parse --abbrev-ref HEAD | grep -v HEAD || git describe --exact-match HEAD || git rev-parse HEAD`
|
||||
# 修改为项目特定的变量路径
|
||||
flags="-X '{{.ModuleName}}/pkg/utils.Version=${version}' -X '{{.ModuleName}}/pkg/utils.BuildAt=${buildAt}' -X '{{.ModuleName}}/pkg/utils.GitHash=${gitHash}'"
|
||||
release_flags="-w -s ${flags}"
|
||||
|
||||
GOPATH:=$(shell go env GOPATH)
|
||||
@@ -15,9 +16,25 @@ release:
|
||||
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags=${flags} -o bin/release/{{.ProjectName}} .
|
||||
@cp config.toml bin/release/
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
@go build -ldflags=${flags} -o bin/{{.ProjectName}} .
|
||||
|
||||
.PHONY: run
|
||||
run: build
|
||||
@./bin/{{.ProjectName}}
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
@go test -v ./... -cover
|
||||
@go test -v ./tests/... -cover
|
||||
|
||||
.PHONY: info
|
||||
info:
|
||||
@echo "Build Information:"
|
||||
@echo "=================="
|
||||
@echo "Build Time: $(buildAt)"
|
||||
@echo "Git Hash: $(gitHash)"
|
||||
@echo "Version: $(version)"
|
||||
|
||||
.PHONY: lint
|
||||
lint:
|
||||
@@ -44,4 +61,4 @@ init: tools
|
||||
@buf generate
|
||||
@go mod tidy
|
||||
@go get -u
|
||||
@go mod tidy
|
||||
@go mod tidy
|
||||
99
templates/project/config.toml.tpl
Normal file
99
templates/project/config.toml.tpl
Normal file
@@ -0,0 +1,99 @@
|
||||
# =========================
|
||||
# 应用基础配置
|
||||
# =========================
|
||||
[App]
|
||||
# 应用运行模式:development | production | testing
|
||||
Mode = "development"
|
||||
# 应用基础URI,用于生成完整URL
|
||||
BaseURI = "http://localhost:8080"
|
||||
|
||||
# =========================
|
||||
# HTTP 服务器配置
|
||||
# =========================
|
||||
[Http]
|
||||
# HTTP服务监听端口
|
||||
Port = 8080
|
||||
# 监听地址(可选,默认 0.0.0.0)
|
||||
# Host = "0.0.0.0"
|
||||
# 全局路由前缀(可选)
|
||||
# BaseURI = "/api/v1"
|
||||
|
||||
# =========================
|
||||
# 数据库配置
|
||||
# =========================
|
||||
[Database]
|
||||
# 数据库主机地址
|
||||
Host = "localhost"
|
||||
# 数据库端口
|
||||
Port = 5432
|
||||
# 数据库名称
|
||||
Database = "{{.ProjectName}}"
|
||||
# 数据库用户名
|
||||
Username = "postgres"
|
||||
# 数据库密码
|
||||
Password = "password"
|
||||
# SSL模式:disable | require | verify-ca | verify-full
|
||||
SslMode = "disable"
|
||||
# 时区
|
||||
TimeZone = "Asia/Shanghai"
|
||||
# 连接池配置(可选)
|
||||
MaxIdleConns = 10
|
||||
MaxOpenConns = 100
|
||||
ConnMaxLifetime = "1800s"
|
||||
ConnMaxIdleTime = "300s"
|
||||
|
||||
# =========================
|
||||
# JWT 认证配置
|
||||
# =========================
|
||||
[JWT]
|
||||
# JWT签名密钥(生产环境请使用强密钥)
|
||||
SigningKey = "your-secret-key-change-in-production"
|
||||
# Token过期时间,如:72h, 168h, 720h
|
||||
ExpiresTime = "168h"
|
||||
# 签发者(可选)
|
||||
Issuer = "{{.ProjectName}}"
|
||||
|
||||
# =========================
|
||||
# HashIDs 配置
|
||||
# =========================
|
||||
[HashIDs]
|
||||
# 盐值(用于ID加密,请使用随机字符串)
|
||||
Salt = "your-random-salt-here"
|
||||
# 最小长度(可选)
|
||||
MinLength = 8
|
||||
# 自定义字符集(可选)
|
||||
# Alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
||||
|
||||
# =========================
|
||||
# Redis 缓存配置
|
||||
# =========================
|
||||
[Redis]
|
||||
# Redis主机地址
|
||||
Host = "localhost"
|
||||
# Redis端口
|
||||
Port = 6379
|
||||
# Redis密码(可选)
|
||||
Password = ""
|
||||
# 数据库编号
|
||||
DB = 0
|
||||
# 连接池配置(可选)
|
||||
PoolSize = 50
|
||||
MinIdleConns = 10
|
||||
MaxRetries = 3
|
||||
# 超时配置(可选)
|
||||
DialTimeout = "5s"
|
||||
ReadTimeout = "3s"
|
||||
WriteTimeout = "3s"
|
||||
|
||||
# =========================
|
||||
# 日志配置
|
||||
# =========================
|
||||
[Log]
|
||||
# 日志级别:debug | info | warn | error
|
||||
Level = "info"
|
||||
# 日志格式:json | text
|
||||
Format = "json"
|
||||
# 输出文件(可选,未配置则输出到控制台)
|
||||
# Output = "./logs/app.log"
|
||||
# 是否启用调用者信息(文件名:行号)
|
||||
EnableCaller = true
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"{{.ModuleName}}/app/commands/http"
|
||||
"{{.ModuleName}}/app/commands/migrate"
|
||||
"{{.ModuleName}}/pkg/utils"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.ipao.vip/atom"
|
||||
@@ -22,7 +23,11 @@ import (
|
||||
// @securityDefinitions.basic BasicAuth
|
||||
// @externalDocs.description OpenAPI
|
||||
// @externalDocs.url https://swagger.io/resources/open-api/
|
||||
|
||||
func main() {
|
||||
// 打印构建信息
|
||||
utils.PrintBuildInfo("{{.ProjectName}}")
|
||||
|
||||
opts := []atom.Option{
|
||||
atom.Name("{{ .ProjectName }}"),
|
||||
http.Command(),
|
||||
|
||||
44
templates/project/pkg/utils/build_info.go.tpl
Normal file
44
templates/project/pkg/utils/build_info.go.tpl
Normal file
@@ -0,0 +1,44 @@
|
||||
package utils
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 构建信息变量,通过 ldflags 在构建时注入
|
||||
var (
|
||||
// Version 应用版本信息
|
||||
Version string
|
||||
|
||||
// BuildAt 构建时间
|
||||
BuildAt string
|
||||
|
||||
// GitHash Git 提交哈希
|
||||
GitHash string
|
||||
)
|
||||
|
||||
// GetBuildInfo 获取构建信息
|
||||
func GetBuildInfo() map[string]string {
|
||||
return map[string]string{
|
||||
"version": Version,
|
||||
"buildAt": BuildAt,
|
||||
"gitHash": GitHash,
|
||||
}
|
||||
}
|
||||
|
||||
// PrintBuildInfo 打印构建信息
|
||||
func PrintBuildInfo(appName string) {
|
||||
buildInfo := GetBuildInfo()
|
||||
|
||||
println("========================================")
|
||||
printf("🚀 %s\n", appName)
|
||||
println("========================================")
|
||||
printf("📋 Version: %s\n", buildInfo["version"])
|
||||
printf("🕐 Build Time: %s\n", buildInfo["buildAt"])
|
||||
printf("🔗 Git Hash: %s\n", buildInfo["gitHash"])
|
||||
println("========================================")
|
||||
println("🌟 Application is starting...")
|
||||
println()
|
||||
}
|
||||
|
||||
// 为了避免导入 fmt 包,我们使用内置的 print 和 printf 函数
|
||||
func printf(format string, args ...interface{}) {
|
||||
print(fmt.Sprintf(format, args...))
|
||||
}
|
||||
44
templates/project/utils/build_info.go
Normal file
44
templates/project/utils/build_info.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package utils
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 构建信息变量,通过 ldflags 在构建时注入
|
||||
var (
|
||||
// Version 应用版本信息
|
||||
Version string
|
||||
|
||||
// BuildAt 构建时间
|
||||
BuildAt string
|
||||
|
||||
// GitHash Git 提交哈希
|
||||
GitHash string
|
||||
)
|
||||
|
||||
// GetBuildInfo 获取构建信息
|
||||
func GetBuildInfo() map[string]string {
|
||||
return map[string]string{
|
||||
"version": Version,
|
||||
"buildAt": BuildAt,
|
||||
"gitHash": GitHash,
|
||||
}
|
||||
}
|
||||
|
||||
// PrintBuildInfo 打印构建信息
|
||||
func PrintBuildInfo(appName string) {
|
||||
buildInfo := GetBuildInfo()
|
||||
|
||||
println("========================================")
|
||||
printf("🚀 %s\n", appName)
|
||||
println("========================================")
|
||||
printf("📋 Version: %s\n", buildInfo["version"])
|
||||
printf("🕐 Build Time: %s\n", buildInfo["buildAt"])
|
||||
printf("🔗 Git Hash: %s\n", buildInfo["gitHash"])
|
||||
println("========================================")
|
||||
println("🌟 Application is starting...")
|
||||
println()
|
||||
}
|
||||
|
||||
// 为了避免导入 fmt 包,我们使用内置的 print 和 printf 函数
|
||||
func printf(format string, args ...interface{}) {
|
||||
print(fmt.Sprintf(format, args...))
|
||||
}
|
||||
Reference in New Issue
Block a user