mirror of
https://github.com/morten-olsen/mini-loader.git
synced 2026-02-08 01:36:26 +01:00
docs: completed v1 docs (#36)
This commit is contained in:
49
docs/03-tutorial/01-first-workload.md
Normal file
49
docs/03-tutorial/01-first-workload.md
Normal file
@@ -0,0 +1,49 @@
|
||||
## Getting Started with mini loader CLI
|
||||
|
||||
Welcome to the mini loader CLI! This guide will walk you through the installation of the CLI, creating a simple script, and executing it locally to ensure everything works smoothly.
|
||||
|
||||
### Step 1: Install the CLI
|
||||
|
||||
The mini loader CLI is your gateway to interacting with a mini-loader server and running workloads locally for validation. Install it globally using npm with the following command:
|
||||
|
||||
```bash
|
||||
npm install -g @morten-olsen/mini-loader-cli
|
||||
```
|
||||
|
||||
### Step 2: Create Your First Script
|
||||
|
||||
Now, let's write a basic script that outputs a single artifact named “hello”. Create a new file with the following JavaScript code:
|
||||
|
||||
```javascript
|
||||
import { artifacts } from '@morten-olsen/mini-loader';
|
||||
|
||||
const run = async () => {
|
||||
artifacts.create('hello', 'world');
|
||||
};
|
||||
|
||||
run();
|
||||
```
|
||||
|
||||
Save this file as `script.js`.
|
||||
|
||||
#### A Note on Dependencies
|
||||
|
||||
In this script, we're using the `@morten-olsen/mini-loader` package, which might not be installed in your local environment. No worries though, as mini loader can automatically download necessary packages when preparing the script. Alternatively, for a more structured approach (especially if you're using TypeScript), you can initialize a Node.js project and install the dependencies for complete access to typings.
|
||||
|
||||
### Step 3: Run the Script Locally
|
||||
|
||||
To validate that your script is functioning correctly, execute it locally using the following command:
|
||||
|
||||
```bash
|
||||
mini-loader local run script.js -ai
|
||||
```
|
||||
|
||||
The `-ai` flag instructs the CLI to automatically download any referenced packages when bundling the script.
|
||||
|
||||
After running the command, you should see an output confirming that a new artifact named “hello” was created successfully.
|
||||
|
||||
### What's Next?
|
||||
|
||||
Congratulations on setting up and running your first script with mini loader! You're now ready to take the next step.
|
||||
|
||||
[Next: Setting Up the Server](./02-setup-server.md)
|
||||
61
docs/03-tutorial/02-setup-server.md
Normal file
61
docs/03-tutorial/02-setup-server.md
Normal file
@@ -0,0 +1,61 @@
|
||||
Certainly! Here's a revised version of your documentation page to make it
|
||||
|
||||
## Quick Start with mini loader using Docker
|
||||
|
||||
This guide will help you quickly set up and run a mini loader server using Docker. Follow these simple steps to deploy your server and start interacting with it using the [mini-loader CLI](./01-first-workload.md).
|
||||
|
||||
### Step 1: Deploy the mini loader Container
|
||||
|
||||
To begin, let's deploy the mini loader container. Run the following command in your terminal:
|
||||
|
||||
```bash
|
||||
docker run -p 4500:4500 -n mini-loader ghcr.io/morten-olsen/mini-loader:latest
|
||||
```
|
||||
|
||||
This command downloads the latest mini loader image and runs it, exposing port 4500.
|
||||
|
||||
### Step 2: Verify Server Health
|
||||
|
||||
Once the container is running, you can check the server's health:
|
||||
|
||||
```bash
|
||||
curl http://localhost:4500/health
|
||||
```
|
||||
|
||||
This command should return a response indicating that the server is running smoothly.
|
||||
|
||||
### Step 3: Authorize Using a Token
|
||||
|
||||
Initially, your server won't have any authorization setup. You can issue a token directly from the container to use for authorization:
|
||||
|
||||
```bash
|
||||
docker exec mini-loader mini-loader create-token
|
||||
```
|
||||
|
||||
This command will output a token. Keep this token handy as you'll need it for the next step.
|
||||
|
||||
### Step 4: Authorize the CLI Client
|
||||
|
||||
Now, authorize your CLI client to interact with the server:
|
||||
|
||||
```bash
|
||||
mini-loader auth login https://localhost:4500
|
||||
```
|
||||
|
||||
Enter the token when prompted.
|
||||
|
||||
### Step 5: Verify CLI Connection
|
||||
|
||||
Finally, verify that the CLI is properly configured to interact with your server:
|
||||
|
||||
```bash
|
||||
mini-loader loads ls
|
||||
```
|
||||
|
||||
This command lists all the loads currently on your server, confirming that the CLI is communicating successfully with the server.
|
||||
|
||||
### Ready to Go!
|
||||
|
||||
You've successfully deployed and configured your mini loader server using Docker! You're now ready to start interacting with the server.
|
||||
|
||||
[Next: Interacting with the Server](./03-interacting-with-server.md)
|
||||
70
docs/03-tutorial/03-interacting-with-server.md
Normal file
70
docs/03-tutorial/03-interacting-with-server.md
Normal file
@@ -0,0 +1,70 @@
|
||||
## Interacting with the server
|
||||
|
||||
The mini loader CLI is your primary tool for interacting with the server. This guide will walk you through the basic commands and their usage.
|
||||
|
||||
### Getting Help
|
||||
|
||||
For assistance with any command, append `--help` to it. For example, to see documentation for the `loads push` command:
|
||||
|
||||
```bash
|
||||
mini-loader loads push --help
|
||||
```
|
||||
|
||||
### Push a Load to the Server
|
||||
|
||||
To push a script to your server:
|
||||
|
||||
```bash
|
||||
mini-loader loads push <script-path> -i <id>
|
||||
```
|
||||
|
||||
- `<id>` is the identifier for your script. If omitted, an ID will be auto-generated.
|
||||
- To immediately trigger a run of the load after pushing, add the `-r` flag.
|
||||
|
||||
### Checking Run Status
|
||||
|
||||
To list the status of all runs:
|
||||
|
||||
```bash
|
||||
mini-loader runs ls
|
||||
```
|
||||
|
||||
- To filter runs by a specific load, add `-l <load-id>`.
|
||||
|
||||
### Retrieving Logs
|
||||
|
||||
To view logs:
|
||||
|
||||
```bash
|
||||
mini-loader logs ls
|
||||
```
|
||||
|
||||
- To view logs from a specific run, use `-r <run-id>`.
|
||||
- To view logs for a specific load, use `-l <load-id>`.
|
||||
|
||||
### Listing Artifacts
|
||||
|
||||
To list all artifacts:
|
||||
|
||||
```bash
|
||||
mini-loader artifacts ls
|
||||
```
|
||||
|
||||
- You can filter the artifacts by a specific run using `-r <run-id>`.
|
||||
- Alternatively, filter by load using `-l <load-id>`.
|
||||
|
||||
### Downloading an Artifact
|
||||
|
||||
To download a specific artifact:
|
||||
|
||||
```bash
|
||||
mini-loader artifacts pull <id> myfile.txt
|
||||
```
|
||||
|
||||
Replace `<id>` with the identifier of the artifact you wish to download.
|
||||
|
||||
### Ready for More?
|
||||
|
||||
You're now equipped to manage loads, runs, logs, and artifacts using the mini loader CLI. For advanced usage, such as managing secrets, proceed to the next section.
|
||||
|
||||
[Next: Managing Secrets](./04-managing-secrets.md)
|
||||
81
docs/03-tutorial/04-managing-secrets.md
Normal file
81
docs/03-tutorial/04-managing-secrets.md
Normal file
@@ -0,0 +1,81 @@
|
||||
## Managing Secrets
|
||||
|
||||
### Introduction
|
||||
|
||||
In many workflows, accessing sensitive data such as API tokens or credentials is essential. To handle this securely, you can use secrets management. This tutorial demonstrates how to manage secrets using the CLI and implement them in a simple Node.js workload.
|
||||
|
||||
### Creating Secrets with the CLI
|
||||
|
||||
To create a new secret, use the `mini-loader` CLI as follows:
|
||||
|
||||
```bash
|
||||
mini-loader secrets set <id>
|
||||
```
|
||||
|
||||
For example, to store a GitHub personal access token, you would use:
|
||||
|
||||
```bash
|
||||
mini-loader secrets set githubtoken
|
||||
```
|
||||
|
||||
Upon execution, you'll be prompted to enter your access token.
|
||||
|
||||
### Implementing Secrets in Your Workload
|
||||
|
||||
Next, let's create a Node.js script (`github.js`) that uses this token to fetch your GitHub username and saves it as an artifact.
|
||||
|
||||
1. **Create `github.js` File:**
|
||||
|
||||
```javascript
|
||||
import { secrets, artifacts } from '@morten-olsen/mini-loader';
|
||||
import { Octokit } from '@octokit/rest';
|
||||
|
||||
// Retrieve the secret
|
||||
const accessToken = secrets.get('githubtoken');
|
||||
|
||||
// Main async function to fetch and save GitHub username
|
||||
async function run() {
|
||||
const octokit = new Octokit({ auth: accessToken });
|
||||
const user = await octokit.users.getAuthenticated();
|
||||
await artifacts.create('user', JSON.stringify(user.data.login));
|
||||
}
|
||||
|
||||
// Execute the function
|
||||
run().catch(console.error);
|
||||
```
|
||||
|
||||
This script initializes the Octokit client with the access token, fetches the authenticated user's data, and then saves the username as an artifact.
|
||||
|
||||
2. **Run the Script:**
|
||||
|
||||
Execute your script with `mini-loader`:
|
||||
|
||||
```bash
|
||||
mini-loader loads push github.js -r -ai
|
||||
```
|
||||
|
||||
### Managing Local Secrets
|
||||
|
||||
If you're running the script locally, you can manage secrets either by using a `.secrets` file or setting an environment variable.
|
||||
|
||||
1. **Using a `.secrets` File:**
|
||||
|
||||
Create a file named `.secrets` and add your token:
|
||||
|
||||
```
|
||||
githubtoken=<your-token>
|
||||
```
|
||||
|
||||
2. **Using Environment Variables:**
|
||||
|
||||
Prefix your environment variable with `ML_S_` and run the script:
|
||||
|
||||
```bash
|
||||
ML_S_githubtoken=<your-token> mini-loader local run github.js -ai
|
||||
```
|
||||
|
||||
### Conclusion
|
||||
|
||||
By following these steps, you can securely manage and use secrets within your workloads, enhancing the security and integrity of your applications.
|
||||
|
||||
[Next: Creating an API](./05-creating-an-api.md)
|
||||
52
docs/03-tutorial/05-creating-an-api.md
Normal file
52
docs/03-tutorial/05-creating-an-api.md
Normal file
@@ -0,0 +1,52 @@
|
||||
## Creating an API Inside Your Workload
|
||||
|
||||
Workloads in mini loader can set up simple HTTP servers by connecting to a socket file, a feature supported by many JavaScript server libraries.
|
||||
|
||||
### Binding Your Workload to an HTTP Endpoint
|
||||
|
||||
To expose your workload as an HTTP server, specify the path parameter using the `getPath()` method provided by the `@morten-olsen/mini-loader` package. This method dynamically assigns a path for your API.
|
||||
|
||||
### Important Note
|
||||
|
||||
Please be aware that the gateway provided by mini loader isn't fully featured. As such, certain functionalities like streaming and WebSockets may not be supported.
|
||||
|
||||
### Example: Setting Up a Server with Fastify
|
||||
|
||||
Here's how you can create a simple API server using Fastify in TypeScript:
|
||||
|
||||
```typescript
|
||||
import { http } from '@morten-olsen/mini-loader';
|
||||
import fastify from 'fastify';
|
||||
|
||||
const server = fastify();
|
||||
|
||||
// Handling all requests and returning the requested URL
|
||||
server.all('*', async (req) => {
|
||||
return req.url;
|
||||
});
|
||||
|
||||
// Listening on the path provided by mini loader
|
||||
server.listen({
|
||||
path: http.getPath(),
|
||||
});
|
||||
```
|
||||
|
||||
With this setup, your server will respond to all incoming requests by returning the requested URL.
|
||||
|
||||
### Deploying Your Workload
|
||||
|
||||
Now, you can push and run your workload just like any other script:
|
||||
|
||||
```bash
|
||||
mini-loader loads push -r my-script.ts
|
||||
```
|
||||
|
||||
### Accessing Your Server
|
||||
|
||||
After pushing your workload, mini loader will display the run ID. You can use this ID to access your server. For example, to make a request to your server, you can use `curl`:
|
||||
|
||||
```bash
|
||||
curl http://localhost:4500/gateway/{your-run-id}
|
||||
```
|
||||
|
||||
Replace `{your-run-id}` with the actual run ID provided by mini loader.
|
||||
Reference in New Issue
Block a user