-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,720 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# simple-counter | ||
|
||
___ | ||
|
||
As a default, the counter is started with 0 and ends with 1000. These values can be changed by passing the `start` and `end` parameters. | ||
|
||
## Running | ||
|
||
> ❗ Remember to [setup transform-hub locally](https://docs.scramjet.org/transform-hub/installation) or use the [platform's environment](https://docs.scramjet.org/platform/get-started/) for the sequence deployment. | ||
Open the terminal and run the following commands: | ||
|
||
```bash | ||
# go to 'simple-counter-js' directory | ||
cd javascript/simple-counter-js | ||
|
||
# instal dependencies | ||
npm run build | ||
|
||
# deploy 'simple-counter-js' Sequence | ||
si seq deploy dist | ||
|
||
> 💡**NOTE:** Command `deploy` performs three actions at once: `pack`, `send` and `start` the Sequence. It is the same as if you would run those three commands separately: | ||
|
||
```bash | ||
si seq pack . -o simple-counter-js.tar.gz # compress 'simple-counter-js/' directory into file named 'simple-counter-js.tar.gz' | ||
si seq send simple-counter-js.tar.gz # send compressed Sequence to STH, this will output Sequence ID | ||
si seq start - # start the Sequence, this will output Instance ID | ||
``` | ||
|
||
## Output | ||
|
||
```bash | ||
{ x: 1 } | ||
{ x: 2 } | ||
{ x: 3 } | ||
{ x: 4 } | ||
{ x: 5 } | ||
{ x: 6 } | ||
{ x: 7 } | ||
{ x: 8 } | ||
{ x: 9 } | ||
{ x: 10 } | ||
{ x: 11 } | ||
{ x: 12 } | ||
{ x: 13 } | ||
{ x: 14 } | ||
... | ||
``` | ||
## Running the same Sequence but with some parameters | ||
```bash | ||
# go to 'simple-counter-js' directory | ||
cd samples/simple-counter-js | ||
# instal dependencies | ||
npm run build | ||
# deploy sequence with arguments | ||
si seq deploy dist --args [100, 200] | ||
## Output | ||
```bash | ||
# the counter will start counting at 100 and finish at 200 | ||
{ x: 101 } | ||
{ x: 102 } | ||
{ x: 103 } | ||
{ x: 104 } | ||
{ x: 105 } | ||
{ x: 106 } | ||
... | ||
{ x: 193 } | ||
{ x: 194 } | ||
{ x: 195 } | ||
{ x: 196 } | ||
{ x: 197 } | ||
{ x: 198 } | ||
{ x: 199 } | ||
``` |
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,26 @@ | ||
const http = require('http'); | ||
|
||
module.exports = [ | ||
{ requires: "inputs", contentType: "application/x-ndjson" }, | ||
async function (input) { | ||
let data = null; | ||
|
||
const server = http.createServer((req, res) => { | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.end(JSON.stringify({ greet: 'Hello World', data })); | ||
}); | ||
|
||
server.listen(9080, () => { | ||
console.log('Server is listening on port 9080'); | ||
}); | ||
|
||
for await (const message of input) { | ||
data = message; | ||
} | ||
|
||
await new Promise((res, rej) => { | ||
server.on('close', res); | ||
server.on('error', rej); | ||
}); | ||
} | ||
]; |
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,26 @@ | ||
{ | ||
"name": "@scramjet/simple-api", | ||
"version": "0.23.0", | ||
"main": "index.js", | ||
"author": "MichalCz", | ||
"license": "GPL-3.0", | ||
"description": "A simple Sequence, that exposes a hello world API with last message on topic.", | ||
"keywords": [ | ||
"easy", | ||
"streaming", | ||
"api", | ||
"Data Consumer" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/scramjetorg/platform-samples/tree/main/javascript/simple-api" | ||
}, | ||
"scripts": { | ||
"build": "mkdir -p dist/ && cp -rt dist/ *.js package.json && (cd dist && npm i --omit=dev)", | ||
"pack": "si seq pack ./dist/", | ||
"clean": "rm -rf ./dist ./*.tar.gz" | ||
}, | ||
"engines": { | ||
"node": ">=16" | ||
} | ||
} |
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,83 @@ | ||
# simple-counter | ||
|
||
___ | ||
|
||
As a default, the counter is started with 0 and ends with 1000. These values can be changed by passing the `start` and `end` parameters. | ||
|
||
## Running | ||
|
||
> ❗ Remember to [setup transform-hub locally](https://docs.scramjet.org/transform-hub/installation) or use the [platform's environment](https://docs.scramjet.org/platform/get-started/) for the sequence deployment. | ||
Open the terminal and run the following commands: | ||
|
||
```bash | ||
# go to 'simple-counter-js' directory | ||
cd javascript/simple-counter-js | ||
|
||
# instal dependencies | ||
npm run build | ||
|
||
# deploy 'simple-counter-js' Sequence | ||
si seq deploy dist | ||
|
||
> 💡**NOTE:** Command `deploy` performs three actions at once: `pack`, `send` and `start` the Sequence. It is the same as if you would run those three commands separately: | ||
|
||
```bash | ||
si seq pack . -o simple-counter-js.tar.gz # compress 'simple-counter-js/' directory into file named 'simple-counter-js.tar.gz' | ||
si seq send simple-counter-js.tar.gz # send compressed Sequence to STH, this will output Sequence ID | ||
si seq start - # start the Sequence, this will output Instance ID | ||
``` | ||
|
||
## Output | ||
|
||
```bash | ||
{ x: 1 } | ||
{ x: 2 } | ||
{ x: 3 } | ||
{ x: 4 } | ||
{ x: 5 } | ||
{ x: 6 } | ||
{ x: 7 } | ||
{ x: 8 } | ||
{ x: 9 } | ||
{ x: 10 } | ||
{ x: 11 } | ||
{ x: 12 } | ||
{ x: 13 } | ||
{ x: 14 } | ||
... | ||
``` | ||
## Running the same Sequence but with some parameters | ||
```bash | ||
# go to 'simple-counter-js' directory | ||
cd samples/simple-counter-js | ||
# instal dependencies | ||
npm run build | ||
# deploy sequence with arguments | ||
si seq deploy dist --args [100, 200] | ||
## Output | ||
```bash | ||
# the counter will start counting at 100 and finish at 200 | ||
{ x: 101 } | ||
{ x: 102 } | ||
{ x: 103 } | ||
{ x: 104 } | ||
{ x: 105 } | ||
{ x: 106 } | ||
... | ||
{ x: 193 } | ||
{ x: 194 } | ||
{ x: 195 } | ||
{ x: 196 } | ||
{ x: 197 } | ||
{ x: 198 } | ||
{ x: 199 } | ||
``` |
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,62 @@ | ||
const http = require('http'); | ||
const qs = require('querystring'); | ||
const sqlite3 = require('sqlite3'); | ||
const path = require('path'); | ||
|
||
// Assuming the database structure is defined in prime.js | ||
const dbPath = path.join(__dirname, 'products.db');; // Replace with the actual path to the database file | ||
module.exports = async function (input) { | ||
const db = new sqlite3.Database(dbPath); | ||
|
||
// Function to query the database and extract the product list | ||
async function extractProductList(query) { | ||
return new Promise((res, rej) => { | ||
db.all(query, (err, rows) => { | ||
if (err) { | ||
rej(err); | ||
} else { | ||
res(rows); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
const server = http.createServer((req, res) => { | ||
res.setHeader('Content-Type', 'application/json'); | ||
|
||
try { | ||
const { category } = qs.parse(req.url.split('?')[1]); | ||
|
||
let query; | ||
if (category) { | ||
query = `SELECT sku, msrp, name FROM products WHERE category = '${category}'`; | ||
} else { | ||
query = `SELECT sku, msrp, name FROM products`; | ||
} | ||
extractProductList(query) | ||
.then((rows) => { | ||
res.statusCode = 200; | ||
res.end(JSON.stringify(rows)); | ||
}) | ||
.catch((error) => { | ||
res.statusCode = 500; | ||
res.end(JSON.stringify({ error: 'Query Error', message: error.message })); | ||
}); | ||
|
||
} catch (error) { | ||
res.statusCode = 500; | ||
res.end(JSON.stringify({ error: 'Internal Server Error', message: error.message })); | ||
} | ||
}); | ||
|
||
server.listen(9080, () => { | ||
console.log('Server is listening on port 9080'); | ||
}); | ||
|
||
await new Promise((res, rej) => { | ||
server.on('close', res); | ||
server.on('error', rej); | ||
}); | ||
|
||
db.close(); | ||
}; |
Oops, something went wrong.