Skip to content

Commit

Permalink
Switch to sqlite (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Mar 23, 2024
1 parent 92d1731 commit 5b0161c
Show file tree
Hide file tree
Showing 16 changed files with 530 additions and 26 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
run: pnpm lint
- name: Check formatting
run: pnpm prettier --check .

ts:
name: Type Check
runs-on: ubuntu-latest
Expand All @@ -19,22 +20,25 @@ jobs:
run: pnpm db:generate
- name: Type check
run: pnpm ts

test:
name: Test
runs-on: ubuntu-latest
env:
DATABASE_URL: file:./test.db
steps:
- uses: mskelton/setup-pnpm@v2
- run: pnpm playwright install chromium --with-deps
- run: pnpm db:generate
- run: pnpm prisma migrate dev
- run: pnpm db:seed
- run: pnpm test
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
- name: Upload test results
uses: actions/upload-artifact@v2
if: ${{ failure() }}
with:
name: test-results
path: test-results

deploy:
name: Deploy
if: github.ref == 'refs/heads/main'
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ out/

.env*
!.env.example

# DB
prisma/*.db*
13 changes: 12 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,15 @@ EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"

CMD ["node", "server.js"]
# Install LiteFS and SQLite3
RUN apt-get update -y && apt-get install -y ca-certificates fuse3 sqlite3

# Copy LiteFS binary
COPY --from=flyio/litefs:0.5 /usr/local/bin/litefs /usr/local/bin/litefs

# Move the appropriate LiteFS config file to /etc/ (this one will be used by LiteFS).
COPY litefs.yml /etc/litefs.yml

# Run LiteFS as the entrypoint. After it has connected and sync'd with the
# cluster, it will run the commands listed in the "exec" field of the config.
ENTRYPOINT litefs mount
8 changes: 5 additions & 3 deletions app/api/bytes/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ export async function POST(req: Request) {

const { commits } = body
await remove(commits.flatMap((c) => c.removed))
await Promise.all(
commits.flatMap((c) => [...c.added, ...c.modified]).map(upsert),
)

const bytes = commits.flatMap((c) => [...c.added, ...c.modified])
for await (const byte of bytes) {
await upsert(byte)
}

return NextResponse.json({ message: "ok" })
}
13 changes: 8 additions & 5 deletions app/api/reindex/route.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { headers } from "next/headers"
import { NextResponse } from "next/server"
import { upsertByte } from "lib/api/bytes"
import { getByteSource, octokit } from "lib/api/github"
Expand All @@ -22,11 +23,13 @@ async function getAllByteIds() {
}

export async function POST() {
if (process.env.NODE_ENV === "production") {
return NextResponse.json(
{ message: "Reindexing is not allowed in production." },
{ status: 400 },
)
const token = headers().get("Authorization")?.replace("Bearer ", "")

if (
process.env.NODE_ENV === "production" &&
process.env.API_AUTH_TOKEN !== token
) {
return NextResponse.json({ message: "Forbidden" }, { status: 403 })
}

// Prep the reindexing before clearing content. Make sure we get all the
Expand Down
88 changes: 88 additions & 0 deletions e2e/bytes/20230903044640.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: Controlling Browsers With AppleScript
tags: [cli]
---

I recently came across a [Raycast](https://www.raycast.com) extension that
allows you to play/pause music from YouTube music while it's running in your
browser. After exploring how it worked internally given I was a bit astonished
it is even possible, I found that it's all just using AppleScript.

So, a bit of bash code later and I had a working implementation of a new `js`
command to allow running JavaScript either the active browser tab or in all
windows with a given URL.

```bash showLineNumbers js
#!/usr/bin/env bash

# Default values
browser="Arc"
url=""
code=""

# Parse the command line args
while [[ $# -gt 0 ]]; do
case "$1" in
-b | --browser)
browser="$2"
shift 2
;;
-u | --url)
url="$2"
shift 2
;;
*)
code="$1"
shift
;;
esac
done

_wrap() {
if [[ -n "$url" ]]; then
printf "repeat with w in (every window)\n"
printf "repeat with t in (every tab whose URL contains \"$url\") of w\n"
printf "tell t\n"
else
printf "tell front window\n"
printf "tell active tab\n"
fi

# Run the JS
printf "$1\n"
printf "end tell\n"

if [[ -n "$url" ]]; then
printf "end repeat\n"
printf "end repeat\n"
else
printf "end tell\n"
fi
}

osascript -e "
tell application \"$browser\"
$(_wrap "execute javascript \"$code\"")
end tell
" >/dev/null
```

Here's the command that will play music from YouTube music like what the Raycast
extension was doing. But this now can be a terminal command!

```bash play
js --url music.youtube.com "document.querySelector('#play-pause-button[aria-label=Play]').click()"
```

Definitely planning on using this more for other areas where I have simple
JavaScript I want to execute, such as clicking something after a hot reload,
etc.

## Chromium security

In Chromium based browsers (e.g. Chrome, Arc), you need to enable a developer
flag in order for this to work properly. You can find it in the menu:

```
View -> Developer -> Allow JavaScript from Apple Events
```
40 changes: 40 additions & 0 deletions e2e/bytes/20230904035410.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Better Shell Aliases
tags: [fish, cli]
---

I use shell aliases quite heavily to simplify common CLI commands. However, one
of my biggest pain points when using them is they didn't work as well in certain
complex commands.

For example, suppose I have the following alias in my Fish config that assigns
the `task` command to an alias `t`:

```bash
alias t="task"
```

Now I can run commands like this:

```bash
t add New task
```

But with a complex command like this, it doesn't work:

```fish
seq 10 | xargs -I {} t add Testing {}
```

I found that a simple solution was to simple create a binary in addition to the
alias. For the `t` alias, this would look like this:

```bash
#!/usr/bin/env bash

task "$@"
```

Now, you could just create the binary and remove the alias, but the alias
ensures fish can provide autocompletion properly, so I find that the combination
of both gives the best of both worlds.
25 changes: 25 additions & 0 deletions e2e/bytes/20230906143340.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Using Git Hooks When Creating Worktrees
tags: [git]
---

I've started to use `git worktree`s more lately but one of the pain points I had
was that they don't copied ignored files such as `.env` files. I found I was
able to improve this workflow by adding a `post-checkout` hook.

In a nutshell, the hook will check if we are checking out a new worktree (that's
what the `"$1" == "0000..."` is all about). If we are, then we can run some code
to copy over whatever files we need to.

```bash showLineNumbers .git/hooks/post-checkout
#!/bin/bash

if [[ "$1" == "0000000000000000000000000000000000000000" ]]; then
basePath="$HOME/dev/repo"
paths=(.env)

for path in "${paths[@]}"; do
cp "$basePath/$path" "$(pwd)/$path"
done
fi
```
16 changes: 10 additions & 6 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ swap_size_mb = 512
[build]

[http_service]
internal_port = 3000
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 1
processes = ["app"]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 1
processes = ["app"]

[mounts]
source = "litefs"
destination = "/var/lib/litefs"
24 changes: 24 additions & 0 deletions litefs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fuse:
dir: "/litefs"

data:
dir: "/var/lib/litefs"

exit-on-error: false

proxy:
addr: ":8080"
target: "127.0.0.1:3000"
db: "prod.db"

exec:
- cmd: "npx prisma migrate deploy"
if-candidate: true

- cmd: "node server.js"

lease:
type: "static"
advertise-url: "http://${FLY_ALLOC_ID}.vm.${FLY_APP_NAME}.internal:20202"
candidate: ${FLY_REGION == PRIMARY_REGION}
promote: true
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"scripts": {
"build": "prisma generate && next build",
"db:generate": "prisma generate",
"db:push": "prisma db push",
"db:studio": "prisma studio",
"db:reindex": "curl -X POST http://localhost:3000/api/reindex",
"db:seed": "tsx scripts/seed.ts",
"dev": "next dev",
"serve": "next start",
"lint": "next lint",
Expand Down Expand Up @@ -91,6 +90,7 @@
"prettier-plugin-jsdoc": "^1.3.0",
"prettier-plugin-tailwindcss": "^0.5.11",
"prisma": "^5.5.1",
"tsx": "^4.7.1",
"typescript": "^5.3.3"
}
}

0 comments on commit 5b0161c

Please sign in to comment.