-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (49 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-errors/errors"
"WireguardAPI/router"
"WireguardAPI/middlewares/token"
"WireguardAPI/utils/config"
"WireguardAPI/utils/errutil"
)
func main() {
if !config.Debug {
gin.SetMode(gin.ReleaseMode)
}
backend := gin.Default()
backend.Use(errorHandler)
backend.Use(gin.CustomRecovery(panicHandler))
backend.Use(token.AddMeta)
router.Init(&backend.RouterGroup)
backend.Run(fmt.Sprintf("%s:%s", config.Address, config.Port))
}
func panicHandler(c *gin.Context, err any) {
goErr := errors.Wrap(err, 2)
errutil.AbortAndError(c, &errutil.Err{
Code: 500,
Msg: "Internal server error",
Data: goErr.Error(),
})
}
func errorHandler(c *gin.Context) {
c.Next()
for _, e := range c.Errors {
err := e.Err
if myErr, ok := err.(*errutil.Err); ok {
if myErr.Msg != nil {
c.JSON(myErr.Code, myErr.ToH())
} else {
c.Status(myErr.Code)
}
} else {
c.JSON(500, gin.H{
"code": 500,
"msg": "Internal server error",
//"data": err.Error(),
})
}
return
}
}