-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added graceful shutdown of the server
- Loading branch information
1 parent
663d786
commit 96f2a59
Showing
5 changed files
with
105 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package goravel | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
// ListenAndServe starts the web server | ||
func (g *Goravel) ListenAndServe() error { | ||
port := os.Getenv("PORT") | ||
srv := &http.Server{ | ||
Addr: ":" + port, | ||
Handler: g.Routes, | ||
ErrorLog: g.ErrorLog, | ||
IdleTimeout: time.Second * 30, | ||
ReadTimeout: time.Second * 30, | ||
WriteTimeout: time.Second * 600, | ||
} | ||
|
||
if g.DB.Pool != nil { | ||
defer g.DB.Pool.Close() // close the database connection when the server stops | ||
} | ||
|
||
if redisPool != nil { | ||
defer redisPool.Close() // close the redis connection when the server stops | ||
} | ||
|
||
if badgerConn != nil { | ||
defer badgerConn.Close() | ||
} | ||
|
||
color.Yellow(Banner, Version) | ||
color.Green("Starting server on port %s", port) | ||
err := srv.ListenAndServe() | ||
|
||
if err != nil { | ||
return err | ||
} else { | ||
return nil | ||
} | ||
} |