Compare commits

..

No commits in common. "5245c9a2dd3aeb39e6ab53205e682d4a2d242eef" and "c60a0e1de066b18d1ae0adb3b815697c213c1c03" have entirely different histories.

20 changed files with 105 additions and 489 deletions

178
CLAUDE.md
View file

@ -1,178 +0,0 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
A REST API backend for managing code snippets, written in Clojure using XTDB for data storage. The API provides endpoints for creating, reading, editing, and deleting snippets, with support for tagging and slug-based lookup.
**Related Projects:**
- [Snippets CMS](https://git.travisshears.com/travisshears/snippets_cms) - TUI CMS and MCP server companion project
## Development Commands
### Run Development Server
```sh
clojure -M -m snippets.main
```
Starts the API server on the configured host/port (default: localhost:8080). The REPL can be used to develop with the server:
```clojure
; Start server in background from REPL:
(def server (jetty/run-jetty #'app {:port 3000 :join? false}))
; Stop server:
(.stop server)
```
### Build Uberjar
```sh
clojure -T:build uber
```
Creates a standalone JAR file at `target/snippets-standalone.jar` that includes all dependencies.
### REPL Commands
Common REPL operations:
```clojure
; Pretty-print data structures
(clojure.pprint/pprint {:name "Alice" :age 30})
; Require a namespace (fresh import)
(require 'clojure.string)
(require '[clojure.string :as str])
(require 'my.namespace :reload)
; List namespace contents
(dir clojure.string)
; Switch namespaces
(in-ns 'my.namespace)
(in-ns 'user)
```
### Linting
The project uses clj-kondo for static analysis. Configuration is in `.clj-kondo/config.edn`. Linting is integrated into most IDEs via LSP.
## Code Architecture
### Directory Structure
- **src/snippets/infra/** - Infrastructure layer (HTTP API, database, configuration)
- `api.clj` - HTTP routing and handlers using reitit
- `db.clj` - XTDB queries and transactions
- `config.clj` - Configuration loading (config.edn + environment variables)
- **src/snippets/use_cases/** - Business logic layer
- `view.clj` - Query/read operations, includes serialization for JSON output
- `create.clj` - Create snippets
- `edit.clj` - Patch snippets with validation
- `delete.clj` - Delete snippets
- **test/** - Test suite
### Architecture Pattern
Clean separation between layers:
1. **Infrastructure (infra/)**: Handles external concerns
- HTTP/REST via reitit + ring
- XTDB database client and queries
- Environment configuration
2. **Use Cases**: Business logic implementing application features
- Each use case is a module with focused responsibility
- Calls infrastructure layer (infra.db, infra.config) as needed
- Performs serialization/transformation for API responses
3. **Main Entry Point (main.clj)**: Minimal bootstrap that runs the API server
### Data Model
Snippets are XTDB documents with:
- `xt/id` - UUID identifier (set by create use case)
- `title` - Snippet title
- `slug` - URL-friendly identifier for query lookups
- `markdown` - Snippet content in markdown format
- `tags` - Vector of strings for categorization
- `pub-date` - Date object (serialized to ISO-8601 strings for API responses)
### API Endpoints
All routes under `/api`:
- `GET /api/ping` - Health check
- `GET /api/snippets` - List snippets (with optional `limit` and `skip` query params)
- `GET /api/snippet?id=<id>` - Get snippet by UUID
- `GET /api/snippet-by-slug?slug=<slug>` - Get snippet by slug
- `POST /api/snippet` - Create snippet
- `PATCH /api/snippet?id=<id>` - Edit snippet (patches are validated against schema)
- `DELETE /api/snippet?id=<id>` - Delete snippet
- `GET /api/tags` - List all tags with counts
- `GET /api/tag?tag=<tag>` - Get snippets by tag
### Key Dependencies
- **reitit** (0.9.1) - HTTP routing and coercion
- **ring** (1.13.0) - HTTP server (jetty adapter)
- **XTDB** (2.0.0-beta9) - Temporal database
- **malli** (0.18.0) - Schema validation and generation
- **muuntaja** (0.6.11) - JSON/EDN encoding/decoding
- **telemere** (1.0.0) - Structured logging
- **environ** (1.2.0) - Environment variable loading
### Configuration
Configuration comes from `config.edn` with environment variable overrides:
```edn
{:jetty {:host "localhost" :port 8080}
:xtdb {:host "192.168.1.157" :port "5007" :user "xtdb" :dbname "xtdb"}}
```
Environment variables can override specific values:
- `HOST` - Jetty host
- `PORT` - Jetty port (parsed as integer)
- `XTDB_HOST`, `XTDB_PORT`, `XTDB_USER`, `XTDB_DBNAME` - XTDB connection details
See `infra/config.clj` for how overrides are applied.
## Testing
Run the test suite:
```sh
clojure -M:test:clojure.test/run
```
Or from REPL:
```clojure
(clojure.test/run-tests 'snippets-test)
```
The test namespace `snippets-test` includes:
- Basic arithmetic tests
- XTDB integration tests using test node
- HTML rendering tests using rum
## Deployment
Docker image built and pushed to AWS ECR via `build.sh`:
```sh
./build.sh
```
Requires AWS CLI configured with `personal` profile and permission to push to ECR repository.
## Notes
- XTDB queries use XTQL (temporal query language)
- Date serialization happens in `view.clj` - pub-date objects are converted to ISO-8601 strings for JSON responses
- Patch validation is strict (`{:closed true}`) - only specific fields can be updated
- String formatting in some XTQL queries uses `eval` and `read-string` for dynamic query construction
- UUIDs used for snippet IDs; generated client-side on creation

View file

@ -18,16 +18,8 @@ This project is written in [Clojure](https://clojure.org/) and data is stored in
### How to run dev server
Run the server
```
$ clojure -M -m snippets.main
```
Hot reload:
```shell
$ fd -e clj | entr -r clojure -M -m snippets.main
$ clojure -M -m snippets.infra.api
```
### Repl

View file

@ -12,9 +12,9 @@ post {
body:json {
{
"title": "Mock snippet sent via Bruno",
"title": "TEST from Bruno",
"slug": "bruno-test",
"markdown": "## MOCK SNIPPET\nCreated via bruno",
"tags": ["mock"]
"markdown": "this is a test",
"tags": ["test"]
}
}

View file

@ -5,13 +5,13 @@ meta {
}
delete {
url: {{host}}/api/snippet?slug=bruno-test
url: {{host}}/api/snippet?id=d77d3463-c76e-4c53-a1d5-ecaf16c6c54e
body: none
auth: none
}
params:query {
slug: bruno-test
id: d77d3463-c76e-4c53-a1d5-ecaf16c6c54e
}
body:json {

View file

@ -5,19 +5,18 @@ meta {
}
patch {
url: {{host}}/api/snippet?slug=bruno-test
url: {{host}}/api/snippet?id=680a3508-7709-4f71-b5c3-3dcbffe6f5cf
body: json
auth: none
}
params:query {
slug: bruno-test
id: 680a3508-7709-4f71-b5c3-3dcbffe6f5cf
}
body:json {
{
"title": "Mock snippet sent via Bruno with updated title",
"markdown": "## MOCK SNIPPET\nUpdated via bruno",
"tags": ["mock", "updated"]
"title": "quick way to push last jj commit to git",
"tags": ["jj", "git"]
}
}

View file

@ -0,0 +1,23 @@
meta {
name: get_snippet
type: http
seq: 6
}
get {
url: {{host}}/api/snippet?id=aea69336-5116-49ac-ab52-bc221bdb7830
body: none
auth: none
}
params:query {
id: aea69336-5116-49ac-ab52-bc221bdb7830
}
body:json {
{
"title": "Test Snippet",
"markdown": "## Cool Snippet\ndoes a cool thing",
"tags": ["git", "jj"]
}
}

View file

@ -5,13 +5,13 @@ meta {
}
get {
url: {{host}}/api/snippet-by-slug?slug=bruno-test
url: {{host}}/api/snippet-by-slug?slug=netcat-over-ping
body: none
auth: none
}
params:query {
slug: bruno-test
slug: netcat-over-ping
}
body:json {

View file

@ -5,13 +5,13 @@ meta {
}
get {
url: {{host}}/api/snippets?limit=2&skip=0
url: {{host}}/api/snippets?limit=25&skip=0
body: none
auth: none
}
params:query {
limit: 2
limit: 25
skip: 0
}

View file

@ -5,13 +5,13 @@ meta {
}
get {
url: {{host}}/api/tag?tag=mock
url: {{host}}/api/tag?tag=git
body: none
auth: none
}
params:query {
tag: mock
tag: git
}
body:json {

View file

@ -5,7 +5,6 @@
org.slf4j/slf4j-simple {:mvn/version "2.0.16"}
;; db
com.datomic/local {:mvn/version "1.0.291"}
com.xtdb/xtdb-api {:mvn/version "2.0.0-beta9"}
com.github.seancorfield/next.jdbc {:mvn/version "1.3.1002"}
org.postgresql/postgresql {:mvn/version "42.7.6"}

View file

@ -23,9 +23,9 @@
{:status 200, :body "snippet created"})
(defn handle-edit-snippet [{body :body-params params :query-params}]
(let [slug (get params "slug")]
(t/log! {:level :info, :data {:body body :slug slug}} "Received request to edit snippet")
(let [{success :success :as res} (snippets.use-cases.edit/edit-snippet slug body)]
(let [id (get params "id")]
(t/log! {:level :info, :data {:body body :id id}} "Received request to edit snippet")
(let [{success :success :as res} (snippets.use-cases.edit/edit-snippet id body)]
(cond
success {:status 200, :body "snippet updated"}
(= (:reason res) :invalid-patch) {:status 400, :body "invalid patch"}
@ -38,16 +38,18 @@
{:status 200
:body (snippets.use-cases.view/view-snippets {:limit limit-num :skip skip-num})})
{:status 200
:body (snippets.use-cases.view/view-snippets nil)}))
:body (snippets.use-cases.view/view-snippets)}))
(defn handle-view-snippet [{params :query-params}]
(let [id (get params "id")]
{:status 200
:body (snippets.use-cases.view/view-snippet id)}))
(defn handle-delete-snippet [{params :query-params}]
(let [slug (get params "slug")
res (snippets.use-cases.delete/delete-snippet slug)]
(if (nil? res)
{:status 404
:body "No snippet with that slug found"}
(let [id (get params "id")]
(snippets.use-cases.delete/delete-snippet id)
{:status 200
:body (format "Deleted snippet with slug: %s if it existed" slug)})))
:body (format "Deleted snippet with id: %s if it existed" id)}))
(defn handle-view-tags [_args]
(let [tags (snippets.use-cases.view/view-tags)]
@ -60,13 +62,9 @@
:body (snippets.use-cases.view/view-snippets-by-tag tag)}))
(defn handle-view-snippet-by-slug [{params :query-params}]
(let [slug (get params "slug")
snippet (snippets.use-cases.view/view-snippet-by-slug slug)]
(if (nil? snippet)
{:status 404
:body "No snippet with that slug found"}
(let [slug (get params "slug")]
{:status 200
:body snippet})))
:body (snippets.use-cases.view/view-snippet-by-slug slug)}))
(defn wrap [handler id]
(fn [request]
@ -84,6 +82,7 @@
["/snippet-by-slug" {:get handle-view-snippet-by-slug}]
["/snippets" {:get handle-view-snippets}]
["/snippet" {:post handle-create-snippet
:get handle-view-snippet
:patch handle-edit-snippet
:delete handle-delete-snippet}]])
(rr/create-default-handler)))

View file

@ -1,218 +0,0 @@
(ns snippets.infra.db2
(:require
[clojure.set :as set]
[datomic.client.api :as d]
[malli.core :as m]
[taoensso.telemere :as t]))
;; Initialize the Datomic Local client
;; :system "dev" groups your databases in the "dev" system
;; In production, you'd set :storage-dir to a persistent path
;; TODO: add save file location for prod
(def client (d/client {:server-type :datomic-local
:system "dev"}))
(def db-name "snippets")
;; Create the database if it doesn't exist
(defn- ensure-db
"Check if db exists, create it if not."
[]
(d/create-database client {:db-name db-name})
(t/log! {:level :info} "Snippets database created if needed"))
;; Get a connection to the database
(defn- get-conn []
(d/connect client {:db-name db-name}))
;; Define the schema for snippets
;; Transact this once to set up the database structure
(def snippet-schema
[{:db/ident :snippet/title
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :snippet/slug
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/value}
{:db/ident :snippet/markdown
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :snippet/tags
:db/valueType :db.type/string
:db/cardinality :db.cardinality/many}
{:db/ident :snippet/pub-date
:db/valueType :db.type/instant
:db/cardinality :db.cardinality/one}])
(defn- ensure-schema
"Transact the schema if it doesn't exist. Call this once on startup."
[]
(let [conn (get-conn)]
(d/transact conn {:tx-data snippet-schema})
(t/log! {:level :info} "Snippet schema created if needed")))
(defn start-up-check
"Should be run at startup to ensure the database and schema are created."
[]
(ensure-db)
(ensure-schema))
(defn- snippet-to-entity
"Convert a snippet map to a Datomic DB entity."
[snippet]
{:snippet/title (:title snippet)
:snippet/slug (:slug snippet)
:snippet/markdown (:markdown snippet)
:snippet/tags (:tags snippet)
:snippet/pub-date (:pub-date snippet)})
(defn- entity-to-snippet
"Convert a Datomic DB entity to a snippet map."
[entity]
{:title (:snippet/title entity)
:slug (:snippet/slug entity)
:markdown (:snippet/markdown entity)
:tags (:snippet/tags entity)
:pub-date (:snippet/pub-date entity)})
(defn- wrap-snippet-return
"Wraps an fn that returns snippet, snippet[], or nil; converting the entity to a snippet map."
[snippet-fn]
(fn [& args]
(let [res (apply snippet-fn args)]
(cond
(nil? res) nil
:else (if (sequential? res)
(map entity-to-snippet res)
(entity-to-snippet res))))))
;; create
(def create-schema
"Malli schema for a valid snippet entity creation."
[:map
[:snippet/title :string]
[:snippet/slug :string]
[:snippet/markdown :string]
[:snippet/tags [:vector :string]]
[:snippet/pub-date [:fn #(instance? java.util.Date %)]]])
(defn- valid-create?
"Check if a snippet map is a valid Datomic entity."
[entity]
(m/validate create-schema entity))
(defn- put-snippets
"Create new snippets in the database."
[snippets]
(t/log! {:level :info, :data {:slugs (map :slug snippets)}} "Saving new snippets to db")
(let [conn (get-conn)
entities (map snippet-to-entity snippets)]
(if (every? valid-create? entities)
(d/transact conn {:tx-data entities})
(throw (ex-info "Invalid snippet entity" {:entities entities})))))
(def create-snippets
(wrap-snippet-return put-snippets))
;; read
(defn- get-snippet-by-slug-from-db
"Get a single snippet by its slug."
[slug]
(let [conn (get-conn)
db (d/db conn)
query '[:find (pull ?e [*])
:in $ ?slug
:where [?e :snippet/slug ?slug]]
snippet (ffirst (d/q query db slug))]
(t/log! {:level :info, :data {:slug slug :snippet snippet}} "Got snippet by slug")
snippet))
(def get-snippet-by-slug
(wrap-snippet-return get-snippet-by-slug-from-db))
;; update
(def update-schema
"Malli schema for a valid update to a snippet entity."
[:map
[:db/id :int]
[:snippet/title {:optional true} :string]
[:snippet/slug {:optional true} :string]
[:snippet/markdown {:optional true} :string]
[:snippet/tags {:optional true} [:vector :string]]])
(defn- to-update [patch]
(cond-> {}
(some? (:title patch)) (assoc :snippet/title (:title patch))
(some? (:slug patch)) (assoc :snippet/slug (:slug patch))
(some? (:markdown patch)) (assoc :snippet/markdown (:markdown patch))
(some? (:tags patch)) (assoc :snippet/tags (:tags patch))))
(defn- patch-snippet-in-db
"Update specific fields of a snippet."
[slug raw-patch]
(let [conn (get-conn)
snippet (get-snippet-by-slug-from-db slug)
eid (:db/id snippet)
new-tags (get raw-patch :tags '[])
existing-tags (get snippet :snippet/tags '[])
tags-to-remove (vec (set/difference (set existing-tags) (set new-tags)))
retracts (map #(vector :db/retract eid :snippet/tags %) tags-to-remove)
patch (merge (to-update raw-patch) {:db/id eid})]
(t/log! {:level :info, :data {:patch patch :retracts retracts :slug slug :eid eid}} "Patching snippet")
(when (nil? eid)
(throw (ex-info "Snippet not found" {:slug slug})))
(when-not (m/validate update-schema patch)
(throw (ex-info "Invalid patch" {:errors (m/explain update-schema patch) :patch patch})))
(d/transact conn {:tx-data (into [patch] retracts)})))
(defn update-snippet [& args]
(let [res (apply patch-snippet-in-db args)]
(t/log! {:level :info, :data {:res res :args args}} "Finished patching snippet")))
(defn list-snippets-in-db
"List all the snippets"
[]
(let [conn (get-conn)
db (d/db conn)
query '[:find (pull ?e [*])
:where
[?e :snippet/slug]]]
(->> (d/q query db)
(map first))))
(def list-snippets (wrap-snippet-return list-snippets-in-db))
(defn delete-snippet-by-slug
"Soft delete a snippet (retract its entity)."
[slug]
(t/log! {:level :info, :data {:slug slug}} "Retracting snippet")
(let [conn (get-conn)
eid (:db/id (get-snippet-by-slug-from-db slug))]
(if (nil? eid)
nil
(d/transact conn {:tx-data [[:db/retractEntity eid]]}))))
(defn list-tags
"List all tags used in snippets with their counts."
[]
(let [conn (get-conn)
db (d/db conn)
query '[:find ?tag (count ?e)
:where
[?e :snippet/tags ?tag]]]
(d/q query db)))
(defn get-snippets-by-tag-in-db
"Get all snippets that have a specific tag."
[tag]
(let [conn (get-conn)
db (d/db conn)
query '[:find (pull ?e [*])
:in $ ?tag
:where
[?e :snippet/tags ?tag]]
results (d/q query db tag)]
(mapv first results)))
(def get-snippets-by-tag (wrap-snippet-return get-snippets-by-tag-in-db))

View file

@ -1,9 +1,6 @@
(ns snippets.main
(:require
[snippets.infra.api :as api]
[snippets.infra.db2 :refer [start-up-check]])
(:require [snippets.infra.api :as api])
(:gen-class))
(defn -main []
(start-up-check)
(api/run-server))

View file

@ -1,7 +0,0 @@
(ns snippets.mocks)
(def mock-snippet {:slug "mock-1"
:title "Mock Snippet 1"
:markdown "## Mock Snippet 1"
:tags ["mock"]
:pub-date (java.util.Date/from (java.time.Instant/parse "2020-01-01T00:00:00Z"))})

View file

@ -1,15 +0,0 @@
(ns snippets.use-cases.backfill-db2
(:require
[snippets.infra.db :as old-db]
[snippets.infra.db2 :as new-db]
[taoensso.telemere :as t]))
(defn- zdt-to-date [zdt]
(java.util.Date/from (.toInstant zdt)))
(defn backfill []
(t/log! {:level :info} "Backfilling DB2")
(let [old-snippets (old-db/list-snippets {})
new-snippets (map #(assoc % :pub-date (zdt-to-date (:pub-date %))) old-snippets)]
(t/log! {:level :info :data {:count (count new-snippets)}} "Creating snippets")
(new-db/create-snippets new-snippets)))

View file

@ -0,0 +1,25 @@
(ns snippets.use-cases.backfill-from-file
(:require
[clojure.java.io :as io]
[clojure.pprint :as pprint]
[clojure.string :as str]
[frontmatter.core :as fm]))
(defn scrape-files []
(let [dir "./old_snippets"]
(->> (io/file dir)
(.listFiles)
(map #(.getName %))
(map #(hash-map :slug (first (str/split % #"\.")) :full-path (str dir "/" %)))
;; (map #(fm/parse (:full-path %))))))
(map #(let [{frontmatter :frontmatter body :body} (fm/parse (:full-path %))]
(assoc %
:title (:title frontmatter)
:pub-date (:date frontmatter)
:markdown body
:scraped true
:tags (:snippet_types frontmatter))))
(map #(dissoc % :full-path)))))
;; used repl to do backfill
;; (doseq [s old-snippets] (xt/execute-tx db/client [[:put-docs :snippets (merge {:xt/id (:slug s)} s)]]))

View file

@ -1,9 +1,12 @@
(ns snippets.use-cases.create
(:require
[taoensso.telemere :as t]
[snippets.infra.db2 :as db]))
[snippets.infra.db :as db]))
(defn- uuid [] (str (java.util.UUID/randomUUID)))
(defn create-snippet [{:keys [title slug markdown tags]}]
(let [pub-date (java.util.Date.)]
(t/log! {:level :info, :data {:title title :slug slug}} "Creating snippet")
(db/create-snippets [{:title title :slug slug :markdown markdown :tags tags :pub-date pub-date}])))
(let [id (uuid)
pub-date (java.util.Date.)]
(t/log! {:level :info, :data {:title title :slug slug :id id}} "Creating snippet")
(db/put-snippet id {:title title :slug slug :markdown markdown :tags tags :pub-date pub-date})))

View file

@ -1,8 +1,8 @@
(ns snippets.use-cases.delete
(:require
[snippets.infra.db2 :as db]
[snippets.infra.db :as db]
[taoensso.telemere :as t]))
(defn delete-snippet [slug]
(t/log! {:level :info, :data {:slug slug}} "Deleting snippet by slug")
(db/delete-snippet-by-slug slug))
(defn delete-snippet [key]
(t/log! {:level :info, :data {:key key}} "Deleting snippet by id")
(db/delete-snippet key))

View file

@ -2,7 +2,7 @@
(:require
[taoensso.telemere :as t]
[malli.core :as m]
[snippets.infra.db2 :as db]))
[snippets.infra.db :as db]))
(def valid-patch?
(m/validator
@ -12,11 +12,11 @@
[:tags {:optional true} [:seqable :string]]
[:slug {:optional true} :string]]))
(defn edit-snippet [slug patch]
(t/log! {:level :info, :data {:patch patch :slug slug}} "Editing snippet")
(defn edit-snippet [id patch]
(t/log! {:level :info, :data {:patch patch :id id}} "Editing snippet")
(if (valid-patch? patch)
(do
(t/log! {:level :info, :data {:patch patch :slug slug}} "Valid changes editing snippet")
(db/update-snippet slug patch)
(t/log! {:level :info, :data {:patch patch :id id}} "Valid changes editing snippet")
(db/patch-snippet id patch)
{:success true})
{:success false :reason :invalid-patch}))

View file

@ -1,7 +1,7 @@
(ns snippets.use-cases.view
(:require
[taoensso.telemere :as t]
[snippets.infra.db2 :as db]))
[snippets.infra.db :as db]))
(defn serialize-snippet
"Converts snippet pub-date to ISO-8601 string for EDN serialization"
@ -9,15 +9,12 @@
(when snippet
(assoc snippet :pub-date (.toString (:pub-date snippet)))))
(defn view-snippets [options]
(if (nil? options)
(map serialize-snippet (db/list-snippets))
(let [limit (:limit options)
skip (:skip options)]
(->> (db/list-snippets)
(drop skip)
(take limit)
(map serialize-snippet)))))
(defn view-snippet [key]
(t/log! {:level :info, :data {:key key}} "Viewing snippet by id")
(serialize-snippet (db/get-snippet-by-id key)))
(defn view-snippets [& args]
(map serialize-snippet (db/list-snippets args)))
(defn view-tags []
(t/log! {:level :info} "Viewing tags")