将 web 框架从 gin 切换到 fiber

This commit is contained in:
2025-05-15 10:04:28 +08:00
parent 8b7dc9e4ff
commit b29882f0a7
8 changed files with 46 additions and 24 deletions

View File

@@ -2,13 +2,12 @@ package handlers
import (
"fmt"
"github.com/gofiber/fiber/v2"
"proxy-server/server/debug"
"slices"
"github.com/gin-gonic/gin"
)
func GetConsuming(c *gin.Context) {
func GetConsuming(c *fiber.Ctx) error {
list := debug.ConsumingList()
// sort by total time
slices.SortFunc(list, func(a debug.Consuming, b debug.Consuming) int {
@@ -25,12 +24,12 @@ func GetConsuming(c *gin.Context) {
times := list[i]
strList[i] = fmt.Sprintf("Auth: %s, Data: %s, Proxy: %s, Total: %s", times.Auth, times.Data, times.Proxy, times.Total)
}
c.JSON(200, strList)
return c.JSON(strList)
}
func RestConsuming(c *gin.Context) {
func RestConsuming(c *fiber.Ctx) error {
debug.InitConsumingList()
c.JSON(200, gin.H{
return c.JSON(fiber.Map{
"message": "success",
})
}

View File

@@ -1,12 +1,12 @@
package web
import (
"github.com/gofiber/fiber/v2"
"proxy-server/server/web/handlers"
"github.com/gin-gonic/gin"
)
func Router(r *gin.Engine) {
r.Handle("GET", "/debug/consuming/list", handlers.GetConsuming)
r.Handle("GET", "/debug/consuming/reset", handlers.RestConsuming)
func Router(r *fiber.App) {
var debug = r.Group("/debug")
debug.Get("/debug/consuming/list", handlers.GetConsuming)
debug.Get("/debug/consuming/reset", handlers.RestConsuming)
}

View File

@@ -1,16 +1,14 @@
package web
import (
"context"
"net/http"
"proxy-server/server/pkg/env"
"strconv"
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
)
type Server struct {
http *http.Server
web *fiber.App
}
func New() *Server {
@@ -18,15 +16,14 @@ func New() *Server {
}
func (s *Server) Run() error {
address := ":" + strconv.Itoa(int(env.AppWebPort))
engine := gin.Default()
s.http = &http.Server{Addr: address, Handler: engine}
s.web = fiber.New()
// 配置中间件和路由
Router(engine)
Router(s.web)
// 启动服务
err := s.http.ListenAndServe()
address := ":" + strconv.Itoa(int(env.AppWebPort))
err := s.web.Listen(address)
if err != nil {
return err
}
@@ -35,7 +32,7 @@ func (s *Server) Run() error {
}
func (s *Server) Stop() error {
err := s.http.Shutdown(context.Background())
err := s.web.Shutdown()
if err != nil {
return err
}