Go library providing support functionality for application startup, configuration, logging, and profiling.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
testdata
tools
.gitignore
.gitkeep
.travis.yml
AUTHORS
LICENSE
README.md
common_integration_test.go
config.go
container.go
container_test.go
default_https_server_integration_test.go
doc.go
http_service.go
logging.go
runtime.go
server.go
services.go
tls.go
tls_1.1.go
tls_1.4.go
tls_generic.go

README.md

Phoenix

GoDoc Build Status

Introduction

Package phoenix provides runtime support for long running server processes.

In particular, it provides standardized mechanisms for handling logging, configuration, and HTTP server startup, as well as profiling support.

Additionally, lifecycle management facilities for application services which should respond to server state changes are provided, as are a full suite of signal handling functionality, including configuration reload on SIGHUP.

Usage

Import into your workspace via go get:

go get github.com/strukturag/phoenix

And then use in your application as follows:

package main

import (
    "flag"
	"fmt"
    "net/http"
    "os"

    "github.com/strukturag/phoenix"
)

var version = "unreleased"
var defaultConfig = "./server.conf"

func boot() error {
	configPath := flag.String("c", defaultConfig, "Configuration file.")
	logPath := flag.String("l", "", "Log file, defaults to stderr.")
	showVersion := flag.Bool("v", false, "Display version number and exit.")
    memprofile := flag.String("memprofile", "", "Write memory profile to this file.")
    cpuprofile := flag.String("cpuprofile", "", "Write cpu profile to file.")
	showHelp := flag.Bool("h", false, "Show this usage information and exit.")
	flag.Parse()

	if *showHelp {
		flag.Usage()
		return nil
	} else if *showVersion {
		fmt.Printf("Version %s\n", version)
		return nil
	}

	return phoenix.NewServer("myapp", version).
		Config(configPath).
		Log(logPath).
		CpuProfile(cpuprofile).
		MemProfile(memprofile).
		Run(func(runtime phoenix.Runtime) error {
            runtime.DefaultHTTPHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.Header().Set("Content-Type", "text/plain")
                if _, err := w.Write([]byte("Hello again, phoenix!\n")); err != nil {
					runtime.Printf("Failed to write response: %v", err)
				}
            }))

            return runtime.Start()
        })
}

func main() {
   if err := boot(); err != nil {
       os.Exit(-1)
   }
}

License

This package is licensed by struktur AG under the 3-clause BSD license, see LICENSE for more details.