Deploy a Go Gin Application on Koyeb

edouardb_

Edouard Bonlieu

Posted on January 12, 2022

Deploy a Go Gin Application on Koyeb

This guide explains how to deploy a Golang Gin application on the Koyeb serverless platform using git-driven deployment.

Gin is a web framework written in Golang focusing on performance and simplicity.

By deploying a Go Gin application on Koyeb using git-driven deployment, each time you push your code changes, your application will be automatically built and promoted once the built and health checks are completed.
Once your application is deployed, it will benefit from Koyeb native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network with zero configuration.

Requirements

To successfully follow and complete this guide, you need:

Steps

To successfully complete this guide and deploy the Go API on Koyeb, you need to follow these steps:

  1. Building a minimalist Go Gin application
  2. Deploy the Go Gin application on Koyeb

Building a minimalist Go Gin application

To get started, create a new directory to build our sample application:

mkdir ~/gin-demo
cd ~/gin-demo
Enter fullscreen mode Exit fullscreen mode

Next, run go mod init to create a go.mod file and track your code's dependencies.
As you add dependencies, the go.mod file will list the versions your code depends on allowing you to keep your builds reproducible and giving you direct control over which module versions to use.

In your project folder, create a new file named app.go with the following content inside:

package main

import (
  "fmt"
  "os"

  "github.com/gin-gonic/gin"
)

func main() {
  port := os.Getenv("PORT")

  if port == "" {
    port = "8000"
  }

  r := gin.Default()

  r.GET("/", func(c *gin.Context) {
    c.JSON(200, gin.H{
      "message": "Hello, world!",
    })
  })

  r.GET("/:name", func(c *gin.Context) {
    name := c.Param("name")

    c.JSON(200, gin.H{
      "message": fmt.Sprintf("Hello, %s!", name),
    })
  })

  r.Run(fmt.Sprintf(":%s", port))
}
Enter fullscreen mode Exit fullscreen mode

The code above launches a Gin server listening on port 8000 by default with two API routes configured:

  • / returning "Hello, world!"
  • /:name returning "Hello, :name!" where :name is the string passed in parameter, i.e /john will return "Hello, john!".

Then, in your terminal execute go mod tidy to add missing modules required by our project and add github.com/gin-gonic/gin as a project dependency.

You can now run the application locally by running go run server.go and navigating in your browser at http://localhost:8000.

Deploy the Go Gin application on Koyeb

Next, initialize a new git repository on your local machine running:

git init
Enter fullscreen mode Exit fullscreen mode

Add a new remote pointing to your GitHub repository:

git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_GITHUB_REPOSITORY>.git
Enter fullscreen mode Exit fullscreen mode

Last, add the app.go, go.mod and go.sum files to your repository and commit & push your changes:

git add app.go go.mod go.sum
git commit -m "Initial commit"
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Go to the Koyeb Control Panel and click the Create App button to go to the App creation page:

  1. Select GitHub as the deployment method to use
  2. In the repositories list, select the repository containing your Go Gin application
  3. Specify the branch to deploy, in this case, main
  4. Give your App a name, i.e go-gin-on-koyeb, and click Create App.

Then, click the Create App button to create and deploy the application. You land on the deployment page where you can follow the progress of your Go Gin application's deployment. Once the build and deployment are completed, you can access your application by clicking the App URL ending with koyeb.app in the Koyeb control panel.

If you want to learn about how Koyeb automatically builds your Go Gin applications from git, make sure to read our how we build from git documentation.

Conclusion

In this guide, you learned how to deploy a Go Gin application on Koyeb using git. Each change you push to your repository will automatically trigger a new build and deployment on the Koyeb Serverless Platform.
Your changes then go live as soon as the deployment passes all necessary health checks. In case of a failure during one of your deployments, we ensure to keep the latest working deployment active so your application is always up and running.

If you have any questions or suggestions to improve this guide, feel free to reach out to us on Slack.

💖 💪 🙅 🚩
edouardb_
Edouard Bonlieu

Posted on January 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related