Skip to content

Commit

Permalink
add permissive_cors option
Browse files Browse the repository at this point in the history
  • Loading branch information
seddonm1 committed Jun 26, 2023
1 parent 8934917 commit 02b3bb8
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 35 deletions.
99 changes: 94 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ thiserror = "1.0.40"
time = "0.3.21"
tokio = { version = "1.28.2", features = ["fs", "io-util"] }
tokio-util = { version = "0.7.8", features = ["io"] }
tower = { version = "0.4.13", features = ["full"] }
tower-http = { version = "0.4.1", features = ["cors"] }
tracing = "0.1.37"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.17", optional = true, features = ["env-filter", "time"] }
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This concept is backed by benchmarks from SQLite showing that it can be [faster

Each bucket is saved to a separate `.sqlite3` database named after the bucket name. The [smithy](https://github.com/awslabs/smithy) generated bindings for `s3` are then mapped to the correct SQL calls against a very simple schema that is designed to be human accessible.

### data
### Data

The main table, `data`, is a simple key/value with metadata store.

Expand All @@ -29,7 +29,7 @@ CREATE TABLE IF NOT EXISTS data (
) STRICT, WITHOUT ROWID;
```

### multipart
### Multipart Uploads

For `multipart` uploads two temporary tables are used:

Expand Down
66 changes: 38 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ use s3ite::Sqlite;

use s3s::auth::SimpleAuth;
use s3s::service::S3ServiceBuilder;
use tower::make::Shared;
use tower::ServiceBuilder;
use tower_http::cors::CorsLayer;

use std::net::IpAddr;
use std::net::SocketAddr;
use std::net::TcpListener;
use std::path::PathBuf;

use clap::Parser;
use hyper::server::Server;
use tracing::info;
use tracing_subscriber::EnvFilter;

#[derive(Debug, Parser)]
struct Opt {
#[clap(long, default_value = "localhost")]
host: String,
host: IpAddr,

#[clap(long, default_value = "8014")]
port: u16,

#[clap(long, default_value_t = true)]
permissive_cors: bool,

#[clap(long, requires("secret-key"))]
access_key: Option<String>,

Expand All @@ -34,53 +43,54 @@ struct Opt {
root: PathBuf,
}

fn setup_tracing() {
use tracing_subscriber::EnvFilter;

let env_filter = EnvFilter::from_default_env();
// let enable_color = std::io::stdout().is_terminal(); // TODO
let enable_color = false;

tracing_subscriber::fmt()
.pretty()
.with_env_filter(env_filter)
.with_ansi(enable_color)
.init();
}

#[tokio::main]
async fn main() -> Result {
setup_tracing();
let env_filter = EnvFilter::from_default_env();
tracing_subscriber::fmt().with_env_filter(env_filter).init();

let opt = Opt::parse();

// Parse addr
let addr = SocketAddr::new(opt.host, opt.port);
let listener = TcpListener::bind(addr)?;

// Setup S3 provider
let sqlite = Sqlite::new(opt.root).await?;

// Setup S3 service
let service = {
let mut b = S3ServiceBuilder::new(sqlite);
let s3_service = {
let mut s3 = S3ServiceBuilder::new(sqlite);

// Enable authentication
if let (Some(ak), Some(sk)) = (opt.access_key, opt.secret_key) {
b.set_auth(SimpleAuth::from_single(ak, sk));
s3.set_auth(SimpleAuth::from_single(ak, sk));
}

// Enable parsing virtual-hosted-style requests
if let Some(domain_name) = opt.domain_name {
b.set_base_domain(domain_name);
s3.set_base_domain(domain_name);
}

b.build()
s3.build().into_shared()
};

// Run server
let listener = TcpListener::bind((opt.host.as_str(), opt.port))?;
let local_addr = listener.local_addr()?;

let server = Server::from_tcp(listener)?.serve(service.into_shared().into_make_service());

info!("server is running at http://{local_addr}");
server.with_graceful_shutdown(shutdown_signal()).await?;
// Add CorsLayer if defined
if opt.permissive_cors {
let service = Shared::new(
ServiceBuilder::new()
.layer(CorsLayer::very_permissive())
.service(s3_service),
);
let server = Server::from_tcp(listener)?.serve(service);
info!("server is running at http://{addr}");
server.with_graceful_shutdown(shutdown_signal()).await?;
} else {
let service = Shared::new(ServiceBuilder::new().service(s3_service));
let server = Server::from_tcp(listener)?.serve(service);
info!("server is running at http://{addr}");
server.with_graceful_shutdown(shutdown_signal()).await?;
};

info!("server is stopped");
Ok(())
Expand Down

0 comments on commit 02b3bb8

Please sign in to comment.