Add static file server, CSS styles for home
Also fix the livereloading.
This commit is contained in:
@@ -10,7 +10,7 @@ use crate::partials::layout::Layout;
|
||||
pub async fn get(State(pool): State<PgPool>, layout: Layout) -> Result<Response> {
|
||||
let entries = get_entries(&pool, GetEntriesOptions::default()).await?;
|
||||
Ok(layout.render(html! {
|
||||
ul {
|
||||
ul class="entries" {
|
||||
@for entry in entries {
|
||||
@let title = entry.title.unwrap_or_else(|| "Untitled".to_string());
|
||||
@let url = format!("/entry/{}", entry.id);
|
||||
|
||||
@@ -15,7 +15,8 @@ pub async fn crawl(pool: &PgPool) -> anyhow::Result<()> {
|
||||
let client = Client::new();
|
||||
let feeds = get_feeds(pool).await?;
|
||||
for feed in feeds {
|
||||
let _feed_span = info_span!("feed", id = feed.id, url = feed.url.as_str());
|
||||
let feed_span = info_span!("feed", id = feed.id, url = feed.url.as_str());
|
||||
let _feed_span_guard = feed_span.enter();
|
||||
info!("Fetching feed");
|
||||
// TODO: handle these results
|
||||
let bytes = client.get(feed.url).send().await?.bytes().await?;
|
||||
@@ -23,7 +24,8 @@ pub async fn crawl(pool: &PgPool) -> anyhow::Result<()> {
|
||||
let parsed_feed = parser::parse(&bytes[..])?;
|
||||
let mut payload = Vec::with_capacity(parsed_feed.entries.len());
|
||||
for entry in parsed_feed.entries {
|
||||
let _entry_span = info_span!("entry", id = entry.id, title = entry.title.clone().map(|t| t.content));
|
||||
let entry_span = info_span!("entry", id = entry.id, title = entry.title.clone().map(|t| t.content));
|
||||
let _entry_span_guard = entry_span.enter();
|
||||
if let Some(link) = entry.links.get(0) {
|
||||
// if no scraped or feed date is available, fallback to the current time
|
||||
let published_at = entry.published.unwrap_or_else(Utc::now).naive_utc();
|
||||
@@ -51,7 +53,7 @@ pub async fn crawl(pool: &PgPool) -> anyhow::Result<()> {
|
||||
}
|
||||
}
|
||||
let entries = upsert_entries(pool, payload).await?;
|
||||
info!("Created {} entries for feed {}", entries.len(), feed.id);
|
||||
info!("Created {} entries", entries.len());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
27
src/main.rs
27
src/main.rs
@@ -1,4 +1,4 @@
|
||||
use std::path::Path;
|
||||
use std::{path::Path, net::SocketAddr};
|
||||
|
||||
use anyhow::Result;
|
||||
use axum::{
|
||||
@@ -11,7 +11,7 @@ use notify::Watcher;
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use tokio::sync::watch::channel;
|
||||
use tower::ServiceBuilder;
|
||||
use tower_http::trace::TraceLayer;
|
||||
use tower_http::{trace::TraceLayer, services::ServeDir};
|
||||
use tower_livereload::LiveReloadLayer;
|
||||
use tracing::debug;
|
||||
|
||||
@@ -20,6 +20,14 @@ use lib::handlers;
|
||||
use lib::log::init_tracing;
|
||||
use lib::state::AppState;
|
||||
|
||||
async fn serve(app: Router, addr: SocketAddr) -> Result<()> {
|
||||
debug!("listening on {}", addr);
|
||||
axum::Server::bind(&addr)
|
||||
.serve(app.into_make_service())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
dotenv().ok();
|
||||
@@ -49,6 +57,7 @@ async fn main() -> Result<()> {
|
||||
.route("/entry/:id", get(handlers::entry::get))
|
||||
.route("/log", get(handlers::log::get))
|
||||
.route("/log/stream", get(handlers::log::stream))
|
||||
.nest_service("/static", ServeDir::new("static"))
|
||||
.with_state(AppState {
|
||||
pool,
|
||||
config,
|
||||
@@ -56,22 +65,20 @@ async fn main() -> Result<()> {
|
||||
})
|
||||
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()));
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
if cfg!(debug_assertions) {
|
||||
debug!("starting livereload");
|
||||
let livereload = LiveReloadLayer::new();
|
||||
let reloader = livereload.reloader();
|
||||
let mut watcher = notify::recommended_watcher(move |_| reloader.reload())?;
|
||||
watcher.watch(
|
||||
Path::new("target/debug/crawlnicle"),
|
||||
Path::new("static"),
|
||||
notify::RecursiveMode::Recursive,
|
||||
)?;
|
||||
app = app.layer(livereload);
|
||||
serve(app, addr).await?;
|
||||
} else {
|
||||
serve(app, addr).await?;
|
||||
}
|
||||
|
||||
debug!("listening on {}", addr);
|
||||
axum::Server::bind(&addr)
|
||||
.serve(app.into_make_service())
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use maud::{html, Markup};
|
||||
|
||||
pub fn header(title: &str) -> Markup {
|
||||
html! {
|
||||
header {
|
||||
header class="header" {
|
||||
nav {
|
||||
h1 { a href="/" data-turbo-frame="main" { (title) } }
|
||||
ul {
|
||||
|
||||
@@ -42,6 +42,7 @@ impl Layout {
|
||||
script type="module" {
|
||||
r#"import * as Turbo from 'https://cdn.skypack.dev/@hotwired/turbo';"#
|
||||
}
|
||||
link rel="stylesheet" href="/static/styles.css";
|
||||
}
|
||||
body {
|
||||
(header(&self.title))
|
||||
|
||||
Reference in New Issue
Block a user