-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (53 loc) · 1.39 KB
/
main.go
File metadata and controls
63 lines (53 loc) · 1.39 KB
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
package main
import (
"log"
"os"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"github.com/visitha2001/go-fiber-postgres/handlers"
"github.com/visitha2001/go-fiber-postgres/models"
"github.com/visitha2001/go-fiber-postgres/routes"
"github.com/visitha2001/go-fiber-postgres/storage"
)
func main() {
// Load .env
if err := godotenv.Load(".env"); err != nil {
log.Fatal("Error loading .env file")
}
// Database config
config := storage.Config{
Host: os.Getenv("DB_HOST"),
User: os.Getenv("DB_USER"),
Password: os.Getenv("DB_PASSWORD"),
DBName: os.Getenv("DB_NAME"),
Port: os.Getenv("DB_PORT"),
SSLMode: os.Getenv("DB_SSLMODE"),
}
db, err := storage.NewConnection(&config)
if err != nil {
log.Fatal("Could not connect to the database")
} else {
log.Println("Connected to the database")
}
if err := models.MigrateBooks(db); err != nil {
log.Fatal("Error migrating books:", err)
}
// Initialize app and handler
app := fiber.New()
bookHandler := &handlers.BookHandler{DB: db}
// Root endpoint
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
// Register routes
routes.RegisterBookRoutes(app, bookHandler)
// Run server
port := ":" + os.Getenv("PORT")
if port == "" {
port = ":8081"
}
log.Println("🚀 Server running on http://localhost" + port)
if err := app.Listen(port); err != nil {
log.Fatal(err)
}
}