Skip to content

Commit 4b0d31f

Browse files
committed
Add microservices discovery service
* Add a microservices information fetching method, deserializing a meesaged based on the proto associated with that endpoint. * Consume that service on the app main component. * Set an environment variable that points to that microservices and nodes discovery information. This closes #13.
1 parent 24de83d commit 4b0d31f

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

src/app/app.component.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22

33
import { NavTreeDataService } from 'app/services/nav-tree-data.service';
4-
54
import { NavTreeNode } from 'app/shared/nav-tree-node.model';
65

76

@@ -18,7 +17,16 @@ export class AppComponent implements OnInit {
1817

1918
ngOnInit() {
2019
this.navTreeDataService.getJSON()
21-
.subscribe(data => this.navTreeObject = data);
20+
.subscribe(
21+
data => this.navTreeObject = data,
22+
error => console.log('Not able to get navigation tree data', error)
23+
);
24+
25+
this.navTreeDataService.getProto()
26+
.subscribe(
27+
data => console.log('Received following navigation tree data', data),
28+
error => console.log('Not able to get navigation tree data', error)
29+
);
2230
}
2331

2432
}
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
import { Injectable } from '@angular/core';
2-
import { Http } from '@angular/http';
2+
import { Http, ResponseContentType } from '@angular/http';
33
import { Observable } from 'rxjs/Observable';
44
// Operators
55
import 'rxjs/add/operator/map';
66
import 'rxjs/add/operator/catch';
77

8+
import { environment } from 'environments/environment';
9+
import { Microservice, MicroserviceList } from 'app/shared/proto/microservices_pb';
10+
811

912
@Injectable()
1013
export class NavTreeDataService {
1114

1215
constructor(private http: Http) { }
1316

17+
public getProto(): Observable<any> {
18+
return this.http.get(environment.microservicesEndpoint, { responseType: ResponseContentType.ArrayBuffer })
19+
.map((data: any) => {
20+
/**
21+
* The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer.
22+
* You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the
23+
* typed array objects or a DataView object which represents the buffer in a specific format,
24+
* and use that to read and write the contents of the buffer.
25+
*
26+
* google-protobuf/protobuf.js requires a byte-level view on the data, which is provided by Uint8Array
27+
* in the browser (falls back to Array in older browsers) or Buffer under node.js.
28+
* You are actually not converting the ArrayBuffer, just wrapping it in a byte-level view.
29+
* Basically, you tell JS how to interpret the raw binary data.
30+
*
31+
* This is done because decoders work pretty much the same in all environments with a few exceptions,
32+
* and an additional check-and-wrap for ArrayBuffer (as either Buffer or Uint8Array), while certainly possible,
33+
* would usually be unnecessary overhead under node.js / in older browsers / where Uint8Array is used anyway.
34+
*/
35+
const test = data;
36+
const serializedData = new Uint8Array(data.arrayBuffer());
37+
const microserviceList = MicroserviceList.deserializeBinary(serializedData);
38+
return microserviceList.toObject();
39+
});
40+
}
41+
1442
public getJSON(): Observable<any> {
1543
return this.http.get('assets/nav-tree-data.json')
16-
.map((data: any) => data.json())
17-
.catch(error => error);
44+
.map((data: any) => data.json());
1845
}
1946

2047
}

src/environments/environment.prod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const environment = {
22
production: true,
33
// metricsEndpoint: 'ws://localhost:8080/metrics'
4-
metricsEndpoint: 'ws://localhost:3000'
4+
metricsEndpoint: 'ws://localhost:3000',
5+
microservicesEndpoint: '//localhost:8080/microservices'
56
};

src/environments/environment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
export const environment = {
77
production: false,
88
// metricsEndpoint: 'ws://localhost:8080/metrics'
9-
metricsEndpoint: 'ws://localhost:3000'
9+
metricsEndpoint: 'ws://localhost:3000',
10+
microservicesEndpoint: '//localhost:8080/microservices'
1011
};

0 commit comments

Comments
 (0)