-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
74 lines (55 loc) · 1.28 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
package main
import (
"github.com/bxcodec/faker/v4"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"math/rand"
)
type Item struct {
Id uint
Name string
Email string
Phone string
Price int
}
func main() {
// Connect to MySQL Database
db, err := gorm.Open(mysql.Open("root:RootPassword/fakedata"), &gorm.Config{})
if err != nil {
panic("Could not connect to database")
}
// Create Database Table (Items)
db.AutoMigrate(&Item{})
// Initiate GoFiber server
app := fiber.New()
app.Use(cors.New())
// Post handler request
app.Post("/api/item/create", func(c *fiber.Ctx) error {
for i := 0; i < 5000; i++ {
db.Create(&Item{
// Create Fake Name with faker repo
Name: faker.Word(),
// Create Fake Email with faker repo
Email: faker.Email(),
// Create Fake Phone No.
Phone: faker.Phonenumber(),
// Create fake Price using random EQ
Price: rand.Intn(140) + 10,
})
}
// Return message if success
return c.Status(200).JSON(fiber.Map{
"message": "Success",
})
})
// Get request
app.Get("/api/item/all", func(c *fiber.Ctx) error {
var items []Item
db.Find(&items)
return c.Status(200).JSON(items)
})
// Listen to port 8000
app.Listen(":8000")
}