-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (72 loc) · 1.82 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/iriskin77/testgo/configs"
"github.com/iriskin77/testgo/pkg/logging"
"github.com/iriskin77/testgo/server"
"github.com/joho/godotenv"
"go.uber.org/zap"
)
// @title Orders API
// @version 1.0
// @description This is a sample service for managing orders
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.email soberkoder@gmail.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8000
// @BasePath /
func main() {
if err := RunServer(); err != nil {
log.Fatal(err)
}
os.Exit(0)
}
func RunServer() error {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
return err
}
// Initialize logger
logging.InitLogger()
logger := logging.GetLogger()
logger.Info("logger has been initialized")
// Initialize configs
config := configs.NewConfig()
// Initialize server, db, routing
ctx := context.Background()
srv, err := server.NewHttpServer(ctx, logger, config.Postgres, config.Bindaddr)
if err != nil {
logger.Fatal("Failed to start server", err.Error())
return err
}
stopped := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-sigint
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err = srv.Shutdown(ctx); err != nil {
logger.Error("HTTP Server Shutdown", zap.Error(err))
}
close(stopped)
}()
logger.Info("starting API Server...")
if err = srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
logger.Fatal("HTTP server ListenAndServe", err.Error())
return err
}
<-stopped
logger.Info("Bye! Good day :)")
return nil
}