All tutorials
Tutorial

Deploy a Go Gin Application on Koyeb

5 min

Introduction

Gin is a web framework written in Go (Golang) that focuses primarily on performance and simplicity.

This guide explains how to deploy an application written using Gin to the Koyeb serverless platform using git-driven deployment. By using Koyeb to deploy your app from a GitHub repository, each time you push code changes, your application will automatically be built and deployed, replacing the running instance once your new version is healthy.

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.

You can deploy and preview the example application from this guide by clicking the Deploy to Koyeb button below:

Deploy to Koyeb

You can consult the repository on GitHub to find out more about the example application that this guide uses.

Requirements

To successfully follow and complete this guide, you'll need:

  • The Go programming language installed on your local computer
  • A Koyeb account to deploy and run the Go web API
  • A GitHub account to store your application code

Steps

In this guide, we'll be writing and deploying a Go API on Koyeb with the following 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 on your local computer to build the example application:

mkdir ~/gin-democd ~/gin-demo

Next, run the following command to create a go.mod file and track your code's dependencies:

go mod init example-go-gin

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 server.go with the following content inside:

package mainimport (  "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))}

The code above launches a Gin server listening on port 8000 by default (can be overridden by setting the PORT environment variable). It serves two API routes:

  • / returning {"message": "Hello, world!"}
  • /:name returning {"message": "Hello, :name!"} where :name is a parameter taken from the requested URL path. For example, /john will return "Hello, john!".

In your terminal, execute go mod tidy to add the 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 to http://localhost:8000 in your web browser.

Deploy the Go Gin application on Koyeb

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

git init

Download a basic .gitignore file for Go projects from GitHub by typing:

curl -L https://raw.githubusercontent.com/github/gitignore/main/Go.gitignore -o .gitignore

Next, create a new repository on GitHub to host your project code. In your project directory, add a new remote pointing to the GitHub repository:

git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_GITHUB_REPOSITORY>.git

Note: Be sure to substitute your own GitHub user and repository names in the above command.

Lastly, add the server.go, go.mod, go.sum, and .gitignore files to your repository, commit your changes, and push them to GitHub:

git add server.go go.mod go.sum .gitignoregit commit -m "Initial commit"git push -u origin main

Now that your Gin application is available on GitHub, go to the Koyeb control panel and click Create App to go to the App creation page:

  1. Select GitHub for your deployment method.
  2. In the repositories list, select the repository containing your Gin application. Alternatively, you can deploy the public example repository by entering https://github.com/koyeb/example-go-gin in the Public GitHub repository field.
  3. Specify the branch to deploy not selected automatically. The example repository uses main by default.
  4. Give your App a name, like go-gin-on-koyeb, and Deploy at the bottom of the page.

Your application will be cloned from GitHub, built and deployed. You can monitor the progress by viewing your Service's build and deployment logs. Once the deployment is complete, you can access your application by clicking the Public URL ending in koyeb.app on your Service's detail page in the control panel.

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

Conclusion

In this guide, we demonstrated 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, your latest working deployment will continue to function to ensure your application is always up and running.

Questions or suggestions to improve this guide? Join us on the community platform to chat!

Koyeb

Welcome to Koyeb

Koyeb is a developer-friendly serverless platform to deploy any apps globally.

  • Start for free, pay as you grow
  • Deploy your first app in no time
Start for free
The fastest way to deploy applications globally.