-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (67 loc) · 2.31 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
package main
import (
"github.com/jmoiron/sqlx"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
users_api "github.com/spargapees/users-api/config"
"github.com/spargapees/users-api/handler"
repository "github.com/spargapees/users-api/repository"
"github.com/spargapees/users-api/server"
service "github.com/spargapees/users-api/service"
"os"
"os/signal"
"syscall"
)
//TIP To run your code, right-click the code and select <b>Run</b>. Alternatively, click
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.
func main() {
logger := logrus.New()
logger.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
logger.SetOutput(os.Stdout)
if err := godotenv.Load(); err != nil {
logger.WithError(err).Warn("No .env file found, proceeding with environment variables")
} else {
logger.Info("Loaded .env file successfully")
}
cfg, err := users_api.NewConfig()
if err != nil {
logger.WithError(err).Fatal("Failed to load configuration")
}
logger.Info("Configuration loaded successfully")
db, err := sqlx.Open("postgres", cfg.DSN())
if err != nil {
logger.WithError(err).Fatal("Failed to connect to PostgreSQL")
}
defer func() {
if err := db.Close(); err != nil {
logger.WithError(err).Warn("Failed to close database connection")
} else {
logger.Info("Database connection closed successfully")
}
}()
if err := db.Ping(); err != nil {
logger.WithError(err).Fatal("Database connection test failed")
}
logger.Info("Successfully connected to PostgreSQL!")
repository := repository.NewRepository(db)
service := service.NewService(repository)
handler := handler.NewHandler(service)
srv := new(server.Server)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
go func() {
if err := srv.Run(port, handler.InitRoutes()); err != nil {
logger.Fatalf("Error occurred while running HTTP server: %s", err.Error())
}
}()
logger.Info("Users-API Started...")
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
<-quit
logger.Info("Server shutting down...")
}
//TIP See GoLand help at <a href="https://www.jetbrains.com/help/go/">jetbrains.com/help/go/</a>.
// Also, you can try interactive lessons for GoLand by selecting 'Help | Learn IDE Features' from the main menu.