2023-06-28 22:59:26 +00:00
|
|
|
mod content;
|
|
|
|
|
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use axum::{
|
|
|
|
extract::State,
|
|
|
|
http::StatusCode,
|
|
|
|
response::{IntoResponse, Response},
|
|
|
|
routing::get,
|
|
|
|
Router, Server,
|
|
|
|
};
|
2023-06-26 08:08:49 +00:00
|
|
|
use figment::{
|
|
|
|
providers::{Env, Format, Serialized, Toml},
|
|
|
|
Figment,
|
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2023-06-28 23:11:09 +00:00
|
|
|
use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
|
2023-06-28 22:59:26 +00:00
|
|
|
use std::{net::SocketAddr, sync::Arc};
|
|
|
|
|
2023-06-28 23:11:09 +00:00
|
|
|
use crate::content::Page;
|
2023-06-26 08:08:49 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
struct Config {
|
|
|
|
bind: String,
|
|
|
|
database_url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Config {
|
|
|
|
bind: "127.0.0.1:3000".to_owned(),
|
|
|
|
database_url: "postgres://artificiale:changeme@localhost/artificiale".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-28 22:59:26 +00:00
|
|
|
struct GenericError(anyhow::Error);
|
|
|
|
|
|
|
|
impl IntoResponse for GenericError {
|
|
|
|
fn into_response(self) -> Response {
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
format!("Something went wrong: {}", self.0),
|
|
|
|
)
|
|
|
|
.into_response()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<sqlx::Error> for GenericError {
|
|
|
|
fn from(value: sqlx::Error) -> Self {
|
|
|
|
GenericError(anyhow!("Database error: {}", value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AppState {
|
|
|
|
database: Pool<Postgres>,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn root(State(state): State<Arc<AppState>>) -> Result<String, GenericError> {
|
2023-06-28 23:11:09 +00:00
|
|
|
let select_query: sqlx::query::QueryAs<'_, _, Page, _> =
|
|
|
|
sqlx::query_as::<_, Page>("SELECT * FROM pages");
|
|
|
|
let page: Page = select_query.fetch_one(&state.database).await?;
|
2023-06-28 22:59:26 +00:00
|
|
|
Ok(page.title)
|
2023-06-26 08:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
|
|
|
let config: Config = Figment::from(Serialized::defaults(Config::default()))
|
|
|
|
.merge(Toml::file("mabel.toml"))
|
|
|
|
.merge(Env::prefixed("MABEL_"))
|
|
|
|
.extract()?;
|
|
|
|
|
|
|
|
let pool = PgPoolOptions::new()
|
|
|
|
.max_connections(5)
|
|
|
|
.connect(config.database_url.as_str())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
sqlx::migrate!().run(&pool).await?;
|
|
|
|
|
2023-06-28 22:59:26 +00:00
|
|
|
let shared_state = Arc::new(AppState { database: pool });
|
|
|
|
|
|
|
|
let app = Router::new().route("/", get(root)).with_state(shared_state);
|
|
|
|
|
2023-06-26 08:08:49 +00:00
|
|
|
let addr: SocketAddr = config.bind.parse()?;
|
|
|
|
tracing::debug!("listening on {}", addr);
|
|
|
|
Server::bind(&addr)
|
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|