Skip to content

refactor: introduce monorepo-like layout #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
dc718ce
refactor: `lib/migrate.js` → `migrations/index.js`
bajtos May 28, 2024
a737aae
refactor: move spark-api files to a subdir
bajtos May 28, 2024
03237f0
fixup! fix tests + run per-project tests only
bajtos May 28, 2024
55f946e
Merge branch 'main' into monorepo
bajtos May 28, 2024
a202151
fixup! add mocha config
bajtos May 28, 2024
6a82285
fixup! update package lock
bajtos May 28, 2024
a88a17b
docs: update start scripts & README
bajtos May 28, 2024
3166bd7
fix: "npm run migrate"
bajtos May 28, 2024
d77af20
ci: update GHA workflow
bajtos May 28, 2024
1c4d223
fixup! build-api should test only spark-api
bajtos May 28, 2024
6e7af32
docker build using a shared Dockerfile
bajtos May 28, 2024
19c46a1
single Dockerfile shared by all pckages
bajtos May 28, 2024
554f426
remove per-project .dockerignore files
bajtos May 28, 2024
a668dae
fix GHA CI cmd for testing spark api
bajtos May 28, 2024
c7d99f5
fixup! commit .gitignore
bajtos May 29, 2024
bdbe7e9
feat: npm workspaces (#326)
bajtos May 30, 2024
fabab97
Merge branch 'main' into monorepo
bajtos May 30, 2024
dea9f21
drop `spark-` prefix from directory names
bajtos May 30, 2024
052a4b7
shared ie-contract-config and test-helpers
bajtos May 30, 2024
6371ae5
simplify Dockerfile - rework ARG SERVICE to ENV
bajtos May 30, 2024
00da105
update fly.toml to match the new Dockerfile
bajtos May 30, 2024
d964af9
ci: update the deployment command
bajtos May 30, 2024
568f597
fixup! dependabot config - remove /spark-publish
bajtos May 30, 2024
30ed273
Merge branch 'main' into monorepo
bajtos May 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Dockerfile
.dockerignore
node_modules
.git
.env
3 changes: 1 addition & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ updates:
- package-ecosystem: "npm"
directories:
- "/"
- "/spark-publish"
schedule:
interval: "daily"
time: "09:00"
timezone: "Europe/Berlin"
versioning-strategy: increase
commit-message:
prefix: "deps"
prefix-development: "deps(dev)"
Expand All @@ -28,7 +28,6 @@ updates:
- package-ecosystem: "docker"
directories:
- "/"
- "/spark-publish"
schedule:
interval: "daily"
time: "15:00"
Expand Down
44 changes: 16 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
GLIF_TOKEN: ${{ secrets.GLIF_TOKEN }}
NPM_CONFIG_WORKSPACE: api
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand All @@ -50,40 +51,27 @@ jobs:
--health-retries 5
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
NPM_CONFIG_WORKSPACE: publish
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm run migrate
- run: cd spark-publish && npm ci
- run: cd spark-publish && npm test
- run: node bin/migrate.js
- run: npm test

docker-build-api:
lint-all:
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
env:
REGISTRY: ghcr.io
steps:
- uses: actions/checkout@v4

- uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build Docker image
uses: docker/build-push-action@v5
- uses: actions/setup-node@v4
with:
context: .
cache-from: type=registry,ref=ghcr.io/filecoin-station/core
cache-to: type=inline
node-version: 20
- run: npm ci
- run: npm run lint

docker-build-publish:
docker-build:
runs-on: ubuntu-latest
permissions:
contents: read
Expand All @@ -99,21 +87,21 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build Docker image
- name: Build monorepo Docker image
uses: docker/build-push-action@v5
with:
context: spark-publish
context: .
cache-from: type=registry,ref=ghcr.io/filecoin-station/core
cache-to: type=inline

deploy-api:
if: github.ref == 'refs/heads/main'
needs: [build-api, build-publish, docker-build-api, docker-build-publish]
needs: [build-api, build-publish, docker-build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
- run: flyctl deploy --remote-only -c api/fly.toml
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
- if: failure()
Expand All @@ -137,12 +125,12 @@ jobs:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
deploy-publish:
if: github.ref == 'refs/heads/main'
needs: [build-api, build-publish, docker-build-api, docker-build-publish]
needs: [build-api, build-publish, docker-build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: cd spark-publish && flyctl deploy --remote-only
- run: flyctl deploy --remote-only -c publish/fly.toml
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN_PUBLISH }}
- if: failure()
Expand Down
19 changes: 13 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ WORKDIR /app
# Set production environment
ENV NODE_ENV production
ENV SENTRY_ENVIRONMENT production
ENV DOMAIN api.filspark.com
ENV REQUEST_LOGGING false

#######################################################################
# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3
apt-get install -y build-essential pkg-config python-is-python3

# Install node modules
# NPM will not install any package listed in "devDependencies" when NODE_ENV is set to "production",
# to install all modules: "npm install --production=false".
# Ref: https://docs.npmjs.com/cli/v9/commands/npm-install#description
COPY --link package-lock.json package.json ./
RUN npm ci

# We cannot use a wildcard until `COPY --parents` is stabilised
# See https://docs.docker.com/reference/dockerfile/#copy---parents
COPY --link api/package.json ./api/
COPY --link publish/package.json ./publish/

RUN npm ci --workspaces

# Copy application code
COPY --link . .
Expand All @@ -40,5 +44,8 @@ FROM base
# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
CMD [ "npm", "run", "start" ]
# Set to `publish` or `api`
# This argument controls the value passed to npm start --workspace parameter
ENV SERVICE=""

CMD npm start --workspace ${SERVICE}
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,19 @@ docker run -d --name spark-db \
postgres
```

### `spark-api`
### `api`

Start the API service:

```bash
npm start
npm start --workspace api
```

Run tests and linters:

```bash
npm test
npm test --workspace api
npm run lint --workspace api
```

## Deployment
Expand All @@ -105,5 +106,5 @@ Pushes to `main` will be deployed automatically.
Perform manual devops using [Fly.io](https://fly.io):

```bash
$ fly deploy
$ fly deploy api
```
2 changes: 1 addition & 1 deletion bin/spark.js → api/bin/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { once } from 'node:events'
import { createHandler } from '../index.js'
import pg from 'pg'
import { startRoundTracker } from '../lib/round-tracker.js'
import { migrate } from '../lib/migrate.js'
import { migrate } from '../../migrations/index.js'

const {
PORT = 8080,
Expand Down
3 changes: 3 additions & 0 deletions fly.toml → api/fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ processes = []
PORT = "8080"
HOST = "0.0.0.0"
SENTRY_ENVIRONMENT = "production"
DOMAIN = "api.filspark.com"
REQUEST_LOGGING = "false"
SERVICE="api"

[experimental]
auto_rollback = true
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/ie-contract.js → api/lib/ie-contract.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers } from 'ethers'
import { RPC_URL, GLIF_TOKEN } from '../spark-publish/ie-contract-config.js'
import { RPC_URL, GLIF_TOKEN } from '../../common/ie-contract-config.js'
import * as SparkImpactEvaluator from '@filecoin-station/spark-impact-evaluator'

const fetchRequest = new ethers.FetchRequest(RPC_URL)
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions lib/instrument.js → api/lib/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const pkg = JSON.parse(
join(
dirname(fileURLToPath(import.meta.url)),
'..',
'..',
'package.json'
),
'utf8'
Expand Down
File renamed without changes.
File renamed without changes.
38 changes: 38 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@filecoin-station/spark-api",
"private": true,
"version": "0.0.0",
"license": "MIT",
"repository": "filecoin-station/spark-api",
"type": "module",
"description": "API for SPARK",
"scripts": {
"start": "node bin/spark.js",
"lint": "standard",
"test": "mocha"
},
"devDependencies": {
"light-my-request": "^5.13.0",
"mocha": "^10.4.0",
"standard": "^17.1.0",
"varint": "^6.0.0"
},
"dependencies": {
"@filecoin-station/spark-impact-evaluator": "^1.1.1",
"@glif/filecoin-address": "^3.0.5",
"@sentry/node": "^8.7.0",
"compare-versions": "^6.1.0",
"ethers": "^6.12.1",
"http-assert": "^1.5.0",
"http-responders": "^2.0.2",
"multiformats": "^13.1.0",
"pg": "^8.11.5",
"postgrator": "^7.2.0",
"raw-body": "^2.5.2"
},
"standard": {
"env": [
"mocha"
]
}
}
2 changes: 1 addition & 1 deletion test/db.test.js → api/test/db.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pg from 'pg'
import { migrate } from '../lib/migrate.js'
import { migrate } from '../../migrations/index.js'

const { DATABASE_URL } = process.env

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setTimeout } from 'node:timers/promises'
import pg from 'pg'
import { mapRequestToInetGroup, mapRequestToSubnet, mapSubnetToInetGroup } from '../lib/inet-grouping.js'
import { Request as FakeRequest } from 'light-my-request/lib/request.js'
import { migrate } from '../lib/migrate.js'
import { migrate } from '../../migrations/index.js'

const { DATABASE_URL } = process.env

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
mapCurrentMeridianRoundToSparkRound,
startRoundTracker
} from '../lib/round-tracker.js'
import { migrate } from '../lib/migrate.js'
import { assertApproximately } from './test-helpers.js'
import { migrate } from '../../migrations/index.js'
import { assertApproximately } from '../../test-helpers/assert.js'
import { createMeridianContract } from '../lib/ie-contract.js'
import { afterEach, beforeEach } from 'mocha'

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion bin/migrate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { migrate } from '../lib/migrate.js'
import { migrate } from '../migrations/index.js'
import pg from 'pg'

const { DATABASE_URL } = process.env
Expand Down
File renamed without changes.
File renamed without changes.
Loading