DEV Community

Cover image for Build a REST API with Golang and MongoDB - Fiber Version
Demola Malomo for Hackmamba

Posted on • Edited on

Build a REST API with Golang and MongoDB - Fiber Version

Representational state transfer (REST) is an architectural pattern that guides an Application programming interface(API) design and development. REST APIs have become the standard of communication between the server part of the product and its client to increase performance, scalability, simplicity, modifiability, visibility, portability, and reliability.

This post will discuss building a user management application with Golang using the Fiber framework and MongoDB. At the end of this tutorial, we will learn how to structure a Fiber application, build a REST API and persist our data using MongoDB.

Fiber is an Express inspired HTTP web framework written in Golang with performance and zero memory allocation support. Fiber is built on top of Fasthttp, an HTTP engine written in Golang.

MongoDB is a document-based database management program used as an alternative to relational databases. MongoDB supports working with large sets of distributed data with options to store or retrieve information seamlessly.

You can find the complete source code in this repository.

Prerequisites

The following steps in this post require Golang experience. Experience with MongoDB isn’t a requirement, but it’s nice to have.

We will also be needing the following:

Let’s code

Getting Started

To get started, we need to navigate to the desired directory and run the command below in our terminal

mkdir fiber-mongo-api && cd fiber-mongo-api
Enter fullscreen mode Exit fullscreen mode

This command creates a fiber-mongo-api folder and navigates into the project directory.

Next, we need to initialize a Go module to manage project dependencies by running the command below:

go mod init fiber-mongo-api
Enter fullscreen mode Exit fullscreen mode

This command will create a go.mod file for tracking project dependencies.

We proceed to install the required dependencies with:

go get -u github.com/gofiber/fiber/v2 go.mongodb.org/mongo-driver/mongo github.com/joho/godotenv github.com/go-playground/validator/v10
Enter fullscreen mode Exit fullscreen mode

github.com/gofiber/fiber/v2 is a framework for building web application.

go.mongodb.org/mongo-driver/mongo is a driver for connecting to MongoDB.

github.com/joho/godotenv is a library for managing environment variable.

github.com/go-playground/validator/v10 is a library for validating structs and fields.

After installing the required dependencies, we might get github.com/klauspost/compress is not in your go.mod filego mod tidy error in go.mod file. To fix this, we need to manually install the required package with

go get github.com/klauspost/compress
Enter fullscreen mode Exit fullscreen mode

Application Entry Point

With the project dependencies installed, we need to create main.go file in the root directory and add the snippet below:



package main

import (
    "github.com/gofiber/fiber/v2"
)

func main() {
    app := fiber.New()

    app.Get("/", func(c *fiber.Ctx) error {
        return c.JSON(&fiber.Map{"data": "Hello from Fiber & mongoDB"})
    })

    app.Listen(":6000")
}


Enter fullscreen mode Exit fullscreen mode

The snippet above does the following:

  • Import the required dependencies.
  • Initialize a Fiber application using the New function.
  • Use the Get function to route to / path and an handler function that returns a JSON of Hello from Fiber & mongoDB. fiber.Map is a shortcut for map[string]interface{}, useful for JSON returns.
  • Set the application to listen on port 6000.

Next, we can test our application by starting the development server by running the command below in our terminal.

go run main.go
Enter fullscreen mode Exit fullscreen mode

Testing the app

Modularization in Golang

It is essential to have a good folder structure for our project. Good project structure simplifies how we work with dependencies in our application and makes it easier for us and others to read our codebase.
To do this, we need to create configs, controllers, models, responses and routes folder in our project directory.

Updated project folder structure

PS: The go.sum file contains all the dependency checksums, and is managed by the go tools. We don’t have to worry about it.

configs is for modularizing project configuration files

controllers is for modularizing application logics.

models is for modularizing data and database logics.

responses is for modularizing files describing the response we want our API to give. This will become clearer later on.

routes is for modularizing URL pattern and handler information.

Setting up MongoDB

With that done, we need to log in or sign up into our MongoDB account. Click the project dropdown menu and click on the New Project button.

New Project

Enter the golang-api as the project name, click Next, and click Create Project..

enter project name
Create Project

Click on Build a Database

Select Shared as the type of database.

Shared highlighted in red

Click on Create to setup a cluster. This might take sometime to setup.

Creating a cluster

Next, we need to create a user to access the database externally by inputting the Username, Password and then clicking on Create User. We also need to add our IP address to safely connect to the database by clicking on the Add My Current IP Address button. Then click on Finish and Close to save changes.

Create user
Add IP

On saving the changes, we should see a Database Deployments screen, as shown below:

Database Screen

Connecting our application to MongoDB

With the configuration done, we need to connect our application with the database created. To do this, click on the Connect button

Connect to database

Click on Connect your application, change the Driver to Go and the Version as shown below. Then click on the copy icon to copy the connection string.

connect application
Copy connection string

Setup Environment Variable
Next, we must modify the copied connection string with the user's password we created earlier and change the database name. To do this, first, we need to create a .env file in the root directory, and in this file, add the snippet below:

MONGOURI=mongodb+srv://<YOUR USERNAME HERE>:<YOUR PASSWORD HERE>@cluster0.e5akf.mongodb.net/myFirstDatabese?retryWrites=true&w=majority
Enter fullscreen mode Exit fullscreen mode

Sample of a properly filled connection string below:

MONGOURI=mongodb+srv://malomz:malomzPassword@cluster0.e5akf.mongodb.net/golangDB?retryWrites=true&w=majority
Enter fullscreen mode Exit fullscreen mode

Updated folder structure with .env file

Load Environment Variable
With that done, we need to create a helper function to load the environment variable using the github.com/joho/godotenv library we installed earlier. To do this, we need to navigate to the configs folder and in this folder, create an env.go file and add the snippet below:



package configs

import (
    "log"
    "os"
    "github.com/joho/godotenv"
)

func EnvMongoURI() string {
    err := godotenv.Load()
    if err != nil {
        log.Fatal("Error loading .env file")
    }

    return os.Getenv("MONGOURI")
}


Enter fullscreen mode Exit fullscreen mode

The snippet above does the following:

  • Import the required dependencies.
  • Create an EnvMongoURI function that checks if the environment variable is correctly loaded and returns the environment variable.

Connecting to MongoDB
To connect to the MongoDB database from our application, first we need to navigate to the configs folder and in this folder, create a setup.go file and add the snippet below:



package configs

import (
    "context"
    "fmt"
    "log"
    "time"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

func ConnectDB() *mongo.Client  {
    client, err := mongo.NewClient(options.Client().ApplyURI(EnvMongoURI()))
    if err != nil {
        log.Fatal(err)
    }

    ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
    err = client.Connect(ctx)
    if err != nil {
        log.Fatal(err)
    }

    //ping the database
    err = client.Ping(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Connected to MongoDB")
    return client
}

//Client instance
var DB *mongo.Client = ConnectDB()

//getting database collections
func GetCollection(client *mongo.Client, collectionName string) *mongo.Collection {
    collection := client.Database("golangAPI").Collection(collectionName)
    return collection
}


Enter fullscreen mode Exit fullscreen mode

The snippet above does the following:

  • Import the required dependencies.
  • Create a ConnectDB function that first configures the client to use the correct URI and check for errors. Secondly, we defined a timeout of 10 seconds we wanted to use when trying to connect. Thirdly, check if there is an error while connecting to the database and cancel the connection if the connecting period exceeds 10 seconds. Finally, we pinged the database to test our connection and returned the client instance.
  • Create a DB variable instance of the ConnectDB. This will come in handy when creating collections.
  • Create a GetCollection function to retrieve and create collections on the database.

Next, we need to connect to the database when our application startup. To do this, we need to modify main.go as shown below:



package main

import (
    "fiber-mongo-api/configs" //add this
    "github.com/gofiber/fiber/v2" 
)

func main() {
    app := fiber.New()

    //run database
    configs.ConnectDB()

    app.Listen(":6000")
}


Enter fullscreen mode Exit fullscreen mode

Setup API Route Handler and Response Type

Route Handler
With that done, we need to create a user_route.go file inside the routes folder to manage all the user-related routes in our application, as shown below:



package routes

import "github.com/gofiber/fiber/v2"

func UserRoute(app *fiber.App) {
    //All routes related to users comes here
}


Enter fullscreen mode Exit fullscreen mode

Next, we need to attach the newly created route to the http.Server in main.go by modifying it as shown below:



package main

import (
    "fiber-mongo-api/configs"
    "fiber-mongo-api/routes" //add this
    "github.com/gofiber/fiber/v2"
)

func main() {
    app := fiber.New()

    //run database
    configs.ConnectDB()

    //routes
    routes.UserRoute(app) //add this

    app.Listen(":6000")
}


Enter fullscreen mode Exit fullscreen mode

Response Type
Next, we need to create a reusable struct to describe our API’s response. To do this, navigate to the responses folder and in this folder, create a user_response.go file and add the snippet below:



package responses

import "github.com/gofiber/fiber/v2"

type UserResponse struct {
    Status  int        `json:"status"`
    Message string     `json:"message"`
    Data    *fiber.Map `json:"data"`
}


Enter fullscreen mode Exit fullscreen mode

The snippet above creates a UserResponse struct with Status, Message, and Data property to represent the API response type.

PS: json:"status", json:"message", and json:"data" are known as struct tags. Struct tags allow us to attach meta-information to corresponding struct properties. In other words, we use them to reformat the JSON response returned by the API.

Finally, Creating REST API’s

Next, we need a model to represent our application data. To do this, we need to navigate to the models folder, and in this folder, create a user_model.go file and add the snippet below:



package models

import "go.mongodb.org/mongo-driver/bson/primitive"

type User struct {
    Id       primitive.ObjectID `json:"id,omitempty"`
    Name     string             `json:"name,omitempty" validate:"required"`
    Location string             `json:"location,omitempty" validate:"required"`
    Title    string             `json:"title,omitempty" validate:"required"`
}


Enter fullscreen mode Exit fullscreen mode

The snippet above does the following:

  • Import the required dependencies.
  • Create a User struct with required properties. We added omitempty and validate:"required" to the struct tag to tell Fiber to ignore empty fields and make the field required, respectively.

Create a User Endpoint
With the model setup, we can now create a function to create a user. To do this, we need to navigate to the controllers folder, and in this folder, create a user_controller.go file and add the snippet below:



package controllers

import (
    "context"
    "fiber-mongo-api/configs"
    "fiber-mongo-api/models"
    "fiber-mongo-api/responses"
    "net/http"
    "time"

    "github.com/go-playground/validator/v10"
    "github.com/gofiber/fiber/v2"
    "go.mongodb.org/mongo-driver/bson/primitive"
    "go.mongodb.org/mongo-driver/mongo"
)

var userCollection *mongo.Collection = configs.GetCollection(configs.DB, "users")
var validate = validator.New()

func CreateUser(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    var user models.User
    defer cancel()

    //validate the request body
    if err := c.BodyParser(&user); err != nil {
        return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    //use the validator library to validate required fields
    if validationErr := validate.Struct(&user); validationErr != nil {
        return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
    }

    newUser := models.User{
        Id:       primitive.NewObjectID(),
        Name:     user.Name,
        Location: user.Location,
        Title:    user.Title,
    }

    result, err := userCollection.InsertOne(ctx, newUser)
    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    return c.Status(http.StatusCreated).JSON(responses.UserResponse{Status: http.StatusCreated, Message: "success", Data: &fiber.Map{"data": result}})
}


Enter fullscreen mode Exit fullscreen mode

The snippet above does the following:

  • Import the required dependencies.
  • Create userCollection and validate variables to create a collection and validate models using the github.com/go-playground/validator/v10 library we installed earlier on, respectively.
  • Create a CreateUser function that returns an error. Inside the function, we first defined a timeout of 10 seconds when inserting user into the document, validating both the request body and required field using the validator library. We returned the appropriate message and status code using the UserResponse struct we created earlier. Secondly, we created a newUser variable, inserted it using the userCollection.InsertOne function and check for errors if there are any. Finally, we returned the correct response if the insert was successful.

Next, we need to update user_routes.go with the route API URL and corresponding controller as shown below:



package routes

import (
    "fiber-mongo-api/controllers" //add this

    "github.com/gofiber/fiber/v2"
)

func UserRoute(app *fiber.App) {
    app.Post("/user", controllers.CreateUser) //add this
}


Enter fullscreen mode Exit fullscreen mode

Get a User Endpoint
To get the details of a user, we need to modify user_controller.go as shown below:



package controllers

import (
    //other import goes here    
    "go.mongodb.org/mongo-driver/bson" //add this
)

var userCollection *mongo.Collection = configs.GetCollection(configs.DB, "users")
var validate = validator.New()

func CreateUser(c *fiber.Ctx) error {
    //create user code goes here
}

func GetAUser(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    userId := c.Params("userId")
    var user models.User
    defer cancel()

    objId, _ := primitive.ObjectIDFromHex(userId)

    err := userCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&user)
    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    return c.Status(http.StatusOK).JSON(responses.UserResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": user}})
}


Enter fullscreen mode Exit fullscreen mode

The snippet above does the following:

  • Import the required dependencies.
  • Create a GetAUser function that returns an error. Inside the function, we first defined a timeout of 10 seconds when finding a user in the document, a userId variable to get the user’s id from the URL parameter and a user variable. We converted the userId from a string to a primitive.ObjectID type, a BSON type MongoDB uses. Secondly, we searched for the user using the userCollection.FindOne, pass the objId as a filter and use the Decode attribute method to get the corresponding object. Finally, we returned the decoded response.

Next, we need to update user_routes.go with the route API URL and corresponding controller as shown below:



package routes

import (
    //import goes here
)

func UserRoute(app *fiber.App) {
    //other routes goes here
    app.Get("/user/:userId", controllers.GetAUser) //add this
}


Enter fullscreen mode Exit fullscreen mode

PS: We also passed a userId as a parameter to the URL path. The specified parameter must match the one we specified in the controller.

Edit a User Endpoint
To edit a user, we need to modify user_controller.go as shown below:



package controllers

import (
    //other import goes here
)

var userCollection *mongo.Collection = configs.GetCollection(configs.DB, "users")
var validate = validator.New()

func CreateUser(c *fiber.Ctx) error {
    //create user code goes here
}

func GetAUser(c *fiber.Ctx) error {
    //get a user code goes here
}

func EditAUser(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    userId := c.Params("userId")
    var user models.User
    defer cancel()

    objId, _ := primitive.ObjectIDFromHex(userId)

    //validate the request body
    if err := c.BodyParser(&user); err != nil {
        return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    //use the validator library to validate required fields
    if validationErr := validate.Struct(&user); validationErr != nil {
        return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
    }

    update := bson.M{"name": user.Name, "location": user.Location, "title": user.Title}

    result, err := userCollection.UpdateOne(ctx, bson.M{"id": objId}, bson.M{"$set": update})

    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }
    //get updated user details
    var updatedUser models.User
    if result.MatchedCount == 1 {
        err := userCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&updatedUser)

        if err != nil {
            return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
        }
    }

    return c.Status(http.StatusOK).JSON(responses.UserResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": updatedUser}})
}


Enter fullscreen mode Exit fullscreen mode

The EditAUser function above does the same thing as the CreateUser function. However, we included an update variable to get updated fields and updated the collection using the userCollection.UpdateOne. Lastly, we searched for the updated user’s details and returned the decoded response.

Next, we need to update user_routes.go with the route API URL and corresponding controller as shown below:



package routes

import (
    //import goes here
)

func UserRoute(app *fiber.App) {
    //other routes goes here 
    app.Put("/user/:userId", controllers.EditAUser) //add this
}


Enter fullscreen mode Exit fullscreen mode

Delete a User Endpoint
To delete a user, we need to modify user_controller.go as shown below:



package controllers

import (
    //other import goes here
)

var userCollection *mongo.Collection = configs.GetCollection(configs.DB, "users")
var validate = validator.New()

func CreateUser(c *fiber.Ctx) error {
    //create user code goes here
}

func GetAUser(c *fiber.Ctx) error {
    //get a user code goes here
}

func EditAUser(c *fiber.Ctx) error {
    //edit a user code goes here
}

func DeleteAUser(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    userId := c.Params("userId")
    defer cancel()

    objId, _ := primitive.ObjectIDFromHex(userId)

    result, err := userCollection.DeleteOne(ctx, bson.M{"id": objId})
    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    if result.DeletedCount < 1 {
        return c.Status(http.StatusNotFound).JSON(
            responses.UserResponse{Status: http.StatusNotFound, Message: "error", Data: &fiber.Map{"data": "User with specified ID not found!"}},
        )
    }

    return c.Status(http.StatusOK).JSON(
        responses.UserResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": "User successfully deleted!"}},
    )
}


Enter fullscreen mode Exit fullscreen mode

The DeleteAUser function follows the previous steps by deleting the matched record using the userCollection.DeleteOne. We also checked if an item was successfully deleted and returned the appropriate response.

Next, we need to update user_routes.go with the route API URL and corresponding controller as shown below:



package routes

import (
    //import goes here
)

func UserRoute(app *fiber.App) {
    //other routes goes here
    app.Delete("/user/:userId", controllers.DeleteAUser) //add this
}


Enter fullscreen mode Exit fullscreen mode

Get List of Users Endpoint
To get the list of users, we need to modify user_controller.go as shown below:



package controllers

import (
    //other import goes here
)

var userCollection *mongo.Collection = configs.GetCollection(configs.DB, "users")
var validate = validator.New()

func CreateUser(c *fiber.Ctx) error {
    //create user code goes here
}

func GetAUser(c *fiber.Ctx) error {
    //get a user code goes here
}

func EditAUser(c *fiber.Ctx) error {
    //edit a user code goes here
}

func DeleteAUser(c *fiber.Ctx) error {
    //delete a user code goes here
}

func GetAllUsers(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    var users []models.User
    defer cancel()

    results, err := userCollection.Find(ctx, bson.M{})

    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    //reading from the db in an optimal way
    defer results.Close(ctx)
    for results.Next(ctx) {
        var singleUser models.User
        if err = results.Decode(&singleUser); err != nil {
            return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
        }

        users = append(users, singleUser)
    }

    return c.Status(http.StatusOK).JSON(
        responses.UserResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": users}},
    )
}


Enter fullscreen mode Exit fullscreen mode

The GetAllUsers function follows the previous steps by getting the list of users using the userCollection.Find. We also read the retuned list optimally using the Next attribute method to loop through the returned list of users.

Next, we need to update user_routes.go with the route API URL and corresponding controller as shown below:



package routes

import (
    //import goes here
)

func UserRoute(app *fiber.App) {
    //other routes goes here
    app.Get("/users", controllers.GetAllUsers) //add this
}


Enter fullscreen mode Exit fullscreen mode

Complete user_controller.go



package controllers
import (
    "context"
    "fiber-mongo-api/configs"
    "fiber-mongo-api/models"
    "fiber-mongo-api/responses"
    "net/http"
    "time"

    "github.com/go-playground/validator/v10"
    "github.com/gofiber/fiber/v2"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
    "go.mongodb.org/mongo-driver/mongo"
)

var userCollection *mongo.Collection = configs.GetCollection(configs.DB, "users")
var validate = validator.New()

func CreateUser(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    var user models.User
    defer cancel()

    //validate the request body
    if err := c.BodyParser(&user); err != nil {
        return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    //use the validator library to validate required fields
    if validationErr := validate.Struct(&user); validationErr != nil {
        return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
    }

    newUser := models.User{
        Id:       primitive.NewObjectID(),
        Name:     user.Name,
        Location: user.Location,
        Title:    user.Title,
    }

    result, err := userCollection.InsertOne(ctx, newUser)
    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    return c.Status(http.StatusCreated).JSON(responses.UserResponse{Status: http.StatusCreated, Message: "success", Data: &fiber.Map{"data": result}})
}

func GetAUser(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    userId := c.Params("userId")
    var user models.User
    defer cancel()

    objId, _ := primitive.ObjectIDFromHex(userId)

    err := userCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&user)
    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    return c.Status(http.StatusOK).JSON(responses.UserResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": user}})
}

func EditAUser(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    userId := c.Params("userId")
    var user models.User
    defer cancel()

    objId, _ := primitive.ObjectIDFromHex(userId)

    //validate the request body
    if err := c.BodyParser(&user); err != nil {
        return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    //use the validator library to validate required fields
    if validationErr := validate.Struct(&user); validationErr != nil {
        return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
    }

    update := bson.M{"name": user.Name, "location": user.Location, "title": user.Title}

    result, err := userCollection.UpdateOne(ctx, bson.M{"id": objId}, bson.M{"$set": update})
    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    //get updated user details
    var updatedUser models.User
    if result.MatchedCount == 1 {
        err := userCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&updatedUser)
        if err != nil {
            return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
        }
    }

    return c.Status(http.StatusOK).JSON(responses.UserResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": updatedUser}})
}

func DeleteAUser(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    userId := c.Params("userId")
    defer cancel()

    objId, _ := primitive.ObjectIDFromHex(userId)

    result, err := userCollection.DeleteOne(ctx, bson.M{"id": objId})
    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    if result.DeletedCount < 1 {
        return c.Status(http.StatusNotFound).JSON(
            responses.UserResponse{Status: http.StatusNotFound, Message: "error", Data: &fiber.Map{"data": "User with specified ID not found!"}},
        )
    }

    return c.Status(http.StatusOK).JSON(
        responses.UserResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": "User successfully deleted!"}},
    )
}

func GetAllUsers(c *fiber.Ctx) error {
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    var users []models.User
    defer cancel()

    results, err := userCollection.Find(ctx, bson.M{})

    if err != nil {
        return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
    }

    //reading from the db in an optimal way
    defer results.Close(ctx)
    for results.Next(ctx) {
        var singleUser models.User
        if err = results.Decode(&singleUser); err != nil {
            return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
        }

        users = append(users, singleUser)
    }

    return c.Status(http.StatusOK).JSON(
        responses.UserResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": users}},
    )
}


Enter fullscreen mode Exit fullscreen mode

Complete user_route.go



package routes

import (
    "fiber-mongo-api/controllers"
    "github.com/gofiber/fiber/v2"
)

func UserRoute(app *fiber.App) {
    app.Post("/user", controllers.CreateUser)
    app.Get("/user/:userId", controllers.GetAUser)
    app.Put("/user/:userId", controllers.EditAUser)
    app.Delete("/user/:userId", controllers.DeleteAUser)
    app.Get("/users", controllers.GetAllUsers)
}


Enter fullscreen mode Exit fullscreen mode

With that done, we can test our application by starting the development server by running the command below in our terminal.

go run main.go
Enter fullscreen mode Exit fullscreen mode

terminal output

Create a user endpoint

Get a user endpoint

Edit a user endpoint

Delete a user endpoint

Get list of users endpoint

Database with users document

Conclusion

This post discussed how to structure a Fiber application, build a REST API and persist our data using MongoDB.

You may find these resources helpful:

Top comments (17)

Collapse
 
habeebullahtabrez profile image
Mohammed Habeeb Ullah Tabrez

I am a nodejs developer and I was assigned to make one rest API in go. I came across this article tutorial and this was the best tutorial to implement a REST API with all the CRUD operations in go. Loved it, Keep growing! <3

Collapse
 
malomz profile image
Demola Malomo

Thanks for the kind words.

Glad the article helped :)

Collapse
 
muhammadali208 profile image
Muhammad Ali

good work

Collapse
 
josesan94 profile image
José Santillan

Extremelly helpful for those who are starting with GO. Congratulations! Keep it that way

Collapse
 
varun11 profile image
varun Aryan

Thanks sir, go is best for Web api's

Collapse
 
nutniti1 profile image
Nutniti Suteerapongpan

Hi ,
Thank you for sharing.


Could I ask you more question ?
I am trying to deploy it on Heroku.

But it shows :
github.com/{myname}/fiber-mongo-api/configs
! Push rejected, failed to compile Go app.
! Push failed

What should I do to make it able to run on Heroku server?

Collapse
 
malomz profile image
Demola Malomo

Hi,

I'm glad you enjoyed reading the article.

I haven’t deployed on Heroku before.
These resources might help

devcenter.heroku.com/articles/gett...

dev.to/heavykenny/how-to-deploy-a-...

Collapse
 
surajtecho profile image
surajtecho

when i follow this doc and implemented it run succesfully again when i close my pc and run again
its showing
main.go:6:5: cannot find package "github.com/gofiber/fiber/v2" in any of:
C:\Program Files\Go\src\github.com\gofiber\fiber\v2 (from $GOROOT)
C:\Users\Techolution\go\src\github.com\gofiber\fiber\v2 (from $GOPATH)

it is happening again and again i tried to build and run it run succesfully when i tried to close my pc and run again its showing same

Collapse
 
malomz profile image
Demola Malomo

Hi @surajtecho
According to the error, It looks like the fiber package is not installed.

try reinstalling the package using: "go get -u github.com/gofiber/fiber/v2"

Collapse
 
medlabs profile image
MedLabs

why using a dedicated file for every response type ?
maybe putting all the responses types in one file would be better

Collapse
 
snelson1 profile image
Sophia Nelson

Very good stuff

Collapse
 
maprangsoft profile image
Maprangsoft

thank you very much i will use on production.

Collapse
 
raphoester profile image
Info Comment hidden by post author - thread only accessible via permalink
raphoester

This way of coding is absolutely NOT how go is meant to be used. You are supposed to convert your functions in methods and implement interfaces in your packages.

Some comments have been hidden by the post's author - find out more