-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
44 lines (36 loc) · 839 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
}
}