fly

package
v0.0.0-...-bc63a83 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 10 Imported by: 0

README

Fly Package

A Go package for interacting with Fly.io services and applications. This package provides utilities for managing Fly.io machines, retrieving logs, and handling Fly.io-specific concerns in web applications.

Components

Core Library (fly/fly.go)

The core fly package provides Go functions for interacting with Fly.io resources:

  • Machine management (listing, querying status)
  • Log retrieval with support for both streaming and non-streaming modes
  • Region configuration (US/EU regions)
  • Colorized terminal output for better log readability
  • Utilities for tracking flyctl CLI calls
FlyMiddleware (cdns/fly.go)

A middleware for the Echo web framework that handles Fly.io-specific headers:

  • Client IP detection via Fly-Client-IP header
  • Automatic HTTPS redirection based on Fly-Forwarded-Proto
  • Configurable redirect options (custom port, disable redirect)
  • Sets RealIP and IsTLS context values for use in your handlers
FlySuper Utility (flysu)

A command-line tool for Fly.io administrators and operators that provides:

  • Aggregated machine management across multiple applications and regions
  • Two main commands:
    • list: Display machine details across regions with filtering options
    • logs: Retrieve and display logs from machines with filtering options
  • Region filtering (US-only, EU-only)
  • Application-specific targeting
  • Formatted, colorized output for better readability

Installation

go get github.com/presbrey/pkg/fly

Usage Examples

Using the Core Library
package main

import (
	"fmt"
	"log"

	"github.com/presbrey/pkg/fly"
)

func main() {
	// Get list of machines for an app
	machines, err := fly.GetMachineList("my-app")
	if err != nil {
		log.Fatalf("Error getting machines: %v", err)
	}
	
	// Print machine details
	for _, machine := range machines {
		fmt.Printf("Machine: %s, Region: %s, State: %s\n", 
			machine.Name, machine.Region, machine.State)
	}
	
	// Get logs for a specific machine (non-streaming mode)
	if len(machines) > 0 {
		logs, err := fly.GetMachineLogs("my-app", machines[0].ID, false)
		if err != nil {
			log.Fatalf("Error getting logs: %v", err)
		}
		fmt.Println(logs)
	}
}
Using the Echo Middleware
package main

import (
	"fmt"
	"github.com/labstack/echo/v4"
	"github.com/presbrey/pkg/cdns"
)

func main() {
	e := echo.New()
	
	// Add Fly.io middleware with default settings
	e.Use(cdns.FlyWithDefaults())
	
	// Or with custom settings
	flyMiddleware := cdns.NewFlyMiddleware().
		WithRedirectPort(8443).        // Custom HTTPS port
		WithoutRedirect()              // Disable auto-redirect
	e.Use(flyMiddleware.Build())
	
	e.GET("/", func(c echo.Context) error {
		// RealIP will be set from Fly-Client-IP if available
		clientIP, _ := c.Get("RealIP").(string)
		isTLS, _ := c.Get("IsTLS").(bool)
		
		return c.String(200, fmt.Sprintf("Hello from %s (TLS: %v)", clientIP, isTLS))
	})
	
	e.Start(":8080")
}
Using the FlySuper Utility
# List all machines across all regions
flysu list

# View logs for all apps in US regions only
flysu logs --us

# List machines in EU regions only with minimal output
flysu list --eu --quiet

# Follow logs for a specific app
flysu logs -f -a us-east-1-portal

# View help information
flysu help

Configuration

The package can be configured via environment variables:

  • US_REGIONS: Comma-separated list of US regions (default: "us-east-1, us-east-2, us-east-3, us-east-4")
  • EU_REGIONS: Comma-separated list of EU regions (default: "eu-west-1, eu-west-2, eu-west-3, eu-west-4")
  • APP_NAMES: Comma-separated list of application types to monitor (default: "portal, websocket")

Requirements

  • Go 1.15 or higher
  • flyctl installed and configured
  • Valid Fly.io authentication

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorizedAppPrefix

func ColorizedAppPrefix(appName string) string

ColorizedAppPrefix returns a colorized prefix for the app name

func GetAppNames

func GetAppNames() []string

GetAppNames returns the list of application names

func GetColorForApp

func GetColorForApp(appName string) string

GetColorForApp returns a consistent color for a given app name

func GetEURegions

func GetEURegions() []string

GetEURegions returns the list of EU regions

func GetFlyctlCallCount

func GetFlyctlCallCount() int32

GetFlyctlCallCount returns the current count of flyctl calls

func GetMachineLogs

func GetMachineLogs(appName, machineID string, followFlag bool) (string, error)

GetMachineLogs gets the logs for a specific machine

func GetUSRegions

func GetUSRegions() []string

GetUSRegions returns the list of US regions

func IncrementFlyctlCallCount

func IncrementFlyctlCallCount() int32

IncrementFlyctlCallCount increments the flyctl call counter and returns the new value

Types

type Config

type Config struct {
	Env      map[string]string `json:"env"`
	Guest    Guest             `json:"guest"`
	Metadata map[string]string `json:"metadata"`
	Services []interface{}     `json:"services"` // Using interface{} as we don't need to parse this
}

Config contains machine configuration

type Event

type Event struct {
	Type      string                 `json:"type"`
	Status    string                 `json:"status"`
	Source    string                 `json:"source"`
	Timestamp int64                  `json:"timestamp"`
	Request   map[string]interface{} `json:"request"` // Using interface{} as structure may vary
}

Event represents machine events

type Guest

type Guest struct {
	CPUKind  string `json:"cpu_kind"`
	CPUs     int    `json:"cpus"`
	MemoryMB int    `json:"memory_mb"`
}

Guest contains guest VM configuration

type ImageRef

type ImageRef struct {
	Registry   string            `json:"registry"`
	Repository string            `json:"repository"`
	Tag        string            `json:"tag"`
	Digest     string            `json:"digest"`
	Labels     map[string]string `json:"labels"`
}

ImageRef contains image reference information

type Machine

type Machine struct {
	ID       string    `json:"id"`
	Name     string    `json:"name"`
	State    string    `json:"state"`
	Region   string    `json:"region"`
	ImageRef ImageRef  `json:"image_ref"`
	Created  time.Time `json:"created_at"`
	Updated  time.Time `json:"updated_at"`
	Config   Config    `json:"config"`
	Events   []Event   `json:"events"`
}

Machine represents the fly machine data structure

func GetMachineList

func GetMachineList(appName string) ([]Machine, error)

GetMachineList gets the list of machines for a specific app

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL