mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 20:50:06 +00:00
dev: add ddns create, edit and batch delete api (#444)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -55,15 +56,20 @@ func routers(r *gin.Engine) {
|
||||
api := r.Group("api/v1")
|
||||
api.POST("/login", authMiddleware.LoginHandler)
|
||||
|
||||
unrequiredAuth := api.Group("", unrquiredAuthMiddleware(authMiddleware))
|
||||
unrequiredAuth.GET("/ws/server", serverStream)
|
||||
unrequiredAuth.GET("/server-group", listServerGroup)
|
||||
optionalAuth := api.Group("", optionalAuthMiddleware(authMiddleware))
|
||||
optionalAuth.GET("/ws/server", commonHandler[any](serverStream))
|
||||
optionalAuth.GET("/server-group", commonHandler[[]model.ServerGroup](listServerGroup))
|
||||
optionalAuth.GET("/ddns", listDDNS) // TODO
|
||||
|
||||
auth := api.Group("", authMiddleware.MiddlewareFunc())
|
||||
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
|
||||
auth.PATCH("/server/:id", editServer)
|
||||
auth.PATCH("/server/:id", commonHandler[any](editServer))
|
||||
|
||||
api.DELETE("/batch-delete/server", batchDeleteServer)
|
||||
auth.POST("/ddns", commonHandler[any](newDDNS))
|
||||
auth.PATCH("/ddns/:id", commonHandler[any](editDDNS))
|
||||
|
||||
api.POST("/batch-delete/server", commonHandler[any](batchDeleteServer))
|
||||
api.POST("/batch-delete/ddns", commonHandler[any](batchDeleteDDNS))
|
||||
|
||||
// 通用页面
|
||||
// cp := commonPage{r: r}
|
||||
@@ -147,3 +153,45 @@ func recordPath(c *gin.Context) {
|
||||
}
|
||||
c.Set("MatchedPath", url)
|
||||
}
|
||||
|
||||
func newErrorResponse[T any](err error) model.CommonResponse[T] {
|
||||
return model.CommonResponse[T]{
|
||||
Success: false,
|
||||
Error: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
type handlerFunc func(c *gin.Context) error
|
||||
|
||||
// There are many error types in gorm, so create a custom type to represent all
|
||||
// gorm errors here instead
|
||||
type gormError struct {
|
||||
msg string
|
||||
a []interface{}
|
||||
}
|
||||
|
||||
func newGormError(format string, args ...interface{}) error {
|
||||
return &gormError{
|
||||
msg: format,
|
||||
a: args,
|
||||
}
|
||||
}
|
||||
|
||||
func (ge *gormError) Error() string {
|
||||
return fmt.Sprintf(ge.msg, ge.a...)
|
||||
}
|
||||
|
||||
func commonHandler[T any](handler handlerFunc) func(*gin.Context) {
|
||||
return func(c *gin.Context) {
|
||||
if err := handler(c); err != nil {
|
||||
if _, ok := err.(*gormError); ok {
|
||||
log.Printf("NEZHA>> gorm error: %v", err)
|
||||
c.JSON(http.StatusOK, newErrorResponse[T](errors.New("database error")))
|
||||
return
|
||||
} else {
|
||||
c.JSON(http.StatusOK, newErrorResponse[T](err))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user