anchor
Library to manage component lifetime in a Go microservice architecture.
Features
- Run Components in parallel using Go Routines
- Simple API to manage component lifetime
- Graceful shutdown of components
- Freedom of choise for dependency injection
- Convenience methods to wire external APIs into Anchor
Quickstart
cmd/main.go
package main import ( "github.com/kyuff/anchor" "example.com/myapp/internal/app"" ) func main() { os.Exit(app.Run(anchor.DefaultSignalWire())) }
internal/app/anchor.go
package app import ( "github.com/kyuff/anchor" ) func Run(wire anchor.Wire) int { return anchor.New(wire, anchor.WithDefaultSlog(). Add( anchor.Close("database.Connection", func() io.Closer { return database() }), NewHttpServer() ). Run() }
internal/app/database.go
package app import ( "database/sql" "os" ) var database = anchor.Singleton(func() (*sql.DB, error) { return sql.Open("postgres", os.Getenv("DATABASE_URL") })
internal/app/http.go
package app import ( "context" "net/http" "os" "example.com/myapp/internal/api" ) type HttpServer struct { server *http.Server } func NewHttpServer() *HttpServer { return &HttpServer{ server: &http.Server{ Addr: os.Getenv("HTTP_ADDR"), }, } } func (h *HttpServer) Setup(ctx context.Context) error { return api.Register(http.DefaultServeMux, database()) } func (h *HttpServer) Start(ctx context.Context) error { return h.server.ListenAndServe() } func (h *HttpServer) Close(ctx context.Context) error { return h.server.Shutdown(ctx) }