This project is a simple API that serves as an exercise to explore the Nomad API.
The API provides the following endpoint:
This endpoint starts a Nomad job and accepts the following parameters:
name
(string): A unique identifier for the service to create.url
(string): The URL of the page to serve.script
(boolean): Specifies whether theurl
should be interpreted as a script or a page.
The endpoint performs the following actions based on the provided parameters:
- If
script
is set tofalse
, theurl
is downloaded and served asindex.html
. - If
script
is set totrue
, theurl
is downloaded, the script is executed, and its output is downloaded asindex.html
.
The endpoint returns the URL of the page.
curl localhost:3000/services -X POST -d '{"name": "my-page", "url": "https://example.com/mypage.html", "script": false}'
Response:
{
"url": "http://192.168.1.104:27510"
}
Assuming https://example.com/mypage.html
contains the following content:
Hello World!
Then the output of curl http://192.168.1.104:27510
will be:
Hello World!
curl localhost:3000/services -X POST -d '{"name": "my-page", "url": "https://example.com/myscript.sh", "script": true}'
Response:
{
"url": "http://192.168.1.104:27511"
}
Assuming https://example.com/myscript.sh
contains the following content:
#!/bin/sh
echo Hello World!
Then the output of curl http://192.168.1.104:27511
will be:
Hello World!
To install and run this project locally, follow these steps:
- Start Nomad by running the command:
nomad agent -dev -bind 0.0.0.0 -network-interface='{{ GetDefaultInterfaces | attr "name" }}'
. - Start the API by running the command:
go run ./cmd/api
.