forked from TutorialEdge/go-fiber-rest-api-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (35 loc) · 921 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
43
44
45
46
package main
import (
"fmt"
"github.com/elliotforbes/go-fiber-tutorial/book"
"github.com/elliotforbes/go-fiber-tutorial/database"
"github.com/gofiber/fiber"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
func helloWorld(c *fiber.Ctx) {
c.Send("Hello, World!")
}
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("sqlite3", "books.db")
if err != nil {
panic("Failed to connect to database")
}
fmt.Println("Database connection successfully opened")
database.DBConn.AutoMigrate(&book.Book{})
fmt.Println("Database Migrated")
}
func main() {
app := fiber.New()
initDatabase()
defer database.DBConn.Close()
setupRoutes(app)
app.Listen(3000)
}