Skip to main content

Deploy Go Apps

Go installation is powered by webi. To install latest version with deployment script:

features:
- go

Without this, go won't be available since no globally installed Go compiler available.

Install other version using go latest, go 1.22, etc. To uninstall use go off. Check the current version using go version from SSH.

Example

The deployment script below installs the latest go compiler, compiles main.go and runs the main binary serving HTTP from PORT envar.

https://github.com/domcloud/recipes/blob/master/go.yml
source: clear
features:
- go
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT ./main
commands:
- filename: main.go
content: |
package main

import (
"fmt"
"net/http"
"os"
"runtime"
"strings"
)

const html_text = `
<!DOCTYPE html>
<html>
<head>
<title>Go App</title>
<link rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css">
</head>
<body class="p-5 text-center">
<p><img src="//images.unsplash.com/photo-1465153690352-10c1b29577f8?fit=crop&w=200&h=200"
class="img-fluid rounded-circle"></p>
<h1 class="mb-3">Hello, world!</h1>
<p>Serving from Go version %s</p>
<p class="text-muted">DOM Cloud</p>
</body>
</html>
`

func home(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/html")
version := strings.Replace(runtime.Version(), "go", "", 1)
fmt.Fprintf(w, html_text, version)
}

func main() {
http.HandleFunc("/", home)
port := os.Getenv("PORT")
if port == "" {
fmt.Print("PORT environment variable not set")
os.Exit(1)
}
http.ListenAndServe("127.0.0.1:" port, nil)
}

- go build main.go

For existing golang apps, use this deployment script. This project must have a go.mod initialized and has package name as main.

https://github.com/domcloud/recipes/blob/master/go-binary.yml
features:
- go
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT ./app
commands:
- go mod download
- go build -o app

It's possible to run the app via go run .. This eliminates the need to compile the code into a binary, although startup time may be a little bit slower.

https://github.com/domcloud/recipes/blob/master/go-gls.yml
features:
- go
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT go run .
commands:
- go mod download

App Management

Your app do not restarted automatically after file changes. To restart, run restart via SSH.

Environment variables can be set either using NGINX's env_var_list or ~/.bashrc. Usually your language framework also reads .env files.

See NGINX and App Daemon for more information about NGINX and App managements including restarting, environment variables, and other global limitations.

App Logging

You can see app log from Check -> Check Process Logs tab. Only startup problems displayed in the browser.

You might want to use a proper logging mechanism such as the standard logging library then write it to a log file, or any other solution that suits you.

NGINX errors and traffic logs can be examined via Webmin or Check -> Check Process Logs tab.