Teral
Frameworks

Go

Deploy Go services without a Dockerfile.

Teral recognizes a Go project by its go.mod. It uses the Go version from go.mod, builds one binary and runs it.

Which package is built

In this order:

  1. the folder in the variable RAILPACK_GO_BIN, as cmd/<name>, if you set one
  2. the module root, if it has Go files
  3. the first folder under cmd/
  4. for a go.work workspace, the first module with a main.go

In a repository with several commands, such as cmd/api and cmd/worker, create one app per command and set RAILPACK_GO_BIN to api or worker in each app's variables.

Listen on the right port

package main

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

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}
	http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("ok"))
	})
	log.Fatal(http.ListenAndServe(":"+port, nil))
}

Listen on ":"+port, not "localhost:"+port, so the gateway can reach the server.

Shut down cleanly

Teral sends SIGTERM and waits 30 seconds before it stops an instance. Use http.Server.Shutdown with signal.NotifyContext(ctx, syscall.SIGTERM) to let requests in progress finish.

Edit on GitHub

On this page