Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server sent events module #176

Merged
merged 46 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8e2c858
feat: add option to get an auth token
jkoenig134 Jun 3, 2024
299718b
feat: implement the sse module
jkoenig134 Jun 3, 2024
b4d7aeb
feat: add config
jkoenig134 Jun 3, 2024
b6c6aa1
feat: add the eventsource library
jkoenig134 Jun 3, 2024
65205c4
chore: simplify connection strings
jkoenig134 Jun 3, 2024
7711fa3
chore: deprecation comment
jkoenig134 Jun 3, 2024
85ce3f4
chore: more precise comments
jkoenig134 Jun 3, 2024
1432ea3
chore: more developer options
jkoenig134 Jun 3, 2024
2603973
chore: add dev sse server
jkoenig134 Jun 3, 2024
d3c12f8
Merge branch 'main' into feature/server-sent-events
jkoenig134 Jun 4, 2024
30f9790
Merge branch 'main' into feature/server-sent-events
jkoenig134 Jun 4, 2024
4cbc62e
Merge branch 'main' into feature/server-sent-events
jkoenig134 Jun 6, 2024
da708a0
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jun 10, 2024
6fd8e23
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jun 10, 2024
f3faee3
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jun 19, 2024
5d06dd0
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jun 19, 2024
3a860f7
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jun 19, 2024
053b53e
fix: correct routes
jkoenig134 Jun 25, 2024
0db70fa
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jun 25, 2024
abca5a3
chore: move syncer init down
jkoenig134 Jun 26, 2024
6b042dd
feat: enable sse for the connector
jkoenig134 Jun 26, 2024
817cc81
feat: update local backbone connection
jkoenig134 Jun 26, 2024
fbc4129
chore: add script to establish a relationship
jkoenig134 Jun 26, 2024
4f20224
feat: run sync
jkoenig134 Jun 26, 2024
ef09196
chore: move listener
jkoenig134 Jun 26, 2024
4b97ddf
feat: add override for local development
jkoenig134 Jun 26, 2024
ccf0126
chore: update lockfile
jkoenig134 Jun 26, 2024
46fc29d
fix: close infrastructure in dev-mode if runtime was not able to star…
jkoenig134 Jun 26, 2024
743f102
chore: rename variable
jkoenig134 Jun 26, 2024
3a28ce8
feat: restructure / resolve todos
jkoenig134 Jun 26, 2024
173f93e
chore: wording
jkoenig134 Jun 27, 2024
a0fb786
chore: remove checked in stuff
jkoenig134 Jun 27, 2024
26cc591
chore: add task for clearing only connectors
jkoenig134 Jun 27, 2024
82718b4
refactor: move baseUrlOverride to config
jkoenig134 Jun 27, 2024
b6714e4
chore: bump backbone
jkoenig134 Jun 27, 2024
fd3df64
chore: downgrade
jkoenig134 Jun 27, 2024
66b3e9d
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jul 1, 2024
c39b69c
Merge branch 'main' into feature/server-sent-events
jkoenig134 Jul 3, 2024
9a98774
chore: rm TODO
jkoenig134 Jul 3, 2024
42d03ea
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jul 9, 2024
12b3741
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jul 9, 2024
3d75c00
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jul 9, 2024
3dcc3d5
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jul 9, 2024
2639465
Merge branch 'main' into feature/server-sent-events
mergify[bot] Jul 9, 2024
e1a0854
Merge branch 'main' into feature/server-sent-events
jkoenig134 Jul 16, 2024
4c38ad1
chore: remove local sse impl
jkoenig134 Jul 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dev/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ DATABASE_NAME_PREFIX=
# optional - defaults to 'false'
# enable or disable the sync module
SYNC_ENABLED=

# optional - defaults to 'false'
# enable or disable the sse module
modules__sse__enabled=
5 changes: 4 additions & 1 deletion .dev/.env.local
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# required - the information below is used to connect to the backbone
transportLibrary__baseUrl="http://localhost:8090"
transportLibrary__baseUrl="http://host.docker.internal:8090"
transportLibrary__platformClientId="test"
transportLibrary__platformClientSecret="test"

modules__sse__enabled=true
modules__sse__baseUrlOverride="http://host.docker.internal:8092"
4 changes: 4 additions & 0 deletions .dev/appsettings.override.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
"Providers": {
"Dummy": {
"Enabled": true
},
"Sse": {
"Enabled": true,
"SseServerBaseAddress": "http://sse-server:8080"
}
}
}
Expand Down
21 changes: 15 additions & 6 deletions .dev/compose.backbone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ services:
condition: service_started
database-migrator:
condition: service_completed_successfully
sse-server:
condition: service_started
configs:
- source: Config
target: app/appsettings.override.json
Expand All @@ -36,6 +38,19 @@ services:
- source: Config
target: app/appsettings.override.json

sse-server:
image: ghcr.io/nmshd/backbone-sse-server:${BACKBONE_VERSION}
container_name: sse-server
hostname: sse-server
ports:
- "8092:8080"
depends_on:
database:
condition: service_started
configs:
- source: Config
target: app/appsettings.override.json

database-migrator:
container_name: database-migrator-test
image: ghcr.io/nmshd/backbone-database-migrator:${BACKBONE_VERSION}
Expand All @@ -59,8 +74,6 @@ services:
environment:
- POSTGRES_PASSWORD=Passw0rd
- POSTGRES_DB=enmeshed
ports:
- 5432:5432
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 5s
Expand All @@ -72,15 +85,11 @@ services:
hostname: azurite
image: mcr.microsoft.com/azure-storage/azurite
command: azurite -d /data/debug.log -l /data --blobHost "0.0.0.0" --queueHost "0.0.0.0"
ports:
- "10000:10000"

rabbitmq:
container_name: bkb-rabbitmq
hostname: rabbitmq
image: rabbitmq:3.12.10-management-alpine
ports:
- "5672:5672"

### seeds ###

Expand Down
4 changes: 4 additions & 0 deletions .dev/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ services:
- transportLibrary__baseUrl
- transportLibrary__platformClientId
- transportLibrary__platformClientSecret
- modules__sse__enabled=${modules__sse__enabled:-false}
- modules__sse__baseUrlOverride
volumes:
- ..:/usr/app
- ./config.json:/config.json:ro
Expand All @@ -42,6 +44,8 @@ services:
- transportLibrary__baseUrl
- transportLibrary__platformClientId
- transportLibrary__platformClientSecret
- modules__sse__enabled=${modules__sse__enabled:-false}
- modules__sse__baseUrlOverride
volumes:
- ..:/usr/app
- ./config.json:/config.json:ro
Expand Down
2 changes: 1 addition & 1 deletion .dev/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"debug": true,
"database": {
"connectionString": "mongodb://mongo:27017/?readPreference=primary&appname=connector&ssl=false"
"connectionString": "mongodb://mongo:27017"
},
"infrastructure": {
"httpServer": {
Expand Down
2 changes: 1 addition & 1 deletion .dev/scripts/clearDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { MongoDbConnection } from "@js-soft/docdb-access-mongo";

async function clearDb() {
const connectionString = "mongodb://localhost:27017/?readPreference=primary&appname=clearDb&ssl=false";
const connectionString = "mongodb://localhost:27017";
const dbConnection: MongoDbConnection = new MongoDbConnection(connectionString);
await dbConnection.connect();

Expand Down
67 changes: 67 additions & 0 deletions .dev/scripts/establishRelationshipAndSpamMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { sleep } from "@js-soft/ts-utils";
import { ConnectorClient, ConnectorRelationshipStatus } from "@nmshd/connector-sdk";

async function run() {
const connector1 = ConnectorClient.create({
baseUrl: "http://localhost:3000",
apiKey: "xxx"
});

const connector2 = ConnectorClient.create({
baseUrl: "http://localhost:3001",
apiKey: "xxx"
});

const { connector1Address, connector2Address } = await establishOrReturnRelationship(connector1, connector2);

while (true) {
await connector1.messages.sendMessage({ recipients: [connector2Address], content: {} });
await sleep(2000);

await connector2.messages.sendMessage({ recipients: [connector1Address], content: {} });
await sleep(2000);
}
}

async function establishOrReturnRelationship(connector1: ConnectorClient, connector2: ConnectorClient) {
const identityInfo = (await connector1.account.getIdentityInfo()).result;

const relationships = (await connector1.relationships.getRelationships()).result;

if (relationships.length > 0) {
if (relationships[0].status === ConnectorRelationshipStatus.PENDING) {
await connector1.relationships.acceptRelationshipChange(relationships[0].id, relationships[0].changes[0].id);
}

return {
connector1Address: identityInfo.address,
connector2Address: relationships[0].peer
};
}

const template = (await connector1.relationshipTemplates.createOwnRelationshipTemplate({ expiresAt: "2099", maxNumberOfAllocations: 1, content: {} })).result;

await connector2.relationshipTemplates.loadPeerRelationshipTemplate({ reference: template.truncatedReference });

const relationship = (await connector2.relationships.createRelationship({ templateId: template.id, content: {} })).result;

await connector1.account.sync();

const accepted = (await connector1.relationships.acceptRelationshipChange(relationship.id, relationship.changes[0].id)).result;
console.log(accepted);

await connector2.account.sync();

return {
connector1Address: identityInfo.address,
connector2Address: accepted.peer
};
}

run()
.then(() => {
console.log("Script finished successfully");
})
.catch((error) => {
console.error("Script failed with error", error);
});
9 changes: 9 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
"reveal": "always"
}
},
{
"label": "Clear Connectors",
"command": "docker compose -f .dev/compose.yml down -v",
"type": "shell",
"isBackground": true,
"presentation": {
"reveal": "always"
}
},
{
"label": "Run 1",
"command": "docker compose -f .dev/compose.yml --env-file .dev/.env${input:envFilePostfix} up connector-1",
Expand Down
19 changes: 12 additions & 7 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@
"displayName": "Attribute Listener",
"location": "@nmshd/runtime:AttributeListenerModule"
},
"sync": {
"displayName": "Sync",
"location": "sync/SyncModule",
"enabled": false,

"interval": 60
},
"autoAcceptRelationshipCreationChanges": {
"displayName": "Auto Accept Relationship Creation Changes",
"location": "autoAcceptRelationshipCreationChanges/AutoAcceptRelationshipCreationChangesModule",
Expand Down Expand Up @@ -121,6 +114,18 @@
"displayName": "Message Broker Publisher",
"location": "messageBrokerPublisher/MessageBrokerPublisherModule",
"brokers": []
},
"sync": {
"displayName": "Sync",
"location": "sync/SyncModule",
"enabled": false,

"interval": 60
},
"sse": {
"enabled": false,
"displayName": "Server Sent Events",
"location": "sse/SseModule"
}
}
}
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"axios": "^1.7.2",
"compression": "1.7.4",
"cors": "2.8.5",
"eventsource": "^2.0.2",
"express": "4.19.2",
"helmet": "7.1.0",
"json-stringify-safe": "5.0.1",
Expand All @@ -103,6 +104,7 @@
"@types/amqplib": "^0.10.5",
"@types/compression": "^1.7.5",
"@types/cors": "^2.8.17",
"@types/eventsource": "^1.1.15",
"@types/express": "4.17.21",
"@types/jest": "^29.5.12",
"@types/jest-json-schema": "^6.1.4",
Expand Down
18 changes: 14 additions & 4 deletions src/ConnectorRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ export class ConnectorRuntime extends Runtime<ConnectorRuntimeConfig> {
};
}

public async getBackboneAuthenticationToken(): Promise<string> {
return await this.accountController.authenticator.getToken();
}

protected async loadModule(moduleConfiguration: ModuleConfiguration): Promise<void> {
const connectorModuleConfiguration = moduleConfiguration as ConnectorRuntimeModuleConfiguration;

Expand Down Expand Up @@ -304,10 +308,16 @@ export class ConnectorRuntime extends Runtime<ConnectorRuntimeConfig> {
}

protected async stop(): Promise<void> {
try {
await super.stop();
} catch (e) {
this.logger.error(e);
if (this.isStarted) {
try {
await super.stop();
} catch (e) {
this.logger.error(e);
}
} else if (this.connectorMode === "debug") {
this.logger.warn("It seemed like the connector runtime didn't do a proper startup. Closing infrastructure.");

await this.stopInfrastructure();
}

try {
Expand Down
Loading