-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (76 loc) · 2.23 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
package main
import (
"book-management/controllers"
"book-management/database"
middleware "book-management/middleware"
"book-management/seeder"
"database/sql"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
)
var (
DB *sql.DB
err error
)
func main() {
err = godotenv.Load("config/.env")
if err != nil {
panic("Error loading .env file")
}
psqlInfo := fmt.Sprintf(`host=%s port=%s user=%s password=%s dbname=%s sslmode=disable`,
os.Getenv("PGHOST"),
os.Getenv("PGPORT"),
os.Getenv("PGUSER"),
os.Getenv("PGPASSWORD"),
os.Getenv("PGDATABASE"),
)
DB, err = sql.Open("postgres", psqlInfo)
defer DB.Close()
err = DB.Ping()
if err != nil {
panic(err)
}
database.DBMigrate(DB)
psqlSeeder := fmt.Sprintf(`host=%s port=%s user=%s password=%s dbname=%s sslmode=disable`,
os.Getenv("PGHOST"),
os.Getenv("PGPORT"),
os.Getenv("PGUSER"),
os.Getenv("PGPASSWORD"),
os.Getenv("PGDATABASE"),
)
DB, err = sql.Open("postgres", psqlSeeder)
if err != nil {
log.Fatalf("Could not connect to the database: %v", err)
}
defer DB.Close()
// Seed the database
seeder.SeedUsers(DB)
fmt.Println("Successfully connected!")
router := gin.Default()
router.POST("/login", controllers.Login)
authorized := router.Group("/")
authorized.Use(middleware.JWTAuthMiddleware())
{
authorized.GET("/book", controllers.GetAllBooks)
authorized.GET("/book/:id", controllers.GetBook)
authorized.POST("/book", controllers.InsertBook)
authorized.PUT("/book/:id", controllers.UpdateBook)
authorized.DELETE("/book/:id", controllers.DeleteBook)
authorized.GET("/category", controllers.GetAllCategories)
authorized.GET("/category/:id", controllers.GetCategory)
authorized.POST("/category", controllers.InsertCategory)
authorized.PUT("/category/:id", controllers.UpdateCategory)
authorized.DELETE("/category/:id", controllers.DeleteCategory)
authorized.GET("/category/:id/books", controllers.GetBooksByCategory)
authorized.GET("/user", controllers.GetAllUsers)
authorized.POST("/user", controllers.InsertUser)
authorized.PUT("/user/:id", controllers.UpdateUser)
authorized.DELETE("/user/:id", controllers.DeleteUser)
}
router.Run(":" + os.Getenv("PORT"))
// router.Run(":8080")
}