feat: 添加.env配置文件和初始化环境变量

This commit is contained in:
lianghaoyuan 2024-09-24 11:39:02 +08:00
parent 50eab6b4e4
commit 84f40b63b2
5 changed files with 101 additions and 6 deletions

84
.env.example Normal file
View File

@ -0,0 +1,84 @@
# 端口号
PORT=3000
# 前端基础URL
FRONTEND_BASE_URL=https://your-frontend-url.com
# 调试相关配置
# 调试模式
DEBUG=true
# 启用pprof
ENABLE_PPROF=true
# 内存缓存启用
MEMORY_CACHE_ENABLED=true
# 数据库相关配置
# 数据库连接字符串
SQL_DSN=mysql://user:password@tcp(127.0.0.1:3306)/dbname?parseTime=true
# 日志数据库连接字符串
LOG_SQL_DSN=mysql://user:password@tcp(127.0.0.1:3306)/logdb?parseTime=true
# SQLite数据库路径
SQLITE_PATH=/path/to/sqlite.db
# 数据库最大空闲连接数
SQL_MAX_IDLE_CONNS=100
# 数据库最大打开连接数
SQL_MAX_OPEN_CONNS=1000
# 数据库连接最大生命周期(秒)
SQL_MAX_LIFETIME=60
# Redis相关配置
# Redis连接字符串
REDIS_CONN_STRING=redis://user:password@localhost:6379/0
# 同步频率(单位:秒)
SYNC_FREQUENCY=60
# 会话相关配置
# 会话秘密
SESSION_SECRET=random_string
# 任务和功能配置
# 批量更新启用
BATCH_UPDATE_ENABLED=true
# 批量更新间隔(单位:秒)
BATCH_UPDATE_INTERVAL=5
# 更新任务启用
UPDATE_TASK=true
# 禁用通道阈值
CHANNEL_DISABLE_THRESHOLD=5.0
# 其他配置
# 通道更新频率(单位:秒)
CHANNEL_UPDATE_FREQUENCY=30
# 通道测试频率(单位:秒)
CHANNEL_TEST_FREQUENCY=10
# 生成默认令牌
GENERATE_DEFAULT_TOKEN=false
# 气候模式设置
GEMINI_SAFETY_SETTING=BLOCK_NONE
# 文本生成安全设置
COHERE_SAFETY_SETTING=NONE
# 节点类型
# 如果是主节点则为true
NODE_TYPE=master
# SMTP配置可选
# SMTP服务器地址
SMTP_SERVER=smtp.example.com
# SMTP端口
SMTP_PORT=587
# 是否启用SMTP SSL
SMTP_SSL_ENABLED=false
# SMTP账户
SMTP_ACCOUNT=user@example.com
# SMTP发件人地址
SMTP_FROM=admin@example.com
# SMTP令牌
SMTP_TOKEN=your-smtp-token

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ build
*.db-journal
logs
web/dist
.env

1
go.mod
View File

@ -63,6 +63,7 @@ require (
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.4.0 // indirect

2
go.sum
View File

@ -111,6 +111,8 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=

17
main.go
View File

@ -3,10 +3,6 @@ package main
import (
"embed"
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"log"
"net/http"
"one-api/common"
@ -19,6 +15,12 @@ import (
"os"
"strconv"
"github.com/bytedance/gopkg/util/gopool"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
_ "net/http/pprof"
)
@ -29,6 +31,11 @@ var buildFS embed.FS
var indexPage []byte
func main() {
err := godotenv.Load(".env")
if err != nil {
common.SysLog("Can't load .env file")
}
common.SetupLogger()
common.SysLog("New API " + common.Version + " started")
if os.Getenv("GIN_MODE") != "debug" {
@ -38,7 +45,7 @@ func main() {
common.SysLog("running in debug mode")
}
// Initialize SQL Database
err := model.InitDB()
err = model.InitDB()
if err != nil {
common.FatalLog("failed to initialize database: " + err.Error())
}