Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Shutdown to support graceful quit of HTTP Server #83

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/gin-gonic/gin v1.10.0
github.com/gookit/validate v1.5.2
github.com/goravel/framework v1.14.3
github.com/goravel/framework v1.14.5-0.20240904064435-05476fa00c9c
github.com/rs/cors v1.11.0
github.com/savioxavier/termlink v1.3.0
github.com/spf13/cast v1.6.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7a
github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I=
github.com/goravel/framework v1.14.3 h1:H2sKrmsxCvVGRBbeewjXXLWsCAvk1o5kVskNymDfpfw=
github.com/goravel/framework v1.14.3/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
github.com/goravel/framework v1.14.5-0.20240904064435-05476fa00c9c h1:RiMpkbasXxmNmE/pLCy6lqmu3uOadyhICiqMtgroA7I=
github.com/goravel/framework v1.14.5-0.20240904064435-05476fa00c9c/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
Expand Down
29 changes: 24 additions & 5 deletions route.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gin

import (
"context"
"errors"
"fmt"
"net/http"
Expand All @@ -17,8 +18,10 @@ import (

type Route struct {
route.Router
config config.Config
instance *gin.Engine
config config.Config
instance *gin.Engine
server *http.Server
tlsServer *http.Server
}

func NewRoute(config config.Config, parameters map[string]any) (*Route, error) {
Expand Down Expand Up @@ -98,13 +101,13 @@ func (r *Route) Run(host ...string) error {
r.outputRoutes()
color.Green().Println(termlink.Link("[HTTP] Listening and serving HTTP on", "http://"+host[0]))

server := &http.Server{
r.server = &http.Server{
Addr: host[0],
Handler: http.AllowQuerySemicolons(r.instance),
MaxHeaderBytes: r.config.GetInt("http.drivers.gin.header_limit", 4096) << 10,
}

return server.ListenAndServe()
return r.server.ListenAndServe()
}

func (r *Route) RunTLS(host ...string) error {
Expand Down Expand Up @@ -135,13 +138,29 @@ func (r *Route) RunTLSWithCert(host, certFile, keyFile string) error {
r.outputRoutes()
color.Green().Println(termlink.Link("[HTTPS] Listening and serving HTTPS on", "https://"+host))

return r.instance.RunTLS(host, certFile, keyFile)
r.tlsServer = &http.Server{
Addr: host,
Handler: http.AllowQuerySemicolons(r.instance),
MaxHeaderBytes: r.config.GetInt("http.drivers.gin.header_limit", 4096) << 10,
}

return r.tlsServer.ListenAndServeTLS(certFile, keyFile)
}

func (r *Route) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
r.instance.ServeHTTP(writer, request)
}

func (r *Route) Shutdown(ctx context.Context) error {
if r.server != nil {
return r.server.Shutdown(ctx)
}
if r.tlsServer != nil {
return r.tlsServer.Shutdown(ctx)
}
return nil
}

func (r *Route) outputRoutes() {
if r.config.GetBool("app.debug") && support.Env != support.EnvArtisan {
for _, item := range r.instance.Routes() {
Expand Down
Loading
Loading