spin_factor_outbound_mysql/
lib.rs

1pub mod client;
2mod host;
3
4use client::Client;
5use mysql_async::Conn as MysqlClient;
6use spin_factor_outbound_networking::{OutboundAllowedHosts, OutboundNetworkingFactor};
7use spin_factors::{Factor, InitContext, RuntimeFactors, SelfInstanceBuilder};
8use spin_world::v1::mysql as v1;
9use spin_world::v2::mysql::{self as v2};
10
11pub struct OutboundMysqlFactor<C = MysqlClient> {
12    _phantom: std::marker::PhantomData<C>,
13}
14
15impl<C: Send + Sync + Client + 'static> Factor for OutboundMysqlFactor<C> {
16    type RuntimeConfig = ();
17    type AppState = ();
18    type InstanceBuilder = InstanceState<C>;
19
20    fn init<T: Send + 'static>(&mut self, mut ctx: InitContext<T, Self>) -> anyhow::Result<()> {
21        ctx.link_bindings(v1::add_to_linker)?;
22        ctx.link_bindings(v2::add_to_linker)?;
23        Ok(())
24    }
25
26    fn configure_app<T: RuntimeFactors>(
27        &self,
28        _ctx: spin_factors::ConfigureAppContext<T, Self>,
29    ) -> anyhow::Result<Self::AppState> {
30        Ok(())
31    }
32
33    fn prepare<T: spin_factors::RuntimeFactors>(
34        &self,
35        mut ctx: spin_factors::PrepareContext<T, Self>,
36    ) -> anyhow::Result<Self::InstanceBuilder> {
37        let allowed_hosts = ctx
38            .instance_builder::<OutboundNetworkingFactor>()?
39            .allowed_hosts();
40        Ok(InstanceState {
41            allowed_hosts,
42            connections: Default::default(),
43        })
44    }
45}
46
47impl<C> Default for OutboundMysqlFactor<C> {
48    fn default() -> Self {
49        Self {
50            _phantom: Default::default(),
51        }
52    }
53}
54
55impl<C> OutboundMysqlFactor<C> {
56    pub fn new() -> Self {
57        Self::default()
58    }
59}
60
61pub struct InstanceState<C> {
62    allowed_hosts: OutboundAllowedHosts,
63    connections: spin_resource_table::Table<C>,
64}
65
66impl<C: Send + 'static> SelfInstanceBuilder for InstanceState<C> {}