chore: separate crates (#18)

This commit is contained in:
Nathan.fooo 2023-09-07 19:40:11 +08:00 committed by GitHub
parent 36499be26c
commit c1f8d79169
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 119 additions and 40 deletions

40
Cargo.lock generated
View file

@ -450,6 +450,7 @@ dependencies = [
"bincode",
"bytes",
"chrono",
"client-api",
"collab",
"collab-plugins",
"collab-ws",
@ -459,6 +460,7 @@ dependencies = [
"dotenv",
"fancy-regex",
"futures-util",
"gotrue",
"jsonwebtoken",
"lazy_static",
"once_cell",
@ -770,6 +772,17 @@ dependencies = [
"inout",
]
[[package]]
name = "client-api"
version = "0.1.0"
dependencies = [
"anyhow",
"gotrue",
"infra",
"reqwest",
"serde_json",
]
[[package]]
name = "collab"
version = "0.1.0"
@ -1422,6 +1435,19 @@ version = "0.27.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e"
[[package]]
name = "gotrue"
version = "0.1.0"
dependencies = [
"anyhow",
"futures-util",
"infra",
"reqwest",
"serde",
"serde_json",
"tokio",
]
[[package]]
name = "h2"
version = "0.3.20"
@ -1660,6 +1686,16 @@ dependencies = [
"hashbrown 0.14.0",
]
[[package]]
name = "infra"
version = "0.1.0"
dependencies = [
"anyhow",
"reqwest",
"serde",
"serde_json",
]
[[package]]
name = "inout"
version = "0.1.3"
@ -2706,9 +2742,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.104"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c"
checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360"
dependencies = [
"itoa",
"ryu",

View file

@ -61,16 +61,18 @@ tracing-log = "0.1.1"
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio-rustls", "macros", "postgres", "uuid", "chrono", "migrate"] }
#Local crate
token = { path = "./crates/token" }
snowflake = { path = "./crates/snowflake" }
realtime = { path = "crates/realtime" }
storage = { path = "crates/storage" }
token = { path = "libs/token" }
snowflake = { path = "libs/snowflake" }
realtime = { path = "libs/realtime" }
storage = { path = "libs/storage" }
gotrue = { path = "libs/gotrue" }
[dev-dependencies]
once_cell = "1.7.2"
collab = { version = "0.1.0" }
collab-ws = { version = "0.1.0" }
collab-plugins = { version = "0.1.0", features = ["sync_plugin"] }
client-api = { path = "libs/client-api" }
tempfile = "3.4.0"
assert-json-diff = "2.0.2"
@ -83,10 +85,12 @@ path = "src/lib.rs"
[workspace]
members = [
"crates/token",
"crates/snowflake",
"crates/realtime",
"crates/storage",
"libs/token",
"libs/snowflake",
"libs/realtime",
"libs/storage",
"libs/client-api",
"libs/infra",
]
[profile.dev.package.sqlx-macros]

View file

@ -0,0 +1,13 @@
[package]
name = "client-api"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "0.11.20", default-features = false }
anyhow = "1.0.75"
gotrue = { path = "../gotrue" }
infra = { path = "../infra" }
serde_json = "1.0.105"

View file

@ -2,11 +2,8 @@ use anyhow::Error;
use reqwest::Method;
use reqwest::RequestBuilder;
use crate::component::auth::gotrue::models::GoTrueError;
use crate::{
component::auth::gotrue::models::{AccessTokenResponse, OAuthError, TokenResult, User},
utils::http_response::{check_response, from_response},
};
use gotrue::models::{AccessTokenResponse, GoTrueError, OAuthError, TokenResult, User};
use infra::reqwest::{check_response, from_response};
pub struct Client {
http_client: reqwest::Client,

View file

@ -0,0 +1,3 @@
mod http;
pub use http::*;

15
libs/gotrue/Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "gotrue"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.105"
futures-util = "0.3.8"
anyhow = "1.0.75"
reqwest = { version = "0.11.20", default-features = false, features = ["json", "rustls-tls", "cookies"] }
tokio = { version = "1.0.1", features = ["sync"] }
infra = { path = "../infra" }

View file

@ -5,7 +5,7 @@ use super::{
grant::Grant,
models::{AccessTokenResponse, GoTrueError, GoTrueSettings, OAuthError, TokenResult, User},
};
use crate::utils::http_response::{check_response, from_body, from_response};
use infra::reqwest::{check_response, from_body, from_response};
pub struct Client {
client: reqwest::Client,

6
libs/gotrue/src/lib.rs Normal file
View file

@ -0,0 +1,6 @@
mod api;
mod grant;
pub mod models;
pub use api::*;
pub use grant::*;

12
libs/infra/Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "infra"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "0.11.20", default-features = false }
anyhow = "1.0.75"
serde = { version = "1.0.130" }
serde_json = "1.0.105"

1
libs/infra/src/lib.rs Normal file
View file

@ -0,0 +1 @@
pub mod reqwest;

View file

@ -1,18 +1,18 @@
use crate::component::auth::gotrue::grant::{Grant, PasswordGrant};
use crate::component::auth::gotrue::jwt::Authorization;
use crate::component::auth::{
change_password, gotrue, logged_user_from_request, login, logout, register,
ChangePasswordRequest, InternalServerError, RegisterRequest,
change_password, logged_user_from_request, login, logout, register, ChangePasswordRequest,
InternalServerError, RegisterRequest,
};
use crate::component::auth::{InputParamsError, LoginRequest};
use crate::component::token_state::SessionToken;
use crate::domain::{UserEmail, UserName, UserPassword};
use crate::state::State;
use crate::component::auth::jwt::Authorization;
use actix_web::web::{Data, Json};
use actix_web::HttpRequest;
use actix_web::Result;
use actix_web::{web, HttpResponse, Scope};
use gotrue::{Grant, PasswordGrant};
pub fn user_scope() -> Scope {
web::scope("/api/user")
@ -32,7 +32,7 @@ pub fn user_scope() -> Scope {
async fn update_handler(
auth: Authorization,
req: Json<LoginRequest>,
gotrue_client: Data<gotrue::api::Client>,
gotrue_client: Data<gotrue::Client>,
) -> Result<HttpResponse> {
let req = req.into_inner();
let email = UserEmail::parse(req.email)
@ -52,7 +52,7 @@ async fn update_handler(
async fn sign_out_handler(
auth: Authorization,
gotrue_client: Data<gotrue::api::Client>,
gotrue_client: Data<gotrue::Client>,
) -> Result<HttpResponse> {
gotrue_client
.logout(&auth.token)
@ -63,7 +63,7 @@ async fn sign_out_handler(
async fn sign_in_password_handler(
req: Json<LoginRequest>,
gotrue_client: Data<gotrue::api::Client>,
gotrue_client: Data<gotrue::Client>,
) -> Result<HttpResponse> {
let req = req.into_inner();
let email = UserEmail::parse(req.email)
@ -83,7 +83,7 @@ async fn sign_in_password_handler(
async fn sign_up_handler(
req: Json<LoginRequest>,
gotrue_client: Data<gotrue::api::Client>,
gotrue_client: Data<gotrue::Client>,
) -> Result<HttpResponse> {
let req = req.into_inner();
let email = UserEmail::parse(req.email)

View file

@ -1,5 +1,5 @@
use crate::api::{user_scope, ws_scope};
use crate::component::auth::{gotrue, HEADER_TOKEN};
use crate::component::auth::HEADER_TOKEN;
use crate::config::config::{Config, DatabaseSetting, TlsConfig};
use crate::middleware::cors::default_cors;
use crate::self_signed::create_self_signed_certificate;
@ -89,7 +89,7 @@ pub async fn run(
.service(ws_scope())
.app_data(Data::new(collab_server_addr.clone()))
.app_data(Data::new(state.clone()))
.app_data(Data::new(gotrue::api::Client::new(
.app_data(Data::new(gotrue::Client::new(
reqwest::Client::new(),
&config.gotrue.base_url)))
});

View file

@ -1 +0,0 @@
pub mod http;

View file

@ -1,4 +0,0 @@
pub mod api;
pub mod grant;
pub mod jwt;
pub mod models;

View file

@ -1,5 +1,5 @@
mod error;
pub mod gotrue;
pub mod jwt;
mod password;
mod user;

View file

@ -1,6 +1,5 @@
pub mod api;
pub mod application;
pub mod client;
pub mod component;
pub mod config;
pub mod domain;
@ -8,4 +7,3 @@ pub mod middleware;
mod self_signed;
pub mod state;
pub mod telemetry;
pub mod utils;

View file

@ -1 +0,0 @@
pub mod http_response;

View file

@ -1,4 +1,4 @@
use appflowy_cloud::client::http::Client;
use client_api::Client;
use crate::client::{
constants::LOCALHOST_URL,

View file

@ -1,4 +1,4 @@
use appflowy_cloud::client::http::Client;
use client_api::Client;
use crate::client::{
constants::LOCALHOST_URL,

View file

@ -1,5 +1,5 @@
use crate::client::{constants::LOCALHOST_URL, utils::generate_unique_email};
use appflowy_cloud::client::http::Client;
use client_api::Client;
#[tokio::test]
async fn sign_up_success() {

View file

@ -1,4 +1,4 @@
use appflowy_cloud::client::http::Client;
use client_api::Client;
use crate::client::{
constants::LOCALHOST_URL,

View file

@ -1,4 +1,4 @@
use appflowy_cloud::component::auth::gotrue::api::Client;
use gotrue::Client;
#[tokio::test]
async fn gotrue_settings() {