cli/docs/framework/services.md
Snider a57fb4273d docs: restructure for VitePress with flat layout and examples
- Remove mkdocs files (requirements.txt, CNAME)
- Add CLI documentation: build.md, release.md, php.md, run.md
- Add configuration.md with full reference
- Add examples/ directory with sample configurations:
  - go-cli-release.yaml
  - wails-desktop-release.yaml
  - php-laravel-release.yaml
  - linuxkit-server.yml
  - linuxkit-docker-format.yml
  - full-release.yaml
  - minimal-release.yaml
  - official-repos.yaml
- Flatten existing framework docs into framework/
- Update index.md as CLI entry point

Ready for VitePress integration with core-php/docs.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 00:38:49 +00:00

2.4 KiB

Services

Services are the building blocks of a Core application. Each service encapsulates a specific domain of functionality.

Creating a Service

package myservice

import (
    "context"
    "github.com/Snider/Core/pkg/core"
)

type Service struct {
    core *core.Core
}

// Factory function for registration
func NewService(c *core.Core) (any, error) {
    return &Service{core: c}, nil
}

// Implement Startable for startup logic
func (s *Service) OnStartup(ctx context.Context) error {
    // Initialize resources
    return nil
}

// Implement Stoppable for cleanup
func (s *Service) OnShutdown(ctx context.Context) error {
    // Release resources
    return nil
}

Registering Services

c, err := core.New(
    // Auto-discover name from package path
    core.WithService(myservice.NewService),

    // Explicit name
    core.WithName("custom", func(c *core.Core) (any, error) {
        return &CustomService{}, nil
    }),
)

Retrieving Services

// Safe retrieval with error
svc, err := core.ServiceFor[*myservice.Service](c, "myservice")
if err != nil {
    log.Printf("Service not found: %v", err)
}

// Must retrieval (panics if not found)
svc := core.MustServiceFor[*myservice.Service](c, "myservice")

Service Dependencies

Services can depend on other services:

func NewOrderService(c *core.Core) (any, error) {
    // Get required dependencies
    userSvc := core.MustServiceFor[*user.Service](c, "user")
    paymentSvc := core.MustServiceFor[*payment.Service](c, "payment")

    return &OrderService{
        users:    userSvc,
        payments: paymentSvc,
    }, nil
}

!!! warning "Dependency Order" Register dependencies before services that use them. Core does not automatically resolve dependency order.

Exposing to Frontend

Services are automatically exposed to the frontend via Wails bindings:

// Go service method
func (s *Service) GetUser(id string) (*User, error) {
    return s.db.FindUser(id)
}
// TypeScript (auto-generated)
import { GetUser } from '@bindings/myservice/service';

const user = await GetUser("123");

Testing Services

func TestMyService(t *testing.T) {
    // Create mock core
    c, _ := core.New()

    // Create service
    svc, err := NewService(c)
    if err != nil {
        t.Fatal(err)
    }

    // Test methods
    result := svc.(*Service).DoSomething()
    assert.Equal(t, expected, result)
}