import { Injectable } from '@angular/core' ; import { isDevMode } from '@angular/core' ; import { HttpBackend , HttpClient } from '@angular/common/http' ; import { BehaviorSubject , Observable } from 'rxjs' ; @ Injectable ( { providedIn : 'root' } ) export class ConnectionService { private readonly devPort : number = 4000 ; private connectionSubject : BehaviorSubject < string > ; public connection : Observable < string > ; private connectionStatus : boolean = false ; private http : HttpClient ; public constructor ( private backend ) { this . http = new HttpClient ( backend ) ; this . connectionSubject = new BehaviorSubject < string > ( JSON . parse ( localStorage . getItem ( 'connection' ) ) ) ; this . connection = this . connectionSubject . asObservable ( ) ; if ( this . currentConnection === null ) { this . setDefaultConnection ( ) ; } try { const connectionStatusPromise = this . connect ( ) ; connectionStatusPromise . then ( status => { this . connectionStatus = status ; if ( this . connectionStatus === false ) { localStorage . removeItem ( 'connection' ) ; this . connectionSubject . next ( null ) ; } } ) ; } catch ( e ) { this . connectionStatus = false ; localStorage . removeItem ( 'connection' ) ; this . connectionSubject . next ( null ) ; } } private setDefaultConnection ( ) { if ( isDevMode ( ) ) { this . setConnectionURL ( 'http' , '127.0.0.1' , this . devPort ) ; } else { this . setConnectionURL ( window . location . protocol . substring ( 0 , window . location . protocol . length - 1 ) , window . location . hostname , + window . location . port ) ; } } public get status ( ) : boolean { return this . connectionStatus ; } public get currentConnection ( ) : any { return this . connectionSubject . value ; } public async testConnection ( ) { try { await this . connect ( ) ; return true ; } catch ( e ) { return false ; } } private async connect ( ) { console . log ( '######## CONNECTION TRY ########' ) ; console . log ( `With URL: ${ this . currentConnection }/rest/` ) ; return await this . http . get < any > ( `${ this . currentConnection }/rest/` ) . toPromise ( ) ; } public async testCustomURL ( protocol , host , port ) { const customURL = `${ protocol }://${ host }:${ port }/rest/` ; console . log ( `Trying to connect to backend @ ${ customURL }` ) ; return await this . http . get < any > ( customURL ) . toPromise ( ) ; } public setConnectionURL ( protocol , host , port ) { let href = '' ; if ( port === 0 ) { href = `${ protocol }://${ host }` ; } else { href = `${ protocol }://${ host }:${ port }` ; } localStorage . setItem ( 'connection' , JSON . stringify ( href ) ) ; this . connectionSubject . next ( href ) ; } }