Programming

Install Fiber Server with Golang on AlmaLinux 8

In this article, we’ll explain how to install Fiber Server with Golang on AlmaLinux 8.

Fiber is a Go web framework built on top of Fast http, the fastest HTTP engine for Go. It’s designed to ease things up for fast development with zero memory allocation and performance in mind. It features an efficient HTTP engine, a growing middleware community, and an easy-to-use Express-inspired API.

Let’s get started with the installation.

Install Fiber Server with Golang on AlmaLinux

Prerequisite

  • A AlmaLinux 8 installed dedicated server or KVM VPS.
  • A root user access or normal user with administrative privileges.
  • Updated server.

1. Install Golang

1. Download and extract Go 1.17.1

We will download Go using wget command and extract it in /usr/local path.

# wget https://golang.org/dl/go1.17.1.linux-amd64.tar.gz

# tar -zxvf go1.17.1.linux-amd64.tar.gz -C /usr/local

At the time of writing this guide, the latest available version was 1.17.1. You can check the latest Go version from the Go official download page.

2. Setup Environment variables

The Go’s runtime and build executables are now available under /usr/local/go/bin. Add the executable path to PATH environment variable. Add the GOROOT environment variable referencing your local Go installation. Use thesource command to reload the updated values.

# echo ‘export GOROOT=/usr/local/go’ | sudo tee -a /etc/profile

# echo ‘export PATH=$PATH:/usr/local/go/bin’ | sudo tee -a /etc/profile

# source /etc/profile

3. Verify the installation

To verify the installation, we can check the version of the Go by using following command:

# go version

Output will show similar like:

go version go1.17.1 linux/amd64

2. Initialize the Project

1. Create the project directory

# mkdir fiberserver && cd fiberserver

2. Create a Go module using the go mod command

# go mod init fiberserver

3. Install Fiber

# go get github.com/gofiber/fiber/v2

4. Create a main Golang file in the project directory

# vi main.go

5. Copy and paste following content in the main.go file

package main

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

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

  app.Get("/", func(c *fiber.Ctx) error {
    return c.SendString("Hello, World!")
  })

  app.Listen(":3000")
}

If you are a root user you can modify the app.Listen port to 80. You can set up a proxy server like Nginx too.

3. Configure firewall

Let’s configure firewalld.

# firewall-cmd –add-port=3000/tcp –permanent

# firewall-cmd –reload

Note: Add the post which one you have set in main.go app.Listen(“:3000”).

4. Run the Server

1. Compile the project using following command:

# go build

2. Run the compiled binary file:

# ./fiberserver

3 Navigate to your server’s IP address at port 3000 in a web browser. For example:

http://192.0.2.123:3000

Basic routing

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, PUT, POST and so on).

Simple route

// Respond with "Hello, World!" on root path, "/"
app.Get("/", func(c *fiber.Ctx) error {
  return c.SendString("Hello, World!")
})

Parameters

// GET http://localhost:8080/hello%20world

app.Get("/:value", func(c *fiber.Ctx) error {
  return c.SendString("value: " + c.Params("value"))
  // => Get request with value: hello world
})

Optional parameter

// GET http://localhost:3000/john

app.Get("/:name?", func(c *fiber.Ctx) error {
  if c.Params("name") != "" {
    return c.SendString("Hello " + c.Params("name"))
    // => Hello john
  }
  return c.SendString("Where is john?")
})

Wildcards

// GET http://localhost:3000/api/user/john

app.Get("/api/*", func(c *fiber.Ctx) error {
  return c.SendString("API path: " + c.Params("*"))
  // => API path: user/john
})

Static files

To serve static files such as images, CSS, and JavaScript files, replace your function handler with a file or directory string.

Function signature:

app.Static(prefix, root string)

Use the following code to serve files in a directory named ./public:

app := fiber.New()

app.Static("/", "./public") 

app.Listen(":3000")

Now, you can load the files that are in the ./public directory:

http://localhost:8080/hello.html
http://localhost:8080/js/jquery.js
http://localhost:8080/css/style.css

We have successfully installed and deployed Fiber server with Golang.

In this article, we have seen how to install Fiber Server with Golang on AlmaLinux 8.

Reference: https://docs.gofiber.io/

Related Articles