generated from alecodes/base-template
Compare commits
2 commits
e0e8700baa
...
cfaf7ecc1f
| Author | SHA1 | Date | |
|---|---|---|---|
| cfaf7ecc1f | |||
| 48afdaccea |
10 changed files with 1620 additions and 59 deletions
|
|
@ -1,3 +0,0 @@
|
||||||
import sassPlugin from "@alecodes/bun-plugin-sass";
|
|
||||||
|
|
||||||
Bun.plugin(sassPlugin);
|
|
||||||
37
.justfile
37
.justfile
|
|
@ -3,10 +3,6 @@ mod repo ".devfiles/justfile"
|
||||||
|
|
||||||
set dotenv-load := true
|
set dotenv-load := true
|
||||||
|
|
||||||
set shell := ["zsh", "-uc"]
|
|
||||||
|
|
||||||
build_mode := env("BUILD_MODE", "DEVELOPMENT")
|
|
||||||
|
|
||||||
[private]
|
[private]
|
||||||
docker-compose +ARGS:
|
docker-compose +ARGS:
|
||||||
docker compose --file .devfiles/docker/docker-compose.dev.yaml --env-file .env --project-name compendium {{ARGS}}
|
docker compose --file .devfiles/docker/docker-compose.dev.yaml --env-file .env --project-name compendium {{ARGS}}
|
||||||
|
|
@ -14,20 +10,31 @@ docker-compose +ARGS:
|
||||||
start-dev-services: (docker-compose "up --remove-orphans")
|
start-dev-services: (docker-compose "up --remove-orphans")
|
||||||
|
|
||||||
dev:
|
dev:
|
||||||
watchexec --restart --clear --watch src --watch templates cargo run
|
watchexec --restart --clear --watch src --watch dist cargo run
|
||||||
|
|
||||||
migrate: (docker-compose "run dbmate migrate")
|
migrate: (docker-compose "run dbmate migrate")
|
||||||
|
|
||||||
rollback: (docker-compose "run dbmate rollback")
|
rollback: (docker-compose "run dbmate rollback")
|
||||||
|
|
||||||
build-frontend:
|
build-frontend:
|
||||||
bun build \
|
#!/usr/bin/env bun
|
||||||
{{ if uppercase(build_mode) == "DEVELOPMENT" { "--watch" } else { "" } }} \
|
import sassPlugin from "@alecodes/bun-plugin-sass";
|
||||||
--outdir 'dist' \
|
|
||||||
--public-path '/' \
|
const entrypoints = Array.from(
|
||||||
--entry-naming '[dir]/[name].[ext]' \
|
new Bun.Glob("frontend/**/*.html").scanSync(".")
|
||||||
--chunk-naming 'assets/[name]-[hash].[ext]' \
|
);
|
||||||
--asset-naming 'assets/[name]-[hash].[ext]' \
|
|
||||||
--splitting \
|
const result = await Bun.build({
|
||||||
--css-chunking \
|
entrypoints,
|
||||||
{{ './frontend/**/*.html' }}
|
outdir: "dist",
|
||||||
|
publicPath: "/",
|
||||||
|
splitting: true,
|
||||||
|
plugins: [sassPlugin],
|
||||||
|
naming: {
|
||||||
|
entry: "[dir]/[name].[ext]",
|
||||||
|
chunk: "assets/[name]-[hash].[ext]",
|
||||||
|
asset: "assets/[name]-[hash].[ext]",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("Assets compiled!");
|
||||||
|
|
|
||||||
1566
Cargo.lock
generated
1566
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -5,10 +5,18 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = "0.8.1"
|
axum = "0.8.1"
|
||||||
|
axum-sqlx-tx = "0.10.0"
|
||||||
minijinja = { version = "2.7.0", features = ["loader"] }
|
minijinja = { version = "2.7.0", features = ["loader"] }
|
||||||
minijinja-embed = "2.7.0"
|
minijinja-embed = "2.7.0"
|
||||||
notify = "8.0.0"
|
notify = "8.0.0"
|
||||||
serde = { version = "1.0.217", features = ["derive"] }
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
|
sqlx = { version = "0.8.3", features = [
|
||||||
|
"chrono",
|
||||||
|
"derive",
|
||||||
|
"json",
|
||||||
|
"postgres",
|
||||||
|
"runtime-tokio",
|
||||||
|
] }
|
||||||
thiserror = "2.0.11"
|
thiserror = "2.0.11"
|
||||||
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
|
||||||
tower-http = { version = "0.6.2", features = ["fs", "trace"] }
|
tower-http = { version = "0.6.2", features = ["fs", "trace"] }
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
preload = ["./.devfiles/init-bun.ts"]
|
|
||||||
|
|
||||||
[serve.static]
|
[serve.static]
|
||||||
plugins = ["bun-plugin-sass"]
|
plugins = ["bun-plugin-sass"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
h1 {
|
h1 {
|
||||||
color: blue;
|
color: red;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<link rel="stylesheet" href="../css/style.css" />
|
|
||||||
<title>
|
|
||||||
{% block title %}Axum web service!{% endblock %}
|
|
||||||
</title>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
{% include "partials/header.html" %}
|
|
||||||
<main>
|
|
||||||
<nav class="msp-d-sm-none msp-d-flex msp-justify-content-end">
|
|
||||||
<button class="msp-offcanvas-toggle" data-msp-target="#main-offcanvas">Toggle</button>
|
|
||||||
</nav>
|
|
||||||
{% block content %}{% endblock %}
|
|
||||||
</main>
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
||||||
|
|
@ -19,13 +19,17 @@ pub enum Error {
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
IO(#[from] std::io::Error),
|
IO(#[from] std::io::Error),
|
||||||
|
|
||||||
|
#[error(transparent)]
|
||||||
|
Env(#[from] std::env::VarError),
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Template(#[from] minijinja::Error),
|
Template(#[from] minijinja::Error),
|
||||||
|
|
||||||
#[error("Error in runtime execution: {0}")]
|
#[error("Error in runtime execution: {0}")]
|
||||||
Runtime(&'static str),
|
Runtime(&'static str),
|
||||||
// #[error(transparent)]
|
|
||||||
// DatabaseOperation(#[from] sqlx::Error),
|
#[error(transparent)]
|
||||||
|
DatabaseOperation(#[from] sqlx::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for Error {
|
impl IntoResponse for Error {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ use minijinja::{Environment, Template};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub type Tx = axum_sqlx_tx::Tx<sqlx::Postgres>;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Link {
|
pub struct Link {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
|
|
||||||
30
src/main.rs
30
src/main.rs
|
|
@ -1,8 +1,11 @@
|
||||||
#![allow(unused)]
|
#![allow(unused)]
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use compendium::{router, AppState, Error, Link, Result};
|
use compendium::{router, AppState, Error, Link, Result, Tx};
|
||||||
use minijinja::{Environment, Value};
|
use minijinja::{Environment, Value};
|
||||||
|
use sqlx::postgres::PgPoolOptions;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use std::env;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
@ -37,6 +40,23 @@ fn load_templates() -> Result<Environment<'static>> {
|
||||||
Ok(tmpl_env)
|
Ok(tmpl_env)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn init_db() -> Result<PgPool> {
|
||||||
|
let db_string = format!(
|
||||||
|
"postgres://{}:{}@{}/{}",
|
||||||
|
env::var("CPD_DB_USER")?,
|
||||||
|
env::var("CPD_DB_PASSWORD")?,
|
||||||
|
env::var("CPD_DB_HOST")?,
|
||||||
|
env::var("CPD_DB_NAME")?
|
||||||
|
);
|
||||||
|
|
||||||
|
info!("Connecting to database {}", db_string);
|
||||||
|
|
||||||
|
Ok(PgPoolOptions::new()
|
||||||
|
.max_connections(5)
|
||||||
|
.connect(&db_string)
|
||||||
|
.await?)
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
// Logs
|
// Logs
|
||||||
|
|
@ -51,9 +71,15 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
let mut tmpl_env = load_templates()?;
|
let mut tmpl_env = load_templates()?;
|
||||||
|
|
||||||
|
let pool = init_db().await?;
|
||||||
|
|
||||||
|
let (tx_state, tx_layer) = Tx::setup(pool);
|
||||||
|
|
||||||
let app = router::new()
|
let app = router::new()
|
||||||
.layer(TraceLayer::new_for_http().on_request(()))
|
.layer(TraceLayer::new_for_http().on_request(()))
|
||||||
.with_state(AppState::new(tmpl_env));
|
.with_state(AppState::new(tmpl_env))
|
||||||
|
.layer(tx_layer)
|
||||||
|
.with_state(tx_state);
|
||||||
|
|
||||||
// Add hot reload only on dev mode
|
// Add hot reload only on dev mode
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue