Skip to content

Commit

Permalink
Merge pull request #8 from vinayakjaas/scrom_cloud_testing
Browse files Browse the repository at this point in the history
Scrom cloud testing
  • Loading branch information
rks-031 authored Jul 15, 2024
2 parents 6b24aa3 + 1585765 commit 5ca929f
Show file tree
Hide file tree
Showing 10 changed files with 1,430 additions and 0 deletions.
24 changes: 24 additions & 0 deletions ScromCloud_Testing/scrom_cloud_testing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
76 changes: 76 additions & 0 deletions ScromCloud_Testing/scrom_cloud_testing/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const express = require('express');
const bodyParser = require('body-parser');
const TinCan = require('tincanjs');
require('dotenv').config();

const app = express();
const port = process.env.PORT || 5000;

app.use(bodyParser.json());

app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
next();
});

let lrs;
try {
lrs = new TinCan.LRS({
endpoint: process.env.LRS_ENDPOINT,
username: process.env.LRS_USERNAME,
password: process.env.LRS_PASSWORD,
allowFail: false
});
} catch (ex) {
console.log("Failed to setup LRS object: ", ex);
process.exit(1);
}

app.post('/api/statements', (req, res) => {
const statement = new TinCan.Statement(req.body);

lrs.saveStatement(statement, {
callback: function (err, xhr) {
if (err !== null) {
console.log("Failed to save statement: ", err);
res.status(500).send("Failed to save statement");
} else {
console.log("Statement saved successfully");
res.status(200).send("Statement saved successfully");
}
}
});
});

app.get('/api/statements', (req, res) => {
const params = {
limit: req.query.limit || 10,
agent: req.query.agent,
verb: req.query.verb,
activity: req.query.activity
};

lrs.queryStatements({
params: params,
callback: function (err, response) {
if (err !== null) {
console.log("Failed to fetch statements: ", err);
res.status(500).send("Failed to fetch statements");
return;
}
if (response !== null) {
console.log("Statements fetched successfully");
res.status(200).json(response.statements);
} else {
res.status(404).send("No statements found");
}
}
});
});

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});

Loading

0 comments on commit 5ca929f

Please sign in to comment.