Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 404-handler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {

// 404 Handler
app.Use(func(c *fiber.Ctx) error {
return c.SendStatus(404) // => 404 "Not Found"
return c.SendStatus(http.StatusNotFound) // => 404 "Not Found"
})

// Start server
Expand Down
3 changes: 2 additions & 1 deletion 404-handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"log"
"net/http"

"github.com/gofiber/fiber/v2"
)
Expand All @@ -19,7 +20,7 @@ func main() {

// 404 Handler
app.Use(func(c *fiber.Ctx) error {
return c.SendStatus(404) // => 404 "Not Found"
return c.SendStatus(http.StatusNotFound) // => 404 "Not Found"
})

// Start server
Expand Down
7 changes: 4 additions & 3 deletions auth-docker-postgres-jwt/handler/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"app/database"
"app/model"
"net/http"

"github.com/gofiber/fiber/v2"
)
Expand All @@ -22,7 +23,7 @@ func GetProduct(c *fiber.Ctx) error {
var product model.Product
db.Find(&product, id)
if product.Title == "" {
return c.Status(404).JSON(fiber.Map{"status": "error", "message": "No product found with ID", "data": nil})
return c.Status(http.StatusNotFound).JSON(fiber.Map{"status": "error", "message": "No product found with ID", "data": nil})
}
return c.JSON(fiber.Map{"status": "success", "message": "Product found", "data": product})
}
Expand All @@ -32,7 +33,7 @@ func CreateProduct(c *fiber.Ctx) error {
db := database.DB
product := new(model.Product)
if err := c.BodyParser(product); err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Couldn't create product", "data": err})
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"status": "error", "message": "Couldn't create product", "data": err})
}
db.Create(&product)
return c.JSON(fiber.Map{"status": "success", "message": "Created product", "data": product})
Expand All @@ -46,7 +47,7 @@ func DeleteProduct(c *fiber.Ctx) error {
var product model.Product
db.First(&product, id)
if product.Title == "" {
return c.Status(404).JSON(fiber.Map{"status": "error", "message": "No product found with ID", "data": nil})
return c.Status(http.StatusNotFound).JSON(fiber.Map{"status": "error", "message": "No product found with ID", "data": nil})
}
db.Delete(&product)
return c.JSON(fiber.Map{"status": "success", "message": "Product successfully deleted", "data": nil})
Expand Down
20 changes: 11 additions & 9 deletions auth-docker-postgres-jwt/handler/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"net/http"
"strconv"

"app/database"
Expand Down Expand Up @@ -49,8 +50,9 @@ func GetUser(c *fiber.Ctx) error {
var user model.User
db.Find(&user, id)
if user.Username == "" {
return c.Status(404).JSON(fiber.Map{"status": "error", "message": "No user found with ID", "data": nil})
return c.Status(http.StatusNotFound).JSON(fiber.Map{"status": "error", "message": "No user found with ID", "data": nil})
}

return c.JSON(fiber.Map{"status": "success", "message": "User found", "data": user})
}

Expand All @@ -65,7 +67,7 @@ func CreateUser(c *fiber.Ctx) error {
db := database.DB
user := new(model.User)
if err := c.BodyParser(user); err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Review your input", "errors": err.Error()})
return c.Status(http.StatusBadRequest).JSON(fiber.Map{"status": "error", "message": "Review your input", "errors": err.Error()})
}

validate := validator.New()
Expand All @@ -79,12 +81,12 @@ func CreateUser(c *fiber.Ctx) error {

hash, err := hashPassword(user.Password)
if err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Couldn't hash password", "errors": err.Error()})
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"status": "error", "message": "Couldn't hash password", "errors": err.Error()})
}

user.Password = hash
if err := db.Create(&user).Error; err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Couldn't create user", "errors": err.Error()})
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"status": "error", "message": "Couldn't create user", "errors": err.Error()})
}

newUser := NewUser{
Expand All @@ -102,13 +104,13 @@ func UpdateUser(c *fiber.Ctx) error {
}
var uui UpdateUserInput
if err := c.BodyParser(&uui); err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Review your input", "errors": err.Error()})
return c.Status(http.StatusBadRequest).JSON(fiber.Map{"status": "error", "message": "Review your input", "errors": err.Error()})
}
id := c.Params("id")
token := c.Locals("user").(*jwt.Token)

if !validToken(token, id) {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Invalid token id", "data": nil})
return c.Status(http.StatusForbidden).JSON(fiber.Map{"status": "error", "message": "Invalid token id", "data": nil})
}

db := database.DB
Expand All @@ -128,17 +130,17 @@ func DeleteUser(c *fiber.Ctx) error {
}
var pi PasswordInput
if err := c.BodyParser(&pi); err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Review your input", "errors": err.Error()})
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"status": "error", "message": "Review your input", "errors": err.Error()})
}
id := c.Params("id")
token := c.Locals("user").(*jwt.Token)

if !validToken(token, id) {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Invalid token id", "data": nil})
return c.Status(http.StatusForbidden).JSON(fiber.Map{"status": "error", "message": "Invalid token id", "data": nil})
}

if !validUser(id, pi.Password) {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Not valid user", "data": nil})
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"status": "error", "message": "Not valid user", "data": nil})
}

db := database.DB
Expand Down
7 changes: 4 additions & 3 deletions auth-jwt/handler/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"api-fiber-gorm/database"
"api-fiber-gorm/model"
"net/http"

"github.com/gofiber/fiber/v2"
)
Expand All @@ -22,7 +23,7 @@ func GetProduct(c *fiber.Ctx) error {
var product model.Product
db.Find(&product, id)
if product.Title == "" {
return c.Status(404).JSON(fiber.Map{"status": "error", "message": "No product found with ID", "data": nil})
return c.Status(http.StatusNotFound).JSON(fiber.Map{"status": "error", "message": "No product found with ID", "data": nil})
}
return c.JSON(fiber.Map{"status": "success", "message": "Product found", "data": product})
}
Expand All @@ -32,7 +33,7 @@ func CreateProduct(c *fiber.Ctx) error {
db := database.DB
product := new(model.Product)
if err := c.BodyParser(product); err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Couldn't create product", "data": err})
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"status": "error", "message": "Couldn't create product", "data": err})
}
db.Create(&product)
return c.JSON(fiber.Map{"status": "success", "message": "Created product", "data": product})
Expand All @@ -46,7 +47,7 @@ func DeleteProduct(c *fiber.Ctx) error {
var product model.Product
db.First(&product, id)
if product.Title == "" {
return c.Status(404).JSON(fiber.Map{"status": "error", "message": "No product found with ID", "data": nil})
return c.Status(http.StatusNotFound).JSON(fiber.Map{"status": "error", "message": "No product found with ID", "data": nil})
}
db.Delete(&product)
return c.JSON(fiber.Map{"status": "success", "message": "Product successfully deleted", "data": nil})
Expand Down
19 changes: 10 additions & 9 deletions auth-jwt/handler/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"net/http"
"strconv"

"api-fiber-gorm/database"
Expand Down Expand Up @@ -48,7 +49,7 @@ func GetUser(c *fiber.Ctx) error {
var user model.User
db.Find(&user, id)
if user.Username == "" {
return c.Status(404).JSON(fiber.Map{"status": "error", "message": "No user found with ID", "data": nil})
return c.Status(http.StatusNotFound).JSON(fiber.Map{"status": "error", "message": "No user found with ID", "data": nil})
}
return c.JSON(fiber.Map{"status": "success", "message": "User found", "data": user})
}
Expand All @@ -63,17 +64,17 @@ func CreateUser(c *fiber.Ctx) error {
db := database.DB
user := new(model.User)
if err := c.BodyParser(user); err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Review your input", "data": err})
return c.Status(http.StatusBadRequest).JSON(fiber.Map{"status": "error", "message": "Review your input", "data": err})
}

hash, err := hashPassword(user.Password)
if err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Couldn't hash password", "data": err})
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"status": "error", "message": "Couldn't hash password", "data": err})
}

user.Password = hash
if err := db.Create(&user).Error; err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Couldn't create user", "data": err})
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"status": "error", "message": "Couldn't create user", "data": err})
}

newUser := NewUser{
Expand All @@ -91,13 +92,13 @@ func UpdateUser(c *fiber.Ctx) error {
}
var uui UpdateUserInput
if err := c.BodyParser(&uui); err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Review your input", "data": err})
return c.Status(http.StatusBadRequest).JSON(fiber.Map{"status": "error", "message": "Review your input", "data": err})
}
id := c.Params("id")
token := c.Locals("user").(*jwt.Token)

if !validToken(token, id) {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Invalid token id", "data": nil})
return c.Status(http.StatusForbidden).JSON(fiber.Map{"status": "error", "message": "Invalid token id", "data": nil})
}

db := database.DB
Expand All @@ -117,17 +118,17 @@ func DeleteUser(c *fiber.Ctx) error {
}
var pi PasswordInput
if err := c.BodyParser(&pi); err != nil {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Review your input", "data": err})
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{"status": "error", "message": "Review your input", "data": err})
}
id := c.Params("id")
token := c.Locals("user").(*jwt.Token)

if !validToken(token, id) {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Invalid token id", "data": nil})
return c.Status(http.StatusForbidden).JSON(fiber.Map{"status": "error", "message": "Invalid token id", "data": nil})
}

if !validUser(id, pi.Password) {
return c.Status(500).JSON(fiber.Map{"status": "error", "message": "Not valid user", "data": nil})
return c.Status(http.StatusForbidden).JSON(fiber.Map{"status": "error", "message": "Not valid user", "data": nil})
}

db := database.DB
Expand Down
7 changes: 4 additions & 3 deletions colly-gorm/app/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"

"fiber-colly-gorm/internals/consts"
"fiber-colly-gorm/internals/services/database"
Expand Down Expand Up @@ -34,23 +35,23 @@ func main() {
}))

micro.Get("/healthchecker", func(c *fiber.Ctx) error {
return c.Status(200).JSON(fiber.Map{
return c.Status(http.StatusOK).JSON(fiber.Map{
"status": "success",
"message": "Welcome to Golang, Fiber, and Colly",
})
})

scrape.Get("quotes", func(c *fiber.Ctx) error {
go scrapers.Quotes()
return c.Status(200).JSON(fiber.Map{
return c.Status(http.StatusOK).JSON(fiber.Map{
"status": "success",
"message": "Start scraping quotes.toscrape.com ...",
})
})

scrape.Get("coursera", func(c *fiber.Ctx) error {
go scrapers.CourseraCourses()
return c.Status(200).JSON(fiber.Map{
return c.Status(http.StatusOK).JSON(fiber.Map{
"status": "success",
"message": "Start scraping courses details from coursera.org...",
})
Expand Down
3 changes: 2 additions & 1 deletion geoip-maxmind/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"fmt"
"net"
"net/http"

"github.com/gofiber/fiber/v2"
"github.com/oschwald/maxminddb-golang"
Expand Down Expand Up @@ -42,7 +43,7 @@ func GeoIP(c *fiber.Ctx) error {
// Check IP address format
ip := net.ParseIP(ipAddr)
if ip == nil {
return c.Status(400).JSON(map[string]string{"status": "error", "message": "Invalid IP address"})
return c.Status(http.StatusBadRequest).JSON(map[string]string{"status": "error", "message": "Invalid IP address"})
}

// Perform lookup
Expand Down
2 changes: 1 addition & 1 deletion geoip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {
parsedIP := net.ParseIP(ip)
record, err := db.City(parsedIP)
if err != nil {
return c.Status(500).SendString(err.Error())
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
return c.JSON(record)
})
Expand Down
3 changes: 2 additions & 1 deletion gorm-mysql/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"net/http"

"gorm-mysql/database"
"gorm-mysql/routes"
Expand All @@ -28,7 +29,7 @@ func main() {
app.Use(cors.New())

app.Use(func(c *fiber.Ctx) error {
return c.SendStatus(404) // => 404 "Not Found"
return c.SendStatus(http.StatusNotFound) // => 404 "Not Found"
})

log.Fatal(app.Listen(":3000"))
Expand Down
15 changes: 8 additions & 7 deletions gorm-mysql/routes/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package routes

import (
"net/http"
"strconv"

"gorm-mysql/database"
Expand All @@ -18,20 +19,20 @@ func Hello(c *fiber.Ctx) error {
func AddBook(c *fiber.Ctx) error {
book := new(models.Book)
if err := c.BodyParser(book); err != nil {
return c.Status(400).JSON(err.Error())
return c.Status(http.StatusBadRequest).JSON(err.Error())
}

database.DBConn.Create(&book)

return c.Status(200).JSON(book)
return c.Status(http.StatusOK).JSON(book)
}

func GetBook(c *fiber.Ctx) error {
books := []models.Book{}

database.DBConn.First(&books, c.Params("id"))

return c.Status(200).JSON(books)
return c.Status(http.StatusOK).JSON(books)
}

// AllBooks
Expand All @@ -40,20 +41,20 @@ func AllBooks(c *fiber.Ctx) error {

database.DBConn.Find(&books)

return c.Status(200).JSON(books)
return c.Status(http.StatusOK).JSON(books)
}

// Update
func Update(c *fiber.Ctx) error {
book := new(models.Book)
if err := c.BodyParser(book); err != nil {
return c.Status(400).JSON(err.Error())
return c.Status(http.StatusBadRequest).JSON(err.Error())
}
id, _ := strconv.Atoi(c.Params("id"))

database.DBConn.Model(&models.Book{}).Where("id = ?", id).Update("title", book.Title)

return c.Status(200).JSON("updated")
return c.Status(http.StatusOK).JSON("updated")
}

// Delete
Expand All @@ -64,5 +65,5 @@ func Delete(c *fiber.Ctx) error {

database.DBConn.Where("id = ?", id).Delete(&book)

return c.Status(200).JSON("deleted")
return c.Status(http.StatusOK).JSON("deleted")
}
Loading