lifecycle

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2021 License: MIT Imports: 9 Imported by: 5

README ¶

lifecycle

PkgGoDev license Go Report Card codecov Build Status

English | 中文

📖 Introduction

A simple application lifecycle management tool with multiple servers.

🚀 Features

  • Easy to attach servers to application.
  • Convenient to get metadata from application.
  • Handle server shutdown gracefully.
  • Provide hooks for cleanup function.

🧰 How to install

go get -u github.com/qmdx00/lifecycle

🛠 How to use

Just implement server interface, then attach servers to application, here is an example.

package main

import (
    "context"
    "github.com/qmdx00/lifecycle"
    "log"
    "net/http"
)

func main() {
    app := lifecycle.NewApp(
        lifecycle.WithName("test"),
        lifecycle.WithVersion("v1.0"),
    )

    app.Attach("echo", NewEchoServer())
    app.Cleanup(func() error {
        log.Println("do cleanup")
        return nil
    })

    if err := app.Run(); err != nil {
        log.Fatal(err)
    }
}

func NewEchoServer() lifecycle.Server {
    handler := http.NewServeMux()
    handler.HandleFunc("/echo", func(writer http.ResponseWriter, request *http.Request) {
        _, _ = writer.Write([]byte("hello world"))
    })

    return &EchoServer{
        srv: &http.Server{
            Addr:    ":3000",
            Handler: handler,
        },
    }
}

type EchoServer struct {
    srv *http.Server
}

func (e *EchoServer) Run(ctx context.Context) error {
    info, _ := lifecycle.FromContext(ctx)
    log.Printf("server %s start\n", info.Name())
    return e.srv.ListenAndServe()
}

func (e *EchoServer) Stop(ctx context.Context) error {
    info, _ := lifecycle.FromContext(ctx)
    log.Printf("server %s stop\n", info.Name())
    return e.srv.Shutdown(ctx)
}

📄 License

© Wimi Yuan, 2021~time.Now
Released under the MIT License.

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func NewContext ¶

func NewContext(ctx context.Context, s AppInfo) context.Context

NewContext returns a new Context that carries AppInfo

Types ¶

type App ¶

type App struct {
	sync.Mutex
	// contains filtered or unexported fields
}

App application struct

func NewApp ¶

func NewApp(opts ...Option) *App

NewApp ...

func (*App) Attach ¶

func (a *App) Attach(name string, server Server)

Attach add named server ...

func (*App) Cleanup ¶

func (a *App) Cleanup(f ...cleanup)

Cleanup do cleanup functions

func (*App) ID ¶

func (a *App) ID() string

ID ...

func (*App) Metadata ¶

func (a *App) Metadata() map[string]string

Metadata ...

func (*App) Name ¶

func (a *App) Name() string

Name ...

func (*App) Run ¶

func (a *App) Run() (_ error)

Run start application ...

func (*App) Shutdown ¶

func (a *App) Shutdown() (_ error)

Shutdown gracefully stops the application

func (*App) Version ¶

func (a *App) Version() string

Version ...

type AppInfo ¶

type AppInfo interface {
	// ID app id
	ID() string
	// Name app name
	Name() string
	// Version app version
	Version() string
	// Metadata app stored metadata
	Metadata() map[string]string
}

AppInfo application context value

func FromContext ¶

func FromContext(ctx context.Context) (s AppInfo, ok bool)

FromContext returns the AppInfo value stored in ctx, if any

type Option ¶

type Option func(*options)

Option application option

func WithContext ¶

func WithContext(ctx context.Context) Option

WithContext with service context

func WithID ¶

func WithID(id string) Option

WithID with application id

func WithMetadata ¶

func WithMetadata(md map[string]string) Option

WithMetadata with application metadata

func WithName ¶

func WithName(name string) Option

WithName with application name

func WithSignal ¶

func WithSignal(sigs ...os.Signal) Option

WithSignal with exit signals

func WithStopTimeout ¶ added in v1.1.0

func WithStopTimeout(timeout time.Duration) Option

WithStopTimeout with app stop timeout.

func WithVersion ¶

func WithVersion(version string) Option

WithVersion with application version

type Server ¶

type Server interface {
	// Run func for server start
	Run(ctx context.Context) error
	// Stop func for server shutdown
	Stop(ctx context.Context) error
}

Server ...

Jump to

Keyboard shortcuts

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