Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ $ station logs --follow
...
```

### `$ station --listen`

Open HTTP API.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this going to create another process providing only HTTP API for data created by another already running Station Core process? That seems like too many processes to me!

IMO, --listen should be an optional argument for the default command (station) that runs the main station process & modules.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of --listen argument, we can implement the following flag:

  • If PORT is set to some value (including 0), we start the HTTP server
  • If PORT is not specified, we don't create any server.

It's less explicit than --listen, so maybe it's not a good idea. Posting it just for consideration.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it's not a command it's just an option, meaning that it will run the basic $ station command, but also open an HTTP server.

IMO, --listen should be an optional argument for the default command (station) that runs the main station process & modules.

That's exactly how it is, I'm going to improve the README.

It's less explicit than --listen, so maybe it's not a good idea. Posting it just for consideration.

Those are my conerns as well. And we do read $PORT already, but only if --listen is set.


This command has the following additional configuration in addition to common
the configuration options described in
[Common Configuration](#common-configuration):

- `PORT` _(number)_: The port to listen on. Defaults to `7834`.

Routes:

- `/` 404 (Placeholder route)

### `$ station --help`

Show help.
Expand All @@ -145,12 +159,14 @@ $ station --help
Usage: station <command> [options]

Commands:
station Start Station [default]
station metrics Show metrics
station activity Show activity log
station logs [module] Show module logs
station Start Station [default]
station metrics Show metrics
station activity Show activity log
station logs [module] Show module logs

Options:
-l, --listen Open HTTP API [boolean]
-p, --port HTTP API port [number] [default: 7834]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two options are relevant only to station; they don't apply to station metrics and friends, right?

I also don't see the --port argument defined anywhere in bin/station.js.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's outdated. Will fix!

-v, --version Show version number [boolean]
-h, --help Show help [boolean]
```
Expand Down
12 changes: 11 additions & 1 deletion bin/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ await fs.mkdir(paths.moduleLogs, { recursive: true })

yargs(hideBin(process.argv))
.usage('Usage: $0 <command> [options]')
.command('$0', 'Start Station', () => {}, commands.station)
.command(
'$0',
'Start Station',
yargs => yargs
.option('listen', {
alias: 'l',
type: 'boolean',
description: 'Open HTTP API'
}),
commands.station
)
.command('metrics', 'Show metrics', () => {}, commands.metrics)
.commands('activity', 'Show activity log', () => {}, commands.activity)
.command('logs [module]', 'Show module logs', () => {}, commands.logs)
Expand Down
17 changes: 15 additions & 2 deletions commands/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ import { createMetricsStream } from '../lib/metrics.js'
import { createActivityStream } from '../lib/activity.js'
import lockfile from 'proper-lockfile'
import { maybeCreateFile } from '../lib/util.js'
import http from 'node:http'
import { once } from 'node:events'

const { FIL_WALLET_ADDRESS } = process.env
const { FIL_WALLET_ADDRESS, PORT = 7834 } = process.env

export const station = async () => {
const handler = (req, res) => {
res.statusCode = 404
res.end(http.STATUS_CODES[404])
}

export const station = async ({ listen, port }) => {
if (!FIL_WALLET_ADDRESS) {
console.error('FIL_WALLET_ADDRESS required')
process.exit(1)
Expand All @@ -24,6 +31,12 @@ export const station = async () => {
process.exit(1)
}

if (listen) {
const server = http.createServer(handler)
server.listen(PORT).unref()
await once(server, 'listening')
}

await saturnNode.start({
FIL_WALLET_ADDRESS,
storagePath: join(paths.moduleStorage, 'saturn-L2-node'),
Expand Down
158 changes: 158 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"release": "np"
},
"devDependencies": {
"get-port": "^6.1.2",
"gunzip-maybe": "^1.4.2",
"node-fetch": "^3.3.1",
"np": "^7.6.3",
"prettier": "^2.8.4",
"standard": "^17.0.0",
Expand Down
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import fs from 'node:fs/promises'
import { randomUUID } from 'node:crypto'
import { once } from 'node:events'
import { getPaths } from '../lib/paths.js'
import fetch from 'node-fetch'
import getPort from 'get-port'

const __dirname = dirname(fileURLToPath(import.meta.url))
const station = join(__dirname, '..', 'bin', 'station.js')
Expand Down Expand Up @@ -218,6 +220,34 @@ test('Lockfile', async t => {
throw new Error('did not throw')
})

test('HTTP API', async t => {
await t.test('Default port', async t => {
const XDG_STATE_HOME = join(tmpdir(), randomUUID())
const ps = execa(
station,
['--listen'],
{ env: { XDG_STATE_HOME, FIL_WALLET_ADDRESS } }
)
await once(ps.stdout, 'data')
const res = await fetch('http://127.0.0.1:7834')
t.equal(res.status, 404)
ps.kill()
})
await t.test('Custom port', async t => {
const XDG_STATE_HOME = join(tmpdir(), randomUUID())
const port = await getPort()
const ps = execa(
station,
['--listen'],
{ env: { XDG_STATE_HOME, FIL_WALLET_ADDRESS, PORT: port } }
)
await once(ps.stdout, 'data')
const res = await fetch(`http://127.0.0.1:${port}`)
t.equal(res.status, 404)
ps.kill()
})
})

test('Update modules', async t => {
await execa(join(__dirname, '..', 'scripts', 'update-modules.js'))
})