-
-
Notifications
You must be signed in to change notification settings - Fork 456
/
main.go
42 lines (34 loc) · 818 Bytes
/
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
package main
import (
"fiber-gorm/book"
"fiber-gorm/database"
"fmt"
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func setupRoutes(app *fiber.App) {
app.Get("/api/v1/book", book.GetBooks)
app.Get("/api/v1/book/:id", book.GetBook)
app.Post("/api/v1/book", book.NewBook)
app.Delete("/api/v1/book/:id", book.DeleteBook)
}
func initDatabase() {
var err error
database.DBConn, err = gorm.Open(sqlite.Open("books.db"))
if err != nil {
panic("failed to connect database")
}
fmt.Println("Connection Opened to Database")
database.DBConn.AutoMigrate(&book.Book{})
fmt.Println("Database Migrated")
}
func main() {
app := fiber.New()
app.Use(cors.New())
initDatabase()
setupRoutes(app)
log.Fatal(app.Listen(":3000"))
}