Skip to content

Commit

Permalink
Add cowllector api
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Dec 5, 2024
1 parent 81fbafb commit 7c93f59
Show file tree
Hide file tree
Showing 17 changed files with 1,240 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: akhileshns/[email protected]
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: ${{secrets.HEROKU_APP_NAME}}
heroku_app_name: ${{secrets.HEROKU_ANALYTICS_APP_NAME}}
heroku_email: ${{secrets.HEROKU_EMAIL}}
usedocker: true
appdir: "analytics"
19 changes: 19 additions & 0 deletions .github/workflows/deploy-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Deploy API to Heroku

on:
push:
branches:
- prod

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/[email protected]
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: ${{secrets.HEROKU_API_APP_NAME}}
heroku_email: ${{secrets.HEROKU_EMAIL}}
usedocker: true
appdir: "api"
6 changes: 6 additions & 0 deletions api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Local development
POSTGRES_USER=postgres
POSTGRES_HOST=localhost
POSTGRES_DB=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_PORT=5432
29 changes: 29 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# dev
.yarn/
!.yarn/releases
.vscode/*
!.vscode/launch.json
!.vscode/*.code-snippets
.idea/workspace.xml
.idea/usage.statistics.xml
.idea/shelf

# deps
node_modules/
dist

# env
.env
.env.production

# logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

# misc
.DS_Store
18 changes: 18 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM oven/bun:1 as base
WORKDIR /app

# Install dependencies only
FROM base AS deps
COPY package.json .
RUN bun install --frozen-lockfile

# Production image
FROM base AS runner
WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENTRYPOINT [ "/app/heroku-run.sh" ]
CMD ["bun", "run", "src/index.ts"]

24 changes: 24 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Cowllector API

## Development

```bash
npm install
npm run dev
```

The server will start at http://localhost:3000

## API Documentation

OpenAPI documentation is available at: http://localhost:3000/api/docs
Swagger UI is available at: http://localhost:3000/swagger

## Test URLs for Last Harvest Reports Endpoint

```
# Get all last harvest reports
http://localhost:3000/api/v1/last-harvest-reports
```

You can test this URL in your browser or using tools like curl or Postman. The API will return a JSON response with an array containing the last harvest report for each vault.
30 changes: 30 additions & 0 deletions api/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": []
},
"formatter": {
"enabled": true,
"indentStyle": "tab"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "double"
}
}
}
20 changes: 20 additions & 0 deletions api/heroku-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash -e

# use a pointer to the environment variable because it may change name
DB_URL=${!COWLLECTOR_DB_URL_ENV_VAR}

[[ $DB_URL =~ ^postgres:\/\/([^:]+):([^@]+)@([^:]+):([^\/]+)\/(.+)$ ]]
export PG_USERNAME="${BASH_REMATCH[1]}"
export PG_PASSWORD="${BASH_REMATCH[2]}"
export PG_HOSTNAME="${BASH_REMATCH[3]}"
export PG_PORT="${BASH_REMATCH[4]}"
export PG_DATABASE="${BASH_REMATCH[5]}"

if [[ -z "$PG_USERNAME" || -z "$PG_PASSWORD" || -z "$PG_HOSTNAME" || -z "$PG_PORT" || -z "$PG_DATABASE" ]]; then
echo "Invalid DATABASE_URL: $DB_URL"
echo "Found looking at the content of '$COWLLECTOR_DB_URL_ENV_VAR'"
echo "Expected format: postgres://username:password@hostname:port/database"
exit 1
fi

exec "$@"
Loading

0 comments on commit 7c93f59

Please sign in to comment.