One of The Easiest Ways to Host your Go Web App
5 Sep 2023

One of The Easiest Ways to Host your Go Web App

Hello! In this post, I will explain the cost-effective method I use to host my Go web applications with varying levels of complexity, all starting from as low as $5 per month. This method also allows to easy deploy and scale your golang application.

As an example, this is how I host whattoreadafter.xyz, a service that recommends books based on the book you just finished reading, by using AI.

Starting off, let’s enumerate the tools we’ll be using alongside Golang:

  • DigitalOcean - a cloud computing platform that provides virtual machines and other resources.
  • Docker - a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.

In order to follow along, you will need to have a DigitalOcean account. If you don’t have one, you can sign up here and get $200 in credit over 60 days.

Let’s get started! For simplicity sake, our application will be a simple web server that returns current time in UTC format.

package main

import (
	"fmt"
	"net/http"
	"time"
)

func currentTimeHandler(w http.ResponseWriter, r *http.Request) {
	currentTime := time.Now().UTC()
	fmt.Fprintf(w, "Current Time (UTC): %s", currentTime.Format(time.RFC3339))
}

func main() {
	http.HandleFunc("/", currentTimeHandler)
	fmt.Println("Server is running on port 8080")
	http.ListenAndServe(":8080", nil)
}

Now let’s host it!

In order for you to easier follow along, I will list the steps we will take to host our application:

1. Prepare a Dockerfile for our application and place it in the root of our project alongside the main.go file.

Example:

FROM golang:alpine AS builder
RUN apk add --no-cache --update \
        git \
        ca-certificates
ADD . /app
WORKDIR /app
COPY go.mod ./
RUN  go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o /main .

FROM alpine
COPY --from=builder /main ./
RUN chmod +x ./main
ENTRYPOINT ["./main"]

 

2. Push your code to a GitHub repository. You can make it private if you want.

 

3. Go to DigitalOcean and create a new App. You can do so by clicking on the “Apps” tab in the left sidebar and then clicking “Create App”.

 

4. Select “GitHub” as your “Source” and click “Continue”.

Hosting Golang on digitalocean

5. Select the repository you want to deploy and click “Next”.

Hosting Golang on digitalocean

6. Select “Dockerfile” as your “Build type” and click “Next”.

Hosting Golang on digitalocean

7. Configure environment variables if you need to and click “Next”.

 

8. Select region for your app and click “Next”.

Hosting Golang on digitalocean

9. In the billing section, select the plan you want to use. You can start with the $5 per month plan and scale up later if you need to.

Hosting Golang on digitalocean

10. Click “Create Resources” and wait for your app to be deployed.

 

Hosting Golang on digitalocean

That’s it! You have successfully deployed your Golang application!

You can also add your own domain name to your app by clicking on the “Settings” tab and then clicking on “Domains”.

Now, every time you push a new commit to your repository, DigitalOcean will automatically build and deploy your application. This is a great way to host your Golang applications, especially if you are just starting out and don’t want to spend a lot of money on hosting.

Thank you for reading! If you have any questions, feel free to reach out to me on Twitter.

Related posts

103 Early Hints in Go, or the new Way of How to Improve Performance of a Web Page written in Go
14 Nov 2022

103 Early Hints in Go, or the new Way of How to Improve Performance of a Web Page written in Go

Since Go 1.19 we can use a new 103 (Early Hints) http status code when we create web applications. Let’s figure out how and when this could help us. We are going to create a simple golang web server that servers some html content. One html page will be served with 103 header and another one without. After loading comparison we will see how early hints can improve page performance.

go golang performance
Example of how Golang generics minimize the amount of code you need to write
9 Jun 2022

Example of how Golang generics minimize the amount of code you need to write

I guess that almost everyone in the go community was exciting when Go 1.18 was released, especially because of generics. Some days ago I decided to try generics in the real-world application, by refactoring some of its pieces, related to a caching logic.

go generics redis cache
Concurrent Map Writing and Reading in Go, or how to deal with the data races.
16 Jul 2021

Concurrent Map Writing and Reading in Go, or how to deal with the data races.

This time, I will show you how to work with the maps in go effectively and prevent the occurrence of the data race errors. Data races happen when several goroutines access the same resource concurrently and at least one of the accesses is a write.

go concurrent map data race
Ristretto - the Most Performant Concurrent Cache Library for Go
2 Mar 2021

Ristretto - the Most Performant Concurrent Cache Library for Go

Recently, I discovered a surprisingly reliable memory caching solution, which I’m planning to use in all my further applications to increase performance. In this blog post, I will share some code examples of how you can integrate Ristretto caching library into your application.

go caching ristretto performance
How to Show Flash Messages in Go web applications (with Echo framework)
4 Feb 2021

How to Show Flash Messages in Go web applications (with Echo framework)

When we create a web application, usually, there a need to communicate with the users to inform them about the results of their actions. The easiest way to communicate - is to send messages. These messages might be warnings, errors, or just informational text. In this article, we will improve the UX of our user authentication application from the previous article by adding an error flash message when the user entered a wrong password and a success message after user authorisation.

go echo flash messages