Initial commit
This commit is contained in:
3
.env
Normal file
3
.env
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
DATABASE_URL=/home/fpasqua/RustroverProjects/htmxssrtest/sqlite.db
|
||||||
|
HTTP_HOST=0.0.0.0
|
||||||
|
HTTP_PORT=3000
|
||||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
1470
Cargo.lock
generated
Normal file
1470
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
20
Cargo.toml
Normal file
20
Cargo.toml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
[package]
|
||||||
|
name = "htmxssrtest"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
axum = "0.7.9"
|
||||||
|
tower = "0.5.2"
|
||||||
|
tracing = "0.1.41"
|
||||||
|
tracing-subscriber = "0.3.19"
|
||||||
|
tower-http = { version = "0.6.2", features = ["cors", "trace"] }
|
||||||
|
chrono = "0.4.39"
|
||||||
|
diesel = { version = "2.2.0", features = ["sqlite", "returning_clauses_for_sqlite_3_35"] }
|
||||||
|
dotenvy = "0.15"
|
||||||
|
anyhow = "1.0.95"
|
||||||
|
deadpool-diesel = { version = "0.6.1", features = ["sqlite", "tracing"]}
|
||||||
|
clap = { version = "4.5.21", features = ["derive", "env"] }
|
||||||
|
askama = { version = "0.12.1", features = ["with-axum"] }
|
||||||
|
askama_axum = "0.4"
|
||||||
4
README.md
Normal file
4
README.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# HTMX SSR Test
|
||||||
|
First of all you need diesel-cli, obtain it [here](https://diesel.rs/guides/getting-started).
|
||||||
|
|
||||||
|
After the setup+migrate step to initialize the database, you can just start the webserver.
|
||||||
9
diesel.toml
Normal file
9
diesel.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# For documentation on how to configure this file,
|
||||||
|
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||||
|
|
||||||
|
[print_schema]
|
||||||
|
file = "src/schema.rs"
|
||||||
|
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]
|
||||||
|
|
||||||
|
[migrations_directory]
|
||||||
|
dir = "/home/fpasqua/RustroverProjects/htmxssrtest/migrations"
|
||||||
4
httptests/Index.http
Normal file
4
httptests/Index.http
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
### GET request to index
|
||||||
|
GET http://{{host}}:{{port}}/
|
||||||
|
|
||||||
|
###
|
||||||
6
httptests/http-client.env.json
Normal file
6
httptests/http-client.env.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"dev": {
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": "3000"
|
||||||
|
}
|
||||||
|
}
|
||||||
0
migrations/.keep
Normal file
0
migrations/.keep
Normal file
2
migrations/2025-01-27-182448_initial/down.sql
Normal file
2
migrations/2025-01-27-182448_initial/down.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
DROP TABLE IF EXISTS rss_feeds;
|
||||||
7
migrations/2025-01-27-182448_initial/up.sql
Normal file
7
migrations/2025-01-27-182448_initial/up.sql
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
-- Your SQL goes here
|
||||||
|
CREATE TABLE rss_feeds (
|
||||||
|
id INTEGER NOT NULL PRIMARY KEY,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
feed_url TEXT NOT NULL,
|
||||||
|
last_pub_date TEXT DEFAULT NULL -- ISO 8601 format e.g. "2023-10-05T14:48:00+02:00"
|
||||||
|
);
|
||||||
135
src/main.rs
Normal file
135
src/main.rs
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
use diesel::prelude::*;
|
||||||
|
use std::env;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use anyhow::{anyhow, Result, Context};
|
||||||
|
use askama_axum::Template;
|
||||||
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
http::{Method, header, StatusCode, Request, Uri},
|
||||||
|
response::{IntoResponse, Response, Html},
|
||||||
|
routing::{get, post},
|
||||||
|
Json, Router,
|
||||||
|
};
|
||||||
|
use deadpool_diesel::sqlite::{Runtime, Manager, Pool};
|
||||||
|
use clap::Parser;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use tower::{ServiceBuilder};
|
||||||
|
use tower_http::{
|
||||||
|
cors::{Any, CorsLayer},
|
||||||
|
trace::{TraceLayer, DefaultOnResponse},
|
||||||
|
LatencyUnit
|
||||||
|
};
|
||||||
|
use tracing::{
|
||||||
|
info, error, debug, info_span, enabled,
|
||||||
|
instrument, Level, Span
|
||||||
|
};
|
||||||
|
|
||||||
|
pub mod models;
|
||||||
|
pub mod schema;
|
||||||
|
|
||||||
|
use self::models::*;
|
||||||
|
|
||||||
|
#[derive(Parser,Clone)]
|
||||||
|
struct Config {
|
||||||
|
#[arg(long,env)]
|
||||||
|
database_url: String,
|
||||||
|
#[arg(long,env)]
|
||||||
|
http_host: String,
|
||||||
|
#[arg(long,env)]
|
||||||
|
http_port: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct AppContext {
|
||||||
|
config: Config,
|
||||||
|
pool: Pool,
|
||||||
|
counter: Arc<Mutex<i32>>
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn establish_connection(database_url: &str) -> Pool {
|
||||||
|
let manager = Manager::new(database_url, Runtime::Tokio1);
|
||||||
|
let pool = Pool::builder(manager)
|
||||||
|
.max_size(8)
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
let _ = pool.get().await.expect("DB connection failed"); // Test if DB connection was fine
|
||||||
|
pool
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
// Load Env File
|
||||||
|
dotenvy::dotenv().ok();
|
||||||
|
// Init Tracing
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
// Parse Env/CLI
|
||||||
|
let config = Config::parse();
|
||||||
|
// Establish Connection Pool
|
||||||
|
let pool: Pool = establish_connection(&config.database_url).await;
|
||||||
|
// Prepare Web Server Context
|
||||||
|
let context = AppContext {
|
||||||
|
config: config.clone(),
|
||||||
|
pool: pool.clone(),
|
||||||
|
counter: Arc::new(Mutex::new(0)),
|
||||||
|
};
|
||||||
|
// Prepare Middlewares
|
||||||
|
let cors = CorsLayer::very_permissive();
|
||||||
|
let tracelayer = TraceLayer::new_for_http()
|
||||||
|
.make_span_with(|request: &Request<_>| {
|
||||||
|
// Log the matched route's path (with placeholders not filled in).
|
||||||
|
let path = request.uri().to_string();
|
||||||
|
info_span!(
|
||||||
|
"request",
|
||||||
|
method = ?request.method(),
|
||||||
|
path
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.on_request(|_request: &Request<_>, _span: &Span| {
|
||||||
|
debug!("Request received");
|
||||||
|
})
|
||||||
|
.on_response(
|
||||||
|
DefaultOnResponse::new()
|
||||||
|
.level(Level::DEBUG)
|
||||||
|
.latency_unit(LatencyUnit::Millis)
|
||||||
|
);
|
||||||
|
let middlewares = ServiceBuilder::new()
|
||||||
|
.layer(tracelayer).layer(cors);
|
||||||
|
// Launch Web Server
|
||||||
|
let app = Router::new()
|
||||||
|
.route("/", get(index))
|
||||||
|
.layer(middlewares)
|
||||||
|
.with_state(context.clone());
|
||||||
|
// Run our app with hyper
|
||||||
|
let listener = tokio::net::TcpListener::bind((config.http_host.as_str(), config.http_port))
|
||||||
|
.await.context(format!("Failed to bind Web Service on {}:{}", config.http_host, config.http_port))?;
|
||||||
|
info!("Listening on {}:{}", config.http_host, config.http_port);
|
||||||
|
axum::serve(listener, app).await.context("Failed to serve Web Service")?;
|
||||||
|
// let connection = &mut establish_connection();
|
||||||
|
// use self::schema::rss_feeds::dsl::*;
|
||||||
|
// let results = rss_feeds
|
||||||
|
// .limit(5)
|
||||||
|
// .select(RssFeed::as_select())
|
||||||
|
// .load(connection)
|
||||||
|
// .expect("Error loading posts");
|
||||||
|
//
|
||||||
|
// println!("Displaying {} posts", results.len());
|
||||||
|
// for rss_feed in results {
|
||||||
|
// println!("{}", rss_feed.name);
|
||||||
|
// println!("-----------\n");
|
||||||
|
// println!("{}", rss_feed.feed_url);
|
||||||
|
// }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(path = "index.html")]
|
||||||
|
struct HelloTemplate<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn index(
|
||||||
|
State(ctx): State<AppContext>,
|
||||||
|
) -> HelloTemplate<'static> {
|
||||||
|
HelloTemplate { name: "world" }
|
||||||
|
}
|
||||||
11
src/models.rs
Normal file
11
src/models.rs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Queryable, Selectable)]
|
||||||
|
#[diesel(table_name = crate::schema::rss_feeds)]
|
||||||
|
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||||
|
pub struct RssFeed {
|
||||||
|
pub id: i32,
|
||||||
|
pub name: String,
|
||||||
|
pub feed_url: String,
|
||||||
|
pub last_pub_date: Option<String>,
|
||||||
|
}
|
||||||
10
src/schema.rs
Normal file
10
src/schema.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
// @generated automatically by Diesel CLI.
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
rss_feeds (id) {
|
||||||
|
id -> Integer,
|
||||||
|
name -> Text,
|
||||||
|
feed_url -> Text,
|
||||||
|
last_pub_date -> Nullable<Text>,
|
||||||
|
}
|
||||||
|
}
|
||||||
14
templates/base.html
Normal file
14
templates/base.html
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<link href="https://matcha.mizu.sh/matcha.css" rel="stylesheet">
|
||||||
|
<title>{% block title %}{{ title }}{% endblock %}</title>
|
||||||
|
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
|
||||||
|
{% block head %}{% endblock %}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="content">
|
||||||
|
{% block content %}{% endblock %}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
7
templates/index.html
Normal file
7
templates/index.html
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block title %}Hello!{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<h1>Hello, {{name}}!</h1>
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user