Deploy Go Apps
Go is a popular programming language for backend development. Go is served as a compiled language, which means you need to compile your code before running it.
There is no default go
compiler at system level. You have to install it first through deployment scripts.
Serving Go apps requires to be run via GLS, that's mean you have to listen from given PORT
env/args.
Example
The deployment script below installs the latest go
compiler and compiles main.go
and serves that main
binary.
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
.
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.
features:
- go
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT go run .
commands:
- go mod download
Use other Go versions
To switch golang version use feature syntax like go stable
, go beta
, go 1.19
, etc. Check your current go version using go version
in SSH.
features:
- go 1.19
Go install scripts is powered by webi.
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.
Please 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.