forked from gofiber/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"gorm-mysql/database" | ||
"gorm-mysql/routes" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/fiber/v2/middleware/cors" | ||
) | ||
|
||
func setUpRoutes(app *fiber.App) { | ||
app.Get("/hello", routes.Hello) | ||
app.Get("/allbooks", routes.AllBooks) | ||
app.Get("/book/:id", routes.GetBook) | ||
app.Post("/addbook", routes.AddBook) | ||
app.Post("/book", routes.Book) | ||
app.Put("/update", routes.Update) | ||
app.Delete("/delete", routes.Delete) | ||
} | ||
|
||
func main() { | ||
database.ConnectDb() | ||
app := fiber.New() | ||
|
||
setUpRoutes(app) | ||
|
||
app.Use(cors.New()) | ||
|
||
app.Use(func(c *fiber.Ctx) error { | ||
return c.SendStatus(404) // => 404 "Not Found" | ||
}) | ||
|
||
log.Fatal(app.Listen(":3000")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package database | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"gorm-mysql/models" | ||
|
||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var ( | ||
DBConn *gorm.DB | ||
) | ||
|
||
// connectDb | ||
func ConnectDb() { | ||
|
||
// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details | ||
dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local" | ||
/* | ||
NOTE: | ||
To handle time.Time correctly, you need to include parseTime as a parameter. (more parameters) | ||
To fully support UTF-8 encoding, you need to change charset=utf8 to charset=utf8mb4. See this article for a detailed explanation | ||
*/ | ||
|
||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) | ||
|
||
if err != nil { | ||
log.Fatal("Failed to connect to database. \n", err) | ||
os.Exit(2) | ||
} | ||
|
||
log.Println("connected") | ||
db.AutoMigrate(&models.Book{}) | ||
DBConn = db | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module gorm-mysql | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/gofiber/fiber/v2 v2.23.0 // indirect | ||
gorm.io/driver/mysql v1.2.2 // indirect | ||
gorm.io/gorm v1.22.4 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
github.com/andybalholm/brotli v1.0.2 h1:JKnhI/XQ75uFBTiuzXpzFrUriDPiZjlOSzh6wXogP0E= | ||
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= | ||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= | ||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | ||
github.com/gofiber/fiber/v2 v2.13.0/go.mod h1:oZTLWqYnqpMMuF922SjGbsYZsdpE1MCfh416HNdweIM= | ||
github.com/gofiber/fiber/v2 v2.23.0 h1:kcJGMC6SULJ2G7p7mbs+A28cVLOeJSR694jfGyGZqRI= | ||
github.com/gofiber/fiber/v2 v2.23.0/go.mod h1:MR1usVH3JHYRyQwMe2eZXRSZHRX38fkV+A7CPB+DlDQ= | ||
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/jinzhu/now v1.1.3 h1:PlHq1bSCSZL9K0wUhbm2pGLoTWs2GwVhsP6emvGV/ZI= | ||
github.com/jinzhu/now v1.1.3/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= | ||
github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= | ||
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= | ||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | ||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | ||
github.com/valyala/fasthttp v1.26.0/go.mod h1:cmWIqlu99AO/RKcp1HWaViTqc57FswJOfYYdPJBl8BA= | ||
github.com/valyala/fasthttp v1.31.0 h1:lrauRLII19afgCs2fnWRJ4M5IkV0lo2FqA61uGkNBfE= | ||
github.com/valyala/fasthttp v1.31.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus= | ||
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= | ||
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= | ||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= | ||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E= | ||
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
gorm.io/driver/mysql v1.2.2 h1:2qoqhOun1maoJOfLtnzJwq+bZlHkEF34rGntgySqp48= | ||
gorm.io/driver/mysql v1.2.2/go.mod h1:qsiz+XcAyMrS6QY+X3M9R6b/lKM1imKmcuK9kac5LTo= | ||
gorm.io/gorm v1.22.4 h1:8aPcyEJhY0MAt8aY6Dc524Pn+pO29K+ydu+e/cXSpQM= | ||
gorm.io/gorm v1.22.4/go.mod h1:1aeVC+pe9ZmvKZban/gW4QPra7PRoTEssyc922qCAkk= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package models | ||
|
||
import "gorm.io/gorm" | ||
|
||
//Book model | ||
type Book struct { | ||
gorm.Model | ||
|
||
Title string `json:"title"` | ||
Author string `json:"author"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package routes | ||
|
||
import ( | ||
"gorm-mysql/database" | ||
"gorm-mysql/models" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
//Hello | ||
func Hello(c *fiber.Ctx) error { | ||
return c.SendString("fiber") | ||
} | ||
|
||
//AddBook | ||
func AddBook(c *fiber.Ctx) error { | ||
book := new(models.Book) | ||
if err := c.BodyParser(book); err != nil { | ||
return c.Status(400).JSON(err.Error()) | ||
} | ||
|
||
database.DBConn.Create(&book) | ||
|
||
return c.Status(200).JSON(book) | ||
} | ||
|
||
//getBook | ||
func GetBook(c *fiber.Ctx) error { | ||
books := []models.Book{} | ||
|
||
database.DBConn.First(&books, c.Params("id")) | ||
|
||
return c.Status(200).JSON(books) | ||
} | ||
|
||
//AllBooks | ||
func AllBooks(c *fiber.Ctx) error { | ||
books := []models.Book{} | ||
|
||
database.DBConn.Find(&books) | ||
|
||
return c.Status(200).JSON(books) | ||
} | ||
|
||
//Book | ||
func Book(c *fiber.Ctx) error { | ||
book := []models.Book{} | ||
title := new(models.Book) | ||
if err := c.BodyParser(title); err != nil { | ||
return c.Status(400).JSON(err.Error()) | ||
} | ||
database.DBConn.Where("title = ?", title.Title).Find(&book) | ||
return c.Status(200).JSON(book) | ||
} | ||
|
||
//Update | ||
func Update(c *fiber.Ctx) error { | ||
book := []models.Book{} | ||
title := new(models.Book) | ||
if err := c.BodyParser(title); err != nil { | ||
return c.Status(400).JSON(err.Error()) | ||
} | ||
|
||
database.DBConn.Model(&book).Where("title = ?", title.Title).Update("author", title.Author) | ||
|
||
return c.Status(400).JSON("updated") | ||
} | ||
|
||
//Delete | ||
func Delete(c *fiber.Ctx) error { | ||
book := []models.Book{} | ||
title := new(models.Book) | ||
if err := c.BodyParser(title); err != nil { | ||
return c.Status(400).JSON(err.Error()) | ||
} | ||
database.DBConn.Where("title = ?", title.Title).Delete(&book) | ||
|
||
return c.Status(200).JSON("deleted") | ||
} |