Skip to content

Commit

Permalink
fix: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 29, 2024
1 parent 0ecfb3c commit fa2aa0b
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,19 @@ func NewRoute(config config.Config, parameters map[string]any) (*Route, error) {
func (r *Route) Fallback(handler httpcontract.HandlerFunc) {
r.instance.Use(func(c fiber.Ctx) error {
ctx := contextPool.Get().(*Context)
defer func() {
contextRequestPool.Put(ctx.request)
contextResponsePool.Put(ctx.response)
ctx.request = nil
ctx.response = nil
contextPool.Put(ctx)
}()

ctx.instance = c
if response := handler(ctx); response != nil {
return response.Render()
}

contextRequestPool.Put(ctx.request)
contextResponsePool.Put(ctx.response)
ctx.request = nil
ctx.response = nil
contextPool.Put(ctx)

return nil
})
}
Expand Down Expand Up @@ -139,17 +140,10 @@ func (r *Route) Run(host ...string) error {
host = append(host, completeHost)
}

network := fiber.NetworkTCP
prefork := r.config.GetBool("http.drivers.fiber.prefork", false)
// Fiber not support prefork on dual stack
// https://docs.gofiber.io/api/fiber#config
if prefork {
network = fiber.NetworkTCP4
}

r.outputRoutes()
color.Green().Println(termlink.Link("[HTTP] Listening and serving HTTP on", "http://"+host[0]))

network, prefork := r.getNetworkConfig()
return r.instance.Listen(host[0], fiber.ListenConfig{DisableStartupMessage: true, EnablePrefork: prefork, ListenerNetwork: network})
}

Expand Down Expand Up @@ -182,17 +176,10 @@ func (r *Route) RunTLSWithCert(host, certFile, keyFile string) error {
return errors.New("certificate can't be empty")
}

network := fiber.NetworkTCP
prefork := r.config.GetBool("http.drivers.fiber.prefork", false)
// Fiber not support prefork on dual stack
// https://docs.gofiber.io/api/fiber#config
if prefork {
network = fiber.NetworkTCP4
}

r.outputRoutes()
color.Green().Println(termlink.Link("[HTTPS] Listening and serving HTTPS on", "https://"+host))

network, prefork := r.getNetworkConfig()
return r.instance.Listen(host, fiber.ListenConfig{DisableStartupMessage: true, EnablePrefork: prefork, ListenerNetwork: network, CertFile: certFile, CertKeyFile: keyFile})
}

Expand Down Expand Up @@ -239,3 +226,12 @@ func (r *Route) setMiddlewares(middlewares []fiber.Handler) {
r.instance.Use(middleware)
}
}

func (r *Route) getNetworkConfig() (string, bool) {
network := fiber.NetworkTCP
prefork := r.config.GetBool("http.drivers.fiber.prefork", false)
if prefork {
network = fiber.NetworkTCP4
}
return network, prefork
}

0 comments on commit fa2aa0b

Please sign in to comment.