-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice_template.go.tmpl
More file actions
108 lines (90 loc) · 3.01 KB
/
Copy pathservice_template.go.tmpl
File metadata and controls
108 lines (90 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package {{.PackageName}}
import (
"context"
"fmt"
"github.com/xraph/forge"
)
// {{.ServiceName}} wraps the {{.Name}} implementation and provides lifecycle management.
// It implements vessel's di.Service interface so Vessel can manage its lifecycle.
type {{.ServiceName}} struct {
config Config
backend {{.BackendInterface}}
logger forge.Logger
metrics forge.Metrics
}
// New{{.ServiceName}} creates a new {{.Name}} service with the given configuration.
// This is the constructor that will be registered with the DI container.
func New{{.ServiceName}}(config Config, logger forge.Logger, metrics forge.Metrics) (*{{.ServiceName}}, error) {
// Validate config
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("invalid {{.Name}} config: %w", err)
}
// Create backend based on config
backend, err := createBackend(config, logger, metrics)
if err != nil {
return nil, fmt.Errorf("failed to create {{.Name}} backend: %w", err)
}
return &{{.ServiceName}}{
config: config,
backend: backend,
logger: logger,
metrics: metrics,
}, nil
}
// Name returns the service name for Vessel's lifecycle management.
func (s *{{.ServiceName}}) Name() string {
return "{{.ServiceKey}}"
}
// Start starts the {{.Name}} service.
// This is called automatically by Vessel during container.Start().
func (s *{{.ServiceName}}) Start(ctx context.Context) error {
s.logger.Info("starting {{.Name}} service",
forge.F("config", s.config),
)
if err := s.backend.Connect(ctx); err != nil {
return fmt.Errorf("failed to connect to {{.Name}}: %w", err)
}
s.logger.Info("{{.Name}} service started")
return nil
}
// Stop stops the {{.Name}} service.
// This is called automatically by Vessel during container.Stop().
func (s *{{.ServiceName}}) Stop(ctx context.Context) error {
s.logger.Info("stopping {{.Name}} service")
if s.backend != nil {
if err := s.backend.Disconnect(ctx); err != nil {
s.logger.Error("failed to disconnect {{.Name}}",
forge.F("error", err),
)
// Don't return error, log and continue
}
}
s.logger.Info("{{.Name}} service stopped")
return nil
}
// Health checks if the {{.Name}} service is healthy.
// This is called by the health check system.
func (s *{{.ServiceName}}) Health(ctx context.Context) error {
if s.backend == nil {
return fmt.Errorf("{{.Name}} not initialized")
}
if err := s.backend.Ping(ctx); err != nil {
return fmt.Errorf("{{.Name}} health check failed: %w", err)
}
return nil
}
// Backend returns the underlying {{.Name}} implementation.
// This allows other services to use the {{.Name}} directly.
func (s *{{.ServiceName}}) Backend() {{.BackendInterface}} {
return s.backend
}
// Helper function to create backend
func createBackend(config Config, logger forge.Logger, metrics forge.Metrics) ({{.BackendInterface}}, error) {
// TODO: Implement backend creation based on config.Driver
switch config.Driver {
case "default":
return NewDefaultBackend(config, logger, metrics), nil
default:
return nil, fmt.Errorf("unknown {{.Name}} driver: %s", config.Driver)
}
}