November 16, 2021
Édouard Bonlieu
@edouardb_
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.
To successfully follow and complete this guide, you need:
To successfully complete this guide and deploy the Go API on Koyeb, you need to follow these steps:
To get started, create a new directory to build our sample application:
mkdir ~/gin-demo cd ~/gin-demo
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)) }
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
.
Next, initialize a new git repository on your local machine running:
git init
Add a new remote pointing to your GitHub repository:
git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_GITHUB_REPOSITORY>.git
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
Go to the Koyeb Control Panel and click the Create App button to go to the App creation page:
main
go-gin-on-koyeb
, and click Create AppThen, 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.
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.
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 2 services for free and enjoy our predictable pricing as you grow
Get up and running in 5 minutes