-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
76 lines (63 loc) · 1.67 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
package main
import (
"appdoki-be/app"
"appdoki-be/config"
"context"
firebase "firebase.google.com/go/v4"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
log "github.com/sirupsen/logrus"
"google.golang.org/api/option"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
conf := config.NewConfig()
firebaseApp := prepareFirebaseApp(conf.AppConfig.GoogleServiceAccountKeyPath)
db := prepareDatabase(&conf.Database)
application := app.NewApplication(conf, db, firebaseApp)
srv := &http.Server{
Addr: conf.Server.Address,
Handler: application.Routes(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Listen: %s\n", err)
}
}()
log.Infof("Server started on %s", srv.Addr)
<-done
log.Info("Server stopped")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
cancel()
}()
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server shutdown failed: %+v", err)
}
log.Info("Server exited gracefully")
}
func prepareDatabase(conf *config.DatabaseConfig) *sqlx.DB {
db, err := sqlx.Connect("postgres", conf.URI)
if err != nil {
log.Fatalln(err)
}
runMigrations(db.DB, conf)
return db
}
func prepareFirebaseApp(keyPath string) *firebase.App {
opt := option.WithCredentialsFile(keyPath)
firebaseApp, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatalf("error initializing app: %+v", err)
}
return firebaseApp
}