Skip to content

Commit

Permalink
feat(#45): --init
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Oct 11, 2024
1 parent 5a5f99f commit 11e2d2b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 51 deletions.
6 changes: 3 additions & 3 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub(crate) struct StartArgs {
/// Run in detach mode.
#[arg(short, long, default_value = "false")]
pub(crate) detach: bool,
/// Path to file or directory with state.
#[arg(long)]
pub(crate) state: String,
/// Path to file or directory with initial state.
#[arg(long, default_value = "")]
pub(crate) init: String,
}
12 changes: 7 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ use fakehub_server::{DtServer, Server};
use crate::args::{Args, Command};
use crate::sys::current_port::current_port;
use crate::sys::kill_unix::kill_unix;
use fakehub_server::init::server_with_init_state::ServerWithInitState;
use fakehub_server::sys::instance_os::instance_os;
use fakehub_server::sys::sys_info::sys_info;
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
use fakehub_server::init::server_with_init_state::ServerWithInitState;

mod args;

Expand Down Expand Up @@ -88,10 +88,12 @@ async fn main() {
}
};
}
let mut server: dyn Server = DtServer::new(start.port);
if !start.state.is_empty() {
server = ServerWithInitState::new(server, start.state);
}
let base = DtServer::new(start.port);
let server: Box<dyn Server> = if !start.init.is_empty() {
Box::new(ServerWithInitState::new(base, start.init))
} else {
Box::new(base)
};
match server.start().await {
Ok(_) => info!("Server started successfully on port {}", start.port),
Err(e) => panic!(
Expand Down
2 changes: 2 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ rand = "0.8.5"
regex = "1.10.6"
parameterized = "2.0.0"
testing_logger = "0.1.1"
fsl = "0.0.4"
futures = "0.3.31"

[features]
mirror_release = []
34 changes: 20 additions & 14 deletions server/src/init/server_with_init_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,38 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::Server;
use crate::{DtServer, Server};
use anyhow::Result;
use fsl::transpiler::errors::err_ast::ErrAst;
use fsl::transpiler::fsl_transpiler::Fslt;
use futures::future::BoxFuture;
use log::{debug, info};
use std::path::Path;

/// Bootstrap server.
pub struct ServerWithInitState<O> {
pub struct ServerWithInitState {
/// Origin.
pub origin: O,
pub origin: DtServer,
/// Path to file or directory with state.
pub path: String,
}

impl<O> ServerWithInitState<O>
where
O: Server,
{
pub fn new(origin: O, path: String) -> ServerWithInitState<O> {
impl ServerWithInitState {
/// New.
pub fn new(origin: DtServer, path: String) -> ServerWithInitState {
ServerWithInitState { origin, path }
}
}

impl<O> Server for ServerWithInitState<O>
where
O: Server,
{
impl Server for ServerWithInitState {
/// Start.
async fn start(self) -> Result<()> {
self.origin.start().await
fn start(&self) -> BoxFuture<'_, Result<()>> {
Box::pin(async move {
let transpiled =
ErrAst::default(Fslt::file(Path::new(&self.path))).decorate();
debug!("Transpiled {}: {}", self.path, transpiled);
info!("Initialized {} as state", self.path);
self.origin.start().await
})
}
}
61 changes: 33 additions & 28 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::io;
use anyhow::Result;
use axum::routing::{get, post};
use axum::Router;
use futures::future::BoxFuture;
use tokio::net::TcpListener;

use crate::handlers::home;
Expand All @@ -38,26 +39,28 @@ use crate::sys::sys_info::sys_info;

/// Handlers.
pub mod handlers;
/// Initialize.
pub mod init;
/// Fakehub objects.
pub mod objects;
/// Reports.
pub mod report;
/// System information.
pub mod sys;
/// Initialize.
pub mod init;

#[allow(unused_imports)]
#[macro_use]
extern crate hamcrest;

/// Server.
pub trait Server {

async fn start(self) -> Result<()>;
/// Start a server.
#[allow(async_fn_in_trait)]
fn start(&self) -> BoxFuture<'_, Result<()>>;
}

/// Default server.
#[derive(Default)]
#[derive(Default, Clone, Copy)]
pub struct DtServer {
/// Port.
port: usize,
Expand Down Expand Up @@ -93,29 +96,31 @@ pub struct ServerConfig {
// info!() for this one.
impl Server for DtServer {
/// Start a server.
async fn start(self) -> Result<()> {
let addr: String = format!("0.0.0.0:{}", self.port);
sys_info();
let started: io::Result<TcpListener> = TcpListener::bind(addr.clone()).await;
match started {
Ok(listener) => axum::serve(
listener,
Router::new()
.route("/", get(home::home))
.route("/users", post(register_user))
.route("/users/:login", get(user))
.route("/users", get(users))
.with_state(ServerConfig {
fakehub: FakeHub::with_addr(addr),
}),
)
.await
.ok(),
Err(err) => {
panic!("Can't bind address {}: '{}'", addr.clone(), err)
}
};
Ok(())
fn start(&self) -> BoxFuture<'_, Result<()>> {
Box::pin(async move {
let addr: String = format!("0.0.0.0:{}", self.port);
sys_info();
let started: io::Result<TcpListener> = TcpListener::bind(addr.clone()).await;
match started {
Ok(listener) => axum::serve(
listener,
Router::new()
.route("/", get(home::home))
.route("/users", post(register_user))
.route("/users/:login", get(user))
.route("/users", get(users))
.with_state(ServerConfig {
fakehub: FakeHub::with_addr(addr),
}),
)
.await
.ok(),
Err(err) => {
panic!("Can't bind address {}: '{}'", addr.clone(), err)
}
};
Ok(())
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/tests/handlers_it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#[cfg(test)]
mod routes_its {
use anyhow::Result;
use fakehub_server::DtServer;
use fakehub_server::{DtServer, Server};
#[allow(deprecated)]
// @todo #79:25min Import assert_that!() macro like in lib.rs instead of assert_that.
// For some reason we can't use '#[macro_use] extern crate hamcrest;'
Expand Down

0 comments on commit 11e2d2b

Please sign in to comment.