> ## Documentation Index
> Fetch the complete documentation index at: https://getfloo.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy a Go API with Postgres and a daily cron job

> Build a shop’s daily reorder report. Deploy one Go binary on floo, run the scheduled job twice, and check the count over HTTP.

A shop has two items on its shelves. Coffee is below its reorder point. Tea is above it. At 06:00 UTC a Go job saves the number of items to reorder in Postgres. The API serves the latest count even after the process restarts.

On floo, the HTTP service and the scheduled job run the same image and receive the same `DATABASE_URL` within an environment. The job exits after writing one row. Its UTC date is the primary key, so running it twice on the same day updates that row.

[Open the daily reorder report](https://seo-demo-go-workload.getfloo.app/).

To deploy your own copy with a coding agent, paste this from an empty project directory:

```text theme={null}
Read https://getfloo.com/docs/deploy/go-api-postgres-cron.md and create the files shown there.
Use an available app name starting with go-reorder- and deploy my copy to floo.
Run the reorder job and check the count at the dev URL.
```

## Build the report

In an empty directory, save these two files. Run `go mod tidy` with Go 1.26 to create `go.sum`.

```mod go.mod theme={null}
module example.com/reorder-report

go 1.26

require github.com/lib/pq v1.10.9
```

```go main.go theme={null}
package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"os"

	_ "github.com/lib/pq"
)

const schema = `
CREATE TABLE IF NOT EXISTS stock (
    sku text PRIMARY KEY, on_hand integer NOT NULL, reorder_at integer NOT NULL
);
CREATE TABLE IF NOT EXISTS reorder_reports (
    day date PRIMARY KEY, items integer NOT NULL
);
INSERT INTO stock VALUES ('coffee', 2, 5), ('tea', 9, 4)
ON CONFLICT (sku) DO NOTHING;`

func run() error {
	url := os.Getenv("DATABASE_URL")
	if url == "" {
		return fmt.Errorf("set DATABASE_URL")
	}
	db, err := sql.Open("postgres", url)
	if err != nil {
		return err
	}
	defer db.Close()
	db.SetMaxOpenConns(4)
	if len(os.Args) > 1 {
		switch os.Args[1] {
		case "migrate":
			_, err = db.Exec(schema)
		case "report":
			_, err = db.Exec(`INSERT INTO reorder_reports
			    SELECT (now() AT TIME ZONE 'UTC')::date, count(*) FROM stock WHERE on_hand <= reorder_at
			    ON CONFLICT (day) DO UPDATE SET items = excluded.items`)
		default:
			return fmt.Errorf("unknown command %q", os.Args[1])
		}
		return err
	}
	mux := http.NewServeMux()
	mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
		var day string
		var items int
		err := db.QueryRowContext(r.Context(),
			"SELECT day::text, items FROM reorder_reports ORDER BY day DESC LIMIT 1").Scan(&day, &items)
		switch err {
		case sql.ErrNoRows:
			fmt.Fprintln(w, "No report yet")
		case nil:
			fmt.Fprintf(w, "%s: SKUs to reorder: %d\n", day, items)
		default:
			log.Print(err)
			http.Error(w, "Report unavailable", http.StatusServiceUnavailable)
		}
	})
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}
	return http.ListenAndServe(":"+port, mux)
}

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}
```

## Deploy the API and schedule

```dockerfile Dockerfile theme={null}
FROM golang:1.26-bookworm AS build
WORKDIR /src
COPY go.mod go.sum main.go ./
RUN CGO_ENABLED=0 go build -mod=readonly -o /go-reorder-report .

FROM debian:bookworm-slim
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /go-reorder-report /go-reorder-report
USER 65532:65532
CMD ["/go-reorder-report"]
```

```toml floo.app.toml theme={null}
[app]
name = "go-reorder-report"
access_mode = "public"

[services.web]
type = "web"
path = "."
port = 8080
ingress = "public"
migrate_command = "/go-reorder-report migrate"

[managed.default]
type = "postgres"

[cron.reorder]
service = "web"
schedule = "0 6 * * *"
command = "/go-reorder-report report"
```

Choose an available app name starting with `go-reorder-` in `floo.app.toml`. The migration creates both tables and seeds the two example items before the new image receives traffic. The sample endpoint is public and contains only fixture data. For real stock levels, [restrict the app to invited staff](https://getfloo.com/docs/guides/app-auth.md).

Install the [floo CLI](https://getfloo.com/docs/introduction.md), sign in, and run these commands from the example directory:

```bash theme={null}
go mod tidy
git init -b main
git add Dockerfile floo.app.toml go.mod go.sum main.go
git commit -m "Deploy the daily reorder report"
floo deploy --preflight
floo deploy --hosted
```

## Run the first report

Open the dev URL printed by the deploy. Before the first job runs, it shows `No report yet`. Run the job from the same directory:

```bash theme={null}
floo cron run reorder
floo logs query --cron reorder
```

Refresh the URL after the job finishes. The UTC date appears followed by `SKUs to reorder: 1`. Run the job again. The count stays at one. Change the reorder condition or the fixture quantities to test a different stock policy.

For an existing Go service, see the [Go deployment guide](https://getfloo.com/docs/build/go.md). [Cron jobs](https://getfloo.com/docs/guides/cron-jobs.md) covers retries, timeouts, and running a job in prod.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.