This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
/
main.go
86 lines (73 loc) · 2.09 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
package main
import (
"context"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/joho/godotenv"
_ "github.com/mattn/go-sqlite3"
"github.com/p12s/uber-popug/auth/pkg/broker"
"github.com/p12s/uber-popug/auth/pkg/handler"
"github.com/p12s/uber-popug/auth/pkg/repository"
"github.com/p12s/uber-popug/auth/pkg/service"
"github.com/sirupsen/logrus"
)
func main() {
runtime.GOMAXPROCS(1)
logrus.SetFormatter(new(logrus.JSONFormatter))
if err := godotenv.Load(); err != nil {
logrus.Fatalf("error loading env variables: %s\n", err.Error())
}
db, err := repository.NewSqlite3DB(repository.Config{
Driver: os.Getenv("DB_DRIVER"),
})
if err != nil {
logrus.Fatalf("failed to initialize db: %s\n", err.Error())
}
repos := repository.NewRepository(db)
services := service.NewService(repos)
kafka, err := broker.NewKafka()
if err != nil {
logrus.Fatalf("kafka error: %s\n", err.Error())
}
handlers := handler.NewHandler(services, kafka)
srv := new(Server)
go func() {
if err := srv.Run(os.Getenv("PORT"), handlers.InitRoutes()); err != nil {
logrus.Fatalf("error while running http server: %s\n", err.Error())
}
}()
logrus.Print("😀 auth app started with port: ", os.Getenv("PORT"))
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
<-quit
logrus.Print("auth app shutting down")
if err := srv.Shutdown(context.Background()); err != nil {
logrus.Errorf("error occurred on server shutting down: %s", err.Error())
}
if err := db.Close(); err != nil {
logrus.Errorf("error occurred on db connection close: %s", err.Error())
}
}
// Server - http server
type Server struct {
httpServer *http.Server
}
// Run - start
func (s *Server) Run(port string, handler http.Handler) error {
s.httpServer = &http.Server{
Addr: ":" + port,
Handler: handler,
MaxHeaderBytes: 1 << 20, // 1 MB
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
return s.httpServer.ListenAndServe()
}
// Shutdown - grace-full
func (s *Server) Shutdown(ctx context.Context) error {
return s.httpServer.Shutdown(ctx)
}