@@ -14,19 +14,15 @@ use spin_world::v2::sqlite as v2;
14
14
15
15
pub use runtime_config:: RuntimeConfig ;
16
16
17
+ #[ derive( Default ) ]
17
18
pub struct SqliteFactor {
18
- default_label_resolver : Arc < dyn DefaultLabelResolver > ,
19
+ _priv : ( ) ,
19
20
}
20
21
21
22
impl SqliteFactor {
22
23
/// Create a new `SqliteFactor`
23
- ///
24
- /// Takes a `default_label_resolver` for how to handle when a database label doesn't
25
- /// have a corresponding runtime configuration.
26
- pub fn new ( default_label_resolver : impl DefaultLabelResolver + ' static ) -> Self {
27
- Self {
28
- default_label_resolver : Arc :: new ( default_label_resolver) ,
29
- }
24
+ pub fn new ( ) -> Self {
25
+ Self { _priv : ( ) }
30
26
}
31
27
}
32
28
@@ -50,8 +46,8 @@ impl Factor for SqliteFactor {
50
46
) -> anyhow:: Result < Self :: AppState > {
51
47
let connection_creators = ctx
52
48
. take_runtime_config ( )
53
- . map ( |r| r . connection_creators )
54
- . unwrap_or_default ( ) ;
49
+ . unwrap_or_default ( )
50
+ . connection_creators ;
55
51
56
52
let allowed_databases = ctx
57
53
. app ( )
@@ -69,19 +65,12 @@ impl Factor for SqliteFactor {
69
65
) )
70
66
} )
71
67
. collect :: < anyhow:: Result < HashMap < _ , _ > > > ( ) ?;
72
- let resolver = self . default_label_resolver . clone ( ) ;
73
- let get_connection_creator: host:: ConnectionCreatorGetter = Arc :: new ( move |label| {
74
- connection_creators
75
- . get ( label)
76
- . cloned ( )
77
- . or_else ( || resolver. default ( label) )
78
- } ) ;
79
68
80
69
ensure_allowed_databases_are_configured ( & allowed_databases, |label| {
81
- get_connection_creator ( label ) . is_some ( )
70
+ connection_creators . contains_key ( label )
82
71
} ) ?;
83
72
84
- Ok ( AppState :: new ( allowed_databases, get_connection_creator ) )
73
+ Ok ( AppState :: new ( allowed_databases, connection_creators ) )
85
74
}
86
75
87
76
fn prepare < T : spin_factors:: RuntimeFactors > (
@@ -94,10 +83,9 @@ impl Factor for SqliteFactor {
94
83
. get ( ctx. app_component ( ) . id ( ) )
95
84
. cloned ( )
96
85
. unwrap_or_default ( ) ;
97
- let get_connection_creator = ctx. app_state ( ) . get_connection_creator . clone ( ) ;
98
86
Ok ( InstanceState :: new (
99
87
allowed_databases,
100
- get_connection_creator ,
88
+ ctx . app_state ( ) . connection_creators . clone ( ) ,
101
89
) )
102
90
}
103
91
}
@@ -138,31 +126,23 @@ fn ensure_allowed_databases_are_configured(
138
126
/// Metadata key for a list of allowed databases for a component.
139
127
pub const ALLOWED_DATABASES_KEY : MetadataKey < Vec < String > > = MetadataKey :: new ( "databases" ) ;
140
128
141
- /// Resolves a label to a default connection creator.
142
- pub trait DefaultLabelResolver : Send + Sync {
143
- /// If there is no runtime configuration for a given database label, return a default connection creator.
144
- ///
145
- /// If `Option::None` is returned, the database is not allowed.
146
- fn default ( & self , label : & str ) -> Option < Arc < dyn ConnectionCreator > > ;
147
- }
148
-
149
129
#[ derive( Clone ) ]
150
130
pub struct AppState {
151
131
/// A map from component id to a set of allowed database labels.
152
132
allowed_databases : HashMap < String , Arc < HashSet < String > > > ,
153
- /// A function for mapping from database name to a connection creator.
154
- get_connection_creator : host :: ConnectionCreatorGetter ,
133
+ /// A mapping from database label to a connection creator.
134
+ connection_creators : HashMap < String , Arc < dyn ConnectionCreator > > ,
155
135
}
156
136
157
137
impl AppState {
158
138
/// Create a new `AppState`
159
139
pub fn new (
160
140
allowed_databases : HashMap < String , Arc < HashSet < String > > > ,
161
- get_connection_creator : host :: ConnectionCreatorGetter ,
141
+ connection_creators : HashMap < String , Arc < dyn ConnectionCreator > > ,
162
142
) -> Self {
163
143
Self {
164
144
allowed_databases,
165
- get_connection_creator ,
145
+ connection_creators ,
166
146
}
167
147
}
168
148
@@ -173,7 +153,9 @@ impl AppState {
173
153
& self ,
174
154
label : & str ,
175
155
) -> Option < Result < Box < dyn Connection > , v2:: Error > > {
176
- let connection = ( self . get_connection_creator ) ( label) ?
156
+ let connection = self
157
+ . connection_creators
158
+ . get ( label) ?
177
159
. create_connection ( label)
178
160
. await ;
179
161
Some ( connection)
0 commit comments