Skip to content

Commit ddbcb5e

Browse files
author
Developer
committed
accept URI_PATH for living behind a reverse proxy
1 parent 5f7c054 commit ddbcb5e

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ help:
77

88
.PHONY: docker
99
docker: ## Build the docker container
10-
docker build -t mobiledoc .
10+
docker build -t rest-mobiledoc .
11+
12+
.PHONY: docker-run
13+
docker-run: ## Run the docker container
14+
docker run --rm -e URI_PATH=/mobiledoc/ -it -p 3000:3000 rest-mobiledoc
1115

1216
.PHONY: test
1317
test: ## Run the tests

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ Example request body:
128128

129129
The API will respond with the converted Mobiledoc content.
130130

131+
## API Usage Example
132+
133+
Here's an example of how to use the API with curl:
134+
135+
```bash
136+
curl -X POST https://api.joat.tools \
137+
-H "Content-Type: application/json" \
138+
-d '{
139+
"source": "markdown",
140+
"payload": "# Welcome to REST-mobiledoc\n\nThis is a sample Markdown document.\n\n## Features\n\n- Converts Markdown to Mobiledoc\n- Easy to use API\n- Lightweight and fast"
141+
}'
142+
```
143+
144+
This command sends a POST request to the API with a Markdown payload. The API should respond with the converted Mobiledoc content.
145+
131146

132147
## Contributing
133148

server.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ function createApp() {
1010
const app = express();
1111
app.use(bodyParser.json());
1212

13+
// Get the URI_PATH from environment variables, default to '/' if not set
14+
const uriPath = process.env.URI_PATH || '/';
15+
1316
// Swagger definition
1417
const swaggerOptions = {
1518
definition: {
@@ -19,12 +22,18 @@ function createApp() {
1922
version: '1.0.0',
2023
description: 'A simple Express API to convert HTML and Markdown to Mobiledoc',
2124
},
25+
servers: [
26+
{
27+
url: uriPath,
28+
description: 'Dynamic base path',
29+
},
30+
],
2231
},
2332
apis: ['./server.js'], // files containing annotations as above
2433
};
2534

2635
const swaggerSpec = swaggerJsdoc(swaggerOptions);
27-
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
36+
app.use(`${uriPath}api-docs`, swaggerUi.serve, swaggerUi.setup(swaggerSpec));
2837

2938
/**
3039
* @swagger
@@ -61,7 +70,7 @@ function createApp() {
6170
* 500:
6271
* description: Internal server error
6372
*/
64-
app.post('/', (req, res) => {
73+
app.post(`${uriPath}`, (req, res) => {
6574
try {
6675
const { source, payload } = req.body;
6776
console.log('Received request:', { source, payload });
@@ -126,10 +135,12 @@ function convertToMobiledoc(content, source) {
126135
if (require.main === module) {
127136
const app = createApp();
128137
const port = process.env.PORT || 3000;
138+
const uriPath = process.env.URI_PATH || '/';
139+
129140

130141
const server = app.listen(port, () => {
131-
console.log(`Server running at http://localhost:${port}`);
132-
console.log(`API documentation available at http://localhost:${port}/api-docs`);
142+
console.log(`Server running at http://localhost:${port}${uriPath}`);
143+
console.log(`API documentation available at http://localhost:${port}${uriPath}api-docs`);
133144
});
134145

135146
// Handle server errors

0 commit comments

Comments
 (0)