Skip to content

Commit

Permalink
Prepare v0.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Caspar Oostendorp committed Feb 23, 2024
1 parent f3e051f commit 3fb12ce
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 84 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.6
0.1.7
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.6"
appVersion: "0.1.7"
13 changes: 6 additions & 7 deletions server/api/src/handlers/keiko/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use crate::server_state::ServerState;
use std::fs;
use std::io::ErrorKind;

pub async fn store_manifest(Path(app_name): Path<String>, Extension(server_state): Extension<ServerState>, Json(manifest): Json<Manifest>, ) -> impl IntoResponse {
let path = &server_state.manifest_directory_path;
pub async fn store_manifest(Path(app_name): Path<String>, Extension(server_state): Extension<ServerState>, Json(manifest): Json<Manifest>) -> impl IntoResponse {
let path = &server_state.manifest_base_dir;
if !std::path::Path::new(path).exists() {
if let Err(_) = fs::create_dir_all(path) {
return (StatusCode::INTERNAL_SERVER_ERROR, "Server Error")
return (StatusCode::INTERNAL_SERVER_ERROR, "Server Error");
}
}
let path = format!("{}/{}.json", &server_state.manifest_directory_path, app_name);
let path = format!("{}/{}.json", &server_state.manifest_base_dir, app_name);
match fs::metadata(&path) {
Ok(_) => (StatusCode::IM_USED, "Already uploaded"),
Err(ref e) if e.kind() == ErrorKind::NotFound => {
Expand All @@ -23,14 +23,13 @@ pub async fn store_manifest(Path(app_name): Path<String>, Extension(server_state
Ok(_) => (StatusCode::CREATED, "Stored manifest"),
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Server error")
}

},
}
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Server error"),
}
}

pub async fn get_manifest(Path(app_name): Path<String>, Extension(server_state): Extension<ServerState>) -> impl IntoResponse {
let path = format!("{}/{}.json", &server_state.manifest_directory_path, app_name);
let path = format!("{}/{}.json", &server_state.manifest_base_dir, app_name);
match fs::read_to_string(&path) {
Ok(content) => (StatusCode::OK, content),
Err(ref e) if e.kind() == ErrorKind::NotFound => (StatusCode::NOT_FOUND, "Not Found".into()),
Expand Down
2 changes: 1 addition & 1 deletion server/api/src/server_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ use url::Url;
pub struct ServerState {
pub json_rpc_client: HttpClient,
pub rpc_url: Url,
pub manifest_directory_path: String,
pub manifest_base_dir: String,
pub torii_url: Url,
}
43 changes: 33 additions & 10 deletions server/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ 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";

#[derive(Debug, Clone)]
pub struct Config {
pub server: ServerOptions,
pub starknet: StarknetOptions,
pub katana: KatanaOptions,
pub world_address: String,
}


impl From<KeikoArgs> for Config {
fn from(args: KeikoArgs) -> Self {
Self {
server: args.server,
starknet: args.starknet,
katana: args.katana,
world_address: "".to_string(),
}
}
}

impl Config {
pub fn new() -> Self {
let keiko_args = KeikoArgs::parse();
Self::from(keiko_args)
}
}

#[derive(Parser, Debug, Clone)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
Expand Down Expand Up @@ -159,7 +186,10 @@ pub struct KatanaOptions {
}


impl KeikoArgs {
impl Config {
pub fn set_world_address(&mut self, world_address: String) {
self.world_address = world_address;
}
/**
* gets all katana args to run katana with
*/
Expand Down Expand Up @@ -251,19 +281,12 @@ impl KeikoArgs {
* gets the server state
*/
pub fn server_state(&self) -> server_state::ServerState {

// add world_address if it is supplied
// let manifest_directory_path = match &self.world.address {
// None => self.server.manifest_directory_path.clone(),
// Some(world_address) => format!("{}/{}", &self.server.manifest_directory_path, world_address)
// };
// TODO
let manifest_directory_path = "".to_string();
let manifest_base_dir = format!("storage/{}", self.world_address);

server_state::ServerState {
json_rpc_client: self.json_rpc_client(),
rpc_url: self.rpc_url(),
manifest_directory_path,
manifest_base_dir,
torii_url: self.torii_url(),
}
}
Expand Down
112 changes: 48 additions & 64 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::net::SocketAddr;
use crate::args::KeikoArgs;
use crate::args::{Config};
use clap::Parser;
use tokio::signal::unix::{signal, SignalKind};
use tokio::task;
Expand All @@ -11,7 +11,6 @@ use tower_http::add_extension::AddExtensionLayer;
use tower_http::services::{ServeDir, ServeFile};
use tower_http::cors::{Any, CorsLayer};
use keiko_api::handlers::{katana, keiko};
// use crate::utils::run_torii;
use std::process::{Command, Stdio};
use std::fs::File;
use serde_json::Value;
Expand All @@ -27,7 +26,7 @@ const CONFIG_MANIFEST: &str = "config/manifest.json";

#[tokio::main]
async fn main() {
let config = KeikoArgs::parse();
let mut config = Config::new();

if config.server.prod {
Command::new("npx")
Expand All @@ -36,60 +35,23 @@ async fn main() {
.expect("Failed to build dashboard");
}

// Start Katana if needed
let katana = {
let katana_args = config.get_katana_args();
let result = task::spawn(async move {
let output = File::create(KATANA_LOG).expect("Failed to create file");

Command::new("katana")
.args(katana_args)
.stdout(Stdio::from(output))
.spawn()
.expect("Failed to start process");
});
// Wait until katana is listening on 5050
utils::wait_for_port("127.0.0.1:5050".parse().unwrap()).await;
result
};
let katana = start_katana(config.get_katana_args()).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");

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

let rpc_url = "http://localhost:5050";

let torii = start_torii(world_address).await;

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

// TODO Deploy Dojo/contracts if needed

// Start Torii if needed
let torii = {
let mut args: Vec<String> = vec![];
args.push("--world".to_string());
args.push(world_address.to_string());

args.push("--database".to_string());
args.push(format!("sqlite:///{}/storage/torii.sqlite", current_dir().unwrap().display()));

let result = task::spawn(async move {
let output = File::create(TORII_LOG).expect("Failed to create file");

Command::new("torii")
.stdout(Stdio::from(output))
.args(args)
.spawn()
.expect("Failed to start torii");
});

utils::wait_for_port("127.0.0.1:8080".parse().unwrap()).await;

result
};

let cors = CorsLayer::new()
// allow `GET` and `POST` when accessing the resource
Expand All @@ -100,19 +62,11 @@ async fn main() {

let mut router = Router::new();

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

router = router
.route(
"/api/state",
get(katana::state::save_state)
.on(MethodFilter::PUT, katana::state::load_state)
.on(MethodFilter::DELETE, katana::state::reset_state),
)
.route("/api/fund", get(katana::funds::handler))
.route("/api/block", on(MethodFilter::POST, katana::block::handler));


router = router
.route("/api/block", on(MethodFilter::POST, katana::block::handler))
// .route("/api/accounts", get(katana::account::handler))
.route(
"/manifests/:app_name",
Expand All @@ -125,28 +79,58 @@ 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()));
.layer(AddExtensionLayer::new(config));

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

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 => {
// This arm will run if the server shuts down by itself.
println!("Stopping server...");

}
_ = sigterm.recv() => {
// This arm will run if a SIGINT signal is received.
println!("sigterm received, stopping server...");
}
_ = server => println!("Stopping server..."),
_ = sigterm.recv() => println!("sigterm received, stopping server...")
}

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

async fn start_katana(katana_args: Vec<String>) -> task::JoinHandle<()> {
let result = task::spawn(async move {
let output = File::create(KATANA_LOG).expect("Failed to create file");

Command::new("katana")
.args(katana_args)
.stdout(Stdio::from(output))
.spawn()
.expect("Failed to start process");
});
// TODO get the server/port from args
utils::wait_for_port("127.0.0.1:5050".parse().unwrap()).await;
result
}

async fn start_torii(world_address: String) -> task::JoinHandle<()> {
let mut args: Vec<String> = vec![
"--world".to_string(),
world_address,
"--database".to_string(),
format!("sqlite:///{}/storage/torii.sqlite", current_dir().unwrap().display()),
];

let result = task::spawn(async move {
let output = File::create(TORII_LOG).expect("Failed to create file");

Command::new("torii")
.stdout(Stdio::from(output))
.args(args)
.spawn()
.expect("Failed to start torii");
});

utils::wait_for_port("127.0.0.1:8080".parse().unwrap()).await;

result
}

0 comments on commit 3fb12ce

Please sign in to comment.