Services

// services/postservice/postservice.go
package postservice

import "context"

type Post struct {
	ID    int
	Title string
}

type Service interface {
	Latest() []Post
	Find(id int) (*Post, error)
}

type service struct{}
type contextKey struct{}

// New matches lazydeps.Func[Service], installs the service in context, and may
// return cleanup for pools, goroutines, files, listeners, or clients.
func New(ctx context.Context) (context.Context, Service, error, context.CancelFunc) {
	service := service{}
	return WithContext(ctx, service), service, nil, nil
}

func WithContext(ctx context.Context, service Service) context.Context {
	return context.WithValue(ctx, contextKey{}, service)
}

func FromContext(ctx context.Context) (Service, bool) {
	service, ok := ctx.Value(contextKey{}).(Service)
	return service, ok
}

func (service) Latest() []Post {
	return []Post{{ID: 1, Title: "Hello"}}
}

func (service) Find(id int) (*Post, error) {
	return &Post{ID: id, Title: "Hello"}, nil
}
// init/dependencies.go
package appinit

import (
	"golazy.dev/lazydeps"
	"sample_app/services/postservice"
)

func Dependencies(deps *lazydeps.Scope) error {
	_, err := lazydeps.Service(deps, "postservice", postservice.New)
	return err
}