spin_factor_outbound_pg/
lib.rs1pub mod client;
2mod host;
3mod types;
4
5use client::ClientFactory;
6use spin_factor_otel::OtelFactorState;
7use spin_factor_outbound_networking::{
8 config::allowed_hosts::OutboundAllowedHosts, OutboundNetworkingFactor,
9};
10use spin_factors::{
11 anyhow, ConfigureAppContext, Factor, FactorData, PrepareContext, RuntimeFactors,
12 SelfInstanceBuilder,
13};
14use std::sync::Arc;
15
16pub struct OutboundPgFactor<CF = crate::client::PooledTokioClientFactory> {
17 _phantom: std::marker::PhantomData<CF>,
18}
19
20impl<CF: ClientFactory> Factor for OutboundPgFactor<CF> {
21 type RuntimeConfig = ();
22 type AppState = Arc<CF>;
23 type InstanceBuilder = InstanceState<CF>;
24
25 fn init(&mut self, ctx: &mut impl spin_factors::InitContext<Self>) -> anyhow::Result<()> {
26 ctx.link_bindings(spin_world::v1::postgres::add_to_linker::<_, FactorData<Self>>)?;
27 ctx.link_bindings(spin_world::v2::postgres::add_to_linker::<_, FactorData<Self>>)?;
28 ctx.link_bindings(
29 spin_world::spin::postgres3_0_0::postgres::add_to_linker::<_, FactorData<Self>>,
30 )?;
31 ctx.link_bindings(
32 spin_world::spin::postgres4_0_0::postgres::add_to_linker::<_, FactorData<Self>>,
33 )?;
34 Ok(())
35 }
36
37 fn configure_app<T: RuntimeFactors>(
38 &self,
39 _ctx: ConfigureAppContext<T, Self>,
40 ) -> anyhow::Result<Self::AppState> {
41 Ok(Arc::new(CF::default()))
42 }
43
44 fn prepare<T: RuntimeFactors>(
45 &self,
46 mut ctx: PrepareContext<T, Self>,
47 ) -> anyhow::Result<Self::InstanceBuilder> {
48 let allowed_hosts = ctx
49 .instance_builder::<OutboundNetworkingFactor>()?
50 .allowed_hosts();
51 let otel = OtelFactorState::from_prepare_context(&mut ctx)?;
52
53 Ok(InstanceState {
54 allowed_hosts,
55 client_factory: ctx.app_state().clone(),
56 connections: Default::default(),
57 otel,
58 })
59 }
60}
61
62impl<C> Default for OutboundPgFactor<C> {
63 fn default() -> Self {
64 Self {
65 _phantom: Default::default(),
66 }
67 }
68}
69
70impl<C> OutboundPgFactor<C> {
71 pub fn new() -> Self {
72 Self::default()
73 }
74}
75
76pub struct InstanceState<CF: ClientFactory> {
77 allowed_hosts: OutboundAllowedHosts,
78 client_factory: Arc<CF>,
79 connections: spin_resource_table::Table<CF::Client>,
80 otel: OtelFactorState,
81}
82
83impl<CF: ClientFactory> SelfInstanceBuilder for InstanceState<CF> {}