Skip to content

Commit

Permalink
📦 update examples for v2 (gofiber#54)
Browse files Browse the repository at this point in the history
* go.mod + workflows

* Update examples

* bump to v2

* Delete go.yml

* update examples for v2
  • Loading branch information
Fenny authored Sep 16, 2020
1 parent c34c703 commit 42aced4
Show file tree
Hide file tree
Showing 49 changed files with 462 additions and 646 deletions.
2 changes: 1 addition & 1 deletion docker-nginx-loadbalancer/middlewares/checkDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

/*CheckDB : Check the DB connection before to execute a handle func*/
func CheckDB(c *fiber.Ctx) {
func CheckDB(c *fiber.Ctx) error {
if !database.ConnectionOK() {
c.Send("Conexión perdida con la base de datos")
c.SendStatus(http.StatusInternalServerError)
Expand Down
2 changes: 1 addition & 1 deletion docker-nginx-loadbalancer/middlewares/checkToken.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

/*CheckToken : Check the validate of the jwt*/
func CheckToken(c *fiber.Ctx) {
func CheckToken(c *fiber.Ctx) error {
_, _, _, err := utilities.ProcessToken(c.Get("Authorization"))
if err != nil {
c.Send("Error in the jwt !" + err.Error())
Expand Down
2 changes: 1 addition & 1 deletion docker-nginx-loadbalancer/middlewares/requestLogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

/*RequestLogger : Check the request*/
func RequestLogger(c *fiber.Ctx) {
func RequestLogger(c *fiber.Ctx) error {
method := string(c.Fasthttp.Request.Header.Method())
url:= string(c.Fasthttp.Request.Header.RequestURI())
log.Println("[ "+method+" ]"+" => "+url)
Expand Down
2 changes: 1 addition & 1 deletion docker-nginx-loadbalancer/routes/userGetProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

/*GetProfile : Get the user profile*/
func GetProfile(c *fiber.Ctx) {
func GetProfile(c *fiber.Ctx) error {
ID := c.Query("id")

if len(ID) < 1 {
Expand Down
2 changes: 1 addition & 1 deletion docker-nginx-loadbalancer/routes/userLogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

/*Login : user try to login in the app*/
func Login(c *fiber.Ctx) {
func Login(c *fiber.Ctx) error {
c.Accepts("application/json")

var user models.User
Expand Down
2 changes: 1 addition & 1 deletion docker-nginx-loadbalancer/routes/userRegister.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

/*Register : Save a user in the database*/
func Register(c *fiber.Ctx) {
func Register(c *fiber.Ctx) error {

var user models.User
if err := c.BodyParser(&user); err !=nil{
Expand Down
2 changes: 1 addition & 1 deletion docker-nginx-loadbalancer/routes/userUpdateProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

/*UpdateProfile : Edit a user profile*/
func UpdateProfile(c *fiber.Ctx) {
func UpdateProfile(c *fiber.Ctx) error {
var user models.User
var isUpdated bool

Expand Down
4 changes: 2 additions & 2 deletions gorm/book/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func GetBook(c *fiber.Ctx) error {
return c.JSON(book)
}

func NewBook(c *fiber.Ctx) {
func NewBook(c *fiber.Ctx) error {
db := database.DBConn
book := new(Book)
if err := c.BodyParser(book); err != nil {
Expand All @@ -40,7 +40,7 @@ func NewBook(c *fiber.Ctx) {
return c.JSON(book)
}

func DeleteBook(c *fiber.Ctx) {
func DeleteBook(c *fiber.Ctx) error {
id := c.Params("id")
db := database.DBConn

Expand Down
2 changes: 1 addition & 1 deletion jwt/handler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package handler
import "github.com/gofiber/fiber/v2"

// Hello hanlde api status
func Hello(c *fiber.Ctx) {
func Hello(c *fiber.Ctx) error {
return c.JSON(fiber.Map{"status": "success", "message": "Hello i'm ok!", "data": nil})
}
2 changes: 1 addition & 1 deletion jwt/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Login get user and password
func Login(c *fiber.Ctx) {
func Login(c *fiber.Ctx) error {
type LoginInput struct {
Identity string `json:"identity"`
Password string `json:"password"`
Expand Down
8 changes: 4 additions & 4 deletions jwt/handler/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

// GetAllProducts query all products
func GetAllProducts(c *fiber.Ctx) {
func GetAllProducts(c *fiber.Ctx) error {
db := database.DB
var products []model.Product
db.Find(&products)
return c.JSON(fiber.Map{"status": "success", "message": "All products", "data": products})
}

// GetProduct query product
func GetProduct(c *fiber.Ctx) {
func GetProduct(c *fiber.Ctx) error {
id := c.Params("id")
db := database.DB
var product model.Product
Expand All @@ -29,7 +29,7 @@ func GetProduct(c *fiber.Ctx) {
}

// CreateProduct new product
func CreateProduct(c *fiber.Ctx) {
func CreateProduct(c *fiber.Ctx) error {
db := database.DB
product := new(model.Product)
if err := c.BodyParser(product); err != nil {
Expand All @@ -41,7 +41,7 @@ func CreateProduct(c *fiber.Ctx) {
}

// DeleteProduct delete product
func DeleteProduct(c *fiber.Ctx) {
func DeleteProduct(c *fiber.Ctx) error {
id := c.Params("id")
db := database.DB

Expand Down
5 changes: 5 additions & 0 deletions multiple-ports/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module main

go 1.15

require github.com/gofiber/fiber/v2 v2.0.1
21 changes: 21 additions & 0 deletions multiple-ports/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/gofiber/fiber/v2 v2.0.1 h1:82Sw1SyHrEaKLYdXbNJGzI/t2okEeDIuw9nJGKpQmaI=
github.com/gofiber/fiber/v2 v2.0.1/go.mod h1:GeIpT8VILgZt3Tn6gATjwb39Ff8OdM0qnZ2grAA0Vts=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.11.0 h1:wJbzvpYMVGG9iTI9VxpnNZfd4DzMPoCWze3GgSqz8yg=
github.com/klauspost/compress v1.11.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
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.16.0 h1:9zAqOYLl8Tuy3E5R6ckzGDJ1g8+pw15oQp2iL9Jl6gQ=
github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 h1:W0lCpv29Hv0UaM1LXb9QlBHLNP8UFfcKjblhVCWftOM=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4 changes: 2 additions & 2 deletions multiple-ports/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ func main() {

// Listen on port 8080
go func() {
log.Fatal(app.Listen(8080))
log.Fatal(app.Listen(":8080"))
}()

// Listen on port 3000
log.Fatal(app.Listen(":3000"))
}

// Handler
func hello(c *fiber.Ctx) {
func hello(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
}
8 changes: 8 additions & 0 deletions postgresql/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module main

go 1.15

require (
github.com/gofiber/fiber/v2 v2.0.1
github.com/lib/pq v1.8.0
)
23 changes: 23 additions & 0 deletions postgresql/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/gofiber/fiber/v2 v2.0.1 h1:82Sw1SyHrEaKLYdXbNJGzI/t2okEeDIuw9nJGKpQmaI=
github.com/gofiber/fiber/v2 v2.0.1/go.mod h1:GeIpT8VILgZt3Tn6gATjwb39Ff8OdM0qnZ2grAA0Vts=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.11.0 h1:wJbzvpYMVGG9iTI9VxpnNZfd4DzMPoCWze3GgSqz8yg=
github.com/klauspost/compress v1.11.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/lib/pq v1.8.0 h1:9xohqzkUwzR4Ga4ivdTcawVS89YSDVxXMa3xJX3cGzg=
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
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.16.0 h1:9zAqOYLl8Tuy3E5R6ckzGDJ1g8+pw15oQp2iL9Jl6gQ=
github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 h1:W0lCpv29Hv0UaM1LXb9QlBHLNP8UFfcKjblhVCWftOM=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
71 changes: 31 additions & 40 deletions postgresql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ const (

// Employee struct
type Employee struct {
ID string `json: "id"`
Name string `json: "name"`
Salary string `json: "salary"`
Age string `json: "age"`
ID string `json:"id"`
Name string `json:"name"`
Salary string `json:"salary"`
Age string `json:"age"`
}

// Employees struct
type Employees struct {
Employees []Employee `json: "employees"`
Employees []Employee `json:"employees"`
}

// Connect function
Expand Down Expand Up @@ -64,100 +64,91 @@ func main() {
// Insert Employee into database
rows, err := db.Query("SELECT id, name, salary, age FROM employees order by id")
if err != nil {
c.Status(500).Send(err)
return
return c.Status(500).SendString(err.Error())
}
defer rows.Close()
result := Employees{}

for rows.Next() {
employee := Employee{}
err := rows.Scan(&employee.ID, &employee.Name, &employee.Salary, &employee.Age)
// Exit if we get an error
if err != nil {
c.Status(500).Send(err)
return
if err := rows.Scan(&employee.ID, &employee.Name, &employee.Salary, &employee.Age); err != nil {
return err // Exit if we get an error
}

// Append Employee to Employees
result.Employees = append(result.Employees, employee)
}
// Return Employees in JSON format
if err := c.JSON(result); err != nil {
c.Status(500).Send(err)
return
}
return c.JSON(result)
})

// Add record into postgreSQL
app.Post("/employee", func(c *fiber.Ctx) error {
// New Employee struct
u := new(Employee)

// Parse body into struct
if err := c.BodyParser(u); err != nil {
c.Status(400).Send(err)
return
return c.Status(400).SendString(err.Error())
}

// Insert Employee into database
res, err := db.Query("INSERT INTO employees (name, salary, age)VALUES ($1, $2, $3)", u.Name, u.Salary, u.Age)
if err != nil {
c.Status(500).Send(err)
return
return err
}

// Print result
log.Println(res)

// Return Employee in JSON format
if err := c.JSON(u); err != nil {
c.Status(500).Send(err)
return
}
return c.JSON(u)
})

// Update record into postgreSQL
app.Put("/employee", func(c *fiber.Ctx) error {
// New Employee struct
u := new(Employee)

// Parse body into struct
if err := c.BodyParser(u); err != nil {
c.Status(400).Send(err)
return
return c.Status(400).SendString(err.Error())
}

// Insert Employee into database
res, err := db.Query("UPDATE employees SET name=$1,salary=$2,age=$3 WHERE id=$5", u.Name, u.Salary, u.Age, u.ID)
if err != nil {
c.Status(500).Send(err)
return
return err
}

// Print result
log.Println(res)

// Return Employee in JSON format
if err := c.Status(201).JSON(u); err != nil {
c.Status(500).Send(err)
return
}
return c.Status(201).JSON(u)
})

// Delete record from postgreSQL
app.Delete("/employee", func(c *fiber.Ctx) error {
// New Employee struct
u := new(Employee)

// Parse body into struct
if err := c.BodyParser(u); err != nil {
c.Status(400).Send(err)
return
return c.Status(400).SendString(err.Error())
}

// Insert Employee into database
res, err := db.Query("DELETE FROM employees WHERE id = $1", u.ID)
if err != nil {
c.Status(500).Send(err)
return
return err
}

// Print result
log.Println(res)

// Return Employee in JSON format
if err := c.JSON("Deleted"); err != nil {
c.Status(500).Send(err)
return
}
return c.JSON("Deleted")
})

log.Fatal(app.Listen(":3000"))
Expand Down
5 changes: 5 additions & 0 deletions prefork/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module main

go 1.15

require github.com/gofiber/fiber/v2 v2.0.1
21 changes: 21 additions & 0 deletions prefork/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/gofiber/fiber/v2 v2.0.1 h1:82Sw1SyHrEaKLYdXbNJGzI/t2okEeDIuw9nJGKpQmaI=
github.com/gofiber/fiber/v2 v2.0.1/go.mod h1:GeIpT8VILgZt3Tn6gATjwb39Ff8OdM0qnZ2grAA0Vts=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.11.0 h1:wJbzvpYMVGG9iTI9VxpnNZfd4DzMPoCWze3GgSqz8yg=
github.com/klauspost/compress v1.11.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
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.16.0 h1:9zAqOYLl8Tuy3E5R6ckzGDJ1g8+pw15oQp2iL9Jl6gQ=
github.com/valyala/fasthttp v1.16.0/go.mod h1:YOKImeEosDdBPnxc0gy7INqi3m1zK6A+xl6TwOBhHCA=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a h1:0R4NLDRDZX6JcmhJgXi5E4b8Wg84ihbmUKp/GvSPEzc=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 h1:W0lCpv29Hv0UaM1LXb9QlBHLNP8UFfcKjblhVCWftOM=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Loading

0 comments on commit 42aced4

Please sign in to comment.