renamed sse endpoint to events. Using the technology name in the API isnt a good idea.

This commit is contained in:
Jason Kulatunga 2023-09-08 14:10:37 -07:00
parent 6a3afe150e
commit 74fc682dbf
3 changed files with 54 additions and 2 deletions

View File

@ -79,7 +79,7 @@ func (ae *AppEngine) Setup() (*gin.RouterGroup, *gin.Engine) {
secure.POST("/query", handler.QueryResourceFhir)
//server-side-events handler
secure.GET("/sse/stream", middleware.SSEHeaderMiddleware(), middleware.SSEEventBusServerMiddleware(), handler.SSEStream)
secure.GET("/events/stream", middleware.SSEHeaderMiddleware(), middleware.SSEEventBusServerMiddleware(), handler.SSEStream)
}
if ae.Config.GetBool("web.allow_unsafe_endpoints") {

View File

@ -1,5 +1,9 @@
import {Component, OnInit, TemplateRef} from '@angular/core';
import {ToastService} from '../../services/toast.service';
import {AuthService} from '../../services/auth.service';
import {FastenApiService} from '../../services/fasten-api.service';
import {Subscription} from 'rxjs';
import {NavigationEnd, Router} from '@angular/router';
@Component({
selector: 'app-toast',
@ -7,9 +11,33 @@ import {ToastService} from '../../services/toast.service';
styleUrls: ['./toast.component.scss']
})
export class ToastComponent implements OnInit {
routerSubscription: Subscription | undefined;
constructor(public toastService: ToastService) {}
constructor(
public router: Router,
public toastService: ToastService,
public authService: AuthService,
public fastenApiService: FastenApiService,
) {}
ngOnInit(): void {
}
ngAfterViewInit() {
//TODO: this is a bit kludgey.
// Ideally we want consistently listen to events, but only when the user is authenticated.
this.routerSubscription = this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
if(!event.url.startsWith("/auth") && this.authService.IsAuthenticated()){
console.log("user is authenticated, listening for notifications")
//user is authenticated, lets start listening for notifications
this.routerSubscription?.unsubscribe()
this.fastenApiService.listenEventBus().subscribe((event)=>{
console.log("eventbus event:", event)
//TODO: start toasts.
})
}
}
});
}
}

View File

@ -22,6 +22,7 @@ import * as fhirpath from 'fhirpath';
import _ from 'lodash';
import {DashboardConfig} from '../models/widget/dashboard-config';
import {DashboardWidgetQuery} from '../models/widget/dashboard-widget-query';
import { fetchEventSource } from '@microsoft/fetch-event-source';
@Injectable({
providedIn: 'root'
@ -54,6 +55,29 @@ export class FastenApiService {
SECURE ENDPOINTS
*/
listenEventBus(): Observable<any> {
let eventStreamUrl = `${GetEndpointAbsolutePath(globalThis.location, environment.fasten_api_endpoint_base)}/secure/events/stream`
return new Observable(observer => {
fetchEventSource(eventStreamUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.authService.GetAuthToken()}`
},
onmessage(ev) {
observer.next(ev.data);
},
onerror(event) {
observer.error(event)
},
}).then(
() => observer.complete(),
error => observer.error(error)
)
});
}
getDashboards(): Observable<DashboardConfig[]> {
return this._httpClient.get<any>(`${GetEndpointAbsolutePath(globalThis.location, environment.fasten_api_endpoint_base)}/secure/dashboards`, )
.pipe(