-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (64 loc) · 1.8 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/eze8789/urlshtn-go/pkg/handlers"
"github.com/eze8789/urlshtn-go/pkg/config"
"github.com/eze8789/urlshtn-go/pkg/database/postgres"
"github.com/sirupsen/logrus"
)
const webserverTimeout = 30
func main() {
logger := logrus.New()
configFile := flag.String("config", "./configs/app/config.yaml", "Choose configuration file")
flag.Parse()
cfg, err := config.GenerateConfig(*configFile)
if err != nil {
logger.Fatalf("could not read configuration file %s: %v", *configFile, err)
}
pg, err := postgres.NewConn(cfg.Postgres.Host, cfg.Postgres.Port, cfg.Postgres.Username, cfg.Postgres.Password,
cfg.Postgres.Database, cfg.Postgres.SSLMode)
if err != nil {
logger.Fatalf("could not establish connection: %v", err)
}
defer pg.Close()
srv := &http.Server{
Addr: fmt.Sprintf("%s:%s", cfg.WebServer.Addr, cfg.WebServer.Port),
Handler: handlers.Routes(pg),
}
// gracefully shutdown webserver
done := make(chan bool, 1)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-quit
logger.Info("shutting down webserver")
ctx, cancel := context.WithTimeout(context.Background(), webserverTimeout*time.Second)
defer cancel()
srv.SetKeepAlivesEnabled(false)
if err := srv.Shutdown(ctx); err != nil {
logger.Fatalf("could not gracefully shutdown webserver: %v", err)
}
close(done)
}()
// Start webserver goroutine
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
logger.Infof("starting webserver on: %v", srv.Addr)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
logger.Fatalf("could not start webserver on: %v", srv.Addr)
}
wg.Done()
}()
wg.Wait()
<-done
logger.Info("webserver stopped")
}