Skip to content

Commit

Permalink
Prepare v0.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Caspar Oostendorp committed Feb 24, 2024
1 parent 772dcb1 commit c06bce4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 42 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.8
0.1.9
2 changes: 1 addition & 1 deletion chart/keiko/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ version: 0.1.0
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.1.8"
appVersion: "0.1.9"
27 changes: 26 additions & 1 deletion server/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
use std::fs::File;
use std::path::PathBuf;
use clap::{Args, Parser};
use jsonrpsee_http_client::{HttpClient, HttpClientBuilder};
use url::Url;
use std::str::FromStr;
use keiko_api::server_state;
use std::net::SocketAddr;
use serde_json::Value;

const LOCAL_KATANA: &str = "http://0.0.0.0:5050";
const LOCAL_TORII: &str = "http://0.0.0.0:8080";
const KATANA_GENESIS_PATH: &str = "config/genesis.json";
const KATANA_DB_PATH: &str = "storage/katana-db";

pub const KEIKO_ASSETS: &str = "static/keiko/assets";
pub const KEIKO_INDEX: &str = "static/keiko/index.html";
pub const KATANA_LOG: &str = "log/katana.log.json";
pub const TORII_LOG: &str = "log/torii.log";
pub const TORII_DB: &str = "torii.sqlite";
const CONFIG_MANIFEST: &str = "config/manifest.json";


#[derive(Debug, Clone)]
pub struct Config {
pub server: ServerOptions,
Expand All @@ -34,7 +44,16 @@ impl From<KeikoArgs> for Config {
impl Config {
pub fn new() -> Self {
let keiko_args = KeikoArgs::parse();
Self::from(keiko_args)
let mut config = Self::from(keiko_args);

// Get the world address from the manifest
let manifest_json: Value = serde_json::from_reader(
File::open(CONFIG_MANIFEST).expect("File should open read only")
).expect("Cannot parse config/manifest.json");

config.set_world_address(manifest_json["world"]["address"].as_str().unwrap().to_string());

config
}
}

Expand Down Expand Up @@ -191,13 +210,19 @@ impl Config {
self.world_address = world_address;
}

pub fn get_storage_init_base_dir(&self) -> String {
format!("storage_init/{}", self.world_address)
}
pub fn get_storage_base_dir(&self) -> String {
format!("storage/{}", self.world_address)
}

pub fn get_katana_args(&self) -> Vec<String> {
let mut args = vec![];

args.push("--db-dir".to_string());
args.push(format!("{}/katana-db", self.get_storage_base_dir()));

if self.katana.katana_dev {
args.push("--dev".to_string())
}
Expand Down
86 changes: 47 additions & 39 deletions server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env::current_dir;
use std::fs;
use std::net::SocketAddr;
use crate::args::{Config};
use crate::args::Config;
use clap::Parser;
use tokio::signal::unix::{signal, SignalKind};
use tokio::task;
Expand All @@ -13,18 +13,13 @@ use tower_http::cors::{Any, CorsLayer};
use keiko_api::handlers::{katana, keiko};
use std::process::{Command, Stdio};
use std::fs::File;
use serde_json::Value;
use std::path::Path;
use axum::body::Body;
use args::{KATANA_LOG, KEIKO_ASSETS, KEIKO_INDEX, TORII_DB, TORII_LOG};

mod args;
mod utils;

const KEIKO_ASSETS: &str = "static/keiko/assets";
const KEIKO_INDEX: &str = "static/keiko/index.html";
const KATANA_LOG: &str = "log/katana.log.json";
const TORII_LOG: &str = "log/torii.log";
const TORII_DB: &str = "torii.sqlite";
const CONFIG_MANIFEST: &str = "config/manifest.json";

#[tokio::main]
async fn main() {
let mut config = Config::new();
Expand All @@ -36,36 +31,64 @@ async fn main() {
.expect("Failed to build dashboard");
}

let katana = start_katana(&config).await;

// Get the world address from the manifest
let manifest_json: Value = serde_json::from_reader(
File::open(CONFIG_MANIFEST).expect("File should open read only")
).expect("Cannot parse config/manifest.json");
// Handle storage dir:
ensure_storage_dirs();

let world_address = manifest_json["world"]["address"].as_str().unwrap().to_string();
config.set_world_address(world_address.to_string());
let katana = start_katana(&config).await;

let rpc_url = "http://localhost:5050";
let torii = start_torii(&config).await;

// TODO Modify the Scarb.toml if needed with world address

// TODO Deploy Dojo/contracts if needed

let addr = SocketAddr::from(([0, 0, 0, 0], config.server.port.clone()));

let router = create_router(&config);

let server = axum::Server::bind(&addr)
.serve(router.into_make_service_with_connect_info::<SocketAddr>());

let mut sigterm = signal(SignalKind::terminate()).unwrap();

tokio::select! {
_ = server => println!("Stopping server..."),
_ = sigterm.recv() => println!("sigterm received, stopping server...")
}

// Close Katana and Torii
katana.abort();
torii.abort();
}

fn ensure_storage_dirs() {
// Handle storage dir:
let storage_dir = Path::new("storage/");
let storage_init_dir = Path::new("storage_init/");

fs::create_dir_all(&storage_dir).expect("Failed to create storage dir");
fs::create_dir_all(&storage_init_dir).expect("Failed to create storage_init dir");

if storage_dir.read_dir().expect("read_dir call failed").next().is_none() {
// If dir storage/ is empty, copy all contents of storage_init/ into it
for entry in fs::read_dir(&storage_init_dir).expect("read_dir call failed") {
let entry = entry.expect("Dir entry failed");
let dest_path = storage_dir.join(entry.file_name());
fs::copy(entry.path(), dest_path).expect("Copy failed");
}
}
}

fn create_router(config: &Config) -> Router<(), Body> {
let cors = CorsLayer::new()
// allow `GET` and `POST` when accessing the resource
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
// allow requests from any origin
.allow_origin(Any)
.allow_headers(Any);

let mut router = Router::new();

let addr = SocketAddr::from(([0, 0, 0, 0], config.server.port.clone()));

router = router
Router::new()
.route("/api/fund", get(katana::funds::handler))
.route("/api/block", on(MethodFilter::POST, katana::block::handler))
// .route("/api/accounts", get(katana::account::handler))
Expand All @@ -80,22 +103,7 @@ async fn main() {
.nest_service("/assets", get_service(ServeDir::new(config.server.static_path.join("assets"))))
.fallback_service(get_service(ServeFile::new(config.server.static_path.join("index.html"))))
.layer(cors)
.layer(AddExtensionLayer::new(config.server_state()));


let server = axum::Server::bind(&addr)
.serve(router.into_make_service_with_connect_info::<SocketAddr>());

let mut sigterm = signal(SignalKind::terminate()).unwrap();

tokio::select! {
_ = server => println!("Stopping server..."),
_ = sigterm.recv() => println!("sigterm received, stopping server...")
}

// Close Katana and Torii
katana.abort();
torii.abort();
.layer(AddExtensionLayer::new(config.server_state()))
}

async fn start_katana(config: &Config) -> task::JoinHandle<()> {
Expand Down

0 comments on commit c06bce4

Please sign in to comment.