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
48a6123c
authored
Aug 23, 2025
by
Calcium-Ion
Committed by
GitHub
Aug 23, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1161 from lollipopkit/main
feat: query usage of token
parents
fe53424e
94e7f103
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
1 deletions
+63
-1
controller/token.go
+52
-0
router/api-router.go
+11
-1
No files found.
controller/token.go
View file @
48a6123c
...
@@ -5,6 +5,7 @@ import (
...
@@ -5,6 +5,7 @@ import (
"one-api/common"
"one-api/common"
"one-api/model"
"one-api/model"
"strconv"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
)
)
...
@@ -82,6 +83,57 @@ func GetTokenStatus(c *gin.Context) {
...
@@ -82,6 +83,57 @@ func GetTokenStatus(c *gin.Context) {
})
})
}
}
func
GetTokenUsage
(
c
*
gin
.
Context
)
{
authHeader
:=
c
.
GetHeader
(
"Authorization"
)
if
authHeader
==
""
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
"success"
:
false
,
"message"
:
"No Authorization header"
,
})
return
}
parts
:=
strings
.
Split
(
authHeader
,
" "
)
if
len
(
parts
)
!=
2
||
strings
.
ToLower
(
parts
[
0
])
!=
"bearer"
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
"success"
:
false
,
"message"
:
"Invalid Bearer token"
,
})
return
}
tokenKey
:=
parts
[
1
]
token
,
err
:=
model
.
GetTokenByKey
(
strings
.
TrimPrefix
(
tokenKey
,
"sk-"
),
false
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
expiredAt
:=
token
.
ExpiredTime
if
expiredAt
==
-
1
{
expiredAt
=
0
}
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"code"
:
true
,
"message"
:
"ok"
,
"data"
:
gin
.
H
{
"object"
:
"token_usage"
,
"name"
:
token
.
Name
,
"total_granted"
:
token
.
RemainQuota
+
token
.
UsedQuota
,
"total_used"
:
token
.
UsedQuota
,
"total_available"
:
token
.
RemainQuota
,
"unlimited_quota"
:
token
.
UnlimitedQuota
,
"model_limits"
:
token
.
GetModelLimitsMap
(),
"model_limits_enabled"
:
token
.
ModelLimitsEnabled
,
"expires_at"
:
expiredAt
,
},
})
}
func
AddToken
(
c
*
gin
.
Context
)
{
func
AddToken
(
c
*
gin
.
Context
)
{
token
:=
model
.
Token
{}
token
:=
model
.
Token
{}
err
:=
c
.
ShouldBindJSON
(
&
token
)
err
:=
c
.
ShouldBindJSON
(
&
token
)
...
...
router/api-router.go
View file @
48a6123c
...
@@ -145,6 +145,17 @@ func SetApiRouter(router *gin.Engine) {
...
@@ -145,6 +145,17 @@ func SetApiRouter(router *gin.Engine) {
tokenRoute
.
DELETE
(
"/:id"
,
controller
.
DeleteToken
)
tokenRoute
.
DELETE
(
"/:id"
,
controller
.
DeleteToken
)
tokenRoute
.
POST
(
"/batch"
,
controller
.
DeleteTokenBatch
)
tokenRoute
.
POST
(
"/batch"
,
controller
.
DeleteTokenBatch
)
}
}
usageRoute
:=
apiRouter
.
Group
(
"/usage"
)
usageRoute
.
Use
(
middleware
.
CriticalRateLimit
())
{
tokenUsageRoute
:=
usageRoute
.
Group
(
"/token"
)
tokenUsageRoute
.
Use
(
middleware
.
TokenAuth
())
{
tokenUsageRoute
.
GET
(
"/"
,
controller
.
GetTokenUsage
)
}
}
redemptionRoute
:=
apiRouter
.
Group
(
"/redemption"
)
redemptionRoute
:=
apiRouter
.
Group
(
"/redemption"
)
redemptionRoute
.
Use
(
middleware
.
AdminAuth
())
redemptionRoute
.
Use
(
middleware
.
AdminAuth
())
{
{
...
@@ -172,7 +183,6 @@ func SetApiRouter(router *gin.Engine) {
...
@@ -172,7 +183,6 @@ func SetApiRouter(router *gin.Engine) {
logRoute
.
Use
(
middleware
.
CORS
())
logRoute
.
Use
(
middleware
.
CORS
())
{
{
logRoute
.
GET
(
"/token"
,
controller
.
GetLogByKey
)
logRoute
.
GET
(
"/token"
,
controller
.
GetLogByKey
)
}
}
groupRoute
:=
apiRouter
.
Group
(
"/group"
)
groupRoute
:=
apiRouter
.
Group
(
"/group"
)
groupRoute
.
Use
(
middleware
.
AdminAuth
())
groupRoute
.
Use
(
middleware
.
AdminAuth
())
...
...
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