bo

package module
v1.1.1 Latest Latest
Warning

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

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

README

bo

Go Reference

🚀 bo, BootKit for easily bootstrapping multi-goroutine applications, CLIs, can be used as drop-in replacement of uber/fx

  • Forget about signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
  • Forget about context.WithCancel(context.Background())
  • Forget about error passing through multiple layers
bo.New()
bo.Add(YourService())
bo.Add(AnotherService())
bo.Start()

That's it!

Getting Started

go get github.com/nekomeowww/bo

Usage

package main

import (
	"context"
	"net/http"
	"time"

	"github.com/labstack/echo/v4"
	"github.com/nekomeowww/bo"
)

func NewServer1() *http.Server {
	e := echo.New()
	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "Hello, World!")
	})

	return &http.Server{
		Addr:        ":8080",
		Handler:     e,
		ReadTimeout: time.Second * 10,
	}
}

func NewServer2() *http.Server {
	e := echo.New()
	e.GET("/", func(c echo.Context) error {
		return c.JSON(http.StatusOK, map[string]interface{}{
			"message": "Hello, World!",
		})
	})

	return &http.Server{
		Addr:        ":8081",
		Handler:     e,
		ReadTimeout: time.Second * 10,
	}
}

func main() {
	app := bo.New()

	app.Add(func(ctx context.Context, lifeCycle bo.LifeCycle) error {
		srv := NewServer1()

		lifeCycle.Append(bo.Hook{
			OnStart: func(ctx context.Context) error {
				return srv.ListenAndServe()
			},
			OnStop: func(ctx context.Context) error {
				return srv.Shutdown(ctx)
			},
		})

		return nil
	})

	app.Add(func(ctx context.Context, lifeCycle bo.LifeCycle) error {
		srv := NewServer2()

		lifeCycle.Append(bo.Hook{
			OnStart: func(ctx context.Context) error {
				return srv.ListenAndServe()
			},
			OnStop: func(ctx context.Context) error {
				return srv.Shutdown(ctx)
			},
		})

		return nil
	})

	app.Start()
}

🤠 Spec

GoDoc: https://godoc.org/github.com/nekomeowww/bo

👪 Other family members of anyo

Documentation

Index

Constants

View Source
const (
	DefaultStartTimeout = time.Second * 15
	DefaultStopTimeout  = time.Second * 60
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BootKit

type BootKit struct {
	// contains filtered or unexported fields
}

func New

func New(options ...Option) *BootKit

func (*BootKit) Add

func (b *BootKit) Add(invokeFn Runnable) *BootKit

func (*BootKit) Start

func (b *BootKit) Start()

func (*BootKit) Stop

func (b *BootKit) Stop(ctx context.Context) error

type EmptyLifeCycle added in v1.0.1

type EmptyLifeCycle struct{}

func (*EmptyLifeCycle) Append added in v1.0.1

func (*EmptyLifeCycle) Append(Hook)

type Hook

type Hook struct {
	OnStart func(ctx context.Context) error
	OnStop  func(ctx context.Context) error
}

func (Hook) Start

func (l Hook) Start(ctx context.Context) error

func (Hook) Stop

func (l Hook) Stop(ctx context.Context) error

type LifeCycle

type LifeCycle interface {
	Append(hook Hook)
}

func NewEmptyLifeCycle added in v1.0.1

func NewEmptyLifeCycle() LifeCycle

type Option

type Option interface {
	// contains filtered or unexported methods
}

func StartTimeout

func StartTimeout(duration time.Duration) Option

func StopTimeout

func StopTimeout(duration time.Duration) Option

type Runnable

type Runnable func(ctx context.Context, lifeCycle LifeCycle) error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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