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
5ddb37c1
authored
Mar 04, 2024
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add "/api/status/test"
parent
672f261c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
0 deletions
+49
-0
controller/misc.go
+16
-0
model/main.go
+32
-0
router/api-router.go
+1
-0
No files found.
controller/misc.go
View file @
5ddb37c1
...
...
@@ -11,6 +11,22 @@ import (
"github.com/gin-gonic/gin"
)
func
TestStatus
(
c
*
gin
.
Context
)
{
err
:=
model
.
PingDB
()
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusServiceUnavailable
,
gin
.
H
{
"success"
:
false
,
"message"
:
"数据库连接失败"
,
})
return
}
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
"Server is running"
,
})
return
}
func
GetStatus
(
c
*
gin
.
Context
)
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
...
...
model/main.go
View file @
5ddb37c1
...
...
@@ -5,9 +5,11 @@ import (
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
"one-api/common"
"os"
"strings"
"sync"
"time"
)
...
...
@@ -148,3 +150,33 @@ func CloseDB() error {
err
=
sqlDB
.
Close
()
return
err
}
var
(
lastPingTime
time
.
Time
pingMutex
sync
.
Mutex
)
func
PingDB
()
error
{
pingMutex
.
Lock
()
defer
pingMutex
.
Unlock
()
if
time
.
Since
(
lastPingTime
)
<
time
.
Second
*
10
{
return
nil
}
sqlDB
,
err
:=
DB
.
DB
()
if
err
!=
nil
{
log
.
Printf
(
"Error getting sql.DB from GORM: %v"
,
err
)
return
err
}
err
=
sqlDB
.
Ping
()
if
err
!=
nil
{
log
.
Printf
(
"Error pinging DB: %v"
,
err
)
return
err
}
lastPingTime
=
time
.
Now
()
common
.
SysLog
(
"Database pinged successfully"
)
return
nil
}
router/api-router.go
View file @
5ddb37c1
...
...
@@ -14,6 +14,7 @@ func SetApiRouter(router *gin.Engine) {
apiRouter
.
Use
(
middleware
.
GlobalAPIRateLimit
())
{
apiRouter
.
GET
(
"/status"
,
controller
.
GetStatus
)
apiRouter
.
GET
(
"/status/test"
,
middleware
.
AdminAuth
(),
controller
.
TestStatus
)
apiRouter
.
GET
(
"/notice"
,
controller
.
GetNotice
)
apiRouter
.
GET
(
"/about"
,
controller
.
GetAbout
)
apiRouter
.
GET
(
"/midjourney"
,
controller
.
GetMidjourney
)
...
...
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