Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #81 from NethermindEth/hotfix/improved-health-check
Browse files Browse the repository at this point in the history
fix: improve health check logic
  • Loading branch information
varex83 authored Feb 27, 2024
2 parents 8ebd07b + 6ba3ffa commit 2b2d62b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 7 deletions.
73 changes: 70 additions & 3 deletions api/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,83 @@ pub mod types;

use crate::handlers::compile::do_compile;
use crate::handlers::compiler_version::do_compiler_version;
use crate::handlers::types::{ApiCommand, ApiCommandResult};
use crate::handlers::types::{ApiCommand, ApiCommandResult, HealthCheckResponse};
use crate::types::ApiError;
use crate::utils::lib::{get_file_path, init_parent_directories, ARTIFACTS_ROOT};
use rocket::tokio;
use std::path::PathBuf;
use std::str::FromStr;
use tracing::info;
use tracing::instrument;
use uuid::Uuid;

#[instrument]
#[get("/health")]
pub async fn health() -> &'static str {
pub async fn health() -> HealthCheckResponse {
info!("/health");
"OK"

let file_content = generate_mock_solidity_file_content();
let version = String::from("latest");
let path_uuid = generate_remix_file_path();
let local_path = get_file_path(&version, &path_uuid);
let path = PathBuf::from_str(&path_uuid).unwrap();

let artifact_folder = PathBuf::from(ARTIFACTS_ROOT)
.join(version.clone())
.join(path_uuid.clone());

// create file directory from file path
init_parent_directories(local_path.clone()).await;
if tokio::fs::write(&local_path, file_content).await.is_err() {
return HealthCheckResponse::error("Failed to write file");
}

let result = do_compile(version, path.clone()).await;

// cleanup
if tokio::fs::remove_dir_all(local_path.parent().unwrap().parent().unwrap())
.await
.is_err()
{
return HealthCheckResponse::error("Failed to remove directory from local path");
}

println!("Artifacts : {:?}", artifact_folder);
if tokio::fs::remove_dir_all(artifact_folder.parent().unwrap())
.await
.is_err()
{
return HealthCheckResponse::error("Failed to remove directory from artifact path");
}

if result.is_ok() {
HealthCheckResponse::ok()
} else {
HealthCheckResponse::error("Failed to compile")
}
}

pub fn generate_mock_solidity_file_content() -> String {
r#"
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 storedData;
function set(uint256 x) public {
storedData = x;
}
function get() public view returns (uint256) {
return storedData;
}
}
"#
.to_string()
}

pub fn generate_remix_file_path() -> String {
format!("{}/{}", Uuid::new_v4(), "SimpleStorage.sol")
}

#[instrument]
Expand Down
31 changes: 31 additions & 0 deletions api/src/handlers/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use rocket::http::Status;
use rocket::response::Responder;
use rocket::Request;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

Expand Down Expand Up @@ -47,3 +50,31 @@ pub enum ApiCommandResult {
#[allow(dead_code)]
Shutdown,
}

pub struct HealthCheckResponse(pub Result<(), &'static str>);

impl<'r, 'o: 'r> Responder<'r, 'o> for HealthCheckResponse {
fn respond_to(self, request: &'r Request<'_>) -> rocket::response::Result<'o> {
match self.0 {
Ok(_) => {
Ok(rocket::response::status::Custom(Status { code: 200 }, "OK")
.respond_to(request)?)
}
Err(_) => Ok(rocket::response::status::Custom(
Status { code: 500 },
"Internal Server Error",
)
.respond_to(request)?),
}
}
}

impl HealthCheckResponse {
pub fn ok() -> Self {
HealthCheckResponse(Ok(()))
}

pub fn error(value: &'static str) -> Self {
HealthCheckResponse(Err(value))
}
}
8 changes: 4 additions & 4 deletions plugin/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { createWeb3Modal } from '@web3modal/wagmi/react'
import { configureChains, createConfig, WagmiConfig } from 'wagmi'
import { publicProvider } from 'wagmi/providers/public'
import { CoinbaseWalletConnector } from 'wagmi/connectors/coinbaseWallet'

import { zkSync, zkSyncSepoliaTestnet } from 'viem/chains'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'
import { InjectedConnector } from 'wagmi/connectors/injected'
Expand All @@ -16,14 +15,15 @@ import { EIP6963Connector, walletConnectProvider } from '@web3modal/wagmi'
const projectId: string = import.meta.env.VITE_WALLET_CONNECT_PROJECT_ID // TODO who owns this? make sure nethermind owns this

const {
chains,
publicClient
} = configureChains([zkSyncSepoliaTestnet, zkSync], [walletConnectProvider({ projectId }), publicProvider()])
} = configureChains([zkSync, zkSyncSepoliaTestnet], [walletConnectProvider({ projectId }), publicProvider()])

const chains = [zkSyncSepoliaTestnet, zkSync]

const metadata = {
name: 'zkSync remix plugin',
description: 'zkSync remix plugin',
url: '',
url: 'https://remix.ethereum.org',
icons: ['https://avatars.githubusercontent.com/u/37784886']
}

Expand Down

0 comments on commit 2b2d62b

Please sign in to comment.