Skip to content

Commit

Permalink
setup flowy sdk server configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed Dec 6, 2021
1 parent 52d5106 commit 5a89655
Show file tree
Hide file tree
Showing 36 changed files with 270 additions and 123 deletions.
2 changes: 2 additions & 0 deletions backend/Cargo.lock

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

3 changes: 3 additions & 0 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ docker_image:
source $(ROOT)/docker_env.sh && docker-compose up -d db
source $(ROOT)/docker_env.sh && docker-compose up -d backend

local_server:
cargo run

docker_test:
sh $(ROOT)/docker_test.sh

Expand Down
1 change: 0 additions & 1 deletion backend/src/config/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let mut settings = config::Config::default();
let base_path = std::env::current_dir().expect("Failed to determine the current directory");
let configuration_dir = base_path.join("configuration");

settings.merge(config::File::from(configuration_dir.join("base")).required(true))?;

let environment: Environment = std::env::var("APP_ENVIRONMENT")
Expand Down
2 changes: 1 addition & 1 deletion backend/src/entities/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Token {

use crate::service::user::EXPIRED_DURATION_DAYS;
use actix_web::{dev::Payload, FromRequest, HttpRequest};
use backend_service::config::HEADER_TOKEN;
use backend_service::configuration::HEADER_TOKEN;
use futures::future::{ready, Ready};

impl FromRequest for Token {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/middleware/auth_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use actix_web::{

use crate::config::IGNORE_ROUTES;
use actix_web::{body::AnyBody, dev::MessageBody};
use backend_service::{config::HEADER_TOKEN, errors::ServerError};
use backend_service::{configuration::HEADER_TOKEN, errors::ServerError};
use futures::future::{ok, LocalBoxFuture, Ready};
use std::{
convert::TryInto,
Expand Down
11 changes: 4 additions & 7 deletions backend/tests/document/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![cfg_attr(rustfmt, rustfmt::skip)]
use actix_web::web::Data;
use backend::service::doc::{crud::update_doc, manager::DocManager};
use backend_service::config::ServerConfig;
use flowy_document::services::doc::ClientDocEditor as ClientEditDocContext;
use flowy_test::{workspace::ViewTest, FlowyTest};
use flowy_user::services::user::UserSession;
Expand All @@ -29,14 +28,12 @@ pub enum DocScript {
AssertClient(&'static str),
AssertServer(&'static str, i64),
ServerSaveDocument(String, i64), // delta_json, rev_id
Sleep(u64),
}

impl DocumentTest {
pub async fn new() -> Self {
let server = spawn_server().await;
let server_config = ServerConfig::new(&server.host, "http", "ws");
let flowy_test = FlowyTest::setup_with(server_config);
let flowy_test = FlowyTest::setup_with(server.client_server_config.clone());
Self { server, flowy_test }
}

Expand Down Expand Up @@ -136,9 +133,9 @@ async fn run_scripts(context: Arc<RwLock<ScriptContext>>, scripts: Vec<DocScript
let pg_pool = context.read().server_pg_pool.clone();
save_doc(&doc_id, json, rev_id, pg_pool).await;
},
DocScript::Sleep(sec) => {
sleep(Duration::from_secs(sec)).await;
},
// DocScript::Sleep(sec) => {
// sleep(Duration::from_secs(sec)).await;
// },
}
};
fut_scripts.push(fut);
Expand Down
32 changes: 21 additions & 11 deletions backend/tests/util/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ use backend::{
config::{get_configuration, DatabaseSettings},
context::AppContext,
};
use backend_service::{errors::ServerError, user_request::*, workspace_request::*};
use backend_service::{
configuration::{get_client_server_configuration, ClientServerConfiguration},
errors::ServerError,
user_request::*,
workspace_request::*,
};
use flowy_document::services::server::read_doc_request;
use flowy_document_infra::entities::doc::{Doc, DocIdentifier};
use flowy_user_infra::entities::*;
Expand All @@ -12,11 +17,10 @@ use sqlx::{Connection, Executor, PgConnection, PgPool};
use uuid::Uuid;

pub struct TestUserServer {
pub host: String,
pub port: u16,
pub pg_pool: PgPool,
pub user_token: Option<String>,
pub user_id: Option<String>,
pub client_server_config: ClientServerConfiguration,
}

impl TestUserServer {
Expand Down Expand Up @@ -167,19 +171,24 @@ impl TestUserServer {
response
}

pub fn http_addr(&self) -> String { format!("http://{}", self.host) }
pub fn http_addr(&self) -> String { self.client_server_config.base_url() }

pub fn ws_addr(&self) -> String { format!("ws://{}/ws/{}", self.host, self.user_token.as_ref().unwrap()) }
pub fn ws_addr(&self) -> String {
format!(
"{}/{}",
self.client_server_config.ws_addr(),
self.user_token.as_ref().unwrap()
)
}
}

impl std::convert::From<TestServer> for TestUserServer {
fn from(server: TestServer) -> Self {
TestUserServer {
host: server.host,
port: server.port,
pg_pool: server.pg_pool,
user_token: None,
user_id: None,
client_server_config: server.client_server_config.clone(),
}
}
}
Expand All @@ -190,10 +199,9 @@ pub async fn spawn_user_server() -> TestUserServer {
}

pub struct TestServer {
pub host: String,
pub port: u16,
pub pg_pool: PgPool,
pub app_ctx: AppContext,
pub client_server_config: ClientServerConfiguration,
}

pub async fn spawn_server() -> TestServer {
Expand All @@ -218,13 +226,15 @@ pub async fn spawn_server() -> TestServer {
// drop_test_database(database_name).await;
});

let mut client_server_config = get_client_server_configuration().expect("Failed to read configuration.");
client_server_config.reset_host_with_port("localhost", application_port);

TestServer {
host: format!("localhost:{}", application_port),
port: application_port,
pg_pool: get_connection_pool(&configuration.database)
.await
.expect("Failed to connect to the database"),
app_ctx,
client_server_config,
}
}

Expand Down
8 changes: 7 additions & 1 deletion frontend/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ PRODUCT_NAME = "AppFlowy"
#CRATE_TYPE: https://doc.rust-lang.org/reference/linkage.html
CRATE_TYPE = "staticlib"
SDK_EXT = "a"
APP_ENVIRONMENT = "local"

[env.development-mac]
TARGET_OS = "macos"
Expand All @@ -33,14 +34,15 @@ TARGET_OS = "macos"
RUST_COMPILE_TARGET = "aarch64-apple-darwin"
FLUTTER_OUTPUT_DIR = "Release"
PRODUCT_EXT = "app"
APP_ENVIRONMENT = "production"

[env.production-mac-x86]
BUILD_FLAG = "release"
TARGET_OS = "macos"
RUST_COMPILE_TARGET = "x86_64-apple-darwin"
FLUTTER_OUTPUT_DIR = "Release"
PRODUCT_EXT = "app"

APP_ENVIRONMENT = "production"

[env.development-windows-x86]
TARGET_OS = "windows"
Expand All @@ -59,6 +61,7 @@ FLUTTER_OUTPUT_DIR = "Release"
PRODUCT_EXT = "exe"
CRATE_TYPE = "cdylib"
SDK_EXT = "dll"
APP_ENVIRONMENT = "production"

[env.development-linux-x86]
TARGET_OS = "linux"
Expand All @@ -77,6 +80,7 @@ CRATE_TYPE = "cdylib"
FLUTTER_OUTPUT_DIR = "Release"
SDK_EXT = "so"
LINUX_ARCH = "x64"
APP_ENVIRONMENT = "production"

[env.development-linux-aarch64]
TARGET_OS = "linux"
Expand All @@ -95,6 +99,7 @@ CRATE_TYPE = "cdylib"
FLUTTER_OUTPUT_DIR = "Release"
SDK_EXT = "so"
LINUX_ARCH = "arm64"
APP_ENVIRONMENT = "production"

[tasks.echo_env]
script = [
Expand All @@ -106,6 +111,7 @@ script = [
echo RUST_COMPILE_TARGET: ${RUST_COMPILE_TARGET}
echo FEATURES: ${FEATURES}
echo PRODUCT_EXT: ${PRODUCT_EXT}
echo APP_ENVIRONMENT: ${APP_ENVIRONMENT}
echo ${platforms}
echo "-------- Flutter --------"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class SplashScreen extends StatelessWidget {
}

void _handleUnauthenticated(BuildContext context, Unauthenticated result) {
// Log.error(result.error);
// getIt<ISplashRoute>().pushSignInScreen(context);
getIt<ISplashRoute>().pushSkipLoginScreen(context);
}
Expand Down
5 changes: 2 additions & 3 deletions frontend/app_flowy/linux/flutter/dart_ffi/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
#include <stdint.h>
#include <stdlib.h>


int64_t init_sdk(char *path);

void async_command(int64_t port, const uint8_t *input, uintptr_t len);
void async_event(int64_t port, const uint8_t *input, uintptr_t len);

const uint8_t *sync_command(const uint8_t *input, uintptr_t len);
const uint8_t *sync_event(const uint8_t *input, uintptr_t len);

int32_t set_stream_port(int64_t port);

Expand Down
4 changes: 2 additions & 2 deletions frontend/rust-lib/dart-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
let c_str: &CStr = unsafe { CStr::from_ptr(path) };
let path: &str = c_str.to_str().unwrap();

let server_config = ServerConfig::default();
let server_config = get_client_server_configuration().unwrap();
let config = FlowySDKConfig::new(path, server_config, "appflowy").log_filter("debug");
*FLOWY_SDK.write() = Some(Arc::new(FlowySDK::new(config)));

Expand Down Expand Up @@ -70,7 +70,7 @@ pub extern "C" fn set_stream_port(port: i64) -> i32 {
#[no_mangle]
pub extern "C" fn link_me_please() {}

use backend_service::config::ServerConfig;
use backend_service::configuration::get_client_server_configuration;
use lib_dispatch::prelude::ToBytes;

#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions frontend/rust-lib/flowy-document/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
ws::WsDocumentManager,
},
};
use backend_service::config::ServerConfig;
use backend_service::configuration::ClientServerConfiguration;
use flowy_database::ConnectionPool;
use flowy_document_infra::entities::doc::{DocDelta, DocIdentifier};
use std::sync::Arc;
Expand All @@ -27,7 +27,7 @@ impl FlowyDocument {
pub fn new(
user: Arc<dyn DocumentUser>,
ws_manager: Arc<WsDocumentManager>,
server_config: &ServerConfig,
server_config: &ClientServerConfiguration,
) -> FlowyDocument {
let server = construct_doc_server(server_config);
let doc_ctrl = Arc::new(DocController::new(server, user.clone(), ws_manager));
Expand Down
6 changes: 4 additions & 2 deletions frontend/rust-lib/flowy-document/src/services/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod server_api_mock;
pub use server_api::*;
// TODO: ignore mock files in production
use crate::errors::DocError;
use backend_service::config::ServerConfig;
use backend_service::configuration::ClientServerConfiguration;
use flowy_document_infra::entities::doc::{CreateDocParams, Doc, DocIdentifier, UpdateDocParams};
use lib_infra::future::ResultFuture;
pub use server_api_mock::*;
Expand All @@ -20,7 +20,9 @@ pub trait DocumentServerAPI {
fn update_doc(&self, token: &str, params: UpdateDocParams) -> ResultFuture<(), DocError>;
}

pub(crate) fn construct_doc_server(server_config: &ServerConfig) -> Arc<dyn DocumentServerAPI + Send + Sync> {
pub(crate) fn construct_doc_server(
server_config: &ClientServerConfiguration,
) -> Arc<dyn DocumentServerAPI + Send + Sync> {
if cfg!(feature = "http_server") {
Arc::new(DocServer::new(server_config.clone()))
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::{errors::DocError, services::server::DocumentServerAPI};
use backend_service::{config::*, request::HttpRequestBuilder};
use backend_service::{configuration::*, request::HttpRequestBuilder};
use flowy_document_infra::entities::doc::{CreateDocParams, Doc, DocIdentifier, UpdateDocParams};
use lib_infra::future::ResultFuture;

pub struct DocServer {
config: ServerConfig,
config: ClientServerConfiguration,
}

impl DocServer {
pub fn new(config: ServerConfig) -> Self { Self { config } }
pub fn new(config: ClientServerConfiguration) -> Self { Self { config } }
}

impl DocumentServerAPI for DocServer {
Expand Down
8 changes: 4 additions & 4 deletions frontend/rust-lib/flowy-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod deps_resolve;
// mod flowy_server;
pub mod module;
use crate::deps_resolve::WorkspaceDepsResolver;
use backend_service::config::ServerConfig;
use backend_service::configuration::ClientServerConfiguration;
use flowy_document::module::FlowyDocument;
use flowy_user::{
prelude::UserStatus,
Expand All @@ -26,11 +26,11 @@ pub struct FlowySDKConfig {
name: String,
root: String,
log_filter: String,
server_config: ServerConfig,
server_config: ClientServerConfiguration,
}

impl FlowySDKConfig {
pub fn new(root: &str, server_config: ServerConfig, name: &str) -> Self {
pub fn new(root: &str, server_config: ClientServerConfiguration, name: &str) -> Self {
FlowySDKConfig {
name: name.to_owned(),
root: root.to_owned(),
Expand Down Expand Up @@ -173,7 +173,7 @@ fn init_log(config: &FlowySDKConfig) {
fn mk_workspace_controller(
user_session: Arc<UserSession>,
flowy_document: Arc<FlowyDocument>,
server_config: &ServerConfig,
server_config: &ClientServerConfiguration,
) -> Arc<WorkspaceController> {
let workspace_deps = WorkspaceDepsResolver::new(user_session);
let (user, database) = workspace_deps.split_into();
Expand Down
7 changes: 5 additions & 2 deletions frontend/rust-lib/flowy-sdk/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::deps_resolve::DocumentDepsResolver;
use backend_service::config::ServerConfig;
use backend_service::configuration::ClientServerConfiguration;
use flowy_document::module::FlowyDocument;
use flowy_user::services::user::UserSession;
use flowy_workspace::prelude::WorkspaceController;
Expand All @@ -18,7 +18,10 @@ fn mk_workspace_module(workspace_controller: Arc<WorkspaceController>) -> Module
flowy_workspace::module::create(workspace_controller)
}

pub fn mk_document_module(user_session: Arc<UserSession>, server_config: &ServerConfig) -> Arc<FlowyDocument> {
pub fn mk_document_module(
user_session: Arc<UserSession>,
server_config: &ClientServerConfiguration,
) -> Arc<FlowyDocument> {
let document_deps = DocumentDepsResolver::new(user_session);
let (user, ws_manager) = document_deps.split_into();
Arc::new(FlowyDocument::new(user, ws_manager, server_config))
Expand Down
Loading

0 comments on commit 5a89655

Please sign in to comment.