Deploy a Go Gin Application on Koyeb
5 minIntroduction
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.
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:
Building a minimalist Go Gin application
To get started, create a new directory on your local computer to build the example application:
Next, run the following command 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 server.go
with the following content inside:
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:
Download a basic .gitignore
file for Go projects from GitHub by typing:
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:
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:
Now that your Gin application is available on GitHub, go to the Overview tab of the Koyeb control panel and click Create Web Service to begin:
- Select GitHub as the deployment method.
- 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. - Choose a name for your App and Service, for example
go-gin-on-koyeb
, and click Deploy.
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!