Skip to content

Commit

Permalink
(#85) Add copy of kkomelin/sui-explorer single-file-build branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mario4tier committed Jul 19, 2024
1 parent fe52436 commit ef7e17e
Show file tree
Hide file tree
Showing 790 changed files with 51,044 additions and 0 deletions.
8 changes: 8 additions & 0 deletions rust/suibase/crates/suibase-daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ use tokio_graceful_shutdown::{
SubsystemBuilder, Toplevel,
};

use workers::WebserverParams;
use workers::WebserverWorker;

#[allow(clippy::large_enum_variant)]
#[derive(Parser)]
#[clap(
Expand Down Expand Up @@ -114,11 +117,16 @@ impl Command {
let clock_params = ClockTriggerParams::new(netmon_tx.clone(), admctrl_tx.clone());
let clock: ClockTrigger = ClockTrigger::new(clock_params);

let suiexplorer_params =
WebserverParams::new(globals.clone(), admctrl_tx.clone(), "sui-explorer");
let suiexplorer = WebserverWorker::new(suiexplorer_params);

// Start all top levels subsystems.
let errors = Toplevel::new(|s| async move {
s.start(SubsystemBuilder::new("admctrl", |a| admctrl.run(a)));
s.start(SubsystemBuilder::new("netmon", |a| netmon.run(a)));
s.start(SubsystemBuilder::new("clock", |a| clock.run(a)));
s.start(SubsystemBuilder::new("suiexplorer", |a| suiexplorer.run(a)));
s.start(SubsystemBuilder::new("apiserver", |a| apiserver.run(a)));
})
.catch_signals()
Expand Down
2 changes: 2 additions & 0 deletions rust/suibase/crates/suibase-daemon/src/workers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) use self::db_worker::*;
pub(crate) use self::events_writer_worker::*;
pub(crate) use self::packages_poller::*;
pub(crate) use self::request_worker::*;
pub(crate) use self::webserver::*;
pub(crate) use self::websocket_worker::*;

mod cli_poller;
Expand All @@ -25,4 +26,5 @@ mod events_writer_worker;
mod log_worker;
mod packages_poller;
mod request_worker;
mod webserver;
mod websocket_worker;
133 changes: 133 additions & 0 deletions rust/suibase/crates/suibase-daemon/src/workers/webserver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Run a webserver to serve all files in a specified directory.
//
// The tokio task is auto-restart in case of panic.

use crate::shared_types::Globals;

use anyhow::Result;
use axum::async_trait;

use common::basic_types::{AdminControllerTx, AutoThread, Runnable};

use tokio_graceful_shutdown::{FutureExt, SubsystemHandle};

#[derive(Clone)]
pub struct WebserverParams {
globals: Globals,
admctrl_tx: AdminControllerTx,
website_name: String,
}

impl WebserverParams {
pub fn new(globals: Globals, admctrl_tx: AdminControllerTx, website_name: &str) -> Self {
Self {
globals,
admctrl_tx,
website_name: website_name.to_owned(),
}
}
}

pub struct WebserverWorker {
auto_thread: AutoThread<WebserverTask, WebserverParams>,
}

impl WebserverWorker {
pub fn new(params: WebserverParams) -> Self {
let name = format!("Webserver({})", params.website_name);
Self {
auto_thread: AutoThread::new(name, params),
}
}

pub async fn run(self, subsys: SubsystemHandle) -> Result<()> {
self.auto_thread.run(subsys).await
}
}

struct WebserverTask {
task_name: String,
params: WebserverParams,
websites_root: String,
}

#[async_trait]
impl Runnable<WebserverParams> for WebserverTask {
fn new(task_name: String, params: WebserverParams) -> Self {
Self {
task_name,
params,
websites_root: "".to_string(),
}
}

async fn run(mut self, subsys: SubsystemHandle) -> Result<()> {
// The websites (static files) are stored under "~/suibase/typescript".
// websites_root has the ~ resolved (e.g. "/home/user_name/suibase/typescript")
self.websites_root = format!("{}/typescript", {
let workdirs_guard = self.params.globals.workdirs.read().await;
let workdirs = &*workdirs_guard;
workdirs.suibase_home().to_string()
});

let output = format!("started {}", self.task_name);
log::info!("{}", output);

match self.event_loop(&subsys).cancel_on_shutdown(&subsys).await {
Ok(_) => {
log::info!("normal task exit (2)");
Ok(())
}
Err(_cancelled_by_shutdown) => {
log::info!("{} normal task exit (1)", self.task_name);
Ok(())
}
}
}
}

impl WebserverTask {
async fn event_loop(&mut self, _subsys: &SubsystemHandle) -> Result<()> {
// Serve files located under self.websites_root ("~/suibase/typescript" with ~ resolved).
let static_files_path = if self.params.website_name == "sui-explorer" {
// Serve the explorer app
format!("{}/sui-explorer/apps/explorer/build", self.websites_root)
} else {
// Serve whatever is at "~/suibase/typescript/website_name"
format!("{}/{}", self.websites_root, self.params.website_name)
};

// Use tower to handle the serving of the static files + index.html
let tower_srvc = tower_http::services::ServeDir::new(static_files_path)
.append_index_html_on_directories(true);

// Setup the router to always use the tower service.
//
// There is no caching, the files are purposely read and served on each request.
//
// Why? The Suibase webserver favor KISS over performance (modifying the files
// update the "website" on next request/refresh).
let app = axum::Router::new().fallback(
axum::routing::get_service(tower_srvc).handle_error(|error| async move {
(
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
)
}),
);

// Define the address to serve on
//
// TODO Get this from the yaml configuration.
let addr = std::net::SocketAddr::from(([0, 0, 0, 0], 44380));
log::info!("{} listening on {}", self.task_name, addr);

// Run the server
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.map_err(|e| anyhow::anyhow!("Server error: {}", e))?;

Ok(())
}
}
76 changes: 76 additions & 0 deletions typescript/sui-explorer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# IDE
**/.history/
**/.vscode/
**/.idea/

# OSX
.DS_Store

# Rust build directory
**/target
.pre-commit*

# Move build directory
build
!crates/sui-single-node-benchmark/tests/data/package_publish_from_bytecode/package_a/build
!crates/sui-single-node-benchmark/tests/data/package_publish_from_bytecode/package_b/build
storage
!consensus/core/src/storage
!crates/sui-types/src/storage
!narwhal/storage

# Move-related files
Move.lock
!crates/sui-framework/packages/move-stdlib/Move.lock
!crates/sui-framework/packages/sui-framework/Move.lock
!crates/sui-framework/packages/sui-system/Move.lock
!crates/sui-framework/packages/deepbook/Move.lock
.trace
.coverage_map.mvcov

# Thumbnails
._*

# Emacs
\#*\#
.\#*

# Python
.pyc

# Node.js
node_modules
.tsbuildinfo
*.tsbuildinfo
.turbo

# App build directories
dist/
storybook-static/
web-ext-artifacts/
.next/
next-env.d.ts

# App test artifacts
coverage/
test-results/
playwright-report/
playwright/.cache/

# logs
wallet.log.*
sui.log.*
.vercel
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# misc
*.key
.env
checkpoints_dir/*
light_client.yaml
docs/content/references/sui-api/sui-graphql/*
docs/content/references/framework/**

lcov.info
1 change: 1 addition & 0 deletions typescript/sui-explorer/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm build
4 changes: 4 additions & 0 deletions typescript/sui-explorer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
According to the Apache-2.0 license, a fork should clearly state the list of changes made,
so here is the full list of changes after forking the project from https://github.com/MystenLabs/sui-explorer:

https://github.com/kkomelin/sui-explorer/compare/before-fork...kkomelin:sui-explorer:main
Loading

0 comments on commit ef7e17e

Please sign in to comment.