Added proxy to bb frontend (no need for cross-origin headers); support periodic status calls

bb-server
Tal Moran 2017-07-02 17:54:36 +03:00
parent c0b21f82ee
commit 173b952e09
5 changed files with 41 additions and 10 deletions

View File

@ -4,7 +4,7 @@
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"start": "ng serve --proxy-config proxy.conf.json",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
@ -23,7 +23,7 @@
"@angular/router": "^4.0.0",
"@types/long": "^3.0.31",
"core-js": "^2.4.1",
"protobuf.js": "^6.8.0",
"protobufjs": "^6.8.0",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
},

View File

@ -0,0 +1,6 @@
{
"/bbserver" : {
"target" : "http://localhost:8081/",
"secure" : false
}
}

View File

@ -1,6 +1,6 @@
import {Component, OnInit} from '@angular/core';
import {BBService} from "./bb.service"; // respectively "./node_modules/protobufjs"
import {IntervalObservable} from "rxjs/observable/IntervalObservable";
import {BBService} from "./bb.service";
@Component({
selector: 'app-root',
@ -15,14 +15,39 @@ export class AppComponent implements OnInit {
status = 'Not connected';
errorMessage = '';
private alive: boolean; // used to unsubscribe from the IntervalObservable
// when OnDestroy is called.
constructor(private bbservice : BBService) { }
ngOnInit(): void {
// Get initial status
this.bbservice.getStatus().subscribe(
status => this.status = status.name,
status => { this.status = status.name },
error => this.errorMessage = <any>error
);
this.alive = true;
// Get new status every second
let o = IntervalObservable.create(1000)
//.takeWhile(() => this.alive) // only fires when component is alive
.subscribe(n => {
if (!this.alive) {
o.unsubscribe();
return;
}
this.bbservice.getStatus().subscribe(
status => this.status = status.name + " " + n,
error => this.errorMessage = <any>error
);
});
}
ngOnDestroy(){
this.alive = false; // switches your IntervalObservable off
}
}

View File

@ -11,7 +11,7 @@ import 'rxjs/add/operator/map';
@Injectable()
export class BBService {
private headers = new Headers({'Content-Type': 'application/x-protobuf'});
private bbUrl = 'http://localhost:8081/bbserver'; // URL to web api
private bbUrl = '/bbserver'; // URL to web api
private statusPath = '/status';
constructor(private http: Http) { }

View File

@ -56,15 +56,15 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL
String dbType = servletContext.getInitParameter("dbType");
String dbName = servletContext.getInitParameter("dbName");
if ("SQLite".equals(dbType)){
if ("SQLite".equalsIgnoreCase(dbType)){
bulletinBoard = new BulletinBoardSQLServer(new SQLiteQueryProvider(dbName));
} else if ("H2".equals(dbType)) {
} else if ("H2".equalsIgnoreCase(dbType)) {
bulletinBoard = new BulletinBoardSQLServer(new H2QueryProvider(dbName));
} else if ("MySQL".equals(dbType)) {
} else if ("MySQL".equalsIgnoreCase(dbType)) {
String dbAddress = servletContext.getInitParameter("dbAddress");
int dbPort = Integer.parseInt(servletContext.getInitParameter("dbPort"));
@ -93,7 +93,7 @@ public class BulletinBoardWebApp implements BulletinBoardServer, ServletContextL
@Consumes(MEDIATYPE_PROTOBUF)
@Produces(MEDIATYPE_PROTOBUF)
public ServerStatus getStatus(@Context HttpServletResponse response) throws CommunicationException {
response.setHeader("Access-Control-Allow-Origin", "*"); // TODO: Remove
// response.setHeader("Access-Control-Allow-Origin", "*"); // TODO: Remove
return ServerStatus.newBuilder()
.setName("Hello")
.build();