Skip to content

Commit

Permalink
♻️ Final provisioner route.
Browse files Browse the repository at this point in the history
  • Loading branch information
Danieloni1 committed Feb 19, 2024
1 parent 648f41d commit 5a06b98
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 22 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
config.yml
lib/
node_modules/
Expand Down
2 changes: 1 addition & 1 deletion config.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ listeners:

#bot:
# # (Optional) Define profile information for the bot user
# displayname: Hookshot Bot
# displayname: Bridge Bot
# avatar: mxc://half-shot.uk/2876e89ccade4cb615e210c458e2a7a6883fe17d

#serviceBots:
Expand Down
2 changes: 1 addition & 1 deletion helm/hookshot/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ hookshot:
# bot:
# (Optional) Define profile information for the bot user
#
# displayname: Hookshot Bot
# displayname: Bridge Bot
# avatar: mxc://half-shot.uk/2876e89ccade4cb615e210c458e2a7a6883fe17d
# serviceBots:
# (Optional) Define additional bot users for specific services
Expand Down
6 changes: 3 additions & 3 deletions src/Bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ export class Bridge {
const routers = [];
if (this.config.jira) {
routers.push({
route: "/bridge/jira",
route: "/provisioner/jira",
router: new JiraProvisionerRouter(this.config.jira, this.tokenStore).getRouter(),
});
this.connectionManager.registerProvisioningConnection(JiraProjectConnection);
}
if (this.config.github && this.github) {
routers.push({
route: "/bridge/github",
route: "/provisioner/github",
router: new GitHubProvisionerRouter(this.config.github, this.tokenStore, this.github).getRouter(),
});
this.connectionManager.registerProvisioningConnection(GitHubRepoConnection);
Expand All @@ -162,7 +162,7 @@ export class Bridge {
if (this.config.mqtt?.enabled) {
// Register the internal backend route for get all live connections
routers.push({
route: "/bridge/mqtt",
route: "/provisioner/mqtt",
router: new MqttProvisionerRouter(this.config.provisioning).getRouter(),
});
this.connectionManager.registerProvisioningConnection(MqttConnection);
Expand Down
2 changes: 1 addition & 1 deletion src/config/Defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const DefaultConfigRoot: BridgeConfigRoot = {
},
},
bot: {
displayname: "Hookshot Bot",
displayname: "Bridge Bot",
avatar: "mxc://half-shot.uk/2876e89ccade4cb615e210c458e2a7a6883fe17d"
},
serviceBots: [
Expand Down
6 changes: 4 additions & 2 deletions src/mqtt/Router.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { BridgeConfigMqtt, BridgeConfigProvisioning } from "../config/Config";
import { BridgeConfigProvisioning } from "../config/Config";
import { Router, Request, Response, NextFunction } from "express";
import { Logger } from "matrix-appservice-bridge";
import { ApiError, ErrCode } from "../api";
import { selectAllConnections } from "../db/generated/queries_sql";
import { executeSchema } from "./mqttConnectionManager";
import { Pool } from "pg";

const log = new Logger("MqttRouter");
Expand All @@ -15,14 +16,15 @@ export interface AdaptedMqttLiveConnection {
spaces_ids: string[];
}


export class MqttProvisionerRouter {
private dbCli: Pool;

constructor(
private readonly provConfig: BridgeConfigProvisioning) {
this.provConfig = provConfig;
this.dbCli = new Pool({ connectionString: process.env.DATABASE_URL });
log.info('mqtt_router: initialized database connection');
executeSchema(this.dbCli);
}

public getRouter() {
Expand Down
7 changes: 4 additions & 3 deletions src/mqtt/mqttConnectionManager.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Logger } from "matrix-appservice-bridge";
import { BridgeConfigMqtt } from "../config/Config";
import { Pool } from "pg";
import { SelectSpaceConnectionsArgs, selectSpaceConnections, selectConnection, insertConnection, updateConnectionAssociatedSpaces, deleteSpaceFromConnectionAndPrune, selectAllConnections } from "../db/generated/queries_sql";
import { selectConnection, insertConnection, updateConnectionAssociatedSpaces, deleteSpaceFromConnectionAndPrune } from "../db/generated/queries_sql";
import axios from "axios";
import "dotenv/config";
import { MqttConnectionState } from "../Connections/MqttConnection";
import * as fs from 'fs';

const log = new Logger("MqttConnectionsManager");

function executeSchema(dbCli: Pool) {
export async function executeSchema(dbCli: Pool) {
try {
const schemaSql = fs.readFileSync('./src/db/schema.sql', 'utf8');
dbCli.query(schemaSql);
await dbCli.query(schemaSql);
log.info('MQTT table created successfully');
} catch (err) {
log.info('Error creating MQTT table: ', err);
Expand All @@ -24,6 +24,7 @@ export class MqttConnectionsManager {

constructor() {
this.dbCli = new Pool({ connectionString: process.env.DATABASE_URL });
log.info('MqttConnectionsManager: initialized database connection');
executeSchema(this.dbCli);
}

Expand Down
20 changes: 10 additions & 10 deletions src/provisioning/provisioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Provisioner {
private readonly botUsersManager: BotUsersManager,
private readonly as: Appservice,
additionalRoutes: {route: string, router: Router}[]) {
this.expressRouter.use("/bridge", (req, _res, next) => {
this.expressRouter.use("/provisioner", (req, _res, next) => {
Metrics.provisioningHttpRequest.inc({path: req.path, method: req.method});
next();
});
Expand All @@ -42,44 +42,44 @@ export class Provisioner {
maxAge: 86400, // 24 hours
};
this.expressRouter.use(cors(corsOptions));
this.expressRouter.get("/bridge/health", this.getHealth);
this.expressRouter.use("/bridge", this.checkAuth.bind(this));
this.expressRouter.get("/provisioner/health", this.getHealth);
this.expressRouter.use("/provisioner", this.checkAuth.bind(this));
this.expressRouter.use(express.json());
this.expressRouter.get(
"/bridge/connectiontypes",
"/provisioner/connectiontypes",
this.getConnectionTypes.bind(this),
);
this.expressRouter.use("/bridge", this.checkUserId.bind(this));
this.expressRouter.use("/provisioner", this.checkUserId.bind(this));
additionalRoutes.forEach(route => {
this.expressRouter.use(route.route, route.router);
});
// Room Routes
this.expressRouter.get<{roomId: string}, unknown, unknown, {userId: string}>(
"/bridge/:roomId/connections",
"/provisioner/:roomId/connections",
this.checkRoomId.bind(this),
(...args) => this.checkUserPermission("read", ...args),
this.getConnections.bind(this),
);
this.expressRouter.get<{roomId: string, connectionId: string}, unknown, unknown, {userId: string}>(
"/bridge/:roomId/connections/:connectionId",
"/provisioner/:roomId/connections/:connectionId",
this.checkRoomId.bind(this),
(...args) => this.checkUserPermission("read", ...args),
this.getConnection.bind(this),
);
this.expressRouter.put<{roomId: string, type: string}, unknown, Record<string, unknown>, {userId: string}>(
"/bridge/:roomId/connections/:type",
"/provisioner/:roomId/connections/:type",
this.checkRoomId.bind(this),
(...args) => this.checkUserPermission("write", ...args),
this.putConnection.bind(this),
);
this.expressRouter.patch<{roomId: string, connectionId: string}, unknown, Record<string, unknown>, {userId: string}>(
"/bridge/:roomId/connections/:connectionId",
"/provisioner/:roomId/connections/:connectionId",
this.checkRoomId.bind(this),
(...args) => this.checkUserPermission("write", ...args),
this.patchConnection.bind(this),
);
this.expressRouter.delete<{roomId: string, connectionId: string}, unknown, unknown, {userId: string}>(
"/bridge/:roomId/connections/:connectionId",
"/provisioner/:roomId/connections/:connectionId",
this.checkRoomId.bind(this),
(...args) => this.checkUserPermission("write", ...args),
this.deleteConnection.bind(this),
Expand Down

0 comments on commit 5a06b98

Please sign in to comment.