Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(runtime): hide internals from public-facing API, export tokio #1332

Merged
merged 8 commits into from Oct 27, 2023
54 changes: 30 additions & 24 deletions codegen/src/shuttle_main/mod.rs
Expand Up @@ -14,9 +14,15 @@ pub(crate) fn r#impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
let loader = Loader::from_item_fn(&mut fn_decl);

quote! {
#[tokio::main]
async fn main() {
shuttle_runtime::start(loader).await;
fn main() {
// manual expansion of #[tokio::main]
::shuttle_runtime::tokio::runtime::Builder::new_multi_thread()
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
.enable_all()
.build()
.unwrap()
.block_on(async {
::shuttle_runtime::__internals::start(loader).await;
})
}

#loader
Expand Down Expand Up @@ -200,7 +206,7 @@ impl ToTokens for Loader {
lit: Lit::Str(str), ..
}) => {
needs_vars = true;
quote!(&shuttle_runtime::strfmt(#str, &vars)?)
quote!(&::shuttle_runtime::__internals::strfmt(#str, &vars)?)
}
other => quote!(#other),
};
Expand Down Expand Up @@ -228,7 +234,7 @@ impl ToTokens for Loader {
None
} else {
Some(parse_quote!(
use shuttle_runtime::{Factory, ResourceBuilder};
use ::shuttle_runtime::{Factory, ResourceBuilder};
))
};

Expand All @@ -254,13 +260,13 @@ impl ToTokens for Loader {

let loader = quote! {
async fn loader(
mut #factory_ident: shuttle_runtime::ProvisionerFactory,
mut #resource_tracker_ident: shuttle_runtime::ResourceTracker,
mut #factory_ident: ::shuttle_runtime::__internals::ProvisionerFactory,
mut #resource_tracker_ident: ::shuttle_runtime::__internals::ResourceTracker,
) -> #return_type {
use shuttle_runtime::Context;
use ::shuttle_runtime::__internals::Context;
#extra_imports
#vars
#(let #fn_inputs = shuttle_runtime::get_resource(
#(let #fn_inputs = ::shuttle_runtime::__internals::get_resource(
#fn_inputs_builder::new()#fn_inputs_builder_options,
&mut #factory_ident,
&mut #resource_tracker_ident,
Expand Down Expand Up @@ -323,10 +329,10 @@ mod tests {
let actual = quote!(#input);
let expected = quote! {
async fn loader(
mut _factory: shuttle_runtime::ProvisionerFactory,
mut _resource_tracker: shuttle_runtime::ResourceTracker,
mut _factory: ::shuttle_runtime::__internals::ProvisionerFactory,
mut _resource_tracker: ::shuttle_runtime::__internals::ResourceTracker,
) -> ShuttleSimple {
use shuttle_runtime::Context;
use ::shuttle_runtime::__internals::Context;
simple().await
}
};
Expand Down Expand Up @@ -391,17 +397,17 @@ mod tests {
let actual = quote!(#input);
let expected = quote! {
async fn loader(
mut factory: shuttle_runtime::ProvisionerFactory,
mut resource_tracker: shuttle_runtime::ResourceTracker,
mut factory: ::shuttle_runtime::__internals::ProvisionerFactory,
mut resource_tracker: ::shuttle_runtime::__internals::ResourceTracker,
) -> ShuttleComplex {
use shuttle_runtime::Context;
use shuttle_runtime::{Factory, ResourceBuilder};
let pool = shuttle_runtime::get_resource(
use ::shuttle_runtime::__internals::Context;
use ::shuttle_runtime::{Factory, ResourceBuilder};
let pool = ::shuttle_runtime::__internals::get_resource(
shuttle_shared_db::Postgres::new(),
&mut factory,
&mut resource_tracker,
).await.context(format!("failed to provision {}", stringify!(shuttle_shared_db::Postgres)))?;
let redis = shuttle_runtime::get_resource(
let redis = ::shuttle_runtime::__internals::get_resource(
shuttle_shared_db::Redis::new(),
&mut factory,
&mut resource_tracker,
Expand Down Expand Up @@ -502,14 +508,14 @@ mod tests {
let actual = quote!(#input);
let expected = quote! {
async fn loader(
mut factory: shuttle_runtime::ProvisionerFactory,
mut resource_tracker: shuttle_runtime::ResourceTracker,
mut factory: ::shuttle_runtime::__internals::ProvisionerFactory,
mut resource_tracker: ::shuttle_runtime::__internals::ResourceTracker,
) -> ShuttleComplex {
use shuttle_runtime::Context;
use shuttle_runtime::{Factory, ResourceBuilder};
use ::shuttle_runtime::__internals::Context;
use ::shuttle_runtime::{Factory, ResourceBuilder};
let vars = std::collections::HashMap::from_iter(factory.get_secrets().await?.into_iter().map(|(key, value)| (format!("secrets.{}", key), value.expose().clone())));
let pool = shuttle_runtime::get_resource (
shuttle_shared_db::Postgres::new().size(&shuttle_runtime::strfmt("10Gb", &vars)?).public(false),
let pool = ::shuttle_runtime::__internals::get_resource (
shuttle_shared_db::Postgres::new().size(&::shuttle_runtime::__internals::strfmt("10Gb", &vars)?).public(false),
&mut factory,
&mut resource_tracker,
).await.context(format!("failed to provision {}", stringify!(shuttle_shared_db::Postgres)))?;
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/alpha/mod.rs
Expand Up @@ -40,7 +40,7 @@ use tonic::{
};
use tower::ServiceBuilder;

use crate::{print_version, provisioner_factory::ProvisionerFactory, ResourceTracker};
use crate::__internals::{print_version, ProvisionerFactory, ResourceTracker};

use self::args::Args;

Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bin/shuttle-next.rs
Expand Up @@ -5,7 +5,7 @@ use std::{

use shuttle_common::backends::tracing::ExtractPropagationLayer;
use shuttle_proto::runtime::runtime_server::RuntimeServer;
use shuttle_runtime::{print_version, AxumWasm, NextArgs};
use shuttle_runtime::__internals::{print_version, AxumWasm, NextArgs};
use tonic::transport::Server;

#[tokio::main(flavor = "multi_thread")]
Expand Down
65 changes: 36 additions & 29 deletions runtime/src/lib.rs
Expand Up @@ -27,10 +27,10 @@
//! be a binary crate with a few dependencies including `shuttle-runtime` and `shuttle-axum`.
//!
//! ```toml
//! shuttle-runtime = "0.30.1"
//! axum = "0.6.10"
//! axum = "0.6.20"
//! shuttle-axum = "0.30.1"
//! tokio = "1.26"
//! shuttle-runtime = "0.30.1"
//! tokio = "1.28.2"
//! ```
//!
//! A boilerplate code for your axum project can also be found in `src/main.rs`:
Expand All @@ -43,8 +43,8 @@
//! }
//!
//! #[shuttle_runtime::main]
//! async fn axum() -> shuttle_axum::ShuttleAxum {
//! let router = Router::new().route("/hello", get(hello_world));
//! async fn main() -> shuttle_axum::ShuttleAxum {
//! let router = Router::new().route("/", get(hello_world));
//!
//! Ok(router.into())
//! }
Expand All @@ -63,8 +63,7 @@
//! You should see your app build and start on the default port 8000. You can test this using;
//!
//! ```bash
//! $ curl http://localhost:8000/hello
//!
//! $ curl http://localhost:8000/
//! Hello, world!
//! ```
//!
Expand Down Expand Up @@ -96,7 +95,7 @@
//! Your service will immediately be available at `{crate_name}.shuttleapp.rs`. For example:
//!
//! ```bash
//! $ curl https://my-axum-app.shuttleapp.rs/hello
//! $ curl https://my-axum-app.shuttleapp.rs/
//! Hello, world!
//! ```
//!
Expand Down Expand Up @@ -129,7 +128,7 @@
//!
//! struct MyState(PgPool);
//!
//! #[get("/hello")]
//! #[get("/")]
//! fn hello(state: &State<MyState>) -> &'static str {
//! // Do things with `state.0`...
//! "Hello, Postgres!"
Expand Down Expand Up @@ -203,9 +202,14 @@
//! If you have any questions, [join our Discord server](https://discord.gg/shuttle). There's always someone on there that can help!
//!
//! You can also [open an issue or a discussion on GitHub](https://github.com/shuttle-hq/shuttle).
//!

// Public API
pub use shuttle_codegen::main;
pub use shuttle_service::{CustomError, Error, Factory, ResourceBuilder, Service};

// Useful re-exports
pub use async_trait::async_trait;
pub use tokio;

mod alpha;
mod args;
Expand All @@ -214,26 +218,29 @@ mod next;
mod provisioner_factory;
mod resource_tracker;

pub use alpha::{start, Alpha};
#[cfg(feature = "next")]
pub use next::{AxumWasm, NextArgs};
pub use provisioner_factory::ProvisionerFactory;
pub use resource_tracker::{get_resource, ResourceTracker};
pub use shuttle_service::{CustomError, Error, Factory, ResourceBuilder, Service};

pub use async_trait::async_trait;

// Dependencies required by the codegen
pub use anyhow::Context;
pub use strfmt::strfmt;

#[cfg(feature = "setup-tracing")]
pub use {colored, tracing_subscriber};

const NAME: &str = env!("CARGO_PKG_NAME");
const VERSION: &str = env!("CARGO_PKG_VERSION");

// Print the version of the runtime.
pub fn print_version() {
println!("{NAME} {VERSION}");
// Not part of public API
#[doc(hidden)]
pub mod __internals {
// Internals used by the codegen
pub use crate::alpha::{start, Alpha};
#[cfg(feature = "next")]
pub use crate::next::{AxumWasm, NextArgs};
pub use crate::provisioner_factory::ProvisionerFactory;
pub use crate::resource_tracker::{get_resource, ResourceTracker};

// Dependencies required by the codegen
pub use anyhow::Context;
#[cfg(feature = "setup-tracing")]
pub use colored;
pub use strfmt::strfmt;
#[cfg(feature = "setup-tracing")]
pub use tracing_subscriber;

// Print the version of the runtime.
pub fn print_version() {
println!("{} {}", crate::NAME, crate::VERSION);
}
}
2 changes: 1 addition & 1 deletion runtime/src/resource_tracker.rs
Expand Up @@ -6,7 +6,7 @@ use serde_json::Value;
use shuttle_common::resource::{self, Type};
use shuttle_service::ResourceBuilder;

use crate::ProvisionerFactory;
use crate::__internals::ProvisionerFactory;

/// Used to keep track of which resources have been provisioned in the past and what is being provisioned for this deployment
pub struct ResourceTracker {
Expand Down
3 changes: 1 addition & 2 deletions services/shuttle-poise/src/lib.rs
@@ -1,7 +1,6 @@
//! Shuttle service integration for the Poise discord bot framework.
//! ## Example
//! ```rust,no_run
//! use shuttle_runtime::Context as _;
//! use poise::serenity_prelude as serenity;
//! use shuttle_secrets::SecretStore;
//! use shuttle_poise::ShuttlePoise;
Expand All @@ -22,7 +21,7 @@
//! // Get the discord token set in `Secrets.toml`
//! let discord_token = secret_store
//! .get("DISCORD_TOKEN")
//! .context("'DISCORD_TOKEN' was not found")?;
chesedo marked this conversation as resolved.
Show resolved Hide resolved
//! .expect("'DISCORD_TOKEN' was not found");
//!
//! let framework = poise::Framework::builder()
//! .options(poise::FrameworkOptions {
Expand Down