diff --git a/404-handler/README.md b/404-handler/README.md index 8f2ab3ba58..db3a965ddb 100644 --- a/404-handler/README.md +++ b/404-handler/README.md @@ -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 diff --git a/404-handler/main.go b/404-handler/main.go index c2d4da9061..accca78759 100644 --- a/404-handler/main.go +++ b/404-handler/main.go @@ -6,6 +6,7 @@ package main import ( "log" + "net/http" "github.com/gofiber/fiber/v2" ) @@ -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 diff --git a/auth-docker-postgres-jwt/handler/product.go b/auth-docker-postgres-jwt/handler/product.go index a8f06e8ff2..a1da1a8bb2 100644 --- a/auth-docker-postgres-jwt/handler/product.go +++ b/auth-docker-postgres-jwt/handler/product.go @@ -3,6 +3,7 @@ package handler import ( "app/database" "app/model" + "net/http" "github.com/gofiber/fiber/v2" ) @@ -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}) } @@ -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}) @@ -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}) diff --git a/auth-docker-postgres-jwt/handler/user.go b/auth-docker-postgres-jwt/handler/user.go index 3e150b05e7..12280c7f41 100644 --- a/auth-docker-postgres-jwt/handler/user.go +++ b/auth-docker-postgres-jwt/handler/user.go @@ -1,6 +1,7 @@ package handler import ( + "net/http" "strconv" "app/database" @@ -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}) } @@ -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() @@ -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{ @@ -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 @@ -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 diff --git a/auth-jwt/handler/product.go b/auth-jwt/handler/product.go index e3316dcd86..6a7602e6da 100644 --- a/auth-jwt/handler/product.go +++ b/auth-jwt/handler/product.go @@ -3,6 +3,7 @@ package handler import ( "api-fiber-gorm/database" "api-fiber-gorm/model" + "net/http" "github.com/gofiber/fiber/v2" ) @@ -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}) } @@ -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}) @@ -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}) diff --git a/auth-jwt/handler/user.go b/auth-jwt/handler/user.go index feb04fa625..3432262bec 100644 --- a/auth-jwt/handler/user.go +++ b/auth-jwt/handler/user.go @@ -1,6 +1,7 @@ package handler import ( + "net/http" "strconv" "api-fiber-gorm/database" @@ -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}) } @@ -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{ @@ -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 @@ -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 diff --git a/colly-gorm/app/cmd/api/main.go b/colly-gorm/app/cmd/api/main.go index 5d01c45f6a..87d31323c1 100644 --- a/colly-gorm/app/cmd/api/main.go +++ b/colly-gorm/app/cmd/api/main.go @@ -2,6 +2,7 @@ package main import ( "log" + "net/http" "fiber-colly-gorm/internals/consts" "fiber-colly-gorm/internals/services/database" @@ -34,7 +35,7 @@ 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", }) @@ -42,7 +43,7 @@ func main() { 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 ...", }) @@ -50,7 +51,7 @@ func main() { 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...", }) diff --git a/geoip-maxmind/handlers/handlers.go b/geoip-maxmind/handlers/handlers.go index 7b56c163ef..acb3d5a142 100644 --- a/geoip-maxmind/handlers/handlers.go +++ b/geoip-maxmind/handlers/handlers.go @@ -3,6 +3,7 @@ package handlers import ( "fmt" "net" + "net/http" "github.com/gofiber/fiber/v2" "github.com/oschwald/maxminddb-golang" @@ -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 diff --git a/geoip/README.md b/geoip/README.md index f4bf08b9ea..a9666cbfd4 100644 --- a/geoip/README.md +++ b/geoip/README.md @@ -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) }) diff --git a/gorm-mysql/app.go b/gorm-mysql/app.go index 91a1ec085a..66f70953da 100644 --- a/gorm-mysql/app.go +++ b/gorm-mysql/app.go @@ -2,6 +2,7 @@ package main import ( "log" + "net/http" "gorm-mysql/database" "gorm-mysql/routes" @@ -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")) diff --git a/gorm-mysql/routes/routes.go b/gorm-mysql/routes/routes.go index ea87b1ecae..62de0d2b2f 100644 --- a/gorm-mysql/routes/routes.go +++ b/gorm-mysql/routes/routes.go @@ -1,6 +1,7 @@ package routes import ( + "net/http" "strconv" "gorm-mysql/database" @@ -18,12 +19,12 @@ 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 { @@ -31,7 +32,7 @@ func GetBook(c *fiber.Ctx) error { database.DBConn.First(&books, c.Params("id")) - return c.Status(200).JSON(books) + return c.Status(http.StatusOK).JSON(books) } // AllBooks @@ -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 @@ -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") } diff --git a/gorm-postgres/README.md b/gorm-postgres/README.md index 4bf5da9278..458d8eaf0e 100644 --- a/gorm-postgres/README.md +++ b/gorm-postgres/README.md @@ -56,10 +56,11 @@ Here is an example `main.go` file for the Fiber application with GORM and Postgr package main import ( - "log" - "github.com/gofiber/fiber/v2" - "gorm.io/driver/postgres" - "gorm.io/gorm" + "log" + "net/http" + "github.com/gofiber/fiber/v2" + "gorm.io/driver/postgres" + "gorm.io/gorm" ) type User struct { @@ -86,7 +87,7 @@ func main() { app.Post("/users", func(c *fiber.Ctx) error { user := new(User) if err := c.BodyParser(user); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } db.Create(user) return c.JSON(user) diff --git a/gorm-postgres/app.go b/gorm-postgres/app.go index 812a2db613..52b4c74dc5 100644 --- a/gorm-postgres/app.go +++ b/gorm-postgres/app.go @@ -2,6 +2,7 @@ package main import ( "log" + "net/http" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" @@ -27,7 +28,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")) diff --git a/gorm-postgres/routes/routes.go b/gorm-postgres/routes/routes.go index 658567b380..83645145ac 100644 --- a/gorm-postgres/routes/routes.go +++ b/gorm-postgres/routes/routes.go @@ -1,6 +1,8 @@ package routes import ( + "net/http" + "github.com/gofiber/fiber/v2" "github.com/zeimedee/go-postgres/database" "github.com/zeimedee/go-postgres/models" @@ -15,12 +17,12 @@ 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.DB.Db.Create(&book) - return c.Status(200).JSON(book) + return c.Status(http.StatusOK).JSON(book) } // AllBooks @@ -28,7 +30,7 @@ func AllBooks(c *fiber.Ctx) error { books := []models.Book{} database.DB.Db.Find(&books) - return c.Status(200).JSON(books) + return c.Status(http.StatusOK).JSON(books) } // Book @@ -36,10 +38,10 @@ 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()) + return c.Status(http.StatusBadRequest).JSON(err.Error()) } database.DB.Db.Where("title = ?", title.Title).Find(&book) - return c.Status(200).JSON(book) + return c.Status(http.StatusOK).JSON(book) } // Update @@ -47,12 +49,12 @@ 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()) + return c.Status(http.StatusBadRequest).JSON(err.Error()) } database.DB.Db.Model(&book).Where("title = ?", title.Title).Update("author", title.Author) - return c.Status(200).JSON("updated") + return c.Status(http.StatusOK).JSON("updated") } // Delete @@ -60,9 +62,9 @@ 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()) + return c.Status(http.StatusBadRequest).JSON(err.Error()) } database.DB.Db.Where("title = ?", title.Title).Delete(&book) - return c.Status(200).JSON("deleted") + return c.Status(http.StatusOK).JSON("deleted") } diff --git a/gorm/book/book.go b/gorm/book/book.go index c059f6508c..2d27098b81 100644 --- a/gorm/book/book.go +++ b/gorm/book/book.go @@ -2,6 +2,7 @@ package book import ( "fiber-gorm/database" + "net/http" "github.com/gofiber/fiber/v2" "gorm.io/gorm" @@ -46,7 +47,7 @@ func DeleteBook(c *fiber.Ctx) error { var book Book db.First(&book, id) if book.Title == "" { - return c.Status(500).SendString("No Book Found with ID") + return c.Status(http.StatusNotFound).SendString("No Book Found with ID") } db.Delete(&book) return c.SendString("Book Successfully deleted") diff --git a/jwt/handler/product.go b/jwt/handler/product.go index e3316dcd86..6a7602e6da 100644 --- a/jwt/handler/product.go +++ b/jwt/handler/product.go @@ -3,6 +3,7 @@ package handler import ( "api-fiber-gorm/database" "api-fiber-gorm/model" + "net/http" "github.com/gofiber/fiber/v2" ) @@ -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}) } @@ -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}) @@ -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}) diff --git a/mongodb/main.go b/mongodb/main.go index 9e38a40202..df65f3bed8 100644 --- a/mongodb/main.go +++ b/mongodb/main.go @@ -6,6 +6,7 @@ package main import ( "context" "log" + "net/http" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" @@ -81,14 +82,14 @@ func main() { query := bson.D{{}} cursor, err := mg.Db.Collection("employees").Find(c.Context(), query) if err != nil { - return c.Status(500).SendString(err.Error()) + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } var employees []Employee = make([]Employee, 0) // iterate the cursor and decode each item into an Employee if err := cursor.All(c.Context(), &employees); err != nil { - return c.Status(500).SendString(err.Error()) + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } // return employees list in JSON format return c.JSON(employees) @@ -102,7 +103,7 @@ func main() { _id, err := primitive.ObjectIDFromHex(params) if err != nil { - return c.Status(500).SendString(err.Error()) + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } filter := bson.D{{Key: "_id", Value: _id}} @@ -110,7 +111,7 @@ func main() { var result Employee if err := mg.Db.Collection("employees").FindOne(c.Context(), filter).Decode(&result); err != nil { - return c.Status(500).SendString("Something went wrong.") + return c.Status(http.StatusInternalServerError).SendString("Something went wrong.") } return c.Status(fiber.StatusOK).JSON(result) @@ -125,7 +126,7 @@ func main() { employee := new(Employee) // Parse body into struct if err := c.BodyParser(employee); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } // force MongoDB to always set its own generated ObjectIDs @@ -134,7 +135,7 @@ func main() { // insert the record insertionResult, err := collection.InsertOne(c.Context(), employee) if err != nil { - return c.Status(500).SendString(err.Error()) + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } // get the just inserted record in order to return it as response @@ -146,7 +147,7 @@ func main() { createdRecord.Decode(createdEmployee) // return the created Employee in JSON format - return c.Status(201).JSON(createdEmployee) + return c.Status(http.StatusCreated).JSON(createdEmployee) }) // Update an employee record in MongoDB @@ -156,13 +157,13 @@ func main() { employeeID, err := primitive.ObjectIDFromHex(idParam) // the provided ID might be invalid ObjectID if err != nil { - return c.SendStatus(400) + return c.SendStatus(http.StatusBadRequest) } employee := new(Employee) // Parse body into struct if err := c.BodyParser(employee); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } // Find the employee and update its data @@ -181,14 +182,14 @@ func main() { if err != nil { // ErrNoDocuments means that the filter did not match any documents in the collection if err == mongo.ErrNoDocuments { - return c.SendStatus(404) + return c.SendStatus(http.StatusNotFound) } - return c.SendStatus(500) + return c.SendStatus(http.StatusInternalServerError) } // return the updated employee employee.ID = idParam - return c.Status(200).JSON(employee) + return c.Status(http.StatusOK).JSON(employee) }) // Delete an employee from MongoDB @@ -199,23 +200,23 @@ func main() { ) // the provided ID might be invalid ObjectID if err != nil { - return c.SendStatus(400) + return c.SendStatus(http.StatusBadRequest) } // find and delete the employee with the given ID query := bson.D{{Key: "_id", Value: employeeID}} result, err := mg.Db.Collection("employees").DeleteOne(c.Context(), &query) if err != nil { - return c.SendStatus(500) + return c.SendStatus(http.StatusInternalServerError) } // the employee might not exist if result.DeletedCount < 1 { - return c.SendStatus(404) + return c.SendStatus(http.StatusNotFound) } // the record was deleted - return c.SendStatus(204) + return c.SendStatus(http.StatusNoContent) }) log.Fatal(app.Listen(":3000")) diff --git a/mysql/main.go b/mysql/main.go index 1dfbf9dd69..0bb46f120b 100644 --- a/mysql/main.go +++ b/mysql/main.go @@ -7,6 +7,7 @@ import ( "database/sql" "fmt" "log" + "net/http" _ "github.com/go-sql-driver/mysql" "github.com/gofiber/fiber/v2" @@ -65,7 +66,7 @@ func main() { // Get Employee list from database rows, err := db.Query("SELECT id, name, salary, age FROM employees order by id") if err != nil { - return c.Status(500).SendString(err.Error()) + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } defer rows.Close() result := Employees{} @@ -90,7 +91,7 @@ func main() { // Parse body into struct if err := c.BodyParser(u); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } // Insert Employee into database @@ -113,7 +114,7 @@ func main() { // Parse body into struct if err := c.BodyParser(u); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } // Update Employee record in database @@ -126,7 +127,7 @@ func main() { log.Println(res) // Return Employee in JSON format - return c.Status(201).JSON(u) + return c.Status(http.StatusCreated).JSON(u) }) // Delete record from MySQL @@ -136,7 +137,7 @@ func main() { // Parse body into struct if err := c.BodyParser(u); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } // Delete Employee from database diff --git a/neo4j/README.md b/neo4j/README.md index 52979e247e..7c339143e9 100644 --- a/neo4j/README.md +++ b/neo4j/README.md @@ -82,7 +82,7 @@ func main() { return c.SendString(result.Record().Values[0].(string)) } - return c.SendStatus(500) + return c.SendStatus(http.StatusInternalServerError) }) // Start server diff --git a/neo4j/main.go b/neo4j/main.go index bc41e3c0c0..9a699ff3b0 100644 --- a/neo4j/main.go +++ b/neo4j/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "net/http" "strconv" "github.com/gofiber/fiber/v2" @@ -52,7 +53,7 @@ func main() { app.Post("/movie", func(c *fiber.Ctx) error { movie := new(Movie) if err := c.BodyParser(movie); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } query := fmt.Sprintf(`CREATE (n:Movie {title:'%s', tagline:'%s', released:'%d', director:'%s' })`, diff --git a/oauth2-google/handler/handler.go b/oauth2-google/handler/handler.go index 6408c5567d..7889d83ba0 100644 --- a/oauth2-google/handler/handler.go +++ b/oauth2-google/handler/handler.go @@ -2,6 +2,7 @@ package handler import ( "fiber-oauth-google/auth" + "net/http" "github.com/gofiber/fiber/v2" ) @@ -20,5 +21,5 @@ func Callback(c *fiber.Ctx) error { panic(error) } email := auth.GetEmail(token.AccessToken) - return c.Status(200).JSON(fiber.Map{"email": email, "login": true}) + return c.Status(http.StatusOK).JSON(fiber.Map{"email": email, "login": true}) } diff --git a/optional-parameter/main.go b/optional-parameter/main.go index 73da55a45a..bd834dd7cc 100644 --- a/optional-parameter/main.go +++ b/optional-parameter/main.go @@ -6,6 +6,7 @@ package main import ( "log" + "net/http" "strconv" "github.com/gofiber/fiber/v2" @@ -22,7 +23,7 @@ func main() { app.Get("/:id?", func(c *fiber.Ctx) error { id, err := strconv.Atoi(c.Params("id")) // transform id to array index if err != nil || id < 0 || id >= len(users) { - return c.SendStatus(404) // invalid parameter returns 404 + return c.SendStatus(http.StatusNotFound) // invalid parameter returns 404 } return c.SendString("Hello, " + users[id] + "!") // custom hello message to user with the id }) diff --git a/postgresql/main.go b/postgresql/main.go index c23dfd4009..3c5ed3ae45 100644 --- a/postgresql/main.go +++ b/postgresql/main.go @@ -7,6 +7,7 @@ import ( "database/sql" "fmt" "log" + "net/http" "github.com/gofiber/fiber/v2" _ "github.com/lib/pq" @@ -64,7 +65,7 @@ func main() { // Select all Employee(s) from database rows, err := db.Query("SELECT id, name, salary, age FROM employees order by id") if err != nil { - return c.Status(500).SendString(err.Error()) + return c.Status(http.StatusInternalServerError).SendString(err.Error()) } defer rows.Close() result := Employees{} @@ -89,7 +90,7 @@ func main() { // Parse body into struct if err := c.BodyParser(u); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } // Insert Employee into database @@ -112,7 +113,7 @@ func main() { // Parse body into struct if err := c.BodyParser(u); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } // Update Employee into database @@ -125,7 +126,7 @@ func main() { log.Println(res) // Return Employee in JSON format - return c.Status(201).JSON(u) + return c.Status(http.StatusCreated).JSON(u) }) // Delete record from postgreSQL @@ -135,7 +136,7 @@ func main() { // Parse body into struct if err := c.BodyParser(u); err != nil { - return c.Status(400).SendString(err.Error()) + return c.Status(http.StatusBadRequest).SendString(err.Error()) } // Delete Employee from database diff --git a/sqlboiler/api/controller/author_controller.go b/sqlboiler/api/controller/author_controller.go index e29fe678bf..626ba8f6c2 100644 --- a/sqlboiler/api/controller/author_controller.go +++ b/sqlboiler/api/controller/author_controller.go @@ -2,6 +2,7 @@ package controller import ( "context" + "net/http" "strconv" "fiber-sqlboiler/database" @@ -14,71 +15,71 @@ import ( func GetAuthors(c *fiber.Ctx) error { authors, err := models.Authors().All(context.Background(), database.DB) if err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } - return c.Status(200).JSON(authors) + return c.Status(http.StatusOK).JSON(authors) } func GetAuthor(c *fiber.Ctx) error { id := c.Params("id") authorId, err := strconv.Atoi(id) if err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } author, err := models.FindAuthor(context.Background(), database.DB, authorId) if err != nil { - return c.Status(404).JSON(err.Error()) + return c.Status(http.StatusNotFound).JSON(err.Error()) } - return c.Status(200).JSON(author) + return c.Status(http.StatusOK).JSON(author) } func NewAuthor(c *fiber.Ctx) error { author := models.Author{} if err := c.BodyParser(&author); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } if err := author.Insert(context.Background(), database.DB, boil.Infer()); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } - return c.Status(200).JSON(author) + return c.Status(http.StatusOK).JSON(author) } func DeleteAuthor(c *fiber.Ctx) error { id := c.Params("id") authorId, err := strconv.Atoi(id) if err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } author, err := models.FindAuthor(context.Background(), database.DB, authorId) if err != nil { - return c.Status(404).JSON(err.Error()) + return c.Status(http.StatusNotFound).JSON(err.Error()) } if _, err := author.Delete(context.Background(), database.DB); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } - return c.SendStatus(200) + return c.SendStatus(http.StatusOK) } func UpdateAuthor(c *fiber.Ctx) error { id := c.Params("id") authorId, err := strconv.Atoi(id) if err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } newAuthor := models.Author{} if err := c.BodyParser(&newAuthor); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } author, err := models.FindAuthor(context.Background(), database.DB, authorId) if err != nil { - return c.Status(404).JSON(err.Error()) + return c.Status(http.StatusNotFound).JSON(err.Error()) } author.Name = newAuthor.Name if _, err := author.Update(context.Background(), database.DB, boil.Infer()); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } - return c.Status(200).JSON(author) + return c.Status(http.StatusOK).JSON(author) } diff --git a/sqlboiler/api/controller/post_controller.go b/sqlboiler/api/controller/post_controller.go index e6077eefe9..2d3c904752 100644 --- a/sqlboiler/api/controller/post_controller.go +++ b/sqlboiler/api/controller/post_controller.go @@ -2,6 +2,7 @@ package controller import ( "context" + "net/http" "strconv" "fiber-sqlboiler/database" @@ -14,72 +15,72 @@ import ( func GetPosts(c *fiber.Ctx) error { posts, err := models.Posts().All(context.Background(), database.DB) if err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } - return c.Status(200).JSON(posts) + return c.Status(http.StatusOK).JSON(posts) } func GetPost(c *fiber.Ctx) error { id := c.Params("id") postId, err := strconv.Atoi(id) if err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusBadRequest).JSON(err.Error()) } post, err := models.FindPost(context.Background(), database.DB, postId) if err != nil { - return c.Status(404).JSON(err.Error()) + return c.Status(http.StatusNotFound).JSON(err.Error()) } - return c.Status(200).JSON(post) + return c.Status(http.StatusOK).JSON(post) } func NewPost(c *fiber.Ctx) error { post := models.Post{} if err := c.BodyParser(&post); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } if err := post.Insert(context.Background(), database.DB, boil.Infer()); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } - return c.Status(200).JSON(post) + return c.Status(http.StatusOK).JSON(post) } func DeletePost(c *fiber.Ctx) error { id := c.Params("id") postId, err := strconv.Atoi(id) if err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } post, err := models.FindPost(context.Background(), database.DB, postId) if err != nil { - return c.Status(404).JSON(err.Error()) + return c.Status(http.StatusNotFound).JSON(err.Error()) } if _, err := post.Delete(context.Background(), database.DB); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusBadRequest).JSON(err.Error()) } - return c.SendStatus(200) + return c.SendStatus(http.StatusOK) } func UpdatePost(c *fiber.Ctx) error { id := c.Params("id") postId, err := strconv.Atoi(id) if err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusInternalServerError).JSON(err.Error()) } newPost := models.Post{} if err := c.BodyParser(&newPost); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusBadRequest).JSON(err.Error()) } post, err := models.FindPost(context.Background(), database.DB, postId) if err != nil { - return c.Status(404).JSON(err.Error()) + return c.Status(http.StatusNotFound).JSON(err.Error()) } post.Title = newPost.Title post.Content = newPost.Content if _, err := post.Update(context.Background(), database.DB, boil.Infer()); err != nil { - return c.Status(500).JSON(err.Error()) + return c.Status(http.StatusBadRequest).JSON(err.Error()) } - return c.Status(200).JSON(post) + return c.Status(http.StatusOK).JSON(post) } diff --git a/template-asset-bundling/handlers/handlers.go b/template-asset-bundling/handlers/handlers.go index 129f587882..3d334ee55c 100644 --- a/template-asset-bundling/handlers/handlers.go +++ b/template-asset-bundling/handlers/handlers.go @@ -1,6 +1,8 @@ package handlers import ( + "net/http" + "github.com/gofiber/fiber/v2" ) @@ -18,5 +20,5 @@ func About(c *fiber.Ctx) error { // NoutFound renders the 404 view func NotFound(c *fiber.Ctx) error { - return c.Status(404).Render("404", nil) + return c.Status(http.StatusNotFound).Render("404", nil) } diff --git a/websocket/README.md b/websocket/README.md index 4cd23d0074..c02d6be2bb 100644 --- a/websocket/README.md +++ b/websocket/README.md @@ -80,7 +80,7 @@ func main() { c.Locals("Host", "Localhost:3000") return c.Next() } - return c.Status(403).SendString("Request origin not allowed") + return c.Status(http.StatusForbidden).SendString("Request origin not allowed") }) // Upgraded websocket request diff --git a/websocket/main.go b/websocket/main.go index 8e8626bc87..137c10b445 100644 --- a/websocket/main.go +++ b/websocket/main.go @@ -7,6 +7,7 @@ package main import ( "fmt" "log" + "net/http" "github.com/gofiber/contrib/websocket" "github.com/gofiber/fiber/v2" @@ -21,7 +22,7 @@ func main() { c.Locals("Host", "Localhost:3000") return c.Next() } - return c.Status(403).SendString("Request origin not allowed") + return c.Status(http.StatusForbidden).SendString("Request origin not allowed") }) // Upgraded websocket request