Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
6e002a5f
authored
Dec 30, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: enhance environment variable handling and security features
parent
aefd53b6
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
16 additions
and
3 deletions
+16
-3
README.md
+5
-0
common/constants.go
+1
-0
common/crypto.go
+1
-1
common/init.go
+6
-1
main.go
+3
-1
No files found.
README.md
View file @
6e002a5f
...
@@ -89,6 +89,7 @@
...
@@ -89,6 +89,7 @@
-
`COHERE_SAFETY_SETTING`
:Cohere模型
[
安全设置
](
https://docs.cohere.com/docs/safety-modes#overview
)
,可选值为
`NONE`
,
`CONTEXTUAL`
,
`STRICT`
,默认为
`NONE`
。
-
`COHERE_SAFETY_SETTING`
:Cohere模型
[
安全设置
](
https://docs.cohere.com/docs/safety-modes#overview
)
,可选值为
`NONE`
,
`CONTEXTUAL`
,
`STRICT`
,默认为
`NONE`
。
-
`GEMINI_VISION_MAX_IMAGE_NUM`
:Gemini模型最大图片数量,默认为
`16`
,设置为
`-1`
则不限制。
-
`GEMINI_VISION_MAX_IMAGE_NUM`
:Gemini模型最大图片数量,默认为
`16`
,设置为
`-1`
则不限制。
-
`MAX_FILE_DOWNLOAD_MB`
: 最大文件下载大小,单位 MB,默认为
`20`
。
-
`MAX_FILE_DOWNLOAD_MB`
: 最大文件下载大小,单位 MB,默认为
`20`
。
-
`CRYPTO_SECRET`
:加密密钥,用于加密数据库内容。
## 部署
## 部署
> [!TIP]
> [!TIP]
> 最新版Docker镜像:`calciumion/new-api:latest`
> 最新版Docker镜像:`calciumion/new-api:latest`
...
@@ -98,6 +99,10 @@
...
@@ -98,6 +99,10 @@
> docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower -cR
> docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower -cR
> ```
> ```
### 多机部署
-
必须设置环境变量
`SESSION_SECRET`
,否则会导致多机部署时登录状态不一致。
-
如果公用Redis,必须设置
`CRYPTO_SECRET`
,否则会导致多机部署时Redis内容无法解密。
### 部署要求
### 部署要求
-
本地数据库(默认):SQLite(Docker 部署默认使用 SQLite,必须挂载
`/data`
目录到宿主机)
-
本地数据库(默认):SQLite(Docker 部署默认使用 SQLite,必须挂载
`/data`
目录到宿主机)
-
远程数据库:MySQL 版本 >= 5.7.8,PgSQL 版本 >= 9.6
-
远程数据库:MySQL 版本 >= 5.7.8,PgSQL 版本 >= 9.6
...
...
common/constants.go
View file @
6e002a5f
...
@@ -30,6 +30,7 @@ var DefaultCollapseSidebar = false // default value of collapse sidebar
...
@@ -30,6 +30,7 @@ var DefaultCollapseSidebar = false // default value of collapse sidebar
// Any options with "Secret", "Token" in its key won't be return by GetOptions
// Any options with "Secret", "Token" in its key won't be return by GetOptions
var
SessionSecret
=
uuid
.
New
()
.
String
()
var
SessionSecret
=
uuid
.
New
()
.
String
()
var
CryptoSecret
=
uuid
.
New
()
.
String
()
var
OptionMap
map
[
string
]
string
var
OptionMap
map
[
string
]
string
var
OptionMapRWMutex
sync
.
RWMutex
var
OptionMapRWMutex
sync
.
RWMutex
...
...
common/crypto.go
View file @
6e002a5f
...
@@ -14,7 +14,7 @@ func GenerateHMACWithKey(key []byte, data string) string {
...
@@ -14,7 +14,7 @@ func GenerateHMACWithKey(key []byte, data string) string {
}
}
func
GenerateHMAC
(
data
string
)
string
{
func
GenerateHMAC
(
data
string
)
string
{
h
:=
hmac
.
New
(
sha256
.
New
,
[]
byte
(
Session
Secret
))
h
:=
hmac
.
New
(
sha256
.
New
,
[]
byte
(
Crypto
Secret
))
h
.
Write
([]
byte
(
data
))
h
.
Write
([]
byte
(
data
))
return
hex
.
EncodeToString
(
h
.
Sum
(
nil
))
return
hex
.
EncodeToString
(
h
.
Sum
(
nil
))
}
}
...
...
common/init.go
View file @
6e002a5f
...
@@ -22,7 +22,7 @@ func printHelp() {
...
@@ -22,7 +22,7 @@ func printHelp() {
fmt
.
Println
(
"Usage: one-api [--port <port>] [--log-dir <log directory>] [--version] [--help]"
)
fmt
.
Println
(
"Usage: one-api [--port <port>] [--log-dir <log directory>] [--version] [--help]"
)
}
}
func
init
()
{
func
LoadEnv
()
{
flag
.
Parse
()
flag
.
Parse
()
if
*
PrintVersion
{
if
*
PrintVersion
{
...
@@ -45,6 +45,11 @@ func init() {
...
@@ -45,6 +45,11 @@ func init() {
SessionSecret
=
ss
SessionSecret
=
ss
}
}
}
}
if
os
.
Getenv
(
"CRYPTO_SECRET"
)
!=
""
{
CryptoSecret
=
os
.
Getenv
(
"CRYPTO_SECRET"
)
}
else
{
CryptoSecret
=
SessionSecret
}
if
os
.
Getenv
(
"SQLITE_PATH"
)
!=
""
{
if
os
.
Getenv
(
"SQLITE_PATH"
)
!=
""
{
SQLitePath
=
os
.
Getenv
(
"SQLITE_PATH"
)
SQLitePath
=
os
.
Getenv
(
"SQLITE_PATH"
)
}
}
...
...
main.go
View file @
6e002a5f
...
@@ -33,9 +33,11 @@ var indexPage []byte
...
@@ -33,9 +33,11 @@ var indexPage []byte
func
main
()
{
func
main
()
{
err
:=
godotenv
.
Load
(
".env"
)
err
:=
godotenv
.
Load
(
".env"
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
Sys
Error
(
"failed to load .env file: "
+
err
.
Error
()
)
common
.
Sys
Log
(
"Support for .env file is disabled"
)
}
}
common
.
LoadEnv
()
common
.
SetupLogger
()
common
.
SetupLogger
()
common
.
SysLog
(
"New API "
+
common
.
Version
+
" started"
)
common
.
SysLog
(
"New API "
+
common
.
Version
+
" started"
)
if
os
.
Getenv
(
"GIN_MODE"
)
!=
"debug"
{
if
os
.
Getenv
(
"GIN_MODE"
)
!=
"debug"
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment