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

feat(resources): add support for turso client w/o provisioning #996

Merged
merged 14 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ commands:
shuttle-shared-db = { path = "$PWD/resources/shared-db" }
shuttle-secrets = { path = "$PWD/resources/secrets" }
shuttle-static-folder = { path = "$PWD/resources/static-folder" }
shuttle-turso = { path = "$PWD/resources/turso" }

shuttle-axum = { path = "$PWD/services/shuttle-axum" }
shuttle-actix-web = { path = "$PWD/services/shuttle-actix-web" }
Expand Down Expand Up @@ -605,6 +606,7 @@ workflows:
- resources/persist
- resources/secrets
- resources/static-folder
- resources/turso
- services/shuttle-actix-web
- services/shuttle-axum
- services/shuttle-next
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ shuttle-persist = { path = "[base]/shuttle/resources/persist" }
shuttle-shared-db = { path = "[base]/shuttle/resources/shared-db" }
shuttle-secrets = { path = "[base]/shuttle/resources/secrets" }
shuttle-static-folder = { path = "[base]/shuttle/resources/static-folder" }
shuttle-turso = { path = "[base]/shuttle/resources/turso" }

shuttle-axum = { path = "[base]/shuttle/services/shuttle-axum" }
shuttle-actix-web = { path = "[base]/shuttle/services/shuttle-actix-web" }
Expand Down
1 change: 1 addition & 0 deletions common/src/models/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn get_resources_table(resources: &Vec<Response>, service_name: &str) -> Str
Type::Secrets => "Secrets",
Type::StaticFolder => "Static Folder",
Type::Persist => "Persist",
Type::Turso => "Turso",
};

let elements = acc.entry(title).or_insert(Vec::new());
Expand Down
2 changes: 2 additions & 0 deletions common/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum Type {
Secrets,
StaticFolder,
Persist,
Turso,
}

impl Response {
Expand All @@ -58,6 +59,7 @@ impl Display for Type {
Type::Secrets => write!(f, "secrets"),
Type::StaticFolder => write!(f, "static_folder"),
Type::Persist => write!(f, "persist"),
Type::Turso => write!(f, "turso"),
}
}
}
1 change: 1 addition & 0 deletions deployer/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if [[ $PROD != "true" ]]; then
shuttle-shared-db = { path = "/usr/src/shuttle/resources/shared-db" }
shuttle-secrets = { path = "/usr/src/shuttle/resources/secrets" }
shuttle-static-folder = { path = "/usr/src/shuttle/resources/static-folder" }
shuttle-turso = { path = "/usr/src/shuttle/resources/turso" }

shuttle-axum = { path = "/usr/src/shuttle/services/shuttle-axum" }
shuttle-actix-web = { path = "/usr/src/shuttle/services/shuttle-actix-web" }
Expand Down
6 changes: 6 additions & 0 deletions deployer/src/persistence/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum Type {
Secrets,
StaticFolder,
Persist,
Turso,
}

impl From<Type> for shuttle_common::resource::Type {
Expand All @@ -51,6 +52,7 @@ impl From<Type> for shuttle_common::resource::Type {
Type::Secrets => Self::Secrets,
Type::StaticFolder => Self::StaticFolder,
Type::Persist => Self::Persist,
Type::Turso => Self::Turso,
}
}
}
Expand All @@ -62,6 +64,7 @@ impl From<shuttle_common::resource::Type> for Type {
shuttle_common::resource::Type::Secrets => Self::Secrets,
shuttle_common::resource::Type::StaticFolder => Self::StaticFolder,
shuttle_common::resource::Type::Persist => Self::Persist,
shuttle_common::resource::Type::Turso => Self::Turso,
}
}
}
Expand All @@ -73,6 +76,7 @@ impl Display for Type {
Type::Secrets => write!(f, "secrets"),
Type::StaticFolder => write!(f, "static_folder"),
Type::Persist => write!(f, "persist"),
Type::Turso => write!(f, "turso"),
}
}
}
Expand All @@ -91,6 +95,7 @@ impl FromStr for Type {
"secrets" => Ok(Self::Secrets),
"static_folder" => Ok(Self::StaticFolder),
"persist" => Ok(Self::Persist),
"turso" => Ok(Self::Turso),
_ => Err(format!("'{s}' is an unknown resource type")),
}
}
Expand Down Expand Up @@ -139,6 +144,7 @@ mod tests {
Type::Secrets,
Type::StaticFolder,
Type::Persist,
Type::Turso,
];

for input in inputs {
Expand Down
19 changes: 19 additions & 0 deletions resources/turso/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "shuttle-turso"
version = "0.20.0"
edition = "2021"
license = "Apache-2.0"
description = "Plugin to obtain a client connected to a Turso database"
keywords = ["shuttle-service", "turso"]

[dependencies]
async-trait = "0.1.56"
libsql-client = { version = "=0.30.1" }
serde = { version = "1.0.148", features = ["derive"] }
shuttle-service = { path = "../../service", version = "0.20.0", default-features = false }
url = { version = "2.3.1", features = ["serde"] }


[dev-dependencies]
tempfile = "3.3.0"
tokio = "1.28.2"
34 changes: 34 additions & 0 deletions resources/turso/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Shuttle Turso

This plugin allows services to connect to a Turso database. Turso is an edge-hosted distributed database based on libSQL, a SQLite fork.

## Usage

**IMPORTANT**: Currently Shuttle isn't able to provision a database for you (yet). This means you will have to create an account on their [website](https://turso.tech/) and follow the few steps required to create a database and create a token to access it.

Add `shuttle-turso` to the dependencies for your service by running `cargo add shuttle-turso`.
This resource will be provided by adding the `shuttle_turso::Turso` attribute to your Shuttle `main` decorated function.

It returns a `libsql_client::Client`. When running locally it will instantiate a local SQLite database of the name of your service instead of connecting to your edge database.

If you want to connect to a remote database when running locally, you can specify the `local_addr` parameter. In that case, the token will be read from your `Secrets.dev.toml` file.

### Example

In the case of an Axum server, your main function will look like this:

```rust
use libsql_client::client::Client;
use shuttle_axum::ShuttleAxum;

#[shuttle_runtime::main]
async fn app(#[shuttle_turso::Turso(addr="libsql://my-turso-db-name.turso.io", token="{secrets.DB_TURSO_TOKEN}")] client: Client) -> ShuttleAxum { }
```

### Parameters

| Parameter | Type | Default | Description |
| ---------- | ----------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| addr | str | `""` | URL of the database to connect to. If `libsql://` is missing at the beginning, it will be automatically added. |
| token | str | `""` | The value of the token to authenticate against the Turso database. You can use string interpolation to read a secret from your `Secret.toml` file. |
| local_addr | Option<str> | `None` | The URL to use when running your service locally. If not provided, this will default to a local file named `<service name>.db` |
Loading