/
apiserver.go
59 lines (47 loc) 路 1.14 KB
/
apiserver.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package apiserver
import (
"time"
"github.com/gofiber/fiber"
"go.uber.org/zap"
)
// APIServer struct
type APIServer struct {
config *Config
logger *zap.Logger
}
// New method for init new server instance
func New(config *Config) *APIServer {
return &APIServer{
config: config,
logger: Logger(config),
}
}
// Start method for start new server
func (s *APIServer) Start() error {
// Init new app
app := fiber.New()
// App config
host := s.config.Server.Host + ":" + s.config.Server.Port
app.Engine.ReadTimeout = time.Duration(s.config.Server.Timeout.Read) * time.Second
app.Engine.WriteTimeout = time.Duration(s.config.Server.Timeout.Write) * time.Second
app.Engine.IdleTimeout = time.Duration(s.config.Server.Timeout.Idle) * time.Second
// Show Fiber logo on console for debug mode
if s.config.Logging.Level != "debug" {
app.Banner = false
}
app.Use(func(c *fiber.Ctx) {
// Log each request
s.logger.Info(
"fetch URL",
zap.String("method", c.Method()),
zap.String("path", c.Path()),
)
// Go to next middleware
c.Next()
})
// App routes
app.Get("/", IndexHandler)
// Start server
app.Listen(host)
return nil
}