-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.go
More file actions
170 lines (141 loc) · 3.78 KB
/
framework.go
File metadata and controls
170 lines (141 loc) · 3.78 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Package codo provides the main framework entry point for the Codo Framework.
package codo
import (
"context"
"fmt"
"github.com/codoworks/codo-framework/core/clients"
)
// Version is the framework version, set at build time.
var Version = "0.0.0-dev"
// State represents the framework state.
type State int
const (
// StateNew indicates the framework is newly created.
StateNew State = iota
// StateInitialized indicates the framework is initialized.
StateInitialized
// StateRunning indicates the framework is running.
StateRunning
// StateStopped indicates the framework is stopped.
StateStopped
)
// String returns the string representation of the state.
func (s State) String() string {
switch s {
case StateNew:
return "new"
case StateInitialized:
return "initialized"
case StateRunning:
return "running"
case StateStopped:
return "stopped"
default:
return "unknown"
}
}
// Config holds framework configuration.
type Config struct {
// Name is the application name.
Name string `json:"name" yaml:"name"`
// Environment is the application environment (e.g., development, production).
Environment string `json:"environment" yaml:"environment"`
// Clients holds client configurations keyed by client name.
Clients map[string]any `json:"clients" yaml:"clients"`
}
// DefaultConfig returns default framework configuration.
func DefaultConfig() *Config {
return &Config{
Name: "codo-app",
Environment: "development",
Clients: make(map[string]any),
}
}
// Framework represents the main framework instance.
type Framework struct {
config *Config
state State
}
// New creates a new framework instance.
func New() *Framework {
return &Framework{
config: DefaultConfig(),
state: StateNew,
}
}
// NewWithConfig creates a new framework instance with configuration.
func NewWithConfig(cfg *Config) *Framework {
if cfg == nil {
cfg = DefaultConfig()
}
return &Framework{
config: cfg,
state: StateNew,
}
}
// Config returns the framework configuration.
func (f *Framework) Config() *Config {
return f.config
}
// State returns the current framework state.
func (f *Framework) State() State {
return f.state
}
// IsInitialized returns true if the framework is initialized.
func (f *Framework) IsInitialized() bool {
return f.state >= StateInitialized
}
// IsRunning returns true if the framework is running.
func (f *Framework) IsRunning() bool {
return f.state == StateRunning
}
// Initialize sets up the framework and all registered clients.
func (f *Framework) Initialize() error {
if f.state >= StateInitialized {
return nil
}
// Initialize all registered clients
if err := clients.InitializeAll(f.config.Clients); err != nil {
return fmt.Errorf("failed to initialize clients: %w", err)
}
f.state = StateInitialized
return nil
}
// Run starts the framework.
// This is a placeholder for HTTP server startup in future workstreams.
func (f *Framework) Run() error {
if f.state < StateInitialized {
if err := f.Initialize(); err != nil {
return err
}
}
if f.state == StateRunning {
return fmt.Errorf("framework is already running")
}
f.state = StateRunning
return nil
}
// Shutdown gracefully shuts down the framework.
func (f *Framework) Shutdown(ctx context.Context) error {
if f.state == StateStopped {
return nil
}
// Shutdown all clients
if err := clients.ShutdownAll(); err != nil {
return fmt.Errorf("failed to shutdown clients: %w", err)
}
f.state = StateStopped
return nil
}
// Health checks the health of all framework components.
func (f *Framework) Health() error {
return clients.HealthCheck()
}
// HealthAll returns health status of all clients.
func (f *Framework) HealthAll() map[string]error {
return clients.HealthAll()
}
// GetVersion returns the framework version.
func GetVersion() string {
return Version
}