Skip to content

Commit cf348e9

Browse files
committed
Try litefs for db
1 parent 2c7b5e5 commit cf348e9

File tree

4 files changed

+58
-31
lines changed

4 files changed

+58
-31
lines changed

Dockerfile

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ RUN cargo chef prepare --recipe-path recipe.json
77

88
FROM chef AS builder
99
COPY --from=planner /app/recipe.json recipe.json
10-
# Build dependencies - this is the caching Docker layer!
1110
RUN cargo chef cook --release --recipe-path recipe.json
12-
# Build application
1311
COPY . .
1412
RUN cargo build --release --bin mdow
1513

16-
# We do not need the Rust toolchain to run the binary!
17-
FROM debian:bookworm-slim AS runtime
14+
FROM flyio/litefs:0.5 AS runtime
15+
# Install required packages
16+
RUN apt-get update && apt-get install -y \
17+
ca-certificates \
18+
&& rm -rf /var/lib/apt/lists/*
19+
1820
WORKDIR /app
19-
RUN mkdir -p /data && \
20-
chown 1000:1000 /data && \
21-
chmod 755 /data
2221
COPY --from=builder /app/target/release/mdow /usr/local/bin
23-
ENTRYPOINT ["/usr/local/bin/mdow"]
22+
COPY litefs.yml /etc/litefs.yml
23+
24+
ENV DATABASE_URL="sqlite:/litefs/mdow.db"
25+
ENTRYPOINT ["/usr/local/bin/litefs"]

fly.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ primary_region = 'lhr'
99
[build]
1010

1111
[env]
12-
PORT = '8080'
13-
DATABASE_URL = "sqlite:/data/mdow.db"
12+
PORT = '8081'
13+
DATABASE_URL = "sqlite:/litefs/mdow.db"
14+
FLY_CONSUL_URL = "consul+tls://fly-consul.fly.dev:8501"
1415

1516
[http_service]
1617
internal_port = 8080
@@ -21,8 +22,8 @@ primary_region = 'lhr'
2122
processes = ['app']
2223

2324
[[mounts]]
24-
source = "mdow_data"
25-
destination = "/data"
25+
source = "mdow_litefs"
26+
destination = "/var/lib/litefs"
2627

2728
[[vm]]
2829
memory = '256mb'

litefs.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fuse:
2+
dir: "/litefs"
3+
4+
data:
5+
dir: "/var/lib/litefs"
6+
7+
proxy:
8+
addr: ":8080"
9+
target: "localhost:8081"
10+
db: "mdow.db"
11+
12+
exec:
13+
- cmd: "/usr/local/bin/mdow"
14+
if-primary: true
15+
16+
lease:
17+
type: "consul"
18+
consul:
19+
url: "${FLY_CONSUL_URL}"
20+
key: "litefs/${FLY_APP_NAME}"

src/main.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ use std::net::SocketAddr;
1515
use serde::Deserialize;
1616
use pulldown_cmark::{Parser, Options, html::push_html};
1717
use html_escape::encode_text;
18-
use sqlx::sqlite::SqlitePool;
18+
use sqlx::sqlite::{SqlitePool, SqlitePoolOptions, SqliteConnectOptions, SqliteJournalMode};
1919
use uuid::Uuid;
20-
use chrono::{DateTime, Utc, Duration};
20+
use std::str::FromStr;
21+
use std::time::Duration;
22+
use chrono::{DateTime, Utc};
2123

2224
#[derive(Deserialize)]
2325
struct MarkdownInput {
@@ -149,7 +151,7 @@ async fn share_markdown(
149151
) -> impl IntoResponse {
150152
let id = Uuid::new_v4().to_string();
151153
let now = Utc::now();
152-
let expires_at = now + Duration::days(30);
154+
let expires_at = now + chrono::Duration::days(30);
153155

154156
// Store the document
155157
sqlx::query(
@@ -315,21 +317,16 @@ async fn main() {
315317
let db_path = std::env::var("DATABASE_URL")
316318
.unwrap_or_else(|_| "sqlite:data/database.db".to_string());
317319

318-
// If using a custom path, ensure the directory exists
319-
if let Some(path) = db_path.strip_prefix("sqlite:") {
320-
if let Some(parent) = std::path::Path::new(path).parent() {
321-
match std::fs::create_dir_all(parent) {
322-
Ok(_) => println!("Database directory created/verified at: {}", parent.display()),
323-
Err(e) => {
324-
eprintln!("Failed to create database directory: {}", e);
325-
std::process::exit(1);
326-
}
327-
}
328-
}
329-
}
330-
331-
// Initialize the database pool
332-
let pool = SqlitePool::connect(&db_path)
320+
// Configure SQLite connection pool with WAL mode and busy timeout
321+
let pool = SqlitePoolOptions::new()
322+
.max_connections(5)
323+
.connect_with(
324+
SqliteConnectOptions::from_str(&db_path)
325+
.unwrap()
326+
.create_if_missing(true)
327+
.journal_mode(SqliteJournalMode::Wal)
328+
.busy_timeout(Duration::from_secs(30))
329+
)
333330
.await
334331
.expect("Failed to connect to database");
335332

@@ -358,7 +355,14 @@ async fn main() {
358355
.fallback(handle_404)
359356
.with_state(pool);
360357

361-
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
358+
// Get port from environment or default to 8081
359+
let port =
360+
std::env::var("PORT")
361+
.ok()
362+
.and_then(|p| p.parse().ok())
363+
.unwrap_or(8081);
364+
365+
let addr = SocketAddr::from(([0, 0, 0, 0], port));
362366
println!("Listening on {}", addr);
363367
axum::Server::bind(&addr)
364368
.serve(app.into_make_service())

0 commit comments

Comments
 (0)