From 6242294a4c45f46bfee5d1588e67885f73d36cfb Mon Sep 17 00:00:00 2001 From: WriterV2 Date: Sun, 3 Nov 2024 13:47:30 +0100 Subject: Feat: Add docker --- .dockerignore | 4 ++ ...9dc6fd308a8eaf1b9f7d0dab531164961142ba041f.json | 44 +++++++++++++ ...4ec5de803061f81e8a36002812e9eaacee4bd2a47a.json | 44 +++++++++++++ ...51f735379a42653230f17abf8649d944cf5e06f9ab.json | 44 +++++++++++++ Dockerfile | 76 ++++++++++++++++++++++ input.css | 3 + src/handlers/mod.rs | 2 +- src/input.css | 3 - src/main.rs | 7 +- static/styles.css | 1 + 10 files changed, 221 insertions(+), 7 deletions(-) create mode 100644 .dockerignore create mode 100644 .sqlx/query-312cace7ec199361a6df0b9dc6fd308a8eaf1b9f7d0dab531164961142ba041f.json create mode 100644 .sqlx/query-bcf2aee61cbec68a6d3d964ec5de803061f81e8a36002812e9eaacee4bd2a47a.json create mode 100644 .sqlx/query-cb6484175e6e0a7207510351f735379a42653230f17abf8649d944cf5e06f9ab.json create mode 100644 Dockerfile create mode 100644 input.css delete mode 100644 src/input.css create mode 100644 static/styles.css diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c3e7034 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +target/ +.git/ +.env +*.db diff --git a/.sqlx/query-312cace7ec199361a6df0b9dc6fd308a8eaf1b9f7d0dab531164961142ba041f.json b/.sqlx/query-312cace7ec199361a6df0b9dc6fd308a8eaf1b9f7d0dab531164961142ba041f.json new file mode 100644 index 0000000..073f76b --- /dev/null +++ b/.sqlx/query-312cace7ec199361a6df0b9dc6fd308a8eaf1b9f7d0dab531164961142ba041f.json @@ -0,0 +1,44 @@ +{ + "db_name": "SQLite", + "query": "SELECT * FROM story", + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Integer" + }, + { + "name": "language", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "pdf", + "ordinal": 2, + "type_info": "Blob" + }, + { + "name": "epub", + "ordinal": 3, + "type_info": "Blob" + }, + { + "name": "pid", + "ordinal": 4, + "type_info": "Integer" + } + ], + "parameters": { + "Right": 0 + }, + "nullable": [ + false, + false, + false, + false, + false + ] + }, + "hash": "312cace7ec199361a6df0b9dc6fd308a8eaf1b9f7d0dab531164961142ba041f" +} diff --git a/.sqlx/query-bcf2aee61cbec68a6d3d964ec5de803061f81e8a36002812e9eaacee4bd2a47a.json b/.sqlx/query-bcf2aee61cbec68a6d3d964ec5de803061f81e8a36002812e9eaacee4bd2a47a.json new file mode 100644 index 0000000..ad7fec3 --- /dev/null +++ b/.sqlx/query-bcf2aee61cbec68a6d3d964ec5de803061f81e8a36002812e9eaacee4bd2a47a.json @@ -0,0 +1,44 @@ +{ + "db_name": "SQLite", + "query": "INSERT INTO product (name, description, uploaddate, updatedate) VALUES ($1, $2, $3, $4) RETURNING id, name, description, uploaddate, updatedate", + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Integer" + }, + { + "name": "name", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "description", + "ordinal": 2, + "type_info": "Text" + }, + { + "name": "uploaddate", + "ordinal": 3, + "type_info": "Integer" + }, + { + "name": "updatedate", + "ordinal": 4, + "type_info": "Integer" + } + ], + "parameters": { + "Right": 4 + }, + "nullable": [ + false, + false, + false, + false, + false + ] + }, + "hash": "bcf2aee61cbec68a6d3d964ec5de803061f81e8a36002812e9eaacee4bd2a47a" +} diff --git a/.sqlx/query-cb6484175e6e0a7207510351f735379a42653230f17abf8649d944cf5e06f9ab.json b/.sqlx/query-cb6484175e6e0a7207510351f735379a42653230f17abf8649d944cf5e06f9ab.json new file mode 100644 index 0000000..bb7efcc --- /dev/null +++ b/.sqlx/query-cb6484175e6e0a7207510351f735379a42653230f17abf8649d944cf5e06f9ab.json @@ -0,0 +1,44 @@ +{ + "db_name": "SQLite", + "query": "INSERT INTO story (language, pdf, epub, pid) VALUES ($1, $2, $3, $4) RETURNING id, language, pdf, epub, pid", + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Integer" + }, + { + "name": "language", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "pdf", + "ordinal": 2, + "type_info": "Blob" + }, + { + "name": "epub", + "ordinal": 3, + "type_info": "Blob" + }, + { + "name": "pid", + "ordinal": 4, + "type_info": "Integer" + } + ], + "parameters": { + "Right": 4 + }, + "nullable": [ + false, + false, + false, + false, + false + ] + }, + "hash": "cb6484175e6e0a7207510351f735379a42653230f17abf8649d944cf5e06f9ab" +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4be7100 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,76 @@ +# Planner stage - Compute dependencies and create recipe +FROM lukemathwalker/cargo-chef:latest-rust-1.75 AS planner +WORKDIR /app +# Copy the minimal files needed for dependency calculation +COPY Cargo.* ./ +COPY src/ src/ +RUN cargo chef prepare --recipe-path recipe.json + +# Cache dependencies stage +FROM lukemathwalker/cargo-chef:latest-rust-1.75 AS cacher +WORKDIR /app +COPY --from=planner /app/recipe.json recipe.json +RUN cargo chef cook --release --recipe-path recipe.json + +# Builder stage +FROM lukemathwalker/cargo-chef:latest-rust-1.75 AS builder +WORKDIR /app + +# Copy the entire project context +COPY . . + +# Copy cached dependencies +COPY --from=cacher /app/target target +COPY --from=cacher /usr/local/cargo /usr/local/cargo + +# Set sqlx offline mode to enable building without database connection +ENV SQLX_OFFLINE=true + +# Build the application +RUN cargo build --release + +# Runtime stage +FROM debian:bookworm-slim AS runtime + +# Install SSL certificates and sqlite3 +RUN apt-get update && \ + apt-get install -y ca-certificates sqlite3 && \ + rm -rf /var/lib/apt/lists/* + +WORKDIR /app + +# Copy the compiled binary +COPY --from=builder /app/target/release/portfolio-website /app/portfolio-website + +# Copy migrations folder for SQLX +COPY --from=builder /app/migrations /app/migrations + +# Copy static directory +COPY --from=builder /app/static /app/static + +# Create data directory and set up permissions +RUN mkdir -p /app/data && \ + # Create a specific group and user for the application + groupadd -r appgroup && \ + useradd -r -g appgroup appuser && \ + # Give ownership to the new user + chown -R appuser:appgroup /app && \ + # Set directory permissions + chmod -R 755 /app && \ + chmod 777 /app/data && \ + # Create an empty database file with proper permissions + touch /app/data/production.db && \ + chmod 666 /app/data/production.db + +# Create .env file with production settings +RUN echo "DATABASE_URL=sqlite:/app/data/production.db" > /app/.env && \ + chown appuser:appgroup /app/.env + +# Switch to application user +USER appuser + +# Expose the port your application runs on +EXPOSE 3000 + +# Use JSON format for CMD to handle signals properly +CMD ["sh", "-c", "DATABASE_URL=sqlite:/app/data/production.db /app/portfolio-website"] diff --git a/input.css b/input.css new file mode 100644 index 0000000..b5c61c9 --- /dev/null +++ b/input.css @@ -0,0 +1,3 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 9d5aa54..4a31909 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -26,7 +26,7 @@ pub async fn router(pool: SqlitePool) -> Router { .layer(middleware::from_fn(admin_middleware)) .route("/", get(home)) .route("/stories", get(build_page::)) - .nest_service("/static", ServeDir::new("src/static")) + .nest_service("/static", ServeDir::new("static")) .layer(DefaultBodyLimit::max(1074000000)) .layer(AddExtensionLayer::new(ApiContext { pool })) } diff --git a/src/input.css b/src/input.css deleted file mode 100644 index b5c61c9..0000000 --- a/src/input.css +++ /dev/null @@ -1,3 +0,0 @@ -@tailwind base; -@tailwind components; -@tailwind utilities; diff --git a/src/main.rs b/src/main.rs index dfdf658..ffdce27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,10 +10,11 @@ mod handlers; #[tokio::main] async fn main() { dotenv().ok(); + let db_url = + &env::var("DATABASE_URL").expect("Failed to get environment variable DATABASE_URL"); + let pool = SqlitePoolOptions::new() - .connect( - &env::var("DATABASE_URL").expect("Failed to get environment variable DATABASE_URL"), - ) + .connect(db_url) .await .expect("Failed to connect to database"); diff --git a/static/styles.css b/static/styles.css new file mode 100644 index 0000000..fa5f269 --- /dev/null +++ b/static/styles.css @@ -0,0 +1 @@ +/*! tailwindcss v3.4.4 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}:host,html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal;font-variation-settings:normal;-webkit-tap-highlight-color:transparent}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-feature-settings:normal;font-variation-settings:normal;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;letter-spacing:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}button,input:where([type=button]),input:where([type=reset]),input:where([type=submit]){-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: ;--tw-contain-size: ;--tw-contain-layout: ;--tw-contain-paint: ;--tw-contain-style: }.static{position:static}.m-12{margin:3rem}.mt-9{margin-top:2.25rem}.flex{display:flex}.grid{display:grid}.grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.flex-row{flex-direction:row}.flex-wrap{flex-wrap:wrap}.content-center{align-content:center}.justify-around{justify-content:space-around}.gap-8{gap:2rem}.space-x-5>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(1.25rem*var(--tw-space-x-reverse));margin-left:calc(1.25rem*(1 - var(--tw-space-x-reverse)))}.bg-\[\#eae3d2\]{--tw-bg-opacity:1;background-color:rgb(234 227 210/var(--tw-bg-opacity))}.p-3{padding:.75rem}.p-6{padding:1.5rem}.p-9{padding:2.25rem}.text-center{text-align:center}.font-mono{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace}.font-serif{font-family:ui-serif,Georgia,Cambria,Times New Roman,Times,serif}.text-base{font-size:1rem;line-height:1.5rem}.text-sm{font-size:.875rem;line-height:1.25rem}.text-xl{font-size:1.25rem;line-height:1.75rem}.text-xs{font-size:.75rem;line-height:1rem}.tracking-wide{letter-spacing:.025em}.tracking-wider{letter-spacing:.05em}.tracking-widest{letter-spacing:.1em}.text-\[\#333\]{--tw-text-opacity:1;color:rgb(51 51 51/var(--tw-text-opacity))}.text-\[\#601B69\]{--tw-text-opacity:1;color:rgb(96 27 105/var(--tw-text-opacity))}.underline{text-decoration-line:underline}.decoration-\[\#601B69\]{text-decoration-color:#601b69}.decoration-\[\#eae3d2\]{text-decoration-color:#eae3d2}.shadow{--tw-shadow:0 1px 3px 0 #0000001a,0 1px 2px -1px #0000001a;--tw-shadow-colored:0 1px 3px 0 var(--tw-shadow-color),0 1px 2px -1px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow,0 0 #0000),var(--tw-ring-shadow,0 0 #0000),var(--tw-shadow)}.hover\:underline:hover{text-decoration-line:underline}@media (prefers-color-scheme:dark){.dark\:bg-\[\#333\]{--tw-bg-opacity:1;background-color:rgb(51 51 51/var(--tw-bg-opacity))}.dark\:text-\[\#E1B1E7\]{--tw-text-opacity:1;color:rgb(225 177 231/var(--tw-text-opacity))}.dark\:text-\[\#eae3d2\]{--tw-text-opacity:1;color:rgb(234 227 210/var(--tw-text-opacity))}.dark\:decoration-\[\#E1B1E7\],.dark\:decoration-\[\#e1b1e7\]{text-decoration-color:#e1b1e7}} \ No newline at end of file -- cgit v1.2.3