init
This commit is contained in:
26
backend/pkg/utils/buffer.go
Normal file
26
backend/pkg/utils/buffer.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
)
|
||||
|
||||
// NewLogBuffer creates a buffer that can be used to capture output stream
|
||||
// and write to a logger in real time
|
||||
func NewLogBuffer(output func(string)) io.Writer {
|
||||
reader, writer := io.Pipe()
|
||||
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
output(scanner.Text())
|
||||
}
|
||||
}()
|
||||
|
||||
return writer
|
||||
}
|
||||
|
||||
// NewCombinedBuffer combines multiple io.Writers
|
||||
func NewCombinedBuffer(writers ...io.Writer) io.Writer {
|
||||
return io.MultiWriter(writers...)
|
||||
}
|
||||
44
backend/pkg/utils/build_info.go
Normal file
44
backend/pkg/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