@@ -19,33 +19,43 @@ export type FnOnMessageCallback = (arg1: KartoffelstampfTerminalOutputEntry) =>
1919@Injectable ( )
2020export class BackendService {
2121
22- private static WEB_SOCKET_COMPRESS_URL = `ws://${ window . location . hostname } :9999/` ;
23- private static REST_API_UPLOAD_URL = `http://${ window . location . hostname } :9999/upload` ;
24- private static REST_API_DOWNLOAD_URL = `http://${ window . location . hostname } :9999/download` ;
22+ private restApiBaseUrl : string ;
23+ private webSocketBaseUrl : string ;
2524
26- constructor ( private http : HttpClient ) { }
25+ constructor ( private http : HttpClient ) {
26+ // Autodetect URLs
27+ const hostname = window . location . hostname ;
28+ const protocol = window . location . protocol ;
29+ const port = window . location . port ;
30+ if ( protocol === 'https:' ) {
31+ this . restApiBaseUrl = `https://${ hostname } ${ port ? ':' + port : '' } ` ;
32+ this . webSocketBaseUrl = `wss://${ hostname } ${ port ? ':' + port : '' } ` ;
33+ } else {
34+ this . restApiBaseUrl = `http://${ hostname } ${ port ? ':' + port : '' } ` ;
35+ this . webSocketBaseUrl = `ws://${ hostname } ${ port ? ':' + port : '' } ` ;
36+ }
37+ //
38+ // Note on localhost we need to overwrite the URLs
39+ //
40+ if ( localStorage && localStorage . getItem ( 'LOCAL_DEV' ) ) {
41+ this . restApiBaseUrl = `http://localhost:9999` ;
42+ this . webSocketBaseUrl = `ws://localhost:9999` ;
43+ }
44+ }
2745
2846 getDownloadUrl ( temporaryFileName : string , originalFileName : string ) {
29- return `${ BackendService . REST_API_DOWNLOAD_URL } /${ temporaryFileName } /${ originalFileName } ` ;
47+ return `${ this . restApiBaseUrl } /download /${ temporaryFileName } /${ originalFileName } ` ;
3048 }
3149
3250 uploadImage ( base64Image : string , type : string ) {
3351 return this . http . post < KartoffelstampfImageUploadResponse > (
34- BackendService . REST_API_UPLOAD_URL , {
52+ ` ${ this . restApiBaseUrl } /upload` , {
3553 contentDataUri : base64Image ,
36- } as KartoffelstampfImageUploadRequest , httpOptions )
37- . pipe (
38- catchError ( ( e : HttpErrorResponse ) => {
39- console . log ( e ) ;
40- return throwError (
41- 'Something bad happened; please try again later.'
42- ) ;
43- } )
44- ) ;
54+ } as KartoffelstampfImageUploadRequest , httpOptions ) ;
4555 }
4656
4757 runCompressCommand ( compressInstruction : KartoffelstampfCompressInstruction ) : Observable < KartoffelstampfTerminalOutputEntry > {
48- const ws = new WebSocket ( BackendService . WEB_SOCKET_COMPRESS_URL ) ;
58+ const ws = new WebSocket ( ` ${ this . webSocketBaseUrl } /` ) ;
4959 const subject = new Subject < KartoffelstampfTerminalOutputEntry > ( ) ;
5060 ws . onopen = function ( event ) {
5161 ws . send ( JSON . stringify ( compressInstruction ) ) ;
0 commit comments