Introduction

Go is a statically typed, compiled programming language designed at Google. It provides excellent support for building high-performance web applications with its robust standard library and built-in concurrency features.

Prerequisites

  • a UNIX-like system (see WSL if you are using Windows), in order to use the Valyent CLI
  • a Valyent account, with a registered payment method (you can sign up here)
  • Go installed on your machine (version 1.21 or later)

Installing the CLI

curl -L https://cli.valyent.cloud | sh

You can inspect the installation script here and the GitHub repository.

Authenticating

valyent auth login

Initializing the project

First, let’s create a new directory for our project and initialize it as a Go module:

mkdir go-webapp && cd go-webapp
go mod init go-webapp

Create the main application file:

touch main.go

In the main.go file, add the following code:

package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
)

func main() {
    mux := http.NewServeMux()
    
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        if r.URL.Path != "/" {
            http.NotFound(w, r)
            return
        }
        
        w.Header().Set("Content-Type", "text/html; charset=utf-8")
        w.WriteHeader(http.StatusOK)
        fmt.Fprint(w, "Welcome to the Go Web App!")
    })

    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }
    addr := ":" + port

    log.Printf("Server starting on port %s", port)
    if err := http.ListenAndServe(addr, mux); err != nil {
        log.Fatalf("Server failed to start: %v", err)
    }
}

Now, initialize the valyent.toml configuration file:

valyent init

This command will guide you through selecting or creating a project and an associated application.

Adding the Dockerfile

Create a Dockerfile for the application:

touch Dockerfile

Add the following content to the Dockerfile:

# Stage 1: Builder
FROM golang:1.21 AS builder

WORKDIR /app

# Copy module files
COPY go.mod ./

# Copy the source code
COPY . .

# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -o webapp .

# Stage 2: Runtime
FROM alpine:latest

WORKDIR /app

# Copy the binary from builder
COPY --from=builder /app/webapp .

# Expose the application port
EXPOSE 8080

# Run the binary
ENTRYPOINT ["/app/webapp"]

Deploy the project

To expose the application’s port, we need to set up a gateway (in this case, the port 8080):

valyent gateways create

Deploy your application to Valyent:

valyent deploy

Then, you can type the following:

valyent open