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