Compare commits
10 Commits
adfc83c78f
...
b5c164236e
| Author | SHA1 | Date | |
|---|---|---|---|
| b5c164236e | |||
| 34703485ef | |||
| 04684bba48 | |||
| fec97eb018 | |||
| f1ac1c6af0 | |||
| 5de34b68d8 | |||
| e16dff1e4e | |||
| 0f3a03c67e | |||
| 48fa5c8d3b | |||
| d5d7f17339 |
@ -1,4 +1,4 @@
|
||||
name: Build and Push Docker Image
|
||||
name: Build and Push Backend Docker Image
|
||||
|
||||
on:
|
||||
push:
|
||||
@ -9,33 +9,45 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Checkout repository with submodules
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
token: ${{ secrets.ACCESS_TOKEN }}
|
||||
|
||||
- name: Initialize submodules
|
||||
run: |
|
||||
git submodule sync --recursive
|
||||
git submodule update --init --recursive
|
||||
git submodule status
|
||||
ls -la src/modules/db
|
||||
ls -la src/modules/redis
|
||||
|
||||
- name: Login to Registry
|
||||
uses: docker/login-action@v2
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: gitea.spilum.net
|
||||
username: ${{ secrets.REGISTRY_USER }}
|
||||
password: ${{ secrets.REGISTRY_PASSWORD }}
|
||||
|
||||
- name: Build Docker Image
|
||||
env:
|
||||
BRANCH_NAME: ${{ github.ref_name }}
|
||||
SHORT_HASH: ${{ github.sha }}
|
||||
run: |
|
||||
# Build the image with the commit hash tag
|
||||
docker build --build-arg BUILD_IDENTIFIER=${SHORT_HASH:0:5} -t gitea.spilum.net/spilum.net/mtf-backend:${BRANCH_NAME}-${SHORT_HASH:0:5} .
|
||||
- name: Set SHORT_SHA
|
||||
id: vars
|
||||
run: echo "short_sha=${GITHUB_SHA::5}" >> "$GITHUB_OUTPUT"
|
||||
|
||||
# Tag the same image as "latest"
|
||||
docker tag gitea.spilum.net/spilum.net/mtf-backend:${BRANCH_NAME}-${SHORT_HASH:0:5} gitea.spilum.net/spilum.net/mtf-backend:${BRANCH_NAME}-latest
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Push Docker Image
|
||||
env:
|
||||
BRANCH_NAME: ${{ github.ref_name }}
|
||||
SHORT_HASH: ${{ github.sha }}
|
||||
run: |
|
||||
docker push gitea.spilum.net/spilum.net/mtf-backend:${BRANCH_NAME}-${SHORT_HASH:0:5}
|
||||
docker push gitea.spilum.net/spilum.net/mtf-backend:${BRANCH_NAME}-latest
|
||||
- name: Build and Push Backend (multi-arch)
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
platforms: linux/amd64,linux/arm64
|
||||
build-args: |
|
||||
BUILD_IDENTIFIER=${{ steps.vars.outputs.short_sha }}
|
||||
tags: |
|
||||
gitea.spilum.net/spilum.net/mtf-backend:${{ steps.vars.outputs.short_sha }}
|
||||
gitea.spilum.net/spilum.net/mtf-backend:latest
|
||||
|
||||
- name: Log out from registry
|
||||
if: always()
|
||||
|
||||
6
.gitmodules
vendored
Normal file
6
.gitmodules
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
[submodule "src/modules/db"]
|
||||
path = src/modules/db
|
||||
url = https://gitea.spilum.net/Spilum.Net/mtf-module-db
|
||||
[submodule "src/modules/redis"]
|
||||
path = src/modules/redis
|
||||
url = https://gitea.spilum.net/Spilum.Net/mtf-module-redis
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "spilum-backend",
|
||||
"name": "mtf-backend",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "spilum-backend",
|
||||
"name": "mtf-backend",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
module.exports = {
|
||||
REDIS_HOST: process.env.REDIS_HOST,
|
||||
REDIS_PORT: process.env.REDIS_PORT
|
||||
}
|
||||
@ -1,5 +1,4 @@
|
||||
const models = require("../models");
|
||||
const db = models.db;
|
||||
const {db} = require('../modules/db');
|
||||
const config = require("../config/auth.config");
|
||||
const User = db.user;
|
||||
const Role = db.role;
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
const models = require("../models");
|
||||
const db = models.db;
|
||||
const {db} = require('../modules/db');
|
||||
const {sequelize, Sequelize} = db;
|
||||
const {literal} = Sequelize;
|
||||
const Bill = db.bill;
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
const models = require("../models");
|
||||
const db = models.db;
|
||||
const redis = models.redis;
|
||||
const {db} = require('../modules/db');
|
||||
const {sequelize, Sequelize} = db;
|
||||
const Intent = db.intent;
|
||||
const config = require("../config");
|
||||
|
||||
const {redis} = require('../modules/redis');
|
||||
|
||||
// Get all intents
|
||||
exports.getAllIntents = async (req, res) => {
|
||||
try {
|
||||
@ -18,7 +18,21 @@ exports.getAllIntents = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
// Get intent by name
|
||||
exports.getIntentByName = async (req, res) => {
|
||||
try {
|
||||
const intent = await Intent.findOne({where: {name: req.params.name}});
|
||||
|
||||
return res.status(200).send(intent);
|
||||
} catch (err) {
|
||||
console.error("getIntentByName error:", err);
|
||||
|
||||
return res.status(500).send({message: err.message || "Internal server error."});
|
||||
}
|
||||
}
|
||||
|
||||
// Get intent command (for now by description)
|
||||
// [UNUSED FOR NOW] Doesn't work with the current intent model
|
||||
exports.getIntentCommand = async (req, res) => {
|
||||
try {
|
||||
const {text, device_id} = req.body;
|
||||
@ -31,8 +45,6 @@ exports.getIntentCommand = async (req, res) => {
|
||||
|
||||
console.log(`\n[Backend Intents] Processing intent.`);
|
||||
|
||||
await redis.client.connect().catch(console.error)
|
||||
|
||||
const intent = await Intent.findOne({ where: { description: req.body.text } });
|
||||
|
||||
if (intent) {
|
||||
@ -47,8 +59,6 @@ exports.getIntentCommand = async (req, res) => {
|
||||
}
|
||||
);
|
||||
|
||||
await redis.client.quit();
|
||||
|
||||
console.log(`Intent matched and published to stream: ${commandId}`);
|
||||
|
||||
return res.json({
|
||||
@ -68,8 +78,6 @@ exports.getIntentCommand = async (req, res) => {
|
||||
}
|
||||
);
|
||||
|
||||
await redis.client.quit();
|
||||
|
||||
console.log(`No intent match, error published: ${errorId}`);
|
||||
|
||||
return res.status(400).json({
|
||||
@ -81,8 +89,6 @@ exports.getIntentCommand = async (req, res) => {
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
await redis.client.quit();
|
||||
|
||||
console.error("getIntentCommand error:", err);
|
||||
|
||||
return res.status(500).send({message: err.message || "Internal server error."});
|
||||
@ -92,38 +98,22 @@ exports.getIntentCommand = async (req, res) => {
|
||||
// Create intent
|
||||
exports.createIntent = async (req, res) => {
|
||||
try {
|
||||
const {command, description, type, device_id} = req.body;
|
||||
const {name, examples} = req.body;
|
||||
|
||||
console.log(`\n[Backend Intents] Creating intent.`)
|
||||
|
||||
await redis.client.connect().catch(console.error);
|
||||
|
||||
if (!command || !description || !type || !device_id) {
|
||||
return res.status(400).send({message: "Missing required fields: command, description, type, device_id"});
|
||||
if (!name || !examples) {
|
||||
return res.status(400).send({message: "Missing required fields: name, examples"});
|
||||
}
|
||||
|
||||
const intent = await Intent.create({
|
||||
command,
|
||||
description,
|
||||
type
|
||||
name,
|
||||
examples
|
||||
});
|
||||
|
||||
await redis.client.connect().catch(console.error);
|
||||
|
||||
const commandId = await redis.client.xAdd(
|
||||
'communication',
|
||||
'*',
|
||||
{
|
||||
device_id: device_id,
|
||||
command: "CREATE_INTENT",
|
||||
processed_at: new Date().toISOString(),
|
||||
source: config.DEVICE_ID,
|
||||
}
|
||||
);
|
||||
|
||||
await redis.client.quit();
|
||||
|
||||
return res.status(201).send({message: "Intent created successfully.", intent, commandId});
|
||||
return res.status(201).send({message: "Intent created successfully.", intent});
|
||||
} catch (err) {
|
||||
console.error("createIntent error:", err);
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
const jwt = require("jsonwebtoken");
|
||||
const config = require("../config/auth.config");
|
||||
const db = require("../models");
|
||||
const {db} = require('../modules/db');
|
||||
const User = db.user;
|
||||
|
||||
verifyToken = (req, res, next) => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
const db = require('../models');
|
||||
const {db} = require('../modules/db');
|
||||
const ROLES = db.ROLES;
|
||||
const User = db.user;
|
||||
|
||||
|
||||
@ -1,36 +0,0 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Bill = sequelize.define("bills", {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
allowNull: false
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
amount: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false
|
||||
},
|
||||
due: {
|
||||
type: DataTypes.DATEONLY,
|
||||
allowNull: false
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true
|
||||
},
|
||||
shared: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: false
|
||||
}
|
||||
}, {
|
||||
tableName: "bills",
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
return Bill;
|
||||
};
|
||||
@ -1,72 +0,0 @@
|
||||
const config = require("../config");
|
||||
const dbConfig = require("../config/db.config");
|
||||
const redisConfig = require("../config/redis.config");
|
||||
|
||||
const Sequelize = require("sequelize");
|
||||
const sequelize = new Sequelize(
|
||||
dbConfig.DB,
|
||||
dbConfig.USER,
|
||||
dbConfig.PASSWORD,
|
||||
{
|
||||
host: dbConfig.HOST,
|
||||
dialect: dbConfig.dialect,
|
||||
port: dbConfig.PORT,
|
||||
pool: {
|
||||
max: dbConfig.pool.max,
|
||||
min: dbConfig.pool.min,
|
||||
acquire: dbConfig.pool.acquire,
|
||||
idle: dbConfig.pool.idle
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// SQL Connection
|
||||
const db = {};
|
||||
|
||||
db.Sequelize = Sequelize;
|
||||
db.sequelize = sequelize;
|
||||
|
||||
db.user = require('../models/user.model')(sequelize, Sequelize);
|
||||
db.role = require('../models/role.model')(sequelize, Sequelize);
|
||||
db.bill = require('../models/bill.model')(sequelize, Sequelize);
|
||||
db.intent = require('../models/intent.model')(sequelize, Sequelize);
|
||||
|
||||
db.role.belongsToMany(db.user, {
|
||||
through: "user_roles"
|
||||
});
|
||||
|
||||
db.user.belongsToMany(db.role, {
|
||||
through: "user_roles"
|
||||
});
|
||||
|
||||
db.ROLES = ["user", "admin"];
|
||||
|
||||
// Redis Connection
|
||||
const Redis = require('redis');
|
||||
const redis = {};
|
||||
|
||||
redis.client = Redis.createClient({
|
||||
url: `redis://default@${redisConfig.REDIS_HOST}:${redisConfig.REDIS_PORT}`
|
||||
});
|
||||
|
||||
redis.ensureGroup = async (stream, group) => {
|
||||
await redis.client.connect().catch(console.error);
|
||||
|
||||
try {
|
||||
await redis.client.xGroupCreate(stream, group, "0", {MKSTREAM: true});
|
||||
|
||||
console.log(`Created consumer group: ${group} (${stream})`);
|
||||
} catch (err) {
|
||||
if (err.message.includes('BUSYGROUP')) {
|
||||
console.log(`Consumer group already exists: ${group} (${stream})`);
|
||||
} else {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
await redis.client.quit();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
db,
|
||||
redis
|
||||
};
|
||||
@ -1,23 +0,0 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Intent = sequelize.define("intents", {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
allowNull: false
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
examples: {
|
||||
type: DataTypes.ARRAY(DataTypes.STRING),
|
||||
allowNull: true
|
||||
}
|
||||
}, {
|
||||
tableName: "intents",
|
||||
timestamps: false
|
||||
});
|
||||
|
||||
return Intent;
|
||||
};
|
||||
@ -1,19 +0,0 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Role = sequelize.define("roles", {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
allowNull: false,
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
tableName: "roles",
|
||||
timestamps: true
|
||||
});
|
||||
|
||||
return Role;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
module.exports = (sequelize, Sequelize) => {
|
||||
const User = sequelize.define("users", {
|
||||
username: {
|
||||
type: Sequelize.STRING
|
||||
},
|
||||
email: {
|
||||
type: Sequelize.STRING
|
||||
},
|
||||
password: {
|
||||
type: Sequelize.STRING
|
||||
}
|
||||
});
|
||||
|
||||
return User;
|
||||
}
|
||||
1
src/modules/db
Submodule
1
src/modules/db
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 94a4550d0fb1b6e91dcbf8af78bb701720ad9bc4
|
||||
1
src/modules/redis
Submodule
1
src/modules/redis
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit e41a75e7436a059ab963c5db7b62ca524cacc4e5
|
||||
@ -15,10 +15,14 @@ const controller = require("../controllers/intent.controller");
|
||||
// Get all intents
|
||||
router.get("/", /*[authJwt.verifyToken, authJwt.isAdmin],*/ controller.getAllIntents);
|
||||
|
||||
// Get intent command (for now by description)
|
||||
router.post("/getintentcommand", controller.getIntentCommand);
|
||||
// Get intent by name
|
||||
router.get("/name/:name", controller.getIntentByName);
|
||||
|
||||
// Get intent command by description
|
||||
// [UNUSED FOR NOW]
|
||||
//router.post("/getintentcommand", controller.getIntentCommand);
|
||||
|
||||
// Create intent
|
||||
router.post("/createintent", controller.createIntent);
|
||||
router.post("/", controller.createIntent);
|
||||
|
||||
module.exports = router;
|
||||
@ -8,17 +8,19 @@ const cors = require("cors");
|
||||
const helmet = require("helmet");
|
||||
const rateLimit = require("express-rate-limit");
|
||||
const morgan = require("morgan");
|
||||
const models = require('./models');
|
||||
const db = models.db;
|
||||
const redis = models.redis;
|
||||
|
||||
const {db} = require('./modules/db');
|
||||
const {sequelize, Sequelize} = db;
|
||||
const Role = db.role;
|
||||
|
||||
const {redis} = require('./modules/redis');
|
||||
|
||||
const authConfig = require("./config/auth.config");
|
||||
const config = require("./config");
|
||||
|
||||
const createApp = async () => {
|
||||
await redis.ensureGroup("commands", `${config.DEVICE_ID}-cg`);
|
||||
await redis.connect();
|
||||
await redis.ensureGroup("commands", `${config.DEVICE_ID}`);
|
||||
|
||||
const app = express();
|
||||
|
||||
@ -58,7 +60,7 @@ const createApp = async () => {
|
||||
app.use("/auth", require("./routes/auth.routes"));
|
||||
app.use("/user", require("./routes/user.routes"));
|
||||
app.use("/bills", require("./routes/bill.routes"));
|
||||
app.use("/fallback/intent", require("./routes/intent.routes"));
|
||||
app.use("/intent", require("./routes/intent.routes"));
|
||||
|
||||
// 404 handler
|
||||
app.use((req, res) => res.status(404).send({message: "Not Found"}));
|
||||
@ -95,7 +97,7 @@ const start = async () => {
|
||||
|
||||
await initRoles();
|
||||
|
||||
const app = createApp();
|
||||
const app = await createApp();
|
||||
|
||||
// Set port and listen
|
||||
const PORT = parseInt(config.PORT, 10) || 5000;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user