-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add closeCurrentFile action - allow export confirm also via action - upgrade electron
- Loading branch information
Showing
10 changed files
with
676 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# HTTP API | ||
|
||
LosslessCut can be controlled via a HTTP API, if it is being run with the command line option `--http-api`. See also [CLI](./cli.md). **Note that the HTTP API is experimental and may change at any time.** | ||
|
||
## Programmatically opening a file | ||
|
||
This must be done with [the CLI](./cli.md). | ||
|
||
## Enabling the API | ||
|
||
```bash | ||
LosslessCut --http-api | ||
``` | ||
|
||
## API endpoints | ||
|
||
### `POST /api/shortcuts/action` | ||
|
||
Execute a keyboard shortcut `action`, similar to the `--keyboard-action` CLI option. This is different from the CLI in that most of the actions will wait for the action to finish before responding to the HTTP request (but not all). | ||
|
||
#### [Available keyboard actions](./cli.md#available-keyboard-actions) | ||
|
||
#### Example | ||
|
||
Export the currently opened file: | ||
|
||
```bash | ||
curl -X POST http://localhost:8080/api/shortcuts/export | ||
``` | ||
|
||
### Batch example | ||
|
||
Start the main LosslessCut in one terminal with the HTTP API enabled: | ||
|
||
```bash | ||
LosslessCut --http-api | ||
``` | ||
|
||
Then run the script in a different terminal: | ||
|
||
```bash | ||
for PROJECT in /path/to/folder/with/projects/*.llc | ||
LosslessCut $PROJECT | ||
sleep 5 # wait for the file to open | ||
curl -X POST http://localhost:8080/api/shortcuts/export | ||
curl -X POST http://localhost:8080/api/shortcuts/closeCurrentFile | ||
done | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const express = require('express'); | ||
const morgan = require('morgan'); | ||
const http = require('http'); | ||
const asyncHandler = require('express-async-handler'); | ||
const { homepage } = require('./constants'); | ||
|
||
|
||
const logger = require('./logger'); | ||
|
||
|
||
module.exports = ({ port, onKeyboardAction }) => { | ||
const app = express(); | ||
|
||
// https://expressjs.com/en/resources/middleware/morgan.html | ||
const morganFormat = ':remote-addr :method :url HTTP/:http-version :status - :response-time ms'; | ||
// https://stackoverflow.com/questions/27906551/node-js-logging-use-morgan-and-winston | ||
app.use(morgan(morganFormat, { | ||
stream: { write: (message) => logger.info(message.trim()) }, | ||
})); | ||
|
||
const apiRouter = express.Router(); | ||
|
||
app.get('/', (req, res) => res.send(`See ${homepage}`)); | ||
|
||
app.use('/api', apiRouter); | ||
|
||
apiRouter.post('/shortcuts/:action', express.json(), asyncHandler(async (req, res) => { | ||
await onKeyboardAction(req.params.action); | ||
res.end(); | ||
})); | ||
|
||
const server = http.createServer(app); | ||
|
||
server.on('error', (err) => logger.error('http server error', err)); | ||
|
||
const startHttpServer = async () => new Promise((resolve, reject) => { | ||
// force ipv4 | ||
const host = '127.0.0.1'; | ||
server.listen(port, host, () => { | ||
logger.info('HTTP API listening on', `http://${host}:${port}/`); | ||
resolve(); | ||
}); | ||
|
||
server.once('error', reject); | ||
}); | ||
|
||
return { | ||
startHttpServer, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.