chore: bootstrap api-go scaffold
This commit is contained in:
1 parent
aa26549bd1
commit
907164f972
18 files changed
+1919
No files matched your search
@@ -0,0 +1,47 @@
|
||||
// Package scenario holds the single in-memory scenario state.
|
||||
package scenario
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"pulsecast/api/internal/model"
|
||||
)
|
||||
|
||||
// State is safe for concurrent readers and writers.
|
||||
type State struct {
|
||||
mu sync.RWMutex
|
||||
current model.ScenarioState
|
||||
}
|
||||
|
||||
// New returns the initial base scenario at version 1.
|
||||
func New() *State {
|
||||
return &State{current: model.ScenarioState{
|
||||
Version: 1,
|
||||
Scenario: model.ScenarioInput{
|
||||
Scenario: "base",
|
||||
Weather: model.WeatherInput{},
|
||||
Event: model.EventInput{},
|
||||
},
|
||||
At: time.Now().UTC().Truncate(time.Second),
|
||||
}}
|
||||
}
|
||||
|
||||
// Get returns a consistent snapshot.
|
||||
func (s *State) Get() model.ScenarioState {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.current
|
||||
}
|
||||
|
||||
// Set stores a new scenario and returns its snapshot.
|
||||
func (s *State) Set(in model.ScenarioInput) model.ScenarioState {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.current = model.ScenarioState{
|
||||
Version: s.current.Version + 1,
|
||||
Scenario: in,
|
||||
At: time.Now().UTC().Truncate(time.Second),
|
||||
}
|
||||
return s.current
|
||||
}
|
||||
Reference in new issue
Block a user