December 08, 2023
Édouard Bonlieu
@edouardb_
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:
You can consult the repository on GitHub to find out more about the example application that this guide uses.
To successfully follow and complete this guide, you'll need:
In this guide, we'll be writing and deploying a Go API on Koyeb with the following steps:
To get started, create a new directory on your local computer to build the example application:
mkdir ~/gin-demo cd ~/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 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)) }
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.
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 .gitignore git 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:
https://github.com/koyeb/example-go-gin in the Public GitHub repository field.main by default.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.
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 is a developer-friendly serverless platform to deploy any apps globally.
Start for freeDeploy 1 Web Service for free and enjoy our predictable pricing as you grow
Get up and running in 5 minutes