58 Commits
Author SHA1 Message Date
wymiller 1ff76d82f8 add: gemini routes 2026-11-09 18:45:34 -05:00
wymiller 5cb5dd8eb8 add: shared structs, caching services, import task change 2026-11-09 00:04:22 -05:00
wymiller b64d5ba0ad mod: add test gemini endpoint 2026-11-05 23:10:27 -05:00
wymiller 43c53c0d4a mod: add gemi crate 2026-11-05 23:09:47 -05:00
gitea-actions[bot] 2e9db16b12 chore: update flake lock 2026-11-01 07:02:25 +00:00
wymiller b61bdb170a add: update flake lock workflow, manually updated flake in the meantime 2026-10-24 11:18:18 -05:00
wymiller 82cf30447b added color accents to cards, underlines to subheaders
Build and Release Docker Images / build-and-push (./backend, task/Dockerfile, my-website-v2_task) (push) Has been cancelled
Build and Release Docker Images / build-and-push (./frontend, Dockerfile, my-website-v2_frontend) (push) Has been cancelled
Build and Release Docker Images / create-release (push) Has been cancelled
Build and Release Docker Images / build-and-push (./backend, public/Dockerfile, my-website-v2_public) (push) Has been cancelled
2025-07-31 22:45:23 -04:00
wymiller fb071df6e4 modified card colors
Build and Release Docker Images / build-and-push (./backend, public/Dockerfile, my-website-v2_public) (push) Failing after 21m39s
Build and Release Docker Images / build-and-push (./backend, task/Dockerfile, my-website-v2_task) (push) Failing after 24m46s
Build and Release Docker Images / build-and-push (./frontend, Dockerfile, my-website-v2_frontend) (push) Failing after 17m14s
Build and Release Docker Images / create-release (push) Has been skipped
2025-07-28 13:19:07 -04:00
wymiller 49374b646b fixed frontend header background color
Build and Release Docker Images / build-and-push (./backend, public/Dockerfile, my-website-v2_public) (push) Failing after 22m59s
Build and Release Docker Images / build-and-push (./backend, task/Dockerfile, my-website-v2_task) (push) Failing after 23m41s
Build and Release Docker Images / build-and-push (./frontend, Dockerfile, my-website-v2_frontend) (push) Failing after 17m11s
Build and Release Docker Images / create-release (push) Has been skipped
2025-07-27 15:49:37 -04:00
wymiller a48396f1bc adjusted the header, footer
Build and Release Docker Images / build-and-push (./backend, public/Dockerfile, my-website-v2_public) (push) Failing after 26m3s
Build and Release Docker Images / build-and-push (./backend, task/Dockerfile, my-website-v2_task) (push) Failing after 26m40s
Build and Release Docker Images / build-and-push (./frontend, Dockerfile, my-website-v2_frontend) (push) Failing after 17m6s
Build and Release Docker Images / create-release (push) Has been skipped
2025-07-27 00:35:01 -04:00
wymiller 4762a28cc2 added config file to task
Build and Release Docker Images / build-and-push (./backend, public/Dockerfile, my-website-v2_public) (push) Failing after 22m5s
Build and Release Docker Images / build-and-push (./backend, task/Dockerfile, my-website-v2_task) (push) Failing after 24m27s
Build and Release Docker Images / build-and-push (./frontend, Dockerfile, my-website-v2_frontend) (push) Failing after 17m2s
Build and Release Docker Images / create-release (push) Has been skipped
2025-07-19 22:26:52 -04:00
wymiller 4f8e7a654c modified dockerfiles some more
Build and Release Docker Images / build-and-push (./backend, public/Dockerfile, my-website-v2_public) (push) Failing after 21m10s
Build and Release Docker Images / build-and-push (./backend, task/Dockerfile, my-website-v2_task) (push) Failing after 23m37s
Build and Release Docker Images / build-and-push (./frontend, Dockerfile, my-website-v2_frontend) (push) Failing after 17m1s
Build and Release Docker Images / create-release (push) Has been skipped
2025-07-19 21:52:36 -04:00
wymiller 88e6b3f1ee modified dockerfiles
Build and Release Docker Images / build-and-push (./backend, public/Dockerfile, my-website-v2_public) (push) Failing after 20m55s
Build and Release Docker Images / build-and-push (./backend, task/Dockerfile, my-website-v2_task) (push) Failing after 23m36s
Build and Release Docker Images / build-and-push (./frontend, Dockerfile, my-website-v2_frontend) (push) Failing after 17m1s
Build and Release Docker Images / create-release (push) Has been skipped
2025-07-19 21:16:03 -04:00
wymiller d590fc505f modified docker compose 2025-07-19 17:38:13 -04:00
wymiller 9fd936cc31 thriteenth pass in ci 2025-07-19 16:52:14 -04:00
wymiller f3237c6171 throwing in the towel v2 2025-07-19 16:40:28 -04:00
wymiller ea7881416d throwing in the towel 2025-07-19 16:22:12 -04:00
wymiller c98aed3f60 eleventh pass in ci 2025-07-19 16:12:04 -04:00
wymiller 59e10f779f tenth pass in ci 2025-07-19 15:59:37 -04:00
wymiller 4dd094bee0 ninth pass in ci 2025-07-19 15:41:29 -04:00
wymiller 2464d2ab17 eighth pass at ci 2025-07-19 15:27:05 -04:00
wymiller 7e9cf9a7ec i am gonna lose it
aghhhhhhhhhh
2025-07-19 15:19:16 -04:00
wymiller 9b1e742db4 missed something 2025-07-19 15:13:53 -04:00
wymiller 57da15f1e0 sixth pass at ci 2025-07-19 15:11:43 -04:00
wymiller 1e77fbfd4d fifth pass at ci 2025-07-19 14:00:47 -04:00
wymiller b2e22d4b6b fourth pass at ci 2025-07-19 02:05:57 -04:00
wymiller 3539ceced3 third pass of ci 2025-07-19 01:51:09 -04:00
wymiller a83c13a214 second pass of ci 2025-07-19 01:47:09 -04:00
wymiller 613adcb4c4 modified dockerfiles 2025-07-19 01:28:21 -04:00
wymiller 8f0ba5289a first pass of continuous integration 2025-07-19 01:10:25 -04:00
wymiller 79cc4caa58 added dockerfiles
for building the stuff
2025-07-19 00:44:28 -04:00
wymiller 6eea6724bf cleanup 2025-07-19 00:44:08 -04:00
wymiller d73e572527 wip: added docker compose file 2025-07-17 21:23:46 -04:00
wymiller 3014c1f841 fixed share link button 2025-07-17 21:23:16 -04:00
wymiller 742a10fb9b removed hot posts
bc comments are not implemented yet
2025-07-17 21:22:12 -04:00
wymiller 7f04dabf92 fleshed out publish date 2025-07-17 21:21:33 -04:00
wymiller bc8e093651 added routes for rss, sitemap, updated footer 2025-07-17 20:09:01 -04:00
wymiller 71b3b1f42d added publish date 2025-07-17 19:35:45 -04:00
wymiller 7875c23b67 Merge branch 'master' of https://scm.wyattjmiller.com/wymiller/my-website-v2 2025-07-17 12:38:36 -04:00
wymiller 48d9360a69 flake update 2025-07-17 12:37:58 -04:00
wymiller ef0e9d3777 Merge pull request 'Add pagination' (#3) from pagination into master
Reviewed-on: #3
2025-07-16 20:48:34 -05:00
wymiller 92b14e63f2 updated deps 2025-07-16 22:02:41 -04:00
wymiller baccca1cfa Merge remote-tracking branch 'origin' into pagination 2025-07-16 21:57:52 -04:00
wymiller 109f8826ff reverted back to old postcard
onClick doesn't want to work, even with the islands, so back to the
drawing board
2025-07-15 23:20:34 -04:00
wymiller d58538599c added different favicon 2025-07-14 23:39:47 -04:00
wymiller d53f3da4c6 added cache, s3 to taskmanager, ask cache if result is the same, among others 2025-07-14 23:30:29 -04:00
wymiller 57952ec41d added documentation
it's nothing too novel, okay?
2025-07-14 20:35:49 -04:00
wymiller 42dff3f186 switched public api to caching library 2025-07-14 20:26:17 -04:00
wymiller f4937dc382 create caching library 2025-07-14 20:25:36 -04:00
wymiller 13d022d44c adjusted css 2025-07-14 20:09:10 -04:00
wymiller 29011c8f48 added new method to s3clientconfig struct, added from_env to be default 2025-07-14 18:21:13 -04:00
wymiller 6694f47d70 added pagination to a given authors page 2025-07-07 21:05:27 -04:00
wymiller a64b8fdceb impl storage library 2025-06-30 22:58:52 -04:00
wymiller a6b4f6917b added storage library 2025-06-30 22:58:25 -04:00
wymiller 585728de9d fixing tasks upload_sitemap
among other things
2025-06-30 20:38:58 -04:00
wymiller 5a6346617a remove env 2025-06-30 18:41:22 -04:00
wymiller 957858de59 added post container 2025-06-30 00:23:41 -04:00
wymiller f3c96e675b Merge pull request 'Tasks MR' (#2) from import into master
Reviewed-on: #2
2025-06-29 22:29:19 -05:00
110 changed files with 9974 additions and 1905 deletions

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
name: Update flake lock
on:
schedule:
# Gitea cron schedules use UTC. 07:00 UTC is 03:00 EDT.
- cron: "0 7 * * 0"
workflow_dispatch:
permissions:
contents: write
jobs:
update-flake-lock:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Install upstream Nix
uses: cachix/install-nix-action@v31
- name: Update flake lock
run: nix flake update
- name: Commit and push changes
shell: bash
run: |
set -euo pipefail
if git diff --quiet -- flake.lock; then
echo "flake.lock is already up to date"
exit 0
fi
git config user.name "gitea-actions[bot]"
git config user.email "gitea-actions[bot]@noreply.localhost"
git add flake.lock
git commit -m "chore: update flake lock"
git push
+104
View File
@@ -0,0 +1,104 @@
name: Build and Release Docker Images
on:
push:
branches: [master]
pull_request:
branches: [master]
workflow_dispatch:
env:
REGISTRY: scm.wyattjmiller.com
USERNAME: wymiller # Define username here to use consistently
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
strategy:
matrix:
include:
- dockerfile: public/Dockerfile
image: my-website-v2_public
context: ./backend
- dockerfile: task/Dockerfile
image: my-website-v2_task
context: ./backend
- dockerfile: Dockerfile
image: my-website-v2_frontend
context: ./frontend
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.GH_ACTION_USERNAME }}
password: ${{ secrets.GH_ACTION_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: scm.wyattjmiller.com/wymiller/${{ matrix.image }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ${{ matrix.context }}
file: ${{ matrix.context }}/${{ matrix.dockerfile }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
create-release:
if: startsWith(github.ref, 'refs/tags/')
needs: build-and-push
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Create Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GH_ACTION_TOKEN }}
with:
tag_name: ${{ github.ref_name }}
release_name: Release ${{ github.ref_name }}
body: |
## Docker Images Released
The following Docker images have been built and pushed to the container registry:
- `${{ env.REGISTRY }}/${{ env.USERNAME }}/my-website-v2_public:${{ github.ref_name }}`
- `${{ env.REGISTRY }}/${{ env.USERNAME }}/my-website-v2_task:${{ github.ref_name }}`
- `${{ env.REGISTRY }}/${{ env.USERNAME }}/my-website-v2_frontend:${{ github.ref_name }}`
### Usage
```bash
docker pull ${{ env.REGISTRY }}/${{ env.USERNAME }}/my-website-v2_public:${{ github.ref_name }}
docker pull ${{ env.REGISTRY }}/${{ env.USERNAME }}/my-website-v2_task:${{ github.ref_name }}
docker pull ${{ env.REGISTRY }}/${{ env.USERNAME }}/my-website-v2_frontend:${{ github.ref_name }}
```
draft: false
prerelease: false
+2
View File
@@ -8,3 +8,5 @@ This is just an orginizational way of keeping the backend services together (so
- [`public`](./public/README.md) - a RESTful API service - [`public`](./public/README.md) - a RESTful API service
- [`task`](./task/README.md) - a task scheduler service - [`task`](./task/README.md) - a task scheduler service
- [`storage`](./storage/README.md) - an internal storage library
- [`cache`](./cache/README.md) - an internal caching library
+2
View File
@@ -0,0 +1,2 @@
target/
.env
Generated Vendored
+1014
View File
File diff suppressed because it is too large. Load diff
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "cache"
version = "0.1.0"
edition = "2024"
[dependencies]
fred = "10.1.0"
serde = "1.0.219"
serde_json = "1.0.140"
+7
View File
@@ -0,0 +1,7 @@
# Caching library
also known as `cache`
## What is this?
An internal caching library that houses functionality needed for a key-value database like Redis or Valkey. This was turned into a library because both `public` and `task` needed functionality within.
+70
View File
@@ -0,0 +1,70 @@
pub use fred::{
clients::Pool,
interfaces::{ClientLike, KeysInterface},
prelude::*,
types::{Expiration, SetOptions},
};
pub struct Cache {
pub inmem: Pool,
}
impl Cache {
pub async fn get<T>(&mut self, key: String) -> Result<Option<T>, Box<dyn std::error::Error>>
where
T: for<'de> serde::Deserialize<'de>,
{
self.is_connected()?;
let value: Option<String> = self.inmem.get(&key).await?;
match value {
Some(json_str) => match serde_json::from_str::<T>(&json_str) {
Ok(deserialized) => Ok(Some(deserialized)),
Err(_) => Ok(None),
},
None => Ok(None),
}
}
pub async fn set<T>(
&mut self,
key: String,
contents: &T,
expiration: Option<Expiration>,
set_opts: Option<SetOptions>,
get: bool,
) -> Result<(), Box<dyn std::error::Error>>
where
T: for<'de> serde::Deserialize<'de> + serde::Serialize,
{
self.is_connected()?;
let json_string = match serde_json::to_string::<T>(contents) {
Ok(s) => s,
Err(_) => {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Unable to deserialize contents passed to cache".to_string(),
)));
}
};
Ok(self
.inmem
.set(key, json_string, expiration, set_opts, get)
.await?)
}
pub async fn del(&mut self, key: String) -> Result<(), Box<dyn std::error::Error>> {
Ok(self.inmem.del(key).await?)
}
fn is_connected(&mut self) -> Result<(), Box<dyn std::error::Error>> {
match self.inmem.is_connected() {
true => Ok(()),
false => Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Not connected to cache".to_string(),
))),
}
}
}
@@ -0,0 +1,72 @@
{
"db_name": "PostgreSQL",
"query": "SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date, a.author_id FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.author_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "post_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "first_name",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "last_name",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "title",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "body",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "body_gemtext",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 7,
"name": "publish_date",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "author_id",
"type_info": "Int4"
}
],
"parameters": {
"Left": [
"Int4",
"Int8",
"Int8"
]
},
"nullable": [
false,
false,
false,
false,
false,
true,
true,
true,
false
]
},
"hash": "1ae4bbbfd835587ea4389b3b4500d1b0f549d682f892a10586815014519bb8c4"
}
@@ -0,0 +1,42 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO comments (post_id, name, body) VALUES ($1, $2, $3) RETURNING comment_id, name, body, created_at",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "comment_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "name",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "body",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "created_at",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": [
"Int4",
"Varchar",
"Varchar"
]
},
"nullable": [
false,
false,
false,
true
]
},
"hash": "1f5f18ecc0f1fe0ea93ca61e3f167640a56fee610379de45017f2608094867f0"
}
@@ -0,0 +1,68 @@
{
"db_name": "PostgreSQL",
"query": "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.is_featured IS true GROUP BY p.post_id, a.first_name, a.last_name ORDER BY p.created_at DESC LIMIT 3",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "post_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "author_id",
"type_info": "Int4"
},
{
"ordinal": 2,
"name": "first_name",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "last_name",
"type_info": "Varchar"
},
{
"ordinal": 4,
"name": "title",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "body",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "body_gemtext",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "publish_date",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
true,
false,
false,
false,
false,
true,
true,
true
]
},
"hash": "24f4b8dd651c57a8cab3ffc2fae9bfe4cb276521854b2a0a0fba2e7081093421"
}
@@ -0,0 +1,68 @@
{
"db_name": "PostgreSQL",
"query": "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL GROUP BY p.post_id, a.first_name, a.last_name ORDER BY p.created_at DESC LIMIT 3",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "post_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "author_id",
"type_info": "Int4"
},
{
"ordinal": 2,
"name": "first_name",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "last_name",
"type_info": "Varchar"
},
{
"ordinal": 4,
"name": "title",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "body",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "body_gemtext",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "publish_date",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
true,
false,
false,
false,
false,
true,
true,
true
]
},
"hash": "4557c49e323c6b7c3246903e63e1c61c0f008e80308f65066bff7f4b70a8c7dd"
}
@@ -0,0 +1,68 @@
{
"db_name": "PostgreSQL",
"query": "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id LEFT JOIN comments c ON p.post_id = c.post_id WHERE p.deleted_at IS NULL GROUP BY p.post_id, a.first_name, a.last_name ORDER BY COUNT(c.comment_id) DESC LIMIT 3",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "post_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "author_id",
"type_info": "Int4"
},
{
"ordinal": 2,
"name": "first_name",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "last_name",
"type_info": "Varchar"
},
{
"ordinal": 4,
"name": "title",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "body",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "body_gemtext",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "publish_date",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
true,
false,
false,
false,
false,
true,
true,
true
]
},
"hash": "464d1fdbed6800f3b09e3abca989d79dfcca8a6680cb0ee324ba0790e4ec3484"
}
@@ -0,0 +1,41 @@
{
"db_name": "PostgreSQL",
"query": "SELECT comment_id, name, body, created_at FROM comments ORDER BY created_at DESC LIMIT $1 OFFSET $2",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "comment_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "name",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "body",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "created_at",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": [
"Int8",
"Int8"
]
},
"nullable": [
false,
false,
false,
true
]
},
"hash": "4e39696c45b7533e519452425b5a69d607fd8b99a526002ece8978ccb41f2c69"
}
@@ -0,0 +1,22 @@
{
"db_name": "PostgreSQL",
"query": "SELECT COUNT(*) FROM posts p WHERE p.deleted_at IS NULL AND p.author_id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "count",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Int4"
]
},
"nullable": [
null
]
},
"hash": "51fff32b503c65e62320071ff3ec44060b5fb45049b4f489c9a9d92e592ab5a7"
}
@@ -0,0 +1,68 @@
{
"db_name": "PostgreSQL",
"query": "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.view_count DESC LIMIT 3",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "post_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "author_id",
"type_info": "Int4"
},
{
"ordinal": 2,
"name": "first_name",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "last_name",
"type_info": "Varchar"
},
{
"ordinal": 4,
"name": "title",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "body",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "body_gemtext",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "publish_date",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
true,
false,
false,
false,
false,
true,
true,
true
]
},
"hash": "7998c2354eacb1cfbeece7dc001f57e0ce4d3ca15b28940a631cd249b72d6dc3"
}
@@ -0,0 +1,46 @@
{
"db_name": "PostgreSQL",
"query": "SELECT author_id, first_name, last_name, bio, image FROM authors WHERE author_id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "author_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "first_name",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "last_name",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "bio",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "image",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Int4"
]
},
"nullable": [
false,
false,
false,
true,
true
]
},
"hash": "9c0f74750e0f90916b3d2f85d0264e27523c14dff7b7adccd5b4cfbb36918901"
}
@@ -0,0 +1,68 @@
{
"db_name": "PostgreSQL",
"query": "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.created_at DESC LIMIT 10",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "post_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "author_id",
"type_info": "Int4"
},
{
"ordinal": 2,
"name": "first_name",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "last_name",
"type_info": "Varchar"
},
{
"ordinal": 4,
"name": "title",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "body",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "body_gemtext",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "publish_date",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
true,
false,
false,
false,
false,
true,
true,
true
]
},
"hash": "9cc954af48cf92abaa30a8d26499fded1ce30609c29b34d10cf98573d40bdbb6"
}
@@ -0,0 +1,40 @@
{
"db_name": "PostgreSQL",
"query": "SELECT c.comment_id, c.name, c.body, c.created_at FROM comments c LEFT JOIN posts p ON p.post_id = c.post_id WHERE p.post_id = $1 AND c.deleted_at IS NULL ORDER BY created_at DESC LIMIT 20",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "comment_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "name",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "body",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "created_at",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": [
"Int4"
]
},
"nullable": [
false,
false,
false,
true
]
},
"hash": "ae5c1527389fd823f46d3b23e5ab3b8211a6323ceff845487abae26096b3fa01"
}
@@ -0,0 +1,47 @@
{
"db_name": "PostgreSQL",
"query": "SELECT author_id, first_name, last_name, bio, image FROM authors ORDER BY created_at DESC LIMIT $1 OFFSET $2",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "author_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "first_name",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "last_name",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "bio",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "image",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Int8",
"Int8"
]
},
"nullable": [
false,
false,
false,
true,
true
]
},
"hash": "e6764f22ac7966bdb64386aedffb9edb89aefb248a1f980d2d4e2e20b1c3ca50"
}
@@ -0,0 +1,56 @@
{
"db_name": "PostgreSQL",
"query": "SELECT project_id, title, repo, summary, tech, wip, created_at FROM projects p WHERE deleted_at IS NULL ORDER BY p.created_at DESC",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "project_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "title",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "repo",
"type_info": "Text"
},
{
"ordinal": 3,
"name": "summary",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "tech",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "wip",
"type_info": "Bool"
},
{
"ordinal": 6,
"name": "created_at",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": []
},
"nullable": [
false,
false,
true,
false,
false,
true,
true
]
},
"hash": "ed764b77d39df0583dc05c3ca721176b8c38e5df5fb078a53b808080c865e64d"
}
@@ -0,0 +1,76 @@
{
"db_name": "PostgreSQL",
"query": "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date, p.is_featured FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.post_id = $1 ORDER BY p.created_at DESC",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "post_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "author_id",
"type_info": "Int4"
},
{
"ordinal": 2,
"name": "first_name",
"type_info": "Varchar"
},
{
"ordinal": 3,
"name": "last_name",
"type_info": "Varchar"
},
{
"ordinal": 4,
"name": "title",
"type_info": "Text"
},
{
"ordinal": 5,
"name": "body",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "body_gemtext",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 8,
"name": "publish_date",
"type_info": "Timestamptz"
},
{
"ordinal": 9,
"name": "is_featured",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Int4"
]
},
"nullable": [
false,
true,
false,
false,
false,
false,
true,
true,
true,
false
]
},
"hash": "fb4bcc2eb710ceb4bb50ec1c62326b7d709cd53d44944acc2da2e371642bfe1a"
}
+1015 -488
View File
File diff suppressed because it is too large. Load diff
+5 -2
View File
@@ -1,13 +1,14 @@
[package] [package]
name = "public" name = "public"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2021"
authors = ["Wyatt J. Miller <wyatt@wyattjmiller.com"] authors = ["Wyatt J. Miller <wyatt@wyattjmiller.com"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
axum = { version = "0.7.6", features = ["http2", "tokio"] } axum = { version = "0.8.4", features = ["http2", "tokio"] }
gemi = "0.1.0"
tower-http = { version = "0.6.1", features = ["trace", "cors"] } tower-http = { version = "0.6.1", features = ["trace", "cors"] }
tower_governor = "0.4.2" tower_governor = "0.4.2"
tokio = { version = "1.40.0", features = ["full"] } tokio = { version = "1.40.0", features = ["full"] }
@@ -17,6 +18,7 @@ sqlx = { version = "0.8.2", features = [
"chrono", "chrono",
"json", "json",
] } ] }
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
dotenvy = "0.15.7" dotenvy = "0.15.7"
@@ -25,3 +27,4 @@ serde_json = "1.0.128"
chrono = "0.4.38" chrono = "0.4.38"
xml = "0.8.20" xml = "0.8.20"
fred = "10.1.0" fred = "10.1.0"
cache = { version = "*", path = "../cache" }
+12
View File
@@ -0,0 +1,12 @@
FROM rust:1.88.0
WORKDIR /app
COPY ./public ./public
COPY ./cache ./cache
RUN cargo build --release --manifest-path ./public/Cargo.toml
EXPOSE 3000
CMD ["/app/public/target/release/public"]
+22
View File
@@ -18,3 +18,25 @@ For `public` to work properly, please make sure to first create the `.env` file,
- `DATABASE_URL` - needed for connecting to Postgres - `DATABASE_URL` - needed for connecting to Postgres
- `REDIS_URL` - needed for connecting to the cache (Redis or Valkey) - `REDIS_URL` - needed for connecting to the cache (Redis or Valkey)
- `BASE_URI_WEB` - needed for connecting to the frontend user interface of the system to this service - `BASE_URI_WEB` - needed for connecting to the frontend user interface of the system to this service
### Gemini
Set `SERVE_APP=gemini` to run the existing Gemini TLS server on port 1965.
It uses `dev-cert.der` and `dev-key.der` in the working directory; database and
cache configuration is shared with HTTP mode. `BASE_URI_WEB` supplies links back
to the web site for RSS, the sitemap, contact forms, and posts without Gemtext.
The capsule follows the web site's navigation and page sections:
- `/`, `/contact`, `/health`
- `/posts`, `/posts/all`, `/posts/{id}`
- `/posts/featured`, `/posts/recent`, `/posts/popular`, `/posts/hot`
- `/authors`, `/authors/{id}`, `/authors/{id}/posts`
- `/projects`
- `/comments/index`, `/comments/post/{id}`
Author and comment indexes accept `?page=1&limit=12` (maximum limit 100).
Author pages include a biography and paginated post links. Article pages render
`body_gemtext` as Gemtext, with author, publication date, and featured status;
articles without it link to the HTML version. Comments are read-only, with a
link to the web page for participation.
+17
View File
@@ -1,5 +1,22 @@
use std::path::PathBuf; use std::path::PathBuf;
pub enum ServeAppConfig {
Http,
Gemini,
}
impl std::str::FromStr for ServeAppConfig {
type Err = &'static str;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"http" => Ok(Self::Http),
"gemini" => Ok(Self::Gemini),
_ => Err("expected \"http\" or \"gemini\""),
}
}
}
pub fn config() -> Configuration { pub fn config() -> Configuration {
Configuration { Configuration {
env: dotenvy::dotenv(), env: dotenvy::dotenv(),
+30 -8
View File
@@ -1,6 +1,9 @@
use sqlx::{Pool, Postgres}; use sqlx::{Pool, Postgres};
use crate::routes::{authors::Author, comments::Pagination, posts::Post}; use crate::{
models::{authors::Author, posts::Post},
utils::pagination::Pagination,
};
pub struct AuthorsDatasource; pub struct AuthorsDatasource;
impl AuthorsDatasource { impl AuthorsDatasource {
@@ -8,11 +11,11 @@ impl AuthorsDatasource {
pool: &Pool<Postgres>, pool: &Pool<Postgres>,
pagination: Pagination, pagination: Pagination,
) -> Result<Vec<Author>, sqlx::Error> { ) -> Result<Vec<Author>, sqlx::Error> {
let offset: i64 = (pagination.page_number - 1) * pagination.page_size; let offset: i64 = (pagination.page - 1) * pagination.limit;
sqlx::query_as!( sqlx::query_as!(
Author, Author,
"SELECT author_id, first_name, last_name, bio, image FROM authors ORDER BY created_at DESC LIMIT $1 OFFSET $2", "SELECT author_id, first_name, last_name, bio, image FROM authors ORDER BY created_at DESC LIMIT $1 OFFSET $2",
pagination.page_size, pagination.limit,
offset, offset,
) )
.fetch_all(pool) .fetch_all(pool)
@@ -32,13 +35,32 @@ impl AuthorsDatasource {
pub async fn get_authors_posts( pub async fn get_authors_posts(
pool: &Pool<Postgres>, pool: &Pool<Postgres>,
author_id: i32, author_id: i32,
) -> Result<Vec<Post>, sqlx::Error> { pagination: Pagination,
sqlx::query_as!( ) -> Result<(Vec<Post>, i64), sqlx::Error> {
Post, let offset: i64 = (pagination.page - 1) * pagination.limit;
"SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.created_at, a.author_id FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.author_id = $1 ORDER BY created_at DESC", println!(
"Author ID: {}, Page: {}, Size: {}, Offset: {}",
author_id, pagination.page, pagination.limit, offset
);
let total_count = sqlx::query_scalar!(
"SELECT COUNT(*) FROM posts p WHERE p.deleted_at IS NULL AND p.author_id = $1",
author_id author_id
) )
.fetch_one(pool)
.await?
.unwrap_or(0);
let posts_query = sqlx::query_as!(
Post,
"SELECT p.post_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date, a.author_id FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.author_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3",
author_id,
pagination.limit,
offset,
)
.fetch_all(pool) .fetch_all(pool)
.await .await?;
Ok((posts_query, total_count))
} }
} }
+6 -3
View File
@@ -1,4 +1,7 @@
use crate::routes::comments::{Comment, CommentInputPayload, Pagination}; use crate::{
models::comments::{Comment, CommentInputPayload},
utils::pagination::Pagination,
};
use sqlx::{Pool, Postgres}; use sqlx::{Pool, Postgres};
pub struct CommentsDatasource; pub struct CommentsDatasource;
@@ -25,8 +28,8 @@ impl CommentsDatasource {
pool: &Pool<Postgres>, pool: &Pool<Postgres>,
pagination: Pagination, pagination: Pagination,
) -> Result<Vec<Comment>, sqlx::Error> { ) -> Result<Vec<Comment>, sqlx::Error> {
let offset: i64 = (pagination.page_number - 1) * pagination.page_size; let offset: i64 = (pagination.page - 1) * pagination.limit;
sqlx::query_as!(Comment, "SELECT comment_id, name, body, created_at FROM comments ORDER BY created_at DESC LIMIT $1 OFFSET $2", pagination.page_size, offset) sqlx::query_as!(Comment, "SELECT comment_id, name, body, created_at FROM comments ORDER BY created_at DESC LIMIT $1 OFFSET $2", pagination.limit, offset)
.fetch_all(pool) .fetch_all(pool)
.await .await
} }
+7 -7
View File
@@ -1,11 +1,11 @@
use sqlx::{Pool, Postgres}; use sqlx::{Pool, Postgres};
use crate::routes::posts::{Post, PostFeaturedVariant}; use crate::models::posts::{Post, PostFeaturedVariant};
pub struct PostsDatasource; pub struct PostsDatasource;
impl PostsDatasource { impl PostsDatasource {
pub async fn get_all(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> { pub async fn get_all(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.created_at DESC LIMIT 10") sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.created_at DESC LIMIT 10")
.fetch_all(pool) .fetch_all(pool)
.await .await
} }
@@ -19,31 +19,31 @@ impl PostsDatasource {
.execute(pool) .execute(pool)
.await; .await;
sqlx::query_as!(PostFeaturedVariant, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at, p.is_featured FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.post_id = $1 ORDER BY p.created_at DESC", post_id) sqlx::query_as!(PostFeaturedVariant, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date, p.is_featured FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.post_id = $1 ORDER BY p.created_at DESC", post_id)
.fetch_one(pool) .fetch_one(pool)
.await .await
} }
pub async fn get_recent(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> { pub async fn get_recent(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL GROUP BY p.post_id, a.first_name, a.last_name ORDER BY p.created_at DESC LIMIT 3") sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL GROUP BY p.post_id, a.first_name, a.last_name ORDER BY p.created_at DESC LIMIT 3")
.fetch_all(pool) .fetch_all(pool)
.await .await
} }
pub async fn get_popular(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> { pub async fn get_popular(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id LEFT JOIN comments c ON p.post_id = c.post_id WHERE p.deleted_at IS NULL GROUP BY p.post_id, a.first_name, a.last_name ORDER BY COUNT(c.comment_id) DESC LIMIT 3") sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id LEFT JOIN comments c ON p.post_id = c.post_id WHERE p.deleted_at IS NULL GROUP BY p.post_id, a.first_name, a.last_name ORDER BY COUNT(c.comment_id) DESC LIMIT 3")
.fetch_all(pool) .fetch_all(pool)
.await .await
} }
pub async fn get_hot(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> { pub async fn get_hot(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.view_count DESC LIMIT 3") sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL ORDER BY p.view_count DESC LIMIT 3")
.fetch_all(pool) .fetch_all(pool)
.await .await
} }
pub async fn get_featured(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> { pub async fn get_featured(pool: &Pool<Postgres>) -> Result<Vec<Post>, sqlx::Error> {
sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.created_at FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.is_featured IS true GROUP BY p.post_id, a.first_name, a.last_name ORDER BY p.created_at DESC LIMIT 3") sqlx::query_as!(Post, "SELECT p.post_id, p.author_id, a.first_name, a.last_name, p.title, p.body, p.body_gemtext, p.created_at, p.publish_date FROM posts p LEFT JOIN authors a ON a.author_id = p.author_id WHERE p.deleted_at IS NULL AND p.is_featured IS true GROUP BY p.post_id, a.first_name, a.last_name ORDER BY p.created_at DESC LIMIT 3")
.fetch_all(pool) .fetch_all(pool)
.await .await
} }
+2 -2
View File
@@ -1,6 +1,6 @@
use sqlx::{FromRow, Pool, Postgres, Row}; use sqlx::{Pool, Postgres};
use crate::routes::projects::Project; use crate::models::projects::Project;
pub struct ProjectsDatasource; pub struct ProjectsDatasource;
impl ProjectsDatasource { impl ProjectsDatasource {
+41 -30
View File
@@ -1,6 +1,7 @@
use axum::Router; use axum::Router;
use gemi::{EndpointExt, GeminiConfig, GeminiServer};
use cache::ClientLike;
use config::config; use config::config;
use fred::prelude::*;
use sqlx::postgres::PgPoolOptions; use sqlx::postgres::PgPoolOptions;
use std::fs::File; use std::fs::File;
use std::sync::Arc; use std::sync::Arc;
@@ -8,16 +9,18 @@ use std::time::Duration;
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio::signal; use tokio::signal;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer}; // use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer};
use tower_http::{ use tower_http::{
cors::{Any, CorsLayer}, cors::CorsLayer,
trace::{self, TraceLayer}, trace::{self, TraceLayer},
}; };
use tracing_subscriber::{filter, layer::SubscriberExt, prelude::*, util::SubscriberInitExt}; use tracing_subscriber::{filter, layer::SubscriberExt, prelude::*, util::SubscriberInitExt};
mod config; mod config;
mod datasources; mod datasources;
mod models;
mod routes; mod routes;
mod services;
mod state; mod state;
mod utils; mod utils;
@@ -58,11 +61,6 @@ async fn main() {
) )
.init(); .init();
let cors = CorsLayer::new()
.allow_methods(Any)
.allow_headers(Any)
.allow_origin(Any);
// if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string()) != "development" { // if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string()) != "development" {
//println!("we're not in development, starting up the rate limiter"); //println!("we're not in development, starting up the rate limiter");
//let governor_conf = Arc::new( //let governor_conf = Arc::new(
@@ -101,13 +99,13 @@ async fn main() {
.expect("Failed to connect to database"); .expect("Failed to connect to database");
let pool_size = 8; let pool_size = 8;
let config = Config::from_url(&redis_url).unwrap(); // TODO: fix the unwrap <<< let config = cache::Config::from_url(&redis_url).unwrap(); // TODO: fix the unwrap <<<
let redis_pool = Builder::from_config(config) let redis_pool = cache::Builder::from_config(config)
.with_performance_config(|config| { .with_performance_config(|config| {
config.default_command_timeout = Duration::from_secs(60); config.default_command_timeout = Duration::from_secs(60);
}) })
.set_policy(ReconnectPolicy::new_exponential(0, 100, 30_000, 2)) .set_policy(cache::ReconnectPolicy::new_exponential(0, 100, 30_000, 2))
.build_pool(pool_size) .build_pool(pool_size)
.expect("Failed to create cache pool"); .expect("Failed to create cache pool");
@@ -120,32 +118,27 @@ async fn main() {
let app_state = Arc::new(Mutex::new(state::AppInternalState::new(pool, redis_pool))); let app_state = Arc::new(Mutex::new(state::AppInternalState::new(pool, redis_pool)));
// build our application with some routes // build our application with some routes
let app_env = std::env::var("SERVE_APP")
.unwrap_or_else(|_| "http".to_string())
.parse::<config::ServeAppConfig>()
.expect("Invalid SERVE_APP value");
match app_env {
config::ServeAppConfig::Http => {
let state = app_state.clone();
let app = Router::new() let app = Router::new()
.nest("/", routes::root::RootRoute::routes()) .merge(routes::http::root::RootRoute::routes())
.nest("/posts", routes::posts::PostsRoute::routes(&app_state)) .merge(routes::http::posts::PostsRoute::routes(&state))
.nest( .merge(routes::http::comments::CommentsRoute::routes(&state))
"/comments", .merge(routes::http::authors::AuthorsRoute::routes(&state))
routes::comments::CommentsRoute::routes(&app_state), .merge(routes::http::projects::ProjectsRoute::routes(&state))
)
.nest(
"/authors",
routes::authors::AuthorsRoute::routes(&app_state),
)
.nest(
"/projects",
routes::projects::ProjectsRoute::routes(&app_state),
)
.layer(CorsLayer::permissive()) .layer(CorsLayer::permissive())
.layer( .layer(
TraceLayer::new_for_http() TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(tracing::Level::INFO)) .make_span_with(trace::DefaultMakeSpan::new().level(tracing::Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)), .on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
) )
.fallback(routes::root::RootRoute::not_found); .fallback(routes::http::root::RootRoute::not_found);
// .layer(cors);
//.layer(GovernorLayer {
// config: governor_conf,
//});
// run it with hyper // run it with hyper
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap(); let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
@@ -155,6 +148,24 @@ async fn main() {
.await .await
.unwrap(); .unwrap();
} }
config::ServeAppConfig::Gemini => {
rustls::crypto::ring::default_provider()
.install_default()
.expect("failed to install Rustls crypto provider");
let gemini_state = app_state.clone();
let router = routes::gemini::routes().expect("Failed to construct Gemini routes");
let config = GeminiConfig::new("localhost");
let app = router.with_state(gemini_state);
let server = GeminiServer::new(app, "dev-cert.der", "dev-key.der", config).unwrap();
server
.serve_with_shutdown(shutdown_signal())
.await
.unwrap();
}
};
}
async fn shutdown_signal() { async fn shutdown_signal() {
let ctrl_c = async { let ctrl_c = async {
+15
View File
@@ -0,0 +1,15 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Clone)]
pub struct Author {
pub author_id: i32,
pub first_name: String,
pub last_name: String,
pub bio: Option<String>,
pub image: Option<String>,
}
#[derive(Deserialize, Serialize, Clone)]
pub struct AuthorGetOneParams {
pub id: i32,
}
+26
View File
@@ -0,0 +1,26 @@
use serde::{Serialize, Deserialize};
use chrono::Utc;
use crate::utils::datetime::*;
#[derive(Deserialize, Serialize)]
pub struct CommentPathParams {
pub id: i32,
}
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct Comment {
pub comment_id: i32,
pub name: String,
pub body: String,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub created_at: Option<chrono::DateTime<Utc>>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct CommentInputPayload {
pub name: String,
pub body: String,
pub post_id: i32,
}
+4
View File
@@ -0,0 +1,4 @@
pub mod authors;
pub mod posts;
pub mod projects;
pub mod comments;
+44
View File
@@ -0,0 +1,44 @@
use serde::{Deserialize, Serialize};
use chrono::Utc;
use crate::utils::datetime::*;
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct Post {
pub post_id: i32,
pub author_id: Option<i32>,
pub first_name: Option<String>,
pub last_name: Option<String>,
pub title: String,
pub body: String,
pub body_gemtext: Option<String>,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub created_at: Option<chrono::DateTime<Utc>>,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub publish_date: Option<chrono::DateTime<Utc>>,
}
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct PostFeaturedVariant {
pub post_id: i32,
pub author_id: Option<i32>,
pub first_name: Option<String>,
pub last_name: Option<String>,
pub title: String,
pub body: String,
pub body_gemtext: Option<String>,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub created_at: Option<chrono::DateTime<Utc>>,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub publish_date: Option<chrono::DateTime<Utc>>,
pub is_featured: Option<bool>,
}
#[derive(Deserialize)]
pub struct PostGetOneParams {
pub id: i32,
}
+18
View File
@@ -0,0 +1,18 @@
use serde::{Serialize, Deserialize};
use crate::utils::datetime::*;
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct Project {
pub project_id: i32,
pub title: String,
pub repo: Option<String>,
pub summary: String,
pub tech: String,
pub wip: Option<bool>,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
}
-138
View File
@@ -1,138 +0,0 @@
use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::get,
Json,
};
use fred::types::Expiration;
use serde::{Deserialize, Serialize};
use crate::{datasources::authors::AuthorsDatasource, state::AppState};
use super::comments::Pagination;
#[derive(Deserialize, Serialize, Clone)]
pub struct Author {
pub author_id: i32,
pub first_name: String,
pub last_name: String,
pub bio: Option<String>,
pub image: Option<String>,
}
#[derive(Deserialize, Serialize, Clone)]
pub struct AuthorGetOneParams {
pub id: i32,
}
pub struct AuthorsRoute;
impl AuthorsRoute {
pub fn routes(app_state: &AppState) -> axum::Router {
axum::Router::new()
.route("/", get(AuthorsRoute::get_all))
.route("/:id", get(AuthorsRoute::get_one))
.route("/:id/posts", get(AuthorsRoute::get_authors_posts))
.with_state(app_state.clone())
}
async fn get_all(
State(app_state): State<AppState>,
Json(pagination): Json<Pagination>,
) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached: Option<Vec<Author>> = state
.cache
.get(String::from("authors:all"))
.await
.unwrap_or(None);
if let Some(authors) = cached {
tracing::info!("grabbing all authors from cache");
return Ok(Json(authors));
}
match AuthorsDatasource::get_all(&state.database, pagination).await {
Ok(authors) => {
tracing::info!("grabbing all authors from the database");
if let a = &authors {
let author_cloned = a.clone();
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
.cache
.set(
String::from("authors:all"),
&author_cloned,
Some(Expiration::EX(5)),
None,
false,
)
.await;
});
}
Ok(Json(authors))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
async fn get_one(
State(app_state): State<AppState>,
Path(params): Path<AuthorGetOneParams>,
) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached: Option<Author> = state
.cache
.get(format!("authors:{}", params.id))
.await
.unwrap_or(None);
if let Some(author) = cached {
tracing::info!("grabbing one author from cache");
return Ok(Json(author));
}
match AuthorsDatasource::get_one(&state.database, params.id).await {
Ok(author) => {
tracing::info!("grabbing all authors from the database");
if let a = &author {
let author_cloned = a.clone();
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
.cache
.set(
format!("authors:{}", author_cloned.author_id),
&author_cloned,
Some(Expiration::EX(5)),
None,
false,
)
.await;
});
}
Ok(Json(author))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
async fn get_authors_posts(
State(app_state): State<AppState>,
Path(params): Path<AuthorGetOneParams>,
) -> impl IntoResponse {
let state = app_state.lock().await;
match AuthorsDatasource::get_authors_posts(&state.database, params.id).await {
Ok(p) => Ok(Json(p)),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
}
@@ -0,0 +1,93 @@
use super::format::{self, line, page};
use crate::{datasources::authors::AuthorsDatasource, state::AppState};
use gemi::{GeminiRequest, GeminiResponse, GeminiRouter, Params, State};
pub struct AuthorsGeminiRoutes;
impl AuthorsGeminiRoutes {
pub fn routes() -> gemi::Result<GeminiRouter<AppState>> {
let mut router = GeminiRouter::default();
router.add_route("/", Self::all)?;
router.add_route("/{id}", Self::one)?;
router.add_route("/{id}/posts", Self::one)?;
Ok(router)
}
async fn all(State(state): State<AppState>, request: GeminiRequest) -> GeminiResponse {
let p = match format::pagination(&request) {
Ok(p) => p,
Err(e) => return e,
};
let pool = state.lock().await.database.clone();
let result = AuthorsDatasource::get_all(
&pool,
crate::utils::pagination::Pagination {
page: p.page,
limit: p.limit,
},
)
.await;
match result {
Ok(authors) => {
let mut body = String::new();
for author in &authors {
body += &format!(
"=> /authors/{} {} {}\n",
author.author_id,
line(&author.first_name),
line(&author.last_name)
);
}
if authors.is_empty() {
body += "No authors found.\n";
}
body += &format::navigation("/authors", &p, authors.len() as i64 == p.limit);
page("Authors", &body)
}
Err(e) => format::error(e),
}
}
async fn one(
State(state): State<AppState>,
params: Params,
request: GeminiRequest,
) -> GeminiResponse {
let id = match format::id(&params) {
Ok(id) => id,
Err(e) => return e,
};
let p = match format::pagination(&request) {
Ok(p) => p,
Err(e) => return e,
};
let pool = state.lock().await.database.clone();
let author = match AuthorsDatasource::get_one(&pool, id).await {
Ok(a) => a,
Err(e) => return format::error(e),
};
match AuthorsDatasource::get_authors_posts(
&pool,
id,
crate::utils::pagination::Pagination {
page: p.page,
limit: p.limit,
},
)
.await
{
Ok((posts, total)) => {
let body = format!(
"{}\n\n## Posts\n{}{}",
line(author.bio.as_deref().unwrap_or("")),
format::posts(&posts),
format::navigation(&format!("/authors/{id}"), &p, p.page * p.limit < total)
);
page(
&format!("{} {}", author.first_name, author.last_name),
&body,
)
}
Err(e) => format::error(e),
}
}
}
@@ -0,0 +1,80 @@
use super::format::{self, line, page};
use crate::{
datasources::comments::CommentsDatasource, models::comments::Comment, state::AppState,
};
use gemi::{GeminiRequest, GeminiResponse, GeminiRouter, Params, State};
pub struct CommentsGeminiRoutes;
impl CommentsGeminiRoutes {
pub fn routes() -> gemi::Result<GeminiRouter<AppState>> {
let mut router = GeminiRouter::default();
router.add_route("/post/{id}", Self::post)?;
router.add_route("/index", Self::index)?;
Ok(router)
}
async fn post(State(state): State<AppState>, params: Params) -> GeminiResponse {
let id = match format::id(&params) {
Ok(id) => id,
Err(e) => return e,
};
let pool = state.lock().await.database.clone();
match CommentsDatasource::get_posts_comments(&pool, id).await {
Ok(comments) => page(
"Comments",
&format!(
"{}\n=> /posts/{id} Back to post\n=> {} Leave a comment on the web",
render(&comments),
format::web_url(&format!("/posts/{id}"))
),
),
Err(e) => format::error(e),
}
}
async fn index(State(state): State<AppState>, request: GeminiRequest) -> GeminiResponse {
let p = match format::pagination(&request) {
Ok(p) => p,
Err(e) => return e,
};
let pool = state.lock().await.database.clone();
let result = CommentsDatasource::get_index_comments(
&pool,
crate::utils::pagination::Pagination {
page: p.page,
limit: p.limit,
},
)
.await;
match result {
Ok(comments) => page(
"Recent comments",
&format!(
"{}{}",
render(&comments),
format::navigation("/comments/index", &p, comments.len() as i64 == p.limit)
),
),
Err(e) => format::error(e),
}
}
}
fn render(comments: &[Comment]) -> String {
if comments.is_empty() {
return "No comments yet.\n".into();
}
comments
.iter()
.map(|c| {
format!(
"## {}\n{}\n{}\n\n",
line(&c.name),
c.created_at
.map(|d| d.format("%B %d, %Y").to_string())
.unwrap_or_default(),
line(&c.body)
)
})
.collect()
}
+156
View File
@@ -0,0 +1,156 @@
use crate::{models::posts::Post, utils::pagination::Pagination};
use gemi::{GeminiRequest, GeminiResponse, Params};
pub fn line(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
pub fn web_url(path: &str) -> String {
format!(
"{}{}",
std::env::var("BASE_URI_WEB")
.unwrap_or_else(|_| "https://wyattjmiller.com".into())
.trim_end_matches('/'),
path
)
}
pub fn page(title: &str, body: &str) -> GeminiResponse {
format!("# {}\n\n=> / Home\n=> /posts Blog\n=> /projects Projects\n=> /contact Contact\n\n{}\n\n## Elsewhere\n=> /authors Authors\n=> {} RSS\n=> {} Sitemap\n=> mailto:wjmiller2016@gmail.com Email me\n\n© {} Miller Web Solutions. All Rights Reserved. Hosted by Akamai.\n",
line(title), body, web_url("/rss"), web_url("/sitemap"), chrono::Utc::now().format("%Y")).into()
}
pub fn posts(posts: &[Post]) -> String {
if posts.is_empty() {
return "No posts yet.\n".into();
}
posts
.iter()
.map(|p| {
let date = p
.publish_date
.or(p.created_at)
.map(|d| d.format("%B %d, %Y").to_string())
.unwrap_or_default();
format!(
"=> /posts/{} {}\n{} · {} {}\n\n",
p.post_id,
line(&p.title),
date,
line(p.first_name.as_deref().unwrap_or("")),
line(p.last_name.as_deref().unwrap_or(""))
)
})
.collect()
}
pub fn id(params: &Params) -> Result<i32, GeminiResponse> {
params
.0
.get("id")
.and_then(|s| s.parse::<i32>().ok())
.filter(|id| *id > 0)
.ok_or_else(|| GeminiResponse::bad_request("Expected a positive numeric ID"))
}
pub fn pagination(request: &GeminiRequest) -> Result<Pagination, GeminiResponse> {
let mut result = Pagination { page: 1, limit: 12 };
for (key, value) in request.url.query_pairs() {
let target = match key.as_ref() {
"page" => &mut result.page,
"limit" => &mut result.limit,
_ => continue,
};
*target = value
.parse::<i64>()
.ok()
.filter(|n| *n > 0)
.ok_or_else(|| {
GeminiResponse::bad_request("page and limit must be positive integers")
})?;
}
if result.limit > 100 || result.page.checked_mul(result.limit).is_none() {
return Err(GeminiResponse::bad_request(
"Pagination out of range (maximum limit: 100)",
));
}
Ok(result)
}
pub fn navigation(path: &str, p: &Pagination, has_next: bool) -> String {
let mut result = format!("\nPage {}\n", p.page);
if p.page > 1 {
result += &format!(
"=> {path}?page={}&limit={} Previous page\n",
p.page - 1,
p.limit
);
}
if has_next {
result += &format!(
"=> {path}?page={}&limit={} Next page\n",
p.page + 1,
p.limit
);
}
result
}
pub fn error(error: sqlx::Error) -> GeminiResponse {
match error {
sqlx::Error::RowNotFound => GeminiResponse::not_found("Not found"),
error => {
tracing::error!(%error, "Gemini database request failed");
GeminiResponse::temporary_failure("Unable to load this page. Please try again later.")
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn request(query: &str) -> GeminiRequest {
let url = format!("gemini://localhost/authors?{query}")
.parse()
.unwrap();
GeminiRequest {
url,
path: "/authors".into(),
query: Some(query.into()),
}
}
#[test]
fn pagination_rejects_invalid_and_overflowing_values() {
for query in [
"page=0",
"page=-1",
"limit=101",
"limit=no",
"page=9223372036854775807&limit=100",
] {
assert!(pagination(&request(query)).is_err(), "{query}");
}
let p = pagination(&request("page=2&limit=10")).ok().unwrap();
assert_eq!((p.page, p.limit), (2, 10));
let links = navigation("/authors/1", &p, true);
assert!(links.contains("/authors/1?page=1&limit=10"));
assert!(links.contains("/authors/1?page=3&limit=10"));
}
#[test]
fn metadata_cannot_inject_gemtext_lines() {
assert_eq!(line("Title\r\n=> /evil link"), "Title => /evil link");
let response = page("Title\n# Injected", "Article body");
let body = String::from_utf8(response.body).unwrap();
assert!(body.starts_with("# Title # Injected\n"));
assert!(body.contains("=> /posts Blog\n"));
}
#[test]
fn missing_rows_and_database_failures_have_distinct_statuses() {
assert_eq!(error(sqlx::Error::RowNotFound).status_code.to_u8(), 51);
assert_eq!(error(sqlx::Error::PoolClosed).status_code.to_u8(), 40);
}
}
+27
View File
@@ -0,0 +1,27 @@
pub mod authors;
pub mod comments;
mod format;
pub mod posts;
pub mod projects;
pub mod root;
use crate::state::AppState;
use gemi::GeminiRouter;
pub fn routes() -> gemi::Result<GeminiRouter<AppState>> {
let mut router = GeminiRouter::default();
router.nest("/", root::RootGeminiRoutes::routes()?)?;
router.nest("/posts", posts::PostsGeminiRoutes::routes()?)?;
router.nest("/authors", authors::AuthorsGeminiRoutes::routes()?)?;
router.nest("/projects", projects::ProjectsGeminiRoutes::routes()?)?;
router.nest("/comments", comments::CommentsGeminiRoutes::routes()?)?;
Ok(router)
}
#[cfg(test)]
mod tests {
#[test]
fn routers_compose_without_conflicts() {
assert!(super::routes().is_ok());
}
}
+110
View File
@@ -0,0 +1,110 @@
use super::format::{self, page, posts};
use crate::{datasources::posts::PostsDatasource, state::AppState};
use gemi::{GeminiResponse, GeminiRouter, Params, State};
pub struct PostsGeminiRoutes;
impl PostsGeminiRoutes {
pub fn routes() -> gemi::Result<GeminiRouter<AppState>> {
let mut router = GeminiRouter::default();
router.add_route("/", Self::index)?;
router.add_route("/{id}", Self::get_one)?;
router.add_route("/all", Self::all)?;
router.add_route("/recent", Self::recent)?;
router.add_route("/featured", Self::featured)?;
router.add_route("/popular", Self::popular)?;
router.add_route("/hot", Self::hot)?;
Ok(router)
}
async fn index(State(state): State<AppState>) -> GeminiResponse {
let pool = state.lock().await.database.clone();
let result = tokio::try_join!(
PostsDatasource::get_featured(&pool),
PostsDatasource::get_recent(&pool),
PostsDatasource::get_popular(&pool)
);
match result {
Ok((featured, recent, popular)) => page("Blog", &format!("## Featured Posts\nIgnite the impossible\n\n{}## Recent Posts\nNow with 100% fresh perspective\n\n{}## Popular Posts\nContent may cause uncontrollable reading\n\n{}=> /posts/all All posts\n=> /posts/hot Hot posts", posts(&featured), posts(&recent), posts(&popular))),
Err(e) => format::error(e),
}
}
async fn get_one(State(state): State<AppState>, params: Params) -> GeminiResponse {
let id = match format::id(&params) {
Ok(id) => id,
Err(e) => return e,
};
let pool = state.lock().await.database.clone();
match PostsDatasource::get_one(&pool, id).await {
Ok(post) => {
let name = format::line(&format!(
"{} {}",
post.first_name.as_deref().unwrap_or(""),
post.last_name.as_deref().unwrap_or("")
));
let mut body = String::new();
if let Some(author_id) = post.author_id {
body += &format!("=> /authors/{author_id} By {name}\n");
}
if let Some(date) = post.publish_date.or(post.created_at) {
body += &format!("Published {}\n", date.format("%B %d, %Y"));
}
if post.is_featured == Some(true) {
body += "Featured post\n";
}
body += "\n";
match post
.body_gemtext
.as_deref()
.filter(|s| !s.trim().is_empty())
{
Some(gemtext) => body += gemtext,
None => {
body += &format!(
"This post is available on the web.\n=> {} Read this post",
format::web_url(&format!("/posts/{id}"))
)
}
}
body += &format!("\n\n=> /comments/post/{id} Comments\n=> /posts Back to the blog");
page(&post.title, &body)
}
Err(e) => format::error(e),
}
}
async fn all(State(state): State<AppState>) -> GeminiResponse {
let pool = state.lock().await.database.clone();
match PostsDatasource::get_all(&pool).await {
Ok(items) => page("All posts", &posts(&items)),
Err(e) => format::error(e),
}
}
async fn recent(State(state): State<AppState>) -> GeminiResponse {
let pool = state.lock().await.database.clone();
match PostsDatasource::get_recent(&pool).await {
Ok(items) => page("Recent Posts", &posts(&items)),
Err(e) => format::error(e),
}
}
async fn featured(State(state): State<AppState>) -> GeminiResponse {
let pool = state.lock().await.database.clone();
match PostsDatasource::get_featured(&pool).await {
Ok(items) => page("Featured Posts", &posts(&items)),
Err(e) => format::error(e),
}
}
async fn popular(State(state): State<AppState>) -> GeminiResponse {
let pool = state.lock().await.database.clone();
match PostsDatasource::get_popular(&pool).await {
Ok(items) => page("Popular Posts", &posts(&items)),
Err(e) => format::error(e),
}
}
async fn hot(State(state): State<AppState>) -> GeminiResponse {
let pool = state.lock().await.database.clone();
match PostsDatasource::get_hot(&pool).await {
Ok(items) => page("Hot Posts", &posts(&items)),
Err(e) => format::error(e),
}
}
}
@@ -0,0 +1,46 @@
use super::format::{self, line, page};
use crate::{datasources::projects::ProjectsDatasource, state::AppState};
use gemi::{GeminiResponse, GeminiRouter, State};
pub struct ProjectsGeminiRoutes;
impl ProjectsGeminiRoutes {
pub fn routes() -> gemi::Result<GeminiRouter<AppState>> {
let mut router = GeminiRouter::default();
router.add_route("/", Self::all)?;
Ok(router)
}
async fn all(State(state): State<AppState>) -> GeminiResponse {
let pool = state.lock().await.database.clone();
match ProjectsDatasource::get_all(&pool).await {
Ok(projects) => {
let mut body = String::from("Here's a collection of software and electronics projects I've been tinkering with during my free time - some are ongoing adventures, others are finished experiments!\n\n");
if projects.is_empty() {
body += "No projects yet.\n";
}
for project in projects {
body += &format!(
"## {}\n{}\nTechnology: {}\nStatus: {}\n",
line(&project.title),
line(&project.summary),
line(&project.tech),
if project.wip.unwrap_or(true) {
"Work in progress"
} else {
"Complete"
}
);
if let Some(repo) = project
.repo
.filter(|r| !r.is_empty() && !r.chars().any(char::is_whitespace))
{
body += &format!("=> {repo} Repository\n");
}
body += "\n";
}
page("Projects", &body)
}
Err(e) => format::error(e),
}
}
}
+26
View File
@@ -0,0 +1,26 @@
use super::format::{page, web_url};
use crate::state::AppState;
use gemi::{GeminiResponse, GeminiRouter};
pub struct RootGeminiRoutes;
impl RootGeminiRoutes {
pub fn routes() -> gemi::Result<GeminiRouter<AppState>> {
let mut router = GeminiRouter::default();
router.add_route("/", Self::root)?;
router.add_route("/contact", Self::contact)?;
router.add_route("/health", Self::health)?;
Ok(router)
}
async fn root() -> GeminiResponse {
page("Heya! I'm Wyatt", "## Thanks for checking out this corner of the Internet!\n\nI design and develop software for developers, for end users, and everyone in between")
}
async fn contact() -> GeminiResponse {
page("Contact", &format!("Got a question? Here to yell at me? Send me something!\n\n=> mailto:wjmiller2016@gmail.com Email me\n=> {} Contact form on the web", web_url("/contact")))
}
async fn health() -> GeminiResponse {
GeminiResponse::text("OK")
}
}
+116
View File
@@ -0,0 +1,116 @@
use axum::{
extract::{Path, Query, State},
http::StatusCode,
response::IntoResponse,
routing::get,
Json,
};
use cache::Expiration;
use serde::{Deserialize, Serialize};
use crate::{
datasources::authors::AuthorsDatasource,
models::{
posts::Post,
authors::{
Author,
AuthorGetOneParams,
}
},
services::authors::*,
state::AppState,
utils::pagination::{Pagination, PaginationQuery},
};
#[derive(Deserialize, Serialize)]
pub struct AuthorPostsResponse {
posts: Vec<Post>,
total_posts: i64,
}
pub struct AuthorsRoute;
impl AuthorsRoute {
pub fn routes(app_state: &AppState) -> axum::Router {
axum::Router::new()
.route("/authors", get(AuthorsRoute::get_all))
.route("/authors/{id}", get(AuthorsRoute::get_one))
.route("/authors/{id}/posts", get(AuthorsRoute::get_authors_posts))
.with_state(app_state.clone())
}
async fn get_all(
State(app_state): State<AppState>,
Query(query): Query<PaginationQuery>,
) -> impl IntoResponse {
let pagination = Pagination {
page: query.page.unwrap_or(1),
limit: query.limit.unwrap_or(12),
};
let mut state = app_state.lock().await;
let cached = authors_cache::<Vec<Author>>(&mut state, String::from("all")).await;
if let Some(authors) = cached {
return Ok(Json(authors));
}
match AuthorsDatasource::get_all(&state.database, pagination).await {
Ok(authors) => {
set_authors_cache(
&app_state,
String::from("all"),
authors.clone(),
Expiration::EX(5),
);
Ok(Json(authors))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
async fn get_one(
State(app_state): State<AppState>,
Path(params): Path<AuthorGetOneParams>,
) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached = authors_cache::<Author>(&mut state, params.id.to_string()).await;
if let Some(author) = cached {
return Ok(Json(author));
}
match AuthorsDatasource::get_one(&state.database, params.id).await {
Ok(author) => {
set_authors_cache(
&app_state,
author.author_id.to_string(),
author.clone(),
Expiration::EX(5),
);
Ok(Json(author))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
async fn get_authors_posts(
State(app_state): State<AppState>,
Path(params): Path<AuthorGetOneParams>,
Query(pagination): Query<PaginationQuery>,
) -> impl IntoResponse {
let pagination = Pagination {
page: pagination.page.unwrap_or(1),
limit: pagination.limit.unwrap_or(12),
};
let state = app_state.lock().await;
match AuthorsDatasource::get_authors_posts(&state.database, params.id, pagination).await {
Ok((posts, total_posts)) => Ok(Json(AuthorPostsResponse { posts, total_posts })),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
}
@@ -1,51 +1,28 @@
use crate::{datasources::comments::CommentsDatasource, state::AppState, utils::datetime::*}; use crate::{
datasources::comments::CommentsDatasource,
models::comments::*,
state::AppState,
utils::{
pagination::{Pagination, PaginationQuery},
},
};
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, Query, State},
http::StatusCode, http::StatusCode,
response::IntoResponse, response::IntoResponse,
routing::{get, post}, routing::{get, post},
Json, Json,
}; };
use chrono::Utc; use cache::{Expiration, SetOptions};
use fred::types::{Expiration, SetOptions};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug)]
pub struct CommentInputPayload {
pub name: String,
pub body: String,
pub post_id: i32,
}
#[derive(Deserialize, Serialize)]
pub struct CommentPathParams {
id: i32,
}
#[derive(Deserialize, Serialize)]
pub struct Pagination {
pub page_number: i64,
pub page_size: i64,
}
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct Comment {
pub comment_id: i32,
pub name: String,
pub body: String,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub created_at: Option<chrono::DateTime<Utc>>,
}
pub struct CommentsRoute; pub struct CommentsRoute;
impl CommentsRoute { impl CommentsRoute {
pub fn routes(app_state: &AppState) -> axum::Router { pub fn routes(app_state: &AppState) -> axum::Router {
// add more comment routes here! // add more comment routes here!
axum::Router::new() axum::Router::new()
.route("/post/:id", get(CommentsRoute::get_post_comments)) .route("/comments/post/{id}", get(CommentsRoute::get_post_comments))
.route("/add", post(CommentsRoute::insert_comment)) .route("/comments/add", post(CommentsRoute::insert_comment))
.route("/index", get(CommentsRoute::get_comments_index)) .route("/comments/index", get(CommentsRoute::get_comments_index))
.with_state(app_state.clone()) .with_state(app_state.clone())
} }
@@ -74,7 +51,6 @@ impl CommentsRoute {
let state = app_state.clone(); let state = app_state.clone();
tokio::spawn(async move { tokio::spawn(async move {
tracing::info!("update cache if key already exists!");
let mut s = state.lock().await; let mut s = state.lock().await;
let _ = s let _ = s
.cache .cache
@@ -96,8 +72,13 @@ impl CommentsRoute {
async fn get_comments_index( async fn get_comments_index(
State(app_state): State<AppState>, State(app_state): State<AppState>,
Json(pagination): Json<Pagination>, Query(query): Query<PaginationQuery>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let pagination = Pagination {
page: query.page.unwrap_or(1),
limit: query.limit.unwrap_or(12),
};
let state = app_state.lock().await; let state = app_state.lock().await;
match CommentsDatasource::get_index_comments(&state.database, pagination).await { match CommentsDatasource::get_index_comments(&state.database, pagination).await {
@@ -106,3 +87,5 @@ impl CommentsRoute {
} }
} }
} }
+5
View File
@@ -0,0 +1,5 @@
pub mod authors;
pub mod comments;
pub mod posts;
pub mod projects;
pub mod root;
+253
View File
@@ -0,0 +1,253 @@
use crate::{
datasources::posts::PostsDatasource,
models::posts::*,
services::posts::*,
state::AppState,
utils::{
rss,
sitemap::{self, SitemapEntry},
},
};
use axum::http::{HeaderMap, HeaderValue};
use axum::{
extract::{Path, State},
http::{header, StatusCode},
response::IntoResponse,
routing::get,
Json, Router,
};
use cache::Expiration;
use std::collections::HashMap;
pub struct PostsRoute;
impl PostsRoute {
pub fn routes(app_state: &AppState) -> Router {
// add more post routes here!
Router::new()
.route("/posts/all", get(PostsRoute::get_all))
.route("/posts/{id}", get(PostsRoute::get_one))
.route("/posts/recent", get(PostsRoute::get_recent_posts))
.route("/posts/popular", get(PostsRoute::get_popular_posts))
.route("/posts/hot", get(PostsRoute::get_hot_posts))
.route("/posts/featured", get(PostsRoute::get_featured_posts))
.route("/posts/rss", get(PostsRoute::get_rss_posts))
.route("/posts/sitemap", get(PostsRoute::get_sitemap))
.with_state(app_state.clone())
}
// get all posts
async fn get_all(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached = posts_cache::<Vec<Post>>(&mut state, String::from("all")).await;
if let Some(posts) = cached {
return Ok(Json(posts));
};
match PostsDatasource::get_all(&state.database).await {
Ok(posts) => {
set_posts_cache(
&app_state,
String::from("all"),
posts.clone(),
Expiration::EX(10),
);
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get one post
async fn get_one(
State(app_state): State<AppState>,
Path(params): Path<PostGetOneParams>,
) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached = posts_cache::<PostFeaturedVariant>(&mut state, params.id.to_string()).await;
if let Some(post) = cached {
return Ok(Json(post));
};
match PostsDatasource::get_one(&state.database, params.id).await {
Ok(post) => {
set_posts_cache(
&app_state,
params.id.to_string(),
post.clone(),
Expiration::EX(10),
);
Ok(Json(post))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get recent posts
async fn get_recent_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached = posts_cache::<Vec<Post>>(&mut state, String::from("recent")).await;
if let Some(posts) = cached {
return Ok(Json(posts));
};
match PostsDatasource::get_recent(&state.database).await {
Ok(posts) => {
set_posts_cache(
&app_state,
String::from("recent"),
posts.clone(),
Expiration::EX(5),
);
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get the top three posts with the highest view count
async fn get_popular_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached = posts_cache::<Vec<Post>>(&mut state, String::from("popular")).await;
if let Some(posts) = cached {
return Ok(Json(posts));
};
match PostsDatasource::get_popular(&state.database).await {
Ok(posts) => {
set_posts_cache(
&app_state,
String::from("popular"),
posts.clone(),
Expiration::EX(5),
);
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get the top three posts with the most comments
async fn get_hot_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached = posts_cache::<Vec<Post>>(&mut state, String::from("hot")).await;
if let Some(posts) = cached {
return Ok(Json(posts));
};
match PostsDatasource::get_hot(&state.database).await {
Ok(posts) => {
set_posts_cache(
&app_state,
String::from("hot"),
posts.clone(),
Expiration::EX(5),
);
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get posts that are featured
async fn get_featured_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached = posts_cache::<Vec<Post>>(&mut state, String::from("featured")).await;
if let Some(posts) = cached {
return Ok(Json(posts));
};
match PostsDatasource::get_featured(&state.database).await {
Ok(posts) => {
set_posts_cache(
&app_state,
String::from("featured"),
posts.clone(),
Expiration::EX(5),
);
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get rss posts
async fn get_rss_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let state = app_state.lock().await;
match PostsDatasource::get_all(&state.database).await {
Ok(posts) => {
let web_url =
std::env::var("BASE_URI_WEB").expect("Environment BASE_URI_WEB variable found");
let mapped_posts: HashMap<String, Post> = posts
.into_iter()
.map(|post| (post.post_id.to_string(), post))
.collect();
let xml: String = rss::generate_rss(
"Wyatt's blog",
"Wyatt and friends",
web_url.as_str(),
&mapped_posts,
);
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/xml"),
);
(headers, xml)
}
Err(e) => {
let mut headers = HeaderMap::new();
headers.insert("Content-Type", HeaderValue::from_static("text/plain"));
(headers, e.to_string())
}
}
}
async fn get_sitemap(State(app_state): State<AppState>) -> impl IntoResponse {
let state = app_state.lock().await;
// let cached: Option<Vec<Post>> = None; // TODO: maybe implement cache, later??
match PostsDatasource::get_all(&state.database).await {
Ok(posts) => {
let web_url =
std::env::var("BASE_URI_WEB").expect("Environment BASE_URI_WEB variable found");
let mut entries: HashMap<String, SitemapEntry> = posts
.into_iter()
.map(|p| {
(
p.post_id.to_string(),
SitemapEntry {
location: format!("{}/posts/{}", web_url, p.post_id.to_string()),
lastmod: p.created_at.unwrap_or_else(|| chrono::Utc::now()),
},
)
})
.collect();
sitemap::get_static_pages(&mut entries, &web_url);
let xml: String = sitemap::generate_sitemap(&entries);
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/xml"),
);
(headers, xml)
}
Err(e) => {
let mut headers = HeaderMap::new();
headers.insert("Content-Type", HeaderValue::from_static("text/plain"));
(headers, e.to_string())
}
}
}
}
@@ -0,0 +1,52 @@
use crate::{
datasources::projects::ProjectsDatasource,
state::AppState,
services::projects::*,
models::projects::*
};
use axum::{
extract::State,
http::StatusCode,
response::IntoResponse,
routing::get,
Json,
Router
};
use cache::Expiration;
pub struct ProjectsRoute;
impl ProjectsRoute {
pub fn routes(app_state: &AppState) -> Router {
Router::new()
.route("/projects", get(ProjectsRoute::get_all))
.with_state(app_state.clone())
}
async fn get_all(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached = projects_cache::<Vec<Project>>(&mut state, String::from("all")).await;
if let Some(projects) = cached {
return Ok(Json(projects));
};
match ProjectsDatasource::get_all(&state.database).await {
Ok(projects) => {
if let p = &projects {
let projects = p.clone();
set_projects_cache(
&app_state,
String::from("all"),
projects.clone(),
Expiration::EX(10),
);
};
Ok(Json(projects))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
}
@@ -1,15 +1,10 @@
use axum::{ use axum::{
extract::State,
http::StatusCode, http::StatusCode,
response::{Html, IntoResponse}, response::{Html, IntoResponse},
routing::get, routing::get,
Router, Router,
}; };
use crate::{datasources::posts::PostsDatasource, state::AppState};
use super::posts::Post;
pub struct RootRoute; pub struct RootRoute;
impl RootRoute { impl RootRoute {
pub fn routes() -> Router { pub fn routes() -> Router {
+2 -5
View File
@@ -1,5 +1,2 @@
pub mod authors; pub mod http;
pub mod comments; pub mod gemini;
pub mod posts;
pub mod projects;
pub mod root;
-401
View File
@@ -1,401 +0,0 @@
use crate::{
datasources::posts::PostsDatasource,
state::AppState,
utils::{
datetime::*,
rss,
sitemap::{self, SitemapEntry},
},
};
use axum::http::{HeaderMap, HeaderValue};
use axum::{
extract::{Path, State},
http::{header, StatusCode},
response::IntoResponse,
routing::get,
Json, Router,
};
use chrono::Utc;
use fred::types::Expiration;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct Post {
pub post_id: i32,
pub author_id: Option<i32>,
pub first_name: Option<String>,
pub last_name: Option<String>,
pub title: String,
pub body: String,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub created_at: Option<chrono::DateTime<Utc>>,
}
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct PostFeaturedVariant {
pub post_id: i32,
pub author_id: Option<i32>,
pub first_name: Option<String>,
pub last_name: Option<String>,
pub title: String,
pub body: String,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub created_at: Option<chrono::DateTime<Utc>>,
pub is_featured: Option<bool>,
}
#[derive(Deserialize)]
pub struct PostGetOneParams {
pub id: i32,
}
pub struct PostsRoute;
impl PostsRoute {
pub fn routes(app_state: &AppState) -> Router {
// add more post routes here!
Router::new()
.route("/all", get(PostsRoute::get_all))
.route("/:id", get(PostsRoute::get_one))
.route("/recent", get(PostsRoute::get_recent_posts))
.route("/popular", get(PostsRoute::get_popular_posts))
.route("/hot", get(PostsRoute::get_hot_posts))
.route("/featured", get(PostsRoute::get_featured_posts))
.route("/rss", get(PostsRoute::get_rss_posts))
.route("/sitemap", get(PostsRoute::get_sitemap))
.with_state(app_state.clone())
}
// get all posts
async fn get_all(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached: Option<Vec<Post>> = state
.cache
.get(String::from("posts:all"))
.await
.unwrap_or(None);
if let Some(posts) = cached {
tracing::info!("grabbing all posts from cache");
return Ok(Json(posts));
};
match PostsDatasource::get_all(&state.database).await {
Ok(posts) => {
tracing::info!("grabbing all posts from database");
if let p = &posts {
let posts = p.clone();
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
.cache
.set(
String::from("posts:all"),
&posts,
Some(Expiration::EX(10)),
None,
false,
)
.await;
});
};
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get one post
async fn get_one(
State(app_state): State<AppState>,
Path(params): Path<PostGetOneParams>,
) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached: Option<PostFeaturedVariant> = state
.cache
.get(format!("posts:{}", params.id))
.await
.unwrap_or(None);
if let Some(post) = cached {
tracing::info!("grabbing one post from cache");
return Ok(Json(post));
};
match PostsDatasource::get_one(&state.database, params.id).await {
Ok(post) => {
tracing::info!("grabbing one post from database");
if let p = &post {
let post = p.clone();
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
.cache
.set(
format!("posts:{}", params.id),
&post,
Some(Expiration::EX(10)),
None,
false,
)
.await;
});
};
Ok(Json(post))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get recent posts
async fn get_recent_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached: Option<Vec<Post>> = state
.cache
.get(String::from("posts:recent"))
.await
.unwrap_or(None);
if let Some(posts) = cached {
tracing::info!("grabbing recent posts from cache");
return Ok(Json(posts));
};
match PostsDatasource::get_recent(&state.database).await {
Ok(posts) => {
tracing::info!("grabbing recent posts from database");
if let p = &posts {
let posts = p.clone();
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
.cache
.set(
String::from("posts:recent"),
&posts,
Some(Expiration::EX(5)),
None,
false,
)
.await;
});
};
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get the top three posts with the highest view count
async fn get_popular_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached: Option<Vec<Post>> = state
.cache
.get(String::from("posts:popular"))
.await
.unwrap_or(None);
if let Some(posts) = cached {
tracing::info!("grabbing popular posts from cache");
return Ok(Json(posts));
};
match PostsDatasource::get_popular(&state.database).await {
Ok(posts) => {
tracing::info!("grabbing popular posts from database");
if let p = &posts {
let posts = p.clone();
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
.cache
.set(
String::from("posts:popular"),
&posts,
Some(Expiration::EX(5)),
None,
false,
)
.await;
});
};
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get the top three posts with the most comments
async fn get_hot_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached: Option<Vec<Post>> = state
.cache
.get(String::from("posts:hot"))
.await
.unwrap_or(None);
if let Some(posts) = cached {
tracing::info!("grabbing hot posts from cache");
return Ok(Json(posts));
};
match PostsDatasource::get_hot(&state.database).await {
Ok(posts) => {
tracing::info!("grabbing hot posts from database");
if let p = &posts {
let posts = p.clone();
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
.cache
.set(
String::from("posts:hot"),
&posts,
Some(Expiration::EX(5)),
None,
false,
)
.await;
});
};
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get posts that are featured
async fn get_featured_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached: Option<Vec<Post>> = state
.cache
.get(String::from("posts:featured"))
.await
.unwrap_or(None);
if let Some(posts) = cached {
tracing::info!("grabbing featured posts from cache");
return Ok(Json(posts));
};
match PostsDatasource::get_featured(&state.database).await {
Ok(posts) => {
tracing::info!("grabbing featured posts from database");
if let p = &posts {
let posts = p.clone();
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
.cache
.set(
String::from("posts:featured"),
&posts,
Some(Expiration::EX(5)),
None,
false,
)
.await;
});
};
Ok(Json(posts))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
// get rss posts
async fn get_rss_posts(State(app_state): State<AppState>) -> impl IntoResponse {
let state = app_state.lock().await;
match PostsDatasource::get_all(&state.database).await {
Ok(posts) => {
let web_url =
std::env::var("BASE_URI_WEB").expect("Environment BASE_URI_WEB variable found");
let mapped_posts: HashMap<String, Post> = posts
.into_iter()
.map(|post| (post.post_id.to_string(), post))
.collect();
let xml: String = rss::generate_rss(
"Wyatt's blog",
"Wyatt and friends",
web_url.as_str(),
&mapped_posts,
);
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/xml"),
);
(headers, xml)
}
Err(e) => {
let mut headers = HeaderMap::new();
headers.insert("Content-Type", HeaderValue::from_static("text/plain"));
(headers, e.to_string())
}
}
}
async fn get_sitemap(State(app_state): State<AppState>) -> impl IntoResponse {
let state = app_state.lock().await;
// let cached: Option<Vec<Post>> = None; // TODO: maybe implement cache, later??
match PostsDatasource::get_all(&state.database).await {
Ok(posts) => {
let web_url =
std::env::var("BASE_URI_WEB").expect("Environment BASE_URI_WEB variable found");
let mut entries: HashMap<String, SitemapEntry> = posts
.into_iter()
.map(|p| {
(
p.post_id.to_string(),
SitemapEntry {
location: format!("{}/posts/{}", web_url, p.post_id.to_string()),
lastmod: p.created_at.unwrap_or_else(|| chrono::Utc::now()),
},
)
})
.collect();
sitemap::get_static_pages(&mut entries, &web_url);
let xml: String = sitemap::generate_sitemap(&entries);
let mut headers = HeaderMap::new();
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/xml"),
);
(headers, xml)
}
Err(e) => {
let mut headers = HeaderMap::new();
headers.insert("Content-Type", HeaderValue::from_static("text/plain"));
(headers, e.to_string())
}
}
}
}
-69
View File
@@ -1,69 +0,0 @@
use crate::{datasources::projects::ProjectsDatasource, state::AppState, utils::datetime::*};
use axum::http::{HeaderMap, HeaderValue};
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
use fred::types::Expiration;
use serde::{Deserialize, Serialize};
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct Project {
pub project_id: i32,
pub title: String,
pub repo: Option<String>,
pub summary: String,
pub tech: String,
pub wip: Option<bool>,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
}
pub struct ProjectsRoute;
impl ProjectsRoute {
pub fn routes(app_state: &AppState) -> Router {
Router::new()
.route("/", get(ProjectsRoute::get_all))
.with_state(app_state.clone())
}
async fn get_all(State(app_state): State<AppState>) -> impl IntoResponse {
let mut state = app_state.lock().await;
let cached: Option<Vec<Project>> = state
.cache
.get(String::from("projects:all"))
.await
.unwrap_or(None);
if let Some(projects) = cached {
tracing::info!("grabbing all projects from cache");
return Ok(Json(projects));
};
match ProjectsDatasource::get_all(&state.database).await {
Ok(projects) => {
tracing::info!("grabbing all projects from database");
if let p = &projects {
let projects = p.clone();
let state = app_state.clone();
tracing::info!("storing database data in cache");
tokio::spawn(async move {
let mut s = state.lock().await;
let _ = s
.cache
.set(
String::from("projects:all"),
&projects,
Some(Expiration::EX(10)),
None,
false,
)
.await;
});
};
Ok(Json(projects))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
}
+35
View File
@@ -0,0 +1,35 @@
use cache::Expiration;
use crate::state::{AppInternalState, AppState};
use serde::de::DeserializeOwned;
use serde::Serialize;
pub async fn authors_cache<T: DeserializeOwned>(
app_state: &mut AppInternalState,
key: String,
) -> Option<T> {
app_state
.cache
.get(format!("authors:{key}"))
.await
.unwrap_or(None)
}
pub fn set_authors_cache<T>(app_state: &AppState, key: String, value: T, expiration: Expiration)
where
T: Serialize + DeserializeOwned + Send + Sync + 'static,
{
let app_state = app_state.clone();
tokio::spawn(async move {
let mut s = app_state.lock().await;
let _ = s
.cache
.set(
format!("authors:{key}"),
&value,
Some(expiration),
None,
false,
)
.await;
});
}
+3
View File
@@ -0,0 +1,3 @@
pub mod authors;
pub mod posts;
pub mod projects;
+34
View File
@@ -0,0 +1,34 @@
use crate::state::{AppInternalState, AppState};
use cache::Expiration;
use serde::{de::DeserializeOwned, Serialize};
pub async fn posts_cache<T: DeserializeOwned>(
app_state: &mut AppInternalState,
key: String,
) -> Option<T> {
app_state
.cache
.get(format!("posts:{key}"))
.await
.unwrap_or(None)
}
pub fn set_posts_cache<T>(app_state: &AppState, key: String, value: T, expiration: Expiration)
where
T: Serialize + DeserializeOwned + Send + Sync + 'static,
{
let app_state = app_state.clone();
tokio::spawn(async move {
let mut s = app_state.lock().await;
let _ = s
.cache
.set(
format!("posts:{key}"),
&value,
Some(expiration),
None,
false,
)
.await;
});
}
+35
View File
@@ -0,0 +1,35 @@
use cache::Expiration;
use crate::state::{AppInternalState, AppState};
use serde::de::DeserializeOwned;
use serde::Serialize;
pub async fn projects_cache<T: DeserializeOwned>(
app_state: &mut AppInternalState,
key: String,
) -> Option<T> {
app_state
.cache
.get(format!("projects:{key}"))
.await
.unwrap_or(None)
}
pub fn set_projects_cache<T>(app_state: &AppState, key: String, value: T, expiration: Expiration)
where
T: Serialize + DeserializeOwned + Send + Sync + 'static,
{
let app_state = app_state.clone();
tokio::spawn(async move {
let mut s = app_state.lock().await;
let _ = s
.cache
.set(
format!("projects:{key}"),
&value,
Some(expiration),
None,
false,
)
.await;
});
}
+3 -69
View File
@@ -1,83 +1,17 @@
use fred::interfaces::KeysInterface;
use fred::{clients::Pool, prelude::*};
use sqlx::PgPool; use sqlx::PgPool;
pub type AppState = std::sync::Arc<tokio::sync::Mutex<AppInternalState>>; pub type AppState = std::sync::Arc<tokio::sync::Mutex<AppInternalState>>;
pub struct AppInternalState { pub struct AppInternalState {
pub database: sqlx::postgres::PgPool, pub database: sqlx::postgres::PgPool,
pub cache: Cache, pub cache: cache::Cache,
}
pub struct Cache {
pub inmem: Pool,
} }
impl AppInternalState { impl AppInternalState {
pub fn new(database: PgPool, cache: Pool) -> Self { pub fn new(database: PgPool, cache: cache::Pool) -> Self {
AppInternalState { AppInternalState {
database, database,
cache: Cache { inmem: cache }, cache: cache::Cache { inmem: cache },
}
}
}
impl Cache {
pub async fn get<T>(&mut self, key: String) -> Result<Option<T>, Box<dyn std::error::Error>>
where
T: for<'de> serde::Deserialize<'de>,
{
self.is_connected()?;
let value: Option<String> = self.inmem.get(&key).await?;
match value {
Some(json_str) => match serde_json::from_str::<T>(&json_str) {
Ok(deserialized) => Ok(Some(deserialized)),
Err(_) => Ok(None),
},
None => Ok(None),
}
}
pub async fn set<T>(
&mut self,
key: String,
contents: &T,
expiration: Option<Expiration>,
set_opts: Option<SetOptions>,
get: bool,
) -> Result<(), Box<dyn std::error::Error>>
where
T: for<'de> serde::Deserialize<'de> + serde::Serialize,
{
self.is_connected()?;
let json_string = match serde_json::to_string::<T>(contents) {
Ok(s) => s,
Err(_) => {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Unable to deserialize contents passed to cache".to_string(),
)))
}
};
Ok(self
.inmem
.set(key, json_string, expiration, set_opts, get)
.await?)
}
pub async fn del(&mut self, key: String) -> Result<(), Box<dyn std::error::Error>> {
Ok(self.inmem.del(key).await?)
}
fn is_connected(&mut self) -> Result<(), Box<dyn std::error::Error>> {
match self.inmem.is_connected() {
true => Ok(()),
false => Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Not connected to cache".to_string(),
))),
} }
} }
} }
+1
View File
@@ -1,3 +1,4 @@
pub mod datetime; pub mod datetime;
pub mod pagination;
pub mod rss; pub mod rss;
pub mod sitemap; pub mod sitemap;
+13
View File
@@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub struct PaginationQuery {
pub page: Option<i64>,
pub limit: Option<i64>,
}
#[derive(Deserialize, Serialize)]
pub struct Pagination {
pub page: i64,
pub limit: i64,
}
+3 -2
View File
@@ -1,4 +1,4 @@
use crate::routes::posts; use crate::models::posts;
use std::collections::HashMap; use std::collections::HashMap;
use xml::escape::escape_str_pcdata; use xml::escape::escape_str_pcdata;
@@ -83,7 +83,8 @@ pub fn generate_rss(
<atom:link href="https://wyattjmiller.com/posts.xml" rel="self" type="application/rss+xml" /> <atom:link href="https://wyattjmiller.com/posts.xml" rel="self" type="application/rss+xml" />
{} {}
</channel> </channel>
</rss>"#, </rss>
"#,
rss_entries rss_entries
) )
} }
+3 -4
View File
@@ -23,7 +23,6 @@ impl SitemapEntry {
pub fn generate_sitemap(entries: &HashMap<String, SitemapEntry>) -> String { pub fn generate_sitemap(entries: &HashMap<String, SitemapEntry>) -> String {
let urls = entries let urls = entries
.values() .values()
.into_iter()
.map(|entry| entry.to_item()) .map(|entry| entry.to_item())
.collect::<String>(); .collect::<String>();
format!( format!(
@@ -39,21 +38,21 @@ pub fn generate_sitemap(entries: &HashMap<String, SitemapEntry>) -> String {
pub fn get_static_pages(entries: &mut HashMap<String, SitemapEntry>, web_url: &String) { pub fn get_static_pages(entries: &mut HashMap<String, SitemapEntry>, web_url: &String) {
entries.insert( entries.insert(
(entries.len() + 1).to_string(), "10000".to_string(),
SitemapEntry { SitemapEntry {
location: web_url.clone(), location: web_url.clone(),
lastmod: chrono::Utc::now(), lastmod: chrono::Utc::now(),
}, },
); );
entries.insert( entries.insert(
(entries.len() + 1).to_string(), "10001".to_string(),
SitemapEntry { SitemapEntry {
location: format!("{}/posts", web_url), location: format!("{}/posts", web_url),
lastmod: chrono::Utc::now(), lastmod: chrono::Utc::now(),
}, },
); );
entries.insert( entries.insert(
(entries.len() + 1).to_string(), "10002".to_string(),
SitemapEntry { SitemapEntry {
location: format!("{}/projects", web_url), location: format!("{}/projects", web_url),
lastmod: chrono::Utc::now(), lastmod: chrono::Utc::now(),
+2
View File
@@ -0,0 +1,2 @@
target/
.env
+3589
View File
File diff suppressed because it is too large. Load diff
+15
View File
@@ -0,0 +1,15 @@
[package]
name = "storage"
description = "Internal object storage library"
version = "0.1.0"
edition = "2024"
[dependencies]
aws-sdk-s3 = "1.94.0"
aws-config = "1.8"
azure_core = "0.25.0"
azure_storage = "0.21.0"
azure_storage_blobs = "0.21.0"
async-trait = "0.1"
tokio = { version = "1.0", features = ["full"] }
thiserror = "2.0.12"
+7
View File
@@ -0,0 +1,7 @@
# Storage library
also known as `storage`
## What is this?
An internal storage library. This was needed because both `public` and `task` needed storage functionality. Additionally, this helps maintainability and avoids duplicate code.
+11
View File
@@ -0,0 +1,11 @@
use azure_core::error::HttpError;
#[derive(Debug, thiserror::Error)]
pub enum AdapterError {
#[error("Azure error: {0}")]
Azure(#[from] azure_core::Error),
#[error("HTTP error: {0}")]
Http(#[from] HttpError),
#[error("Serialization error: {0}")]
Serialization(String),
}
+2
View File
@@ -0,0 +1,2 @@
pub mod error;
pub mod services;
+126
View File
@@ -0,0 +1,126 @@
use crate::{error::AdapterError, services::ObjectStorageClient};
use async_trait::async_trait;
use aws_config::{BehaviorVersion, Region};
use aws_sdk_s3::{Client, Config, config::Credentials};
use std::env;
#[derive(Clone, Debug)]
pub struct S3ClientConfig {
pub access_key: String,
secret_key: String,
endpoint: String,
pub bucket: String,
region: String,
}
#[derive(Clone)]
pub struct S3Client {
client: Client,
pub client_config: S3ClientConfig,
}
impl S3ClientConfig {
pub fn new(
access_key: &str,
secret_key: &str,
endpoint: &str,
bucket: &str,
region: &str,
) -> Result<Self, Box<dyn std::error::Error>> {
Ok(S3ClientConfig {
access_key: access_key.to_owned(),
secret_key: secret_key.to_owned(),
endpoint: endpoint.to_owned(),
bucket: bucket.to_owned(),
region: region.to_owned(),
})
}
pub fn from_env() -> Result<Self, Box<dyn std::error::Error>> {
Ok(S3ClientConfig {
access_key: env::var("S3_ACCESS_KEY")
.map_err(|_| "S3_ACCESS_KEY environment variable not set")?,
secret_key: env::var("S3_SECRET_KEY")
.map_err(|_| "S3_SECRET_KEY environment variable not set")?,
endpoint: env::var("S3_ENDPOINT")
.unwrap_or_else(|_| "us-ord-1.linodeobjects.com".to_string()),
bucket: env::var("S3_BUCKET").map_err(|_| "S3_BUCKET environment variable not set")?,
region: env::var("S3_REGION").unwrap_or_else(|_| "us-ord".to_string()),
})
}
}
impl S3Client {
pub fn new(config: &S3ClientConfig) -> Self {
let credentials = Credentials::new(
&config.access_key,
&config.secret_key,
None,
None,
"linode-object-storage",
);
let s3_config = Config::builder()
.behavior_version(BehaviorVersion::latest())
.region(Region::new(config.region.clone()))
.endpoint_url(format!("https://{}", config.endpoint))
.credentials_provider(credentials)
.force_path_style(false)
.build();
Self {
client: Client::from_conf(s3_config),
client_config: config.clone(),
}
}
}
#[async_trait]
impl ObjectStorageClient for S3Client {
type Error = AdapterError;
async fn put_object(&self, bucket: &str, key: &str, data: Vec<u8>) -> Result<(), Self::Error> {
println!("Uploading to S3 (or S3 like) Object Storage...");
println!("Bucket: {}", bucket);
let _ = self
.client
.put_object()
.bucket(bucket)
.key(key)
.body(data.into())
.acl(aws_sdk_s3::types::ObjectCannedAcl::PublicRead)
.content_type("application/xml")
.send()
.await
.unwrap();
Ok(())
}
async fn get_object(&self, _bucket: &str, _key: &str) -> Result<Vec<u8>, Self::Error> {
todo!("not impl")
}
async fn delete_object(&self, _bucket: &str, _key: &str) -> Result<(), Self::Error> {
todo!("not impl")
}
async fn list_objects(
&self,
_bucket: &str,
_prefix: Option<&str>,
) -> Result<Vec<String>, Self::Error> {
todo!("not impl")
}
async fn object_exists(&self, _bucket: &str, _key: &str) -> Result<bool, Self::Error> {
todo!("not impl")
}
}
impl Default for S3ClientConfig {
fn default() -> Self {
S3ClientConfig::from_env().unwrap()
}
}
+71
View File
@@ -0,0 +1,71 @@
use crate::error::AdapterError;
use async_trait::async_trait;
use azure_storage::prelude::*;
use azure_storage_blobs::prelude::*;
use super::ObjectStorageClient;
pub struct AzureBlobClient {
client: BlobServiceClient,
}
impl AzureBlobClient {
pub fn new(account_name: &str, account_key: String) -> Self {
let storage_credentials = StorageCredentials::access_key(account_name, account_key);
let client = BlobServiceClient::new(account_name, storage_credentials);
Self { client }
}
// Helper method to get container client
fn get_container_client(&self, container_name: &str) -> ContainerClient {
self.client.container_client(container_name)
}
// Helper method to get blob client
fn get_blob_client(&self, container_name: &str, blob_name: &str) -> BlobClient {
self.get_container_client(container_name)
.blob_client(blob_name)
}
}
#[async_trait]
impl ObjectStorageClient for AzureBlobClient {
type Error = AdapterError;
async fn put_object(
&self,
bucket: &str, // container name
key: &str, // blob name
data: Vec<u8>,
) -> Result<(), Self::Error> {
let blob_client = self.get_blob_client(bucket, key);
let _request = blob_client.put_block_blob(data).await.unwrap();
Ok(())
}
async fn get_object(&self, bucket: &str, key: &str) -> Result<Vec<u8>, Self::Error> {
let blob_client = self.get_blob_client(bucket, key);
let response = blob_client.get_content().await.unwrap();
Ok(response)
}
async fn delete_object(&self, bucket: &str, key: &str) -> Result<(), Self::Error> {
let blob_client = self.get_blob_client(bucket, key);
blob_client.delete().await.unwrap();
Ok(())
}
async fn list_objects(
&self,
_bucket: &str,
_prefix: Option<&str>,
) -> Result<Vec<String>, Self::Error> {
todo!("not impl")
}
async fn object_exists(&self, _bucket: &str, _key: &str) -> Result<bool, Self::Error> {
todo!("not impl")
}
}
+23
View File
@@ -0,0 +1,23 @@
pub mod aws;
pub mod azure;
use async_trait::async_trait;
#[async_trait]
pub trait ObjectStorageClient {
type Error;
async fn put_object(&self, bucket: &str, key: &str, data: Vec<u8>) -> Result<(), Self::Error>;
async fn get_object(&self, bucket: &str, key: &str) -> Result<Vec<u8>, Self::Error>;
async fn delete_object(&self, bucket: &str, key: &str) -> Result<(), Self::Error>;
async fn list_objects(
&self,
bucket: &str,
prefix: Option<&str>,
) -> Result<Vec<String>, Self::Error>;
async fn object_exists(&self, bucket: &str, key: &str) -> Result<bool, Self::Error>;
}
-1
View File
@@ -1 +0,0 @@
DATABASE_URL=postgres://wyatt:wyattisawesome@192.168.100.253:5432/postgres
@@ -0,0 +1,46 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO logs (task_id, created_at, task_status) VALUES ($1, now(), 'pending') RETURNING task_id, log_id, created_at, task_status, finished_at",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "task_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "log_id",
"type_info": "Int4"
},
{
"ordinal": 2,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 3,
"name": "task_status",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "finished_at",
"type_info": "Timestamptz"
}
],
"parameters": {
"Left": [
"Int4"
]
},
"nullable": [
false,
false,
false,
false,
true
]
},
"hash": "364c58ab7678af9d36003af9858e69b876be3939a4d9f34a95950ab7cc166778"
}
@@ -0,0 +1,22 @@
{
"db_name": "PostgreSQL",
"query": "SELECT EXISTS(SELECT 1 FROM posts p WHERE p.filename = $1) as filename",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "filename",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
null
]
},
"hash": "723a24f681b1b7866e4a2636ddda2bb8ed78d60540158ffa0fbebba4bdbfa2b9"
}
@@ -0,0 +1,15 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE logs SET task_status = $1 WHERE task_id = $2",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Int4"
]
},
"nullable": []
},
"hash": "e3f9cdc6fede1a8601c3775e829f04eef5b00cf7bc5a087b5ba5c70f99e76763"
}
+1058 -418
View File
File diff suppressed because it is too large. Load diff
+4 -3
View File
@@ -1,11 +1,13 @@
[package] [package]
name = "task" name = "task"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
storage = { version = "0.1.0", path = "../storage" }
cache = { version = "0.1.0", path = "../cache" }
tokio = { version = "1.19.2", features = ["full"] } tokio = { version = "1.19.2", features = ["full"] }
reqwest = { version = "0.12.20", features = ["json", "rustls-tls"] } reqwest = { version = "0.12.20", features = ["json", "rustls-tls"] }
job_scheduler = "1.2.1" job_scheduler = "1.2.1"
@@ -21,7 +23,6 @@ futures = "0.3.30"
markdown = "1.0.0-alpha.20" markdown = "1.0.0-alpha.20"
serde = { version = "*", features = ["derive"] } serde = { version = "*", features = ["derive"] }
serde_yml = "*" serde_yml = "*"
aws-sdk-s3 = "1.94.0"
aws-config = "1.8"
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
md2gemtext = "0.1.0"
+14
View File
@@ -0,0 +1,14 @@
FROM rust:1.88.0
WORKDIR /app
COPY ./task ./task
COPY ./cache ./cache
COPY ./storage ./storage
RUN mkdir /app/posts
RUN cargo build --release --manifest-path ./task/Cargo.toml
EXPOSE 3000
CMD ["/app/task/target/release/task"]
+5
View File
@@ -12,4 +12,9 @@ This is a task runner/scheduler programs that will fire off various tasks. These
For `task` to work properly, please make sure to first create the `.env` file, then fill out the following environment variables: For `task` to work properly, please make sure to first create the `.env` file, then fill out the following environment variables:
- `BASE_URI_API` - needed for communicating with `public`
- `DATABASE_URL` - needed for communicating to Postgres - `DATABASE_URL` - needed for communicating to Postgres
- `REDIS_URL` - needed for communicating with the cache (Redis or Valkey)
- `S3_ACCESS_KEY` - needed for Amazon S3 (or compatible services) storage
- `S3_SECRET_KEY` - needed for Amazon S3 (or compatible services) storage
- `S3_BUCKET` - needed for Amazon S3 (or compatible services) storage
+11
View File
@@ -0,0 +1,11 @@
use std::path::PathBuf;
pub fn config() -> Configuration {
Configuration {
env: dotenvy::dotenv(),
}
}
pub struct Configuration {
env: Result<PathBuf, dotenvy::Error>,
}
+47 -6
View File
@@ -1,16 +1,20 @@
use cache::ClientLike;
use chrono::Utc; use chrono::Utc;
use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
use std::env; use std::env;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use storage::services::aws;
use tasks::*; use tasks::*;
//mod config; mod config;
mod tasks; mod tasks;
mod utils; mod utils;
pub struct TaskManager<'a> { pub struct TaskManager<'a> {
pool: Pool<Postgres>, pool: Pool<Postgres>,
cache: cache::Pool,
s3_client: aws::S3Client,
jobs: Vec<TaskJob>, jobs: Vec<TaskJob>,
last_activated: Option<chrono::DateTime<Utc>>, last_activated: Option<chrono::DateTime<Utc>>,
last_job: Option<TaskJob>, last_job: Option<TaskJob>,
@@ -29,6 +33,7 @@ pub struct TaskLog {
#[derive(Debug)] #[derive(Debug)]
pub enum TaskStatus { pub enum TaskStatus {
Pending(String), Pending(String),
Running(String),
Completed(String), Completed(String),
Failed(String), Failed(String),
} }
@@ -47,7 +52,9 @@ pub struct TaskJob {
async fn main() { async fn main() {
println!("Hello, world!"); println!("Hello, world!");
dotenvy::dotenv().unwrap(); let _ = config::config();
// setup database
let database_url = let database_url =
env::var("DATABASE_URL").expect("Environment variable DATABASE_URL is not found"); env::var("DATABASE_URL").expect("Environment variable DATABASE_URL is not found");
let pool = PgPoolOptions::new() let pool = PgPoolOptions::new()
@@ -57,7 +64,35 @@ async fn main() {
.await .await
.expect("Failed to connect to the database"); .expect("Failed to connect to the database");
let mut manager = TaskManager::new(pool); // setup redis/valkey
let redis_url = match std::env::var("REDIS_URL").unwrap().as_str() {
// TODO: fix the unwrap ^
"" => "redis://localhost:6379".to_string(),
x => x.to_string(),
};
let pool_size = 8;
let config = cache::Config::from_url(&redis_url).unwrap(); // TODO: fix the unwrap <<<
let redis_pool = cache::Builder::from_config(config)
.with_performance_config(|config| {
config.default_command_timeout = Duration::from_secs(60);
})
.set_policy(cache::ReconnectPolicy::new_exponential(0, 100, 30_000, 2))
.build_pool(pool_size)
.expect("Failed to create cache pool");
if std::env::var("REDIS_URL").unwrap() != "" {
// TODO: fix the unwrap ^
redis_pool.init().await.expect("Failed to connect to cache");
let _ = redis_pool.flushall::<i32>(false).await;
}
// setup storage
let s3_client_config = aws::S3ClientConfig::from_env().unwrap();
let s3_client = aws::S3Client::new(&s3_client_config);
let mut manager = TaskManager::new(pool, redis_pool, s3_client);
manager.register_jobs().await.unwrap(); manager.register_jobs().await.unwrap();
loop { loop {
@@ -67,9 +102,11 @@ async fn main() {
} }
impl<'a> TaskManager<'a> { impl<'a> TaskManager<'a> {
fn new(pool: Pool<Postgres>) -> Self { fn new(pool: Pool<Postgres>, cache: cache::Pool, s3_client: aws::S3Client) -> Self {
TaskManager { TaskManager {
pool, pool,
cache,
s3_client,
jobs: Vec::new(), jobs: Vec::new(),
last_activated: None, last_activated: None,
last_job: None, last_job: None,
@@ -99,11 +136,15 @@ impl<'a> TaskManager<'a> {
} }
2 => { 2 => {
let pool = Arc::new(self.pool.clone()); let pool = Arc::new(self.pool.clone());
Box::new(move || upload_rss::register(&pool)) let cache = Arc::new(self.cache.clone());
let s3_client = Arc::new(self.s3_client.clone());
Box::new(move || upload_rss::register(&pool, &cache, &s3_client))
} }
3 => { 3 => {
let pool = Arc::new(self.pool.clone()); let pool = Arc::new(self.pool.clone());
Box::new(move || upload_sitemap::register(&pool)) let cache = Arc::new(self.cache.clone());
let s3_client = Arc::new(self.s3_client.clone());
Box::new(move || upload_sitemap::register(&pool, &cache, &s3_client))
} }
id => return Err(format!("Unknown task_id: {}", id).into()), id => return Err(format!("Unknown task_id: {}", id).into()),
}; };
+12 -15
View File
@@ -1,13 +1,15 @@
use std::fs; use std::fs;
use std::io::Read;
use crate::utils::task_log; use crate::utils::task_log;
use chrono::DateTime;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) { pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) {
let p = pool.clone(); let p = pool.clone();
tokio::spawn(async move { tokio::spawn(async move {
let _ = import_posts("app/", &p).await; if let Err(error) = import_posts("/app/posts", &p).await {
tracing::error!(%error, "Post import failed");
}
}); });
} }
@@ -19,21 +21,12 @@ async fn import_posts(
// Start task logging // Start task logging
let task = task_log::start(1, pool).await?; let task = task_log::start(1, pool).await?;
// Setup markdown options
let options = MarkdownOptions {
options: markdown::Constructs::gfm(),
};
// Read directory contents
let entries = fs::read_dir(dir_path)?; let entries = fs::read_dir(dir_path)?;
// Process each file
for entry_result in entries { for entry_result in entries {
let file = entry_result?; let file = entry_result?;
let file_path = file.path(); let file_path = file.path();
// Skip non-file entries
if !file_path.is_file() { if !file_path.is_file() {
continue; continue;
} }
@@ -71,22 +64,25 @@ async fn import_posts(
let content = let content =
markdown::to_html_with_options(&document.content, &markdown::Options::default()); markdown::to_html_with_options(&document.content, &markdown::Options::default());
println!("{:?}", content); let gemtext = md2gemtext::convert(&document.content).to_string();
let title = document.metadata.title; let title = document.metadata.title;
let pub_date =
DateTime::parse_from_str(document.metadata.date.as_ref(), "%Y-%m-%d %H:%M:%S %z")?;
let content_final = content.unwrap(); let content_final = content.unwrap();
// Insert into database // Insert into database
let results = sqlx::query_as::<_, InsertPosts>( let _ = sqlx::query_as::<_, InsertPosts>(
"INSERT INTO posts (title, body, filename, author_id) VALUES ($1, $2, $3, $4) RETURNING title, body, filename, author_id" "INSERT INTO posts (title, body, body_gemtext, filename, publish_date, author_id) VALUES ($1, $2, $3, $4, $5, $6) RETURNING title, body, body_gemtext, filename, author_id"
) )
.bind(title) .bind(title)
.bind(content_final) .bind(content_final)
.bind(gemtext)
.bind(file_name_str) .bind(file_name_str)
.bind(pub_date)
.bind(1) // Consider making author_id a parameter .bind(1) // Consider making author_id a parameter
.fetch_one(pool) .fetch_one(pool)
.await?; .await?;
println!("{:?}", results);
println!("Successfully imported: {}", file_name_str); println!("Successfully imported: {}", file_name_str);
} else { } else {
@@ -115,6 +111,7 @@ struct FilenameExists {
struct InsertPosts { struct InsertPosts {
title: String, title: String,
body: String, body: String,
body_gemtext: String,
filename: String, filename: String,
author_id: i32, author_id: i32,
} }
+41 -13
View File
@@ -1,19 +1,25 @@
use sqlx::{Pool, Postgres};
use crate::utils::{ use crate::utils::{
request::{Request, Response}, request::{Request, Response},
task_log, task_log,
{upload::S3ClientConfig, *},
}; };
use cache::KeysInterface;
use storage::services::{aws::S3Client, ObjectStorageClient};
pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) { pub fn register(pool: &sqlx::Pool<sqlx::Postgres>, cache: &cache::Pool, s3_client: &S3Client) {
let p = pool.clone(); let p = pool.clone();
let c = cache.clone();
let s3 = s3_client.to_owned();
tokio::spawn(async move { tokio::spawn(async move {
let _ = upload_rss(&p).await; let _ = upload_rss(&p, &c, s3).await;
}); });
} }
async fn upload_rss(pool: &sqlx::Pool<sqlx::Postgres>) -> Result<(), Box<dyn std::error::Error>> { async fn upload_rss(
pool: &sqlx::Pool<sqlx::Postgres>,
cache: &cache::Pool,
s3_client: S3Client,
) -> Result<(), Box<dyn std::error::Error>> {
// start task logging // start task logging
task_log::start(2, pool).await?; task_log::start(2, pool).await?;
@@ -24,15 +30,37 @@ async fn upload_rss(pool: &sqlx::Pool<sqlx::Postgres>) -> Result<(), Box<dyn std
// upload the sucker to obj storage // upload the sucker to obj storage
if let Response::Xml(rss) = rss_result { if let Response::Xml(rss) = rss_result {
let client_config = S3ClientConfig::from_env().unwrap(); let cached: &Option<String> = &cache.get(String::from("rss")).await.unwrap_or(None);
let s3_client = upload::create_s3_client(&client_config).await.unwrap(); let cache_clone = cache.clone();
let _ = upload::upload( if let Some(cached_value) = cached {
&s3_client, if *cached_value == rss {
client_config.bucket.as_str(), println!("Response is the same in the cache, exiting");
return Ok(());
}
}
let r = rss.clone();
let _ = s3_client
.put_object(
s3_client.client_config.bucket.as_str(),
"feed.xml", "feed.xml",
rss.as_str(), rss.as_bytes().to_vec(),
) )
.await; .await?;
tokio::spawn(async move {
cache_clone
.set::<String, String, &String>(
String::from("rss"),
&r,
Some(cache::Expiration::EX(3600)),
None,
false,
)
.await
.unwrap();
});
println!("Finished uploading RSS feed"); println!("Finished uploading RSS feed");
} }
+39 -12
View File
@@ -1,38 +1,65 @@
use crate::utils::{ use crate::utils::{
request::{Request, Response}, request::{Request, Response},
task_log, task_log,
{upload::S3ClientConfig, *},
}; };
use cache::KeysInterface;
use storage::services::{aws::S3Client, ObjectStorageClient};
pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) { pub fn register(pool: &sqlx::Pool<sqlx::Postgres>, cache: &cache::Pool, s3_client: &S3Client) {
let p = pool.clone(); let p = pool.clone();
let c = cache.clone();
let s3 = s3_client.to_owned();
tokio::spawn(async move { tokio::spawn(async move {
let _ = upload_sitemap(&p).await; let _ = upload_sitemap(&p, &c, s3).await;
}); });
} }
async fn upload_sitemap( async fn upload_sitemap(
pool: &sqlx::Pool<sqlx::Postgres>, pool: &sqlx::Pool<sqlx::Postgres>,
cache: &cache::Pool,
s3_client: S3Client,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
// TODO:: get sitemap and upload it to bucket?? // start task logging
task_log::start(3, pool).await?; task_log::start(3, pool).await?;
// get request and request the things // get request and request the things
let request = Request::new(); let request = Request::new();
let sitemap_url = format!("{}/posts/sitemap", request.base_url); let sitemap_url = format!("{}/posts/sitemap", request.base_url);
let sitemap_result = request.request_url::<String>(&sitemap_url).await; let sitemap_result = request.request_url::<String>(&sitemap_url).await.unwrap();
// upload the sucker to obj storage // upload the sucker to obj storage
if let Response::Xml(sitemap) = sitemap_result { if let Response::Xml(sitemap) = sitemap_result {
let client_config = S3ClientConfig::from_env().unwrap(); let cached: &Option<String> = &cache.get(String::from("sitemap")).await.unwrap_or(None);
let s3_client = upload::create_s3_client(&client_config).await.unwrap(); let cache_clone = cache.clone();
let _ = upload::upload( if let Some(cached_value) = cached {
&s3_client, if *cached_value == sitemap {
client_config.bucket.as_str(), println!("Response is the same in the cache, exiting");
return Ok(());
}
}
let s = sitemap.clone();
let _ = s3_client
.put_object(
s3_client.client_config.bucket.as_str(),
"sitemap.xml", "sitemap.xml",
sitemap.as_str(), sitemap.as_bytes().to_vec(),
) )
.await; .await?;
tokio::spawn(async move {
cache_clone
.set::<String, String, &String>(
String::from("sitemap"),
&s,
Some(cache::Expiration::EX(3600)),
None,
false,
)
.await
.unwrap();
});
println!("Finished uploading sitemap!"); println!("Finished uploading sitemap!");
} }
-73
View File
@@ -1,73 +0,0 @@
use aws_config::{BehaviorVersion, Region};
use aws_sdk_s3::{config::Credentials, Client, Config};
use std::env;
#[derive(Debug)]
pub struct S3ClientConfig {
pub access_key: String,
secret_key: String,
endpoint: String,
pub bucket: String,
region: String,
}
impl S3ClientConfig {
pub fn from_env() -> Result<Self, Box<dyn std::error::Error>> {
Ok(S3ClientConfig {
access_key: env::var("LINODE_ACCESS_KEY")
.map_err(|_| "LINODE_ACCESS_KEY environment variable not set")?,
secret_key: env::var("LINODE_SECRET_KEY")
.map_err(|_| "LINODE_SECRET_KEY environment variable not set")?,
endpoint: env::var("LINODE_ENDPOINT")
.unwrap_or_else(|_| "us-ord-1.linodeobjects.com".to_string()),
bucket: env::var("LINODE_BUCKET")
.map_err(|_| "LINODE_BUCKET environment variable not set")?,
region: env::var("LINODE_REGION").unwrap_or_else(|_| "us-ord".to_string()),
})
}
}
pub async fn create_s3_client(
config: &S3ClientConfig,
) -> Result<Client, Box<dyn std::error::Error>> {
let credentials = Credentials::new(
&config.access_key,
&config.secret_key,
None,
None,
"linode-object-storage",
);
let s3_config = Config::builder()
.behavior_version(BehaviorVersion::latest())
.region(Region::new(config.region.clone()))
.endpoint_url(format!("https://{}", config.endpoint))
.credentials_provider(credentials)
.force_path_style(false)
.build();
Ok(Client::from_conf(s3_config))
}
pub async fn upload(
client: &Client,
bucket: &str,
key: &str,
content: &str,
) -> Result<(), Box<dyn std::error::Error>> {
println!("Uploading to Linode Object Storage...");
println!("Bucket: {}", bucket);
let put_object_req = client
.put_object()
.bucket(bucket)
.key(key)
.body(content.as_bytes().to_vec().into())
.acl(aws_sdk_s3::types::ObjectCannedAcl::PublicRead)
.content_type("application/rss+xml")
.send()
.await?;
println!("Upload successful! ETag: {:?}", put_object_req.e_tag());
Ok(())
}
+137
View File
@@ -0,0 +1,137 @@
version: "3.8"
services:
valkey-mywebsite:
image: valkey/valkey:8.0.2
container_name: valkey-mywebsite
ports:
- "6379:6379"
volumes:
- valkey_data:/data
restart: unless-stopped
networks:
- app_network
healthcheck:
test: ["CMD", "valkey-cli", "ping"]
interval: 30s
timeout: 10s
retries: 3
postgres-mywebsite:
image: postgres:16
container_name: postgres-mywebsite
# fill these in with postgres env vars
environment:
POSTGRES_USER: wyatt
POSTGRES_PASSWORD: test # <<< replace this
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./init-db:/docker-entrypoint-initdb.d
restart: unless-stopped
networks:
- app_network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U wyatt -d postgres"]
interval: 30s
timeout: 10s
retries: 3
frontend:
image: scm.wyattjmiller.com/wymiller/my-website-v2_frontend:master
container_name: frontend
ports:
- "8000:8000"
# fill these in the frontend env vars for prod
environment:
- BASE_URI_API=
- BASE_URI_WEB=
- EMAIL_FORM=
- RSS_URI=
- SITEMAP_URI=
- VIRTUAL_HOST=wyattjmiller.com
- VIRTUAL_PORT=80
- LETSENCRYPT_HOST=wyattjmiller.com
- LETSENCRYPT_EMAIL=wjmiller2016@gmail.com
volumes:
- ./deno-fresh-app:/app
- /app/node_modules
depends_on:
postgres:
condition: service_healthy
valkey:
condition: service_healthy
restart: unless-stopped
networks:
- app_network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30s
timeout: 10s
retries: 3
public-mywebsite:
image: scm.wyattjmiller.com/wymiller/my-website-v2_public:master
container_name: public-mywebsite
ports:
- "3000:3000"
# fill these in with public env vars for prod
environment:
- DATABASE_URL=
- REDIS_URL=
- BASE_URI_WEB=
depends_on:
postgres:
condition: service_healthy
valkey:
condition: service_healthy
restart: unless-stopped
networks:
- app_network
# make sure to change the url too
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
task-mywebsite:
image: scm.wyattjmiller.com/wymiller/my-website-v2_task:master
container_name: task-mywebsite
# fill these in with task env vars for prod
environment:
- DATABASE_URL=
- BASE_URI_API=
- S3_ACCESS_KEY=
- S3_SECRET_KEY=
- S3_BUCKET=
- REDIS_URL=
depends_on:
postgres:
condition: service_healthy
valkey:
condition: service_healthy
volumes:
- ./backend/task/app:/app/posts # <<< place all markdown files here
restart: unless-stopped
networks:
- app_network
healthcheck:
test: ["CMD", "pgrep", "-f", "task-mywebsite"]
interval: 30s
timeout: 10s
retries: 3
networks:
app_network:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
volumes:
valkey_mywebsite_data:
driver: local
postgres_mywebsite_data:
driver: local
Generated
+16 -16
View File
@@ -2,12 +2,12 @@
"nodes": { "nodes": {
"flake-schemas": { "flake-schemas": {
"locked": { "locked": {
"lastModified": 1721999734, "lastModified": 1780327564,
"narHash": "sha256-G5CxYeJVm4lcEtaO87LKzOsVnWeTcHGKbKxNamNWgOw=", "narHash": "sha256-HiRPtA0spK+Dkgbhz/1zW9glXxNVB+L4Rj2VYmdawb8=",
"rev": "0a5c42297d870156d9c57d8f99e476b738dcd982", "rev": "6cc9bd98891b1fc6bb2b8cb3277df8bc72799ca6",
"revCount": 75, "revCount": 149,
"type": "tarball", "type": "tarball",
"url": "https://api.flakehub.com/f/pinned/DeterminateSystems/flake-schemas/0.1.5/0190ef2f-61e0-794b-ba14-e82f225e55e6/source.tar.gz" "url": "https://api.flakehub.com/f/pinned/DeterminateSystems/flake-schemas/0.5.0/019e83cf-9af3-78b1-ac5b-70e68ad1efe1/source.tar.gz"
}, },
"original": { "original": {
"type": "tarball", "type": "tarball",
@@ -21,11 +21,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1729525729, "lastModified": 1748076591,
"narHash": "sha256-YiooFGeR7+sXSkHNfSzT8GQf+xtzbDwUbfbwkCCyuUs=", "narHash": "sha256-zfcYlOBYGfp4uxPC9ctaWf37bjZagbQ0pw7mqgTqfBI=",
"owner": "nekowinston", "owner": "nekowinston",
"repo": "nix-deno", "repo": "nix-deno",
"rev": "e92687492a4faec48ab1eb45adbdba30c876b0e5", "rev": "0b22de7dd34c7d7c7cd46cedee0b65592dc57d3e",
"type": "github" "type": "github"
}, },
"original": { "original": {
@@ -36,12 +36,12 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1741862977, "lastModified": 1793410479,
"narHash": "sha256-prZ0M8vE/ghRGGZcflvxCu40ObKaB+ikn74/xQoNrGQ=", "narHash": "sha256-HzgjxrLY4AEc0044ZuM5CHbLSWLZgDlay1idmzvvpJs=",
"rev": "cdd2ef009676ac92b715ff26630164bb88fec4e0", "rev": "fa1373130d5a21c040a47c5dbe1913d59ed91808",
"revCount": 715614, "revCount": 1017588,
"type": "tarball", "type": "tarball",
"url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.2411.715614%2Brev-cdd2ef009676ac92b715ff26630164bb88fec4e0/019590d8-bf83-7849-9c87-9e373480fc07/source.tar.gz" "url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.2605.1017588%2Brev-fa1373130d5a21c040a47c5dbe1913d59ed91808/01a1959c-245a-7673-9ea9-ccd89a1f8489/source.tar.gz"
}, },
"original": { "original": {
"type": "tarball", "type": "tarball",
@@ -63,11 +63,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1742005800, "lastModified": 1793434469,
"narHash": "sha256-6wuOGWkyW6R4A6Th9NMi6WK2jjddvZt7V2+rLPk6L3o=", "narHash": "sha256-+sgVNI2c67di+XIeMaz5KSei4T65kX68s5Nl0E5Gzbc=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "028cd247a6375f83b94adc33d83676480fc9c294", "rev": "6ecf534a7c16e7c969f26838fdd8c93597ae93f3",
"type": "github" "type": "github"
}, },
"original": { "original": {
+17
View File
@@ -0,0 +1,17 @@
FROM denoland/deno:alpine
RUN apk add bash
# USER deno
RUN deno cache --reload deno.json
COPY . .
RUN bash -c 'deno cache main.ts'
RUN bash -c 'deno task build'
EXPOSE 8000
CMD ["deno", "run", "-A", "main.ts"]
+14 -19
View File
@@ -2,40 +2,42 @@ import * as hi from "jsr:@preact-icons/hi2";
export default function Footer() { export default function Footer() {
return ( return (
<footer class="bg-[#313244] text-[#cdd6f4] py-8"> <footer class="bg-[#313244] text-[#cdd6f4] py-8 mt-auto">
<div class="container mx-auto px-4"> <div class="container mx-auto px-4">
{/* Grid layout that switches from 2 to 1 column on small screens */} {/* 2x2 grid on mobile, horizontal row on desktop - all centered */}
<div class="grid grid-cols-1 md:grid-cols-2 gap-8"> <div class="grid grid-cols-2 place-items-center md:flex md:flex-row items-center justify-center md:gap-8">
<div class="space-y-2">
<a <a
class="mb-8 text-[#cdd6f4] transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer visited:text-[#bac2de]" class="text-[#cdd6f4] transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer visited:text-[#bac2de]"
href={`${Deno.env.get("BASE_URI_API")}/posts/rss`} href="/rss"
> >
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<hi.HiOutlineRss /> <hi.HiOutlineRss />
RSS RSS
</div> </div>
</a> </a>
<a <a
class="mb-8 text-[#cdd6f4] transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer visited:text-[#bac2de]" class="text-[#cdd6f4] transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer visited:text-[#bac2de]"
href="/sitemap.xml" href="/sitemap"
> >
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<hi.HiOutlineMap /> <hi.HiOutlineMap />
Sitemap Sitemap
</div> </div>
</a> </a>
</div>
<div> <a
<a class="mb-8 text-[#cdd6f4] transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer visited:text-[#bac2de]"> class="text-[#cdd6f4] transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer visited:text-[#bac2de]"
href="/resume.pdf"
>
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<hi.HiOutlineBriefcase /> <hi.HiOutlineBriefcase />
Resume Resume
</div> </div>
</a> </a>
<a <a
class="mb-8 text-[#cdd6f4] transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer visited:text-[#bac2de]" class="text-[#cdd6f4] transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer visited:text-[#bac2de]"
href="mailto:wjmiller2016@gmail.com" href="mailto:wjmiller2016@gmail.com"
> >
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
@@ -43,13 +45,6 @@ export default function Footer() {
Email me Email me
</div> </div>
</a> </a>
<a
class="mb-8 text-[#cdd6f4] transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer visited:text-[#bac2de]"
href="https://github.com/wymillerlinux"
>
<div class="flex items-center gap-2">GitHub</div>
</a>
</div>
</div> </div>
<div class="border-t border-gray-700 mt-8 pt-4 text-center"> <div class="border-t border-gray-700 mt-8 pt-4 text-center">
+2 -2
View File
@@ -35,14 +35,14 @@ export default function Header() {
return ( return (
<header> <header>
<nav> <nav>
<div class="bg-[#313244] flex flex-wrap justify-center space-x-6 p-4"> <div class="grid grid-cols-2 mt-4 place-items-center md:flex md:flex-row items-center justify-center md:gap-8">
{headerLinks.map((l) => { {headerLinks.map((l) => {
const newTab = l.newTab ? "_blank" : "_self"; const newTab = l.newTab ? "_blank" : "_self";
return ( return (
<a <a
href={l.linkTo} href={l.linkTo}
target={newTab} target={newTab}
class="text-[#cdd6f4] text-lg font-medium transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer" class="text-[#cdd6f4] text-md sm:text-lg font-medium transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_20px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer"
> >
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
{l.icon} {l.name} {l.icon} {l.name}
+89
View File
@@ -0,0 +1,89 @@
import * as hi from "jsr:@preact-icons/hi2";
export function PaginationControl({
paginatedData,
currentUrl,
authorId,
}: {
paginatedData: PaginatedPosts;
currentUrl: URL;
authorId: number;
}) {
const buildUrl = (page: number, limit?: number) => {
const params = new URLSearchParams(currentUrl.searchParams);
params.set("page", page.toString());
if (limit) params.set("limit", limit.toString());
return `${currentUrl.pathname}?${params.toString()}`;
};
if (paginatedData.totalPages <= 1) return null;
return (
<div class="mt-8 space-y-4">
{/* Pagination info and controls */}
<div class="flex flex-col sm:flex-row justify-center items-center gap-4">
<div class="flex items-center gap-2">
{paginatedData.hasPrevPage && (
<a
href={buildUrl(paginatedData.currentPage - 1)}
class="px-4 py-2 bg-[#45475a] text-[#cdd6f4] shadow-sm rounded hover:bg-[#6A6B7A] transition-colors"
>
<div class="flex items-center gap-2">
<hi.HiChevronDoubleLeft />
Previous
</div>
</a>
)}
{/* Page numbers */}
<div class="flex gap-1">
{Array.from(
{ length: Math.min(paginatedData.totalPages, 7) },
(_, i) => {
let pageNum;
if (paginatedData.totalPages <= 7) {
pageNum = i + 1;
} else {
const start = Math.max(1, paginatedData.currentPage - 3);
const end = Math.min(paginatedData.totalPages, start + 6);
pageNum = start + i;
if (pageNum > end) return null;
}
const isCurrentPage = pageNum === paginatedData.currentPage;
return (
<a
key={pageNum}
href={buildUrl(pageNum)}
class={`px-3 py-1 rounded text-sm shadow-sm ${
isCurrentPage
? "bg-[#6A6B7A] text-[#cdd6f4]"
: "bg-[#45475a] text-[#cdd6f4] hover:bg-[#6A6B7A]"
}`}
>
{pageNum}
</a>
);
},
)}
</div>
{paginatedData.hasNextPage && (
<a
href={buildUrl(paginatedData.currentPage + 1)}
class="px-4 py-2 bg-[#45475a] text-[#cdd6f4] shadow-sm rounded hover:bg-[#6A6B7A] transition-colors"
>
<div class="flex items-center gap-2">
Next
<hi.HiChevronDoubleRight />
</div>
</a>
)}
</div>
{/* Quick jump to page */}
</div>
</div>
);
}
+4 -1
View File
@@ -2,10 +2,13 @@ import { Post } from "../types/index.ts";
export const PostBody = function PostBody({ post }: PostBodyOpts) { export const PostBody = function PostBody({ post }: PostBodyOpts) {
return ( return (
<div class="mx-auto max-w-4xl p-4 bg-[#313244]">
<div <div
class="p-6 bg-[#313244] shadow-md text-[#f5e0dc] post-content" class="p-6 bg-[#484659] shadow-md rounded-lg text-[#f5e0dc] post-content overflow-hidden break-words hyphens-auto max-w-full
[&>*]:max-w-5xl [&>*]:overflow-wrap-anywhere"
dangerouslySetInnerHTML={{ __html: post.body }} dangerouslySetInnerHTML={{ __html: post.body }}
></div> ></div>
</div>
); );
}; };
+14 -5
View File
@@ -2,22 +2,31 @@ import { convertUtc } from "../lib/convertUtc.ts";
import { truncateString } from "../lib/truncate.ts"; import { truncateString } from "../lib/truncate.ts";
import { Post } from "../types/index.ts"; import { Post } from "../types/index.ts";
export const PostCard = function PostCard({ post }: { post: Post }) { export const PostCard = function PostCard({
post,
colorValue,
}: {
post: Post;
colorValue: string;
}) {
return ( return (
<div class="p-6 bg-[#45475a] rounded-lg shadow-md transition-all duration-300 ease-in-out hover:shadow-xl hover:scale-105"> <div
class={`p-6 bg-[#484659] rounded-lg shadow-xl transition-all duration-300 ease-in-out border-b-4 hover:shadow-xl hover:scale-105`}
style={{ borderBottomColor: colorValue }}
>
<a href={`${Deno.env.get("BASE_URI_WEB")}/posts/${post.post_id}`}> <a href={`${Deno.env.get("BASE_URI_WEB")}/posts/${post.post_id}`}>
<h2 class="text-white text-lg font-bold mb-2">{post.title}</h2> <h2 class="text-white text-lg font-bold mb-2">{post.title}</h2>
<p class="text-white"> <p class="text-white">
Written by{" "} Written by{" "}
<a <a
class="text-white transition-all duration-300 ease-in-out hover:text-[#74c7ec] hover:drop-shadow-[0_0_10px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer" class="text-white transition-all duration-300 ease-in-out hover:text-[#cba6f7] hover:drop-shadow-[0_0_10px_rgba(96,165,250,0.7)] hover:scale-110 cursor-pointer"
href={`${Deno.env.get("BASE_URI_WEB")}/authors/${post.author_id}`} href={`${Deno.env.get("BASE_URI_WEB")}/authors/${post.author_id}`}
> >
{post.first_name} {post.last_name} {post.first_name} {post.last_name}
</a>{" "} </a>{" "}
at {convertUtc(post.created_at)} at {convertUtc(post.publish_date)}
</p> </p>
<p class="text-gray-400">{truncateString(post.body, 15)}</p> <p class="text-gray-400">{truncateString(post.body, 45)}</p>
</a> </a>
</div> </div>
); );
+6 -2
View File
@@ -3,15 +3,19 @@ import { Post } from "../types/index.ts";
interface PostOpts { interface PostOpts {
posts: Post[]; posts: Post[];
colorValue: string;
} }
export const PostCarousel = function PostCarousel({ posts }: PostOpts) { export const PostCarousel = function PostCarousel({
posts,
colorValue,
}: PostOpts) {
return ( return (
<div className="flex w-full justify-start items-start bg-[#313244] p-8"> <div className="flex w-full justify-start items-start bg-[#313244] p-8">
<div className="max-w-7xl mx-auto"> <div className="max-w-7xl mx-auto">
<div className="flex flex-wrap justify-center gap-3"> <div className="flex flex-wrap justify-center gap-3">
{posts.map((post: Post) => ( {posts.map((post: Post) => (
<PostCard key={post.post_id} post={post} /> <PostCard key={post.post_id} post={post} colorValue={colorValue} />
))} ))}
</div> </div>
</div> </div>
+3 -3
View File
@@ -8,8 +8,8 @@ export const PostHeader = function PostHeader({ post }: PostHeaderOpts) {
<Head> <Head>
<title>Wyatt J. Miller | {post.title}</title> <title>Wyatt J. Miller | {post.title}</title>
</Head> </Head>
<div class="p-6 bg-[#313244] shadow-md"> <div class="p-4 bg-[#313244]">
<div class="min-w-screen flex flex-col items-center justify-between bg-[#45475a] rounded-lg shadow-md"> <div class="min-w-screen flex flex-col items-center justify-between bg-[#484659] rounded-lg shadow-md">
<div class="sm:mt-14 sm:mb-14 mt-8 mb-8 flex flex-col items-center gap-y-5 gap-x-10 md:flex-row"> <div class="sm:mt-14 sm:mb-14 mt-8 mb-8 flex flex-col items-center gap-y-5 gap-x-10 md:flex-row">
<div class="space-y-2 text-center md:text-left"> <div class="space-y-2 text-center md:text-left">
<p class="text-2xl text-[#f5e0dc] font-bold sm:text-4xl"> <p class="text-2xl text-[#f5e0dc] font-bold sm:text-4xl">
@@ -17,7 +17,7 @@ export const PostHeader = function PostHeader({ post }: PostHeaderOpts) {
</p> </p>
<p class="text-md font-medium text-[#E39A9C] sm:text-xl italic"> <p class="text-md font-medium text-[#E39A9C] sm:text-xl italic">
by {post.first_name} {post.last_name} posted on{" "} by {post.first_name} {post.last_name} posted on{" "}
{convertUtc(post.created_at)} {convertUtc(post.publish_date)}
</p> </p>
</div> </div>
</div> </div>
+6 -7
View File
@@ -1,14 +1,13 @@
export const ShareLinkButton = function ShareLinkButton({ props }) { export const ShareLinkButton = function ShareLinkButton({ props }) {
const [text. setText] = useState("Share"); const [text, setText] = useState("Share");
const onClickHandler = () => { const onClickHandler = () => {
navigator.clipboard.writeText(location.href); navigator.clipboard.writeText(location.href);
setText("Copied to clipboard!"); setText("Copied to clipboard!");
setTimeout(() => {
setText("Share");
}, 1000);
}; };
return ( return <button onClick={onClickHandler}>{text}</button>;
<button onClick={onClickHandler}> };
{text}
</button>
)
}
+4
View File
@@ -12,6 +12,8 @@ import * as $index from "./routes/index.tsx";
import * as $posts_id_ from "./routes/posts/[id].tsx"; import * as $posts_id_ from "./routes/posts/[id].tsx";
import * as $posts_index from "./routes/posts/index.tsx"; import * as $posts_index from "./routes/posts/index.tsx";
import * as $projects_index from "./routes/projects/index.tsx"; import * as $projects_index from "./routes/projects/index.tsx";
import * as $rss_index from "./routes/rss/index.tsx";
import * as $sitemap_index from "./routes/sitemap/index.tsx";
import * as $Counter from "./islands/Counter.tsx"; import * as $Counter from "./islands/Counter.tsx";
import * as $ProjectCard from "./islands/ProjectCard.tsx"; import * as $ProjectCard from "./islands/ProjectCard.tsx";
import { type Manifest } from "$fresh/server.ts"; import { type Manifest } from "$fresh/server.ts";
@@ -28,6 +30,8 @@ const manifest = {
"./routes/posts/[id].tsx": $posts_id_, "./routes/posts/[id].tsx": $posts_id_,
"./routes/posts/index.tsx": $posts_index, "./routes/posts/index.tsx": $posts_index,
"./routes/projects/index.tsx": $projects_index, "./routes/projects/index.tsx": $projects_index,
"./routes/rss/index.tsx": $rss_index,
"./routes/sitemap/index.tsx": $sitemap_index,
}, },
islands: { islands: {
"./islands/Counter.tsx": $Counter, "./islands/Counter.tsx": $Counter,
+12 -3
View File
@@ -1,9 +1,18 @@
export const ProjectCard = function ProjectCard(props: ProjectProps) { export const ProjectCard = function ProjectCard(props: ProjectProps) {
return ( return (
<div <div
class={`group space-y-1 rounded-md ${ class={`md:m-8 group space-y-1 rounded-md ${
props.wip ? "border-2 border-dashed" : "cursor-pointer" props.wip ? "border-2" : "cursor-pointer"
} bg-[#45475a] px-3 py-2 m-10 shadow-md transition-all duration-300 ease-in-out hover:shadow-xl hover:scale-105`} } bg-[#44485b] px-3 py-2 m-4 shadow-md transition-all duration-300 ease-in-out border-b-4 border-b-[#94e2d5] hover:shadow-xl hover:scale-105`}
style={
props.wip
? {
borderTopStyle: "dashed",
borderRightStyle: "dashed",
borderLeftStyle: "dashed",
}
: {}
}
onClick={() => props.repo && open(props.repo, "_blank")} onClick={() => props.repo && open(props.repo, "_blank")}
> >
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
+1
View File
@@ -1,3 +1,4 @@
export const truncateString = (str: string, maxLength: number) => { export const truncateString = (str: string, maxLength: number) => {
str = str.replace(/<[^>]*>/g, "");
return str.length > maxLength ? `${str.slice(0, maxLength)}...` : str; return str.length > maxLength ? `${str.slice(0, maxLength)}...` : str;
}; };
Loaded 100 of 110 files, more files were not shown because too many files have changed in this diff. Show more