-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (128 loc) · 3.95 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"context"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"github.com/FACorreiaa/Aviation-tracker/api"
"github.com/FACorreiaa/Aviation-tracker/app"
"github.com/FACorreiaa/Aviation-tracker/app/repository"
"github.com/FACorreiaa/Aviation-tracker/config"
"github.com/FACorreiaa/Aviation-tracker/db"
"github.com/joho/godotenv"
"github.com/redis/go-redis/v9"
)
func init() {
if err := godotenv.Load(); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
//go:generate npx tailwindcss build -c tailwind.config.js -o ./controller/static/css/style.css -
//go:generate ./tailwindcss -i controller/static/css/main.css -o controller/static/css/output.css --minify
cfg, err := config.NewConfig()
if err != nil {
return err
}
pprofAddr := os.Getenv("PPROF_ADDR")
pprofPort := os.Getenv("PPROF_PORT")
var logHandler slog.Handler
logHandlerOptions := slog.HandlerOptions{
AddSource: true,
Level: cfg.Log.Level,
}
if cfg.Log.Format == "json" {
logHandler = slog.NewJSONHandler(os.Stdout, &logHandlerOptions)
} else {
logHandler = slog.NewTextHandler(os.Stdout, &logHandlerOptions)
}
slog.SetDefault(slog.New(logHandler))
pool, err := db.Init(cfg.Database.ConnectionURL)
if err != nil {
log.Println(err)
}
defer pool.Close()
db.WaitForDB(pool)
redisClient, err := db.InitRedis(cfg.Redis.Host, cfg.Redis.Password, cfg.Redis.DB)
if err != nil {
return fmt.Errorf("failed to set config: %w", err)
}
defer func(redisClient *redis.Client) {
err = redisClient.Close()
if err != nil {
log.Println(err)
os.Exit(1)
}
}(redisClient)
if err = db.Migrate(pool); err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
tableDataMigration := api.NewRepository(pool)
if err = tableDataMigration.MigrateAirlineAPIData(); err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
if err = tableDataMigration.MigrateAircraftAPIData(); err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
if err = tableDataMigration.MigrateTaxAPIData(); err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
if err = tableDataMigration.MigrateAirplaneAPIData(); err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
if err = tableDataMigration.MigrateAirportAPIData(); err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
if err = tableDataMigration.MigrateCountryAPIData(); err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
if err = tableDataMigration.MigrateCityAPIData(); err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
if err = tableDataMigration.MigrateFlightAPIData(); err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
srv := &http.Server{
Addr: cfg.Server.Addr,
WriteTimeout: cfg.Server.WriteTimeout,
ReadTimeout: cfg.Server.ReadTimeout,
IdleTimeout: cfg.Server.IdleTimeout,
Handler: app.Router(pool, []byte(cfg.Server.SessionKey), redisClient),
}
jobRepo := api.NewRepositoryJob(pool)
jobService := api.NewServiceJob(jobRepo)
jobService.StartAPICheckCronJob()
go func() {
slog.Info("Starting server " + cfg.Server.Addr)
if err = srv.ListenAndServe(); err != nil {
slog.Error("ListenAndServe", "error", err)
}
}()
err = config.InitPprof(pprofAddr, pprofPort)
if err != nil {
fmt.Printf("Error initializing pprof config: %s", err)
panic(err)
}
<-ctx.Done() // Wait for cancellation signal
// Shutdown server
ctxShutdown, cancel := context.WithTimeout(context.Background(), cfg.Server.GracefulTimeout)
defer cancel()
if err = srv.Shutdown(ctxShutdown); err != nil {
repository.HandleError(err, "Error shutting down server")
}
slog.Info("Shutting down")
return nil
}
func main() {
ctx := context.Background()
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()
if err := run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
cancel()
os.Exit(1)
}
}