migrate to fiber

This commit is contained in:
Zoe
2024-10-01 03:45:43 -05:00
parent e39e5f51fd
commit e64b9fba7f
17 changed files with 587 additions and 372 deletions

View File

@@ -4,24 +4,53 @@
package main
import (
"net/url"
"strings"
"github.com/labstack/echo/v4"
echoMiddleware "github.com/labstack/echo/v4/middleware"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/proxy"
fastProxy "github.com/yeqown/fasthttp-reverse-proxy/v2"
)
func init() {
initUi = func(e *echo.Echo) {
spawnProcess("bun", []string{"--cwd=ui", "run", "dev"}, e)
initUi = func(app *fiber.App) {
if !fiber.IsChild() {
spawnProcess("bun", []string{"--cwd=ui", "run", "dev"}, app)
}
target := "localhost:3000"
e.Group("/*").Use(echoMiddleware.ProxyWithConfig(echoMiddleware.ProxyConfig{
Balancer: echoMiddleware.NewRoundRobinBalancer([]*echoMiddleware.ProxyTarget{
{URL: &url.URL{
Scheme: "http",
Host: target,
}},
}),
}))
app.All("/*", func(c fiber.Ctx) error {
path := c.Path()
if strings.HasPrefix(path, "/api") {
return c.Next()
}
request := c.Request().URI()
if string(request.RequestURI()) == "/_nuxt/" {
return proxyWebSocket(c, target)
}
return proxy.Do(c, "http://"+target+string(request.RequestURI()))
})
}
}
var proxyServer *fastProxy.ReverseProxy
func proxyWebSocket(c fiber.Ctx, target string) error {
path := c.Path()
// proxyServer, err := fastProxy.NewWSReverseProxyWith(
// fastProxy.WithURL_OptionWS("ws://localhost:3000"+path),
// fastProxy.WithDynamicPath_OptionWS(true, fastProxy.DefaultOverrideHeader),
// )
if proxyServer == nil {
proxyServer, err = fastProxy.NewWSReverseProxyWith(
fastProxy.WithURL_OptionWS("ws://localhost:3000"+path),
fastProxy.WithDynamicPath_OptionWS(true, fastProxy.DefaultOverrideHeader),
)
if err != nil {
panic(err)
}
}
proxyServer.ServeHTTP(c.Context())
return nil
}