-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d2617f
commit 89a2f73
Showing
3 changed files
with
49 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,61 @@ | ||
use axum::{routing::get, Router, extract::State, Json, response::IntoResponse}; | ||
use serde_json::json; | ||
|
||
use shuttle_firecrawl::firecrawl::FirecrawlApp; | ||
use axum::{extract::State, response::IntoResponse, routing::get, Json, Router}; | ||
use firecrawl::{ | ||
scrape::{ScrapeFormats, ScrapeOptions}, | ||
FirecrawlApp, | ||
}; | ||
use serde::Deserialize; | ||
use url::Url; | ||
|
||
#[derive(Clone)] | ||
struct AppState { | ||
firecrawl: FirecrawlApp, | ||
} | ||
|
||
impl AppState { | ||
fn new(firecrawl: FirecrawlApp) -> Self { | ||
fn new() -> Self { | ||
let firecrawl_api_key = | ||
std::env::var("FIRECRAWL_API_KEY").expect("FIRECRAWL_API_KEY to be set as an env var"); | ||
let firecrawl = FirecrawlApp::new(firecrawl_api_key).unwrap(); | ||
Self { firecrawl } | ||
} | ||
} | ||
|
||
async fn scrape_page(State(state): State<AppState>) -> impl IntoResponse { | ||
let scrape_response = state.firecrawl.scrape_url("https://firecrawl.dev", Some(json!({ | ||
"formats": ["markdown", "html"] | ||
}))).await.unwrap(); | ||
#[derive(Deserialize)] | ||
struct Request { | ||
url: Url, | ||
} | ||
|
||
Json(scrape_response) | ||
async fn scrape_page( | ||
State(state): State<AppState>, | ||
Json(json): Json<Request>, | ||
) -> impl IntoResponse { | ||
let scrape_options = ScrapeOptions { | ||
formats: Some(vec![ScrapeFormats::HTML, ScrapeFormats::Markdown]), | ||
..Default::default() | ||
}; | ||
|
||
let scrape_response = state | ||
.firecrawl | ||
.scrape_url(&json.url, scrape_options) | ||
.await | ||
.unwrap(); | ||
|
||
Json(scrape_response) | ||
} | ||
|
||
#[shuttle_runtime::main] | ||
async fn main(#[shuttle_firecrawl::Firecrawl( | ||
api_key = "{secrets.FIRECRAWL_API_KEY}" | ||
)] firecrawl: FirecrawlApp) -> shuttle_axum::ShuttleAxum { | ||
let state = AppState::new(firecrawl); | ||
|
||
let router = Router::new().route("/", get(scrape_page)).with_state(state); | ||
async fn main( | ||
#[shuttle_runtime::Secrets] secrets: shuttle_runtime::SecretStore, | ||
) -> shuttle_axum::ShuttleAxum { | ||
secrets.into_iter().for_each(|(key, val)| { | ||
std::env::set_var(key, val); | ||
}); | ||
|
||
let state = AppState::new(); | ||
|
||
let router = Router::new() | ||
.route("/scrape", get(scrape_page)) | ||
.with_state(state); | ||
|
||
Ok(router.into()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters