Skip to content

Commit 4c9b308

Browse files
authored
Merge pull request #18 from hashtopolis/bug/no-backend-url
Setting backendurl even if config.json returns error
2 parents abb11d1 + 2b11568 commit 4c9b308

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/app/app.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export class AppComponent implements OnInit {
114114
isLogged: boolean;
115115

116116
ngOnInit(): void {
117+
this.configService.refreshEndpoint();
117118
this.authService.autoLogin();
118119
this.authService.isLogged.subscribe(logged => {
119120
this.isLogged = logged;

src/app/auth/auth.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export class AuthComponent implements OnInit {
3333
}
3434

3535
ngOnInit(): void {
36-
this.configService.getEndpoint();
3736
}
3837

3938
onSubmit(form: NgForm){

src/app/core/_services/shared/config.service.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,46 @@ import { environment } from 'src/environments/environment';
88
})
99
export class ConfigService {
1010
private configUrl = 'assets/config.json';
11-
1211
constructor(private http: HttpClient) { }
1312

1413
private getConfigOnce(): Observable<any> {
1514
return this.http.get(this.configUrl);
1615
}
1716

17+
public refreshEndpoint(){
18+
let ApiEndPoint = "";
19+
this.getConfigOnce().subscribe(config => {
20+
// Assuming the JSON contains a property named 'backendUrl'
21+
if (config && config.hashtopolis_backend_url) {
22+
// If we get a config from the backend, we set the config property
23+
ApiEndPoint = config.hashtopolis_backend_url;
24+
// Remove trailing slash, because this causing invalid URLs
25+
if (ApiEndPoint.endsWith('/')) {
26+
ApiEndPoint = ApiEndPoint.slice(0, -1);
27+
}
28+
localStorage.setItem('prodApiEndpoint', ApiEndPoint);
29+
} else if (config && !config.hashtopolis_backend_url) {
30+
console.error('Invalid configuration file. Please check your config.json. Using defaults.');
31+
ApiEndPoint = environment.config.prodApiEndpoint;
32+
localStorage.setItem('prodApiEndpoint', ApiEndPoint);
33+
} else {
34+
ApiEndPoint = environment.config.prodApiEndpoint;
35+
localStorage.setItem('prodApiEndpoint', ApiEndPoint);
36+
}
37+
},
38+
error => {
39+
ApiEndPoint = environment.config.prodApiEndpoint;
40+
localStorage.setItem('prodApiEndpoint', ApiEndPoint);
41+
}
42+
);
43+
}
44+
1845
public getEndpoint(){
1946
let ApiEndPoint = localStorage.getItem('prodApiEndpoint');
2047
if (!ApiEndPoint) {
21-
this.getConfigOnce().subscribe(config => {
22-
// Assuming the JSON contains a property named 'backendUrl'
23-
if (config && config.hashtopolis_backend_url) {
24-
// If we get a config from the backend, we set the config property
25-
ApiEndPoint = config.hashtopolis_backend_url;
26-
localStorage.setItem('prodApiEndpoint', ApiEndPoint);
27-
} else if (config && !config.hashtopolis_backend_url) {
28-
console.error('Invalid configuration file. Please check your config.json. Using defaults.');
29-
ApiEndPoint = environment.config.prodApiEndpoint;
30-
localStorage.setItem('prodApiEndpoint', ApiEndPoint);
31-
} else {
32-
ApiEndPoint = environment.config.prodApiEndpoint;
33-
localStorage.setItem('prodApiEndpoint', ApiEndPoint);
34-
}
35-
});
48+
this.refreshEndpoint();
3649
}
50+
ApiEndPoint = localStorage.getItem('prodApiEndpoint');
3751
return ApiEndPoint;
3852
}
3953
}

0 commit comments

Comments
 (0)