Skip to content

Commit 8bc6151

Browse files
authored
refactor: introduce monorepo-like layout (#323)
Introduce the following repository layout: - `bin` contains scripts shared by all components, currently `bin/migrate.js` - `migrations` contains SQL migrations scripts and `index.js` for running them - each component has its own directory with its own fly.toml - logshipper - api - spark-db - publish - there is a single Dockerfile shared by all workspaces - `publish` and `api` are npm workspaces with their own `package.json` file - the top-level `package.json` file is a npm workspace root Signed-off-by: Miroslav BajtoΕ‘ <[email protected]>
1 parent 386e5b7 commit 8bc6151

37 files changed

+2700
-7698
lines changed

β€Ž.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Dockerfile
33
.dockerignore
44
node_modules
55
.git
6+
.env

β€Ž.github/dependabot.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ updates:
33
- package-ecosystem: "npm"
44
directories:
55
- "/"
6-
- "/spark-publish"
76
schedule:
87
interval: "daily"
98
time: "09:00"
109
timezone: "Europe/Berlin"
10+
versioning-strategy: increase
1111
commit-message:
1212
prefix: "deps"
1313
prefix-development: "deps(dev)"
@@ -28,7 +28,6 @@ updates:
2828
- package-ecosystem: "docker"
2929
directories:
3030
- "/"
31-
- "/spark-publish"
3231
schedule:
3332
interval: "daily"
3433
time: "15:00"

β€Ž.github/workflows/ci.yml

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
env:
2525
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
2626
GLIF_TOKEN: ${{ secrets.GLIF_TOKEN }}
27+
NPM_CONFIG_WORKSPACE: api
2728
steps:
2829
- uses: actions/checkout@v4
2930
- uses: actions/setup-node@v4
@@ -50,40 +51,27 @@ jobs:
5051
--health-retries 5
5152
env:
5253
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
54+
NPM_CONFIG_WORKSPACE: publish
5355
steps:
5456
- uses: actions/checkout@v4
5557
- uses: actions/setup-node@v4
5658
with:
5759
node-version: 20
5860
- run: npm ci
59-
- run: npm run migrate
60-
- run: cd spark-publish && npm ci
61-
- run: cd spark-publish && npm test
61+
- run: node bin/migrate.js
62+
- run: npm test
6263

63-
docker-build-api:
64+
lint-all:
6465
runs-on: ubuntu-latest
65-
permissions:
66-
contents: read
67-
packages: read
68-
env:
69-
REGISTRY: ghcr.io
7066
steps:
7167
- uses: actions/checkout@v4
72-
73-
- uses: docker/login-action@v3
74-
with:
75-
registry: ${{ env.REGISTRY }}
76-
username: ${{ github.actor }}
77-
password: ${{ secrets.GITHUB_TOKEN }}
78-
79-
- name: Build Docker image
80-
uses: docker/build-push-action@v5
68+
- uses: actions/setup-node@v4
8169
with:
82-
context: .
83-
cache-from: type=registry,ref=ghcr.io/filecoin-station/core
84-
cache-to: type=inline
70+
node-version: 20
71+
- run: npm ci
72+
- run: npm run lint
8573

86-
docker-build-publish:
74+
docker-build:
8775
runs-on: ubuntu-latest
8876
permissions:
8977
contents: read
@@ -99,21 +87,21 @@ jobs:
9987
username: ${{ github.actor }}
10088
password: ${{ secrets.GITHUB_TOKEN }}
10189

102-
- name: Build Docker image
90+
- name: Build monorepo Docker image
10391
uses: docker/build-push-action@v5
10492
with:
105-
context: spark-publish
93+
context: .
10694
cache-from: type=registry,ref=ghcr.io/filecoin-station/core
10795
cache-to: type=inline
10896

10997
deploy-api:
11098
if: github.ref == 'refs/heads/main'
111-
needs: [build-api, build-publish, docker-build-api, docker-build-publish]
99+
needs: [build-api, build-publish, docker-build]
112100
runs-on: ubuntu-latest
113101
steps:
114102
- uses: actions/checkout@v4
115103
- uses: superfly/flyctl-actions/setup-flyctl@master
116-
- run: flyctl deploy --remote-only
104+
- run: flyctl deploy --remote-only -c api/fly.toml
117105
env:
118106
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
119107
- if: failure()
@@ -137,12 +125,12 @@ jobs:
137125
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
138126
deploy-publish:
139127
if: github.ref == 'refs/heads/main'
140-
needs: [build-api, build-publish, docker-build-api, docker-build-publish]
128+
needs: [build-api, build-publish, docker-build]
141129
runs-on: ubuntu-latest
142130
steps:
143131
- uses: actions/checkout@v4
144132
- uses: superfly/flyctl-actions/setup-flyctl@master
145-
- run: cd spark-publish && flyctl deploy --remote-only
133+
- run: flyctl deploy --remote-only -c publish/fly.toml
146134
env:
147135
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN_PUBLISH }}
148136
- if: failure()

β€ŽDockerfile

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@ WORKDIR /app
1212
# Set production environment
1313
ENV NODE_ENV production
1414
ENV SENTRY_ENVIRONMENT production
15-
ENV DOMAIN api.filspark.com
16-
ENV REQUEST_LOGGING false
1715

1816
#######################################################################
1917
# Throw-away build stage to reduce size of final image
2018
FROM base as build
2119

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

2624
# Install node modules
2725
# NPM will not install any package listed in "devDependencies" when NODE_ENV is set to "production",
2826
# to install all modules: "npm install --production=false".
2927
# Ref: https://docs.npmjs.com/cli/v9/commands/npm-install#description
3028
COPY --link package-lock.json package.json ./
31-
RUN npm ci
29+
30+
# We cannot use a wildcard until `COPY --parents` is stabilised
31+
# See https://docs.docker.com/reference/dockerfile/#copy---parents
32+
COPY --link api/package.json ./api/
33+
COPY --link publish/package.json ./publish/
34+
35+
RUN npm ci --workspaces
3236

3337
# Copy application code
3438
COPY --link . .
@@ -40,5 +44,8 @@ FROM base
4044
# Copy built application
4145
COPY --from=build /app /app
4246

43-
# Start the server by default, this can be overwritten at runtime
44-
CMD [ "npm", "run", "start" ]
47+
# Set to `publish` or `api`
48+
# This argument controls the value passed to npm start --workspace parameter
49+
ENV SERVICE=""
50+
51+
CMD npm start --workspace ${SERVICE}

β€ŽREADME.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,19 @@ docker run -d --name spark-db \
8484
postgres
8585
```
8686

87-
### `spark-api`
87+
### `api`
8888

8989
Start the API service:
9090

9191
```bash
92-
npm start
92+
npm start --workspace api
9393
```
9494

9595
Run tests and linters:
9696

9797
```bash
98-
npm test
98+
npm test --workspace api
99+
npm run lint --workspace api
99100
```
100101

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

107108
```bash
108-
$ fly deploy
109+
$ fly deploy api
109110
```

β€Žbin/spark.js renamed to β€Žapi/bin/spark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { once } from 'node:events'
44
import { createHandler } from '../index.js'
55
import pg from 'pg'
66
import { startRoundTracker } from '../lib/round-tracker.js'
7-
import { migrate } from '../lib/migrate.js'
7+
import { migrate } from '../../migrations/index.js'
88

99
const {
1010
PORT = 8080,

β€Žfly.toml renamed to β€Žapi/fly.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ processes = []
1010
PORT = "8080"
1111
HOST = "0.0.0.0"
1212
SENTRY_ENVIRONMENT = "production"
13+
DOMAIN = "api.filspark.com"
14+
REQUEST_LOGGING = "false"
15+
SERVICE="api"
1316

1417
[experimental]
1518
auto_rollback = true
File renamed without changes.

β€Žlib/ie-contract.js renamed to β€Žapi/lib/ie-contract.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ethers } from 'ethers'
2-
import { RPC_URL, GLIF_TOKEN } from '../spark-publish/ie-contract-config.js'
2+
import { RPC_URL, GLIF_TOKEN } from '../../common/ie-contract-config.js'
33
import * as SparkImpactEvaluator from '@filecoin-station/spark-impact-evaluator'
44

55
const fetchRequest = new ethers.FetchRequest(RPC_URL)
File renamed without changes.

β€Žlib/instrument.js renamed to β€Žapi/lib/instrument.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const pkg = JSON.parse(
1010
join(
1111
dirname(fileURLToPath(import.meta.url)),
1212
'..',
13+
'..',
1314
'package.json'
1415
),
1516
'utf8'
File renamed without changes.
File renamed without changes.

β€Žapi/package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@filecoin-station/spark-api",
3+
"private": true,
4+
"version": "0.0.0",
5+
"license": "MIT",
6+
"repository": "filecoin-station/spark-api",
7+
"type": "module",
8+
"description": "API for SPARK",
9+
"scripts": {
10+
"start": "node bin/spark.js",
11+
"lint": "standard",
12+
"test": "mocha"
13+
},
14+
"devDependencies": {
15+
"light-my-request": "^5.13.0",
16+
"mocha": "^10.4.0",
17+
"standard": "^17.1.0",
18+
"varint": "^6.0.0"
19+
},
20+
"dependencies": {
21+
"@filecoin-station/spark-impact-evaluator": "^1.1.1",
22+
"@glif/filecoin-address": "^3.0.5",
23+
"@sentry/node": "^8.7.0",
24+
"compare-versions": "^6.1.0",
25+
"ethers": "^6.12.1",
26+
"http-assert": "^1.5.0",
27+
"http-responders": "^2.0.2",
28+
"multiformats": "^13.1.0",
29+
"pg": "^8.11.5",
30+
"postgrator": "^7.2.0",
31+
"raw-body": "^2.5.2"
32+
},
33+
"standard": {
34+
"env": [
35+
"mocha"
36+
]
37+
}
38+
}

β€Žtest/db.test.js renamed to β€Žapi/test/db.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pg from 'pg'
2-
import { migrate } from '../lib/migrate.js'
2+
import { migrate } from '../../migrations/index.js'
33

44
const { DATABASE_URL } = process.env
55

β€Žtest/inet-grouping.test.js renamed to β€Žapi/test/inet-grouping.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { setTimeout } from 'node:timers/promises'
33
import pg from 'pg'
44
import { mapRequestToInetGroup, mapRequestToSubnet, mapSubnetToInetGroup } from '../lib/inet-grouping.js'
55
import { Request as FakeRequest } from 'light-my-request/lib/request.js'
6-
import { migrate } from '../lib/migrate.js'
6+
import { migrate } from '../../migrations/index.js'
77

88
const { DATABASE_URL } = process.env
99

β€Žtest/round-tracker.test.js renamed to β€Žapi/test/round-tracker.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
mapCurrentMeridianRoundToSparkRound,
77
startRoundTracker
88
} from '../lib/round-tracker.js'
9-
import { migrate } from '../lib/migrate.js'
10-
import { assertApproximately } from './test-helpers.js'
9+
import { migrate } from '../../migrations/index.js'
10+
import { assertApproximately } from '../../test-helpers/assert.js'
1111
import { createMeridianContract } from '../lib/ie-contract.js'
1212
import { afterEach, beforeEach } from 'mocha'
1313

File renamed without changes.

β€Žbin/migrate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { migrate } from '../lib/migrate.js'
1+
import { migrate } from '../migrations/index.js'
22
import pg from 'pg'
33

44
const { DATABASE_URL } = process.env
File renamed without changes.

0 commit comments

Comments
Β (0)