Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 4ccb6b62

Přidáno uživatelem Pavel Fidransky před asi 5 roky(ů)

re #33: load frontend app configuration from config.json file

Zobrazit rozdíly:

webapp/src/app/app.module.ts
1 1
import {BrowserModule} from '@angular/platform-browser';
2
import {NgModule} from '@angular/core';
2
import {NgModule, APP_INITIALIZER} from '@angular/core';
3 3

  
4 4
import {AppRoutingModule} from './app-routing.module';
5 5
import {AppComponent} from './app.component';
......
18 18
import {BasicAuthInterceptor} from "./auth/basic-auth.interceptor";
19 19
import {LoginComponent} from "./login/login.component";
20 20

  
21
import {loadConfig} from './loadConfig';
22
import {Config} from './services/util/config.service';
23

  
21 24
@NgModule({
22 25
  declarations: [
23 26
    AppComponent,
......
45 48
    CommonModule
46 49
  ],
47 50
  providers: [
48
    {provide: HTTP_INTERCEPTORS, useClass: BasicAuthInterceptor, multi: true}
51
    {
52
      provide: HTTP_INTERCEPTORS,
53
      useClass: BasicAuthInterceptor,
54
      multi: true,
55
    },
56
    {
57
      provide: APP_INITIALIZER,
58
      useFactory: loadConfig,
59
      deps: [
60
        HttpClient,
61
        Config,
62
      ],
63
      multi: true
64
    },
49 65
  ],
50 66
  bootstrap: [AppComponent]
51 67
})
webapp/src/app/loadConfig.ts
1
import { HttpClient } from '@angular/common/http';
2
import { Config } from './services/util/config.service';
3

  
4
export function loadConfig(http: HttpClient, config: Config) {
5
  return (): Promise<void> => {
6
    const configFile = 'assets/config/config.json';
7

  
8
    return new Promise<void>((resolve, reject) => {
9
      http.get(configFile).toPromise().then((response: any) => {
10
        config.baseUrl = response.baseUrl;
11
        config.redirectUrl = response.redirectUrl;
12
        resolve();
13
      });
14
    });
15
  };
16
}
webapp/src/app/login/login.component.html
3 3
    <h1>Sign-in</h1>
4 4

  
5 5
    <p>Please sign-in using one of the following methods: </p>
6
    <a href="http://localhost:9080/api/signin/google?target=http://localhost">Login with Google</a>
6
    <a href="{{loginUrl}}">Login with Google</a>
7 7
  </div>
8 8
</div>
9 9

  
webapp/src/app/login/login.component.ts
1 1
import {Component, OnInit} from '@angular/core';
2
import { Config } from '../services/util/config.service';
2 3

  
3 4
@Component({
4 5
  selector: 'app-page-not-found',
......
6 7
  styleUrls: ['./login.component.sass']
7 8
})
8 9
export class LoginComponent implements OnInit {
10
  loginUrl: string;
9 11

  
10
  constructor() {
12
  constructor(private config: Config) {
13
    this.loginUrl = config.loginUrl;
11 14
  }
12 15

  
13 16
  ngOnInit() {
webapp/src/app/services/api/basic.service.ts
1 1
import {Injectable} from '@angular/core';
2 2
import {HttpClient, HttpErrorResponse, HttpParams} from '@angular/common/http';
3 3
import {throwError} from 'rxjs';
4
import {environment} from '../../../environments/environment';
5 4
import {MatSnackBar} from '@angular/material';
6 5
import {TranslateService} from '@ngx-translate/core';
6
import { Config } from '../util/config.service';
7

  
7 8
@Injectable({
8 9
  providedIn: 'root'
9 10
})
10 11
export class BasicService {
11
  protected baseUrl = environment.apiUrl;
12
  protected baseUrl: string;
12 13

  
13
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) { }
14
  constructor(protected config: Config, protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
15
    this.baseUrl = config.baseUrl;
16
  }
14 17

  
15 18
  protected handleError(error: HttpErrorResponse) {
16 19
    let errMsg;
webapp/src/app/services/api/file.service.ts
5 5
import {Languages} from '../../enums/common.enum';
6 6
import {MatSnackBar} from '@angular/material';
7 7
import {TranslateService} from '@ngx-translate/core';
8
import { Config } from '../util/config.service';
8 9

  
9 10
@Injectable({
10 11
  providedIn: 'root'
11 12
})
12 13
export class FileService extends BasicService {
13 14

  
14
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
15
    super(http, snackBar, translateService);
15
  constructor(protected config: Config, protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
16
    super(config, http, snackBar, translateService);
16 17
  }
17 18

  
18 19
  /**
webapp/src/app/services/api/settings.service.ts
7 7
import {Languages} from '../../enums/common.enum';
8 8
import {MatSnackBar} from '@angular/material';
9 9
import {TranslateService} from '@ngx-translate/core';
10
import { Config } from '../util/config.service';
10 11

  
11 12
@Injectable({
12 13
  providedIn: 'root'
......
14 15
export class SettingsService extends BasicService {
15 16
  defaultSettingsUrl = this.baseUrl + '/api/settings';
16 17

  
17
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
18
    super(http, snackBar, translateService);
18
  constructor(protected config: Config, protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
19
    super(config, http, snackBar, translateService);
19 20
  }
20 21

  
21 22
  /**
webapp/src/app/services/api/user.service.ts
11 11
import {DateFormatterService} from '../util/date-formatter.service';
12 12
import {TranslateService} from '@ngx-translate/core';
13 13
import {ProfileService} from "../util/profile.service";
14
import { Config } from '../util/config.service';
14 15

  
15 16
@Injectable({
16 17
  providedIn: 'root'
......
18 19
export class UserService extends BasicService { // dost podobny k usersService, mozna zmenit v rest api
19 20
  private _userUrl = this.baseUrl + '/api/user/';
20 21

  
21
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService, protected profileService: ProfileService, private dateFormater: DateFormatterService) {
22
    super(http, snackBar, translateService);
22
  constructor(protected config: Config, protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService, protected profileService: ProfileService, private dateFormater: DateFormatterService) {
23
    super(config, http, snackBar, translateService);
23 24
  }
24 25

  
25 26
  /**
webapp/src/app/services/api/users.service.ts
9 9
import {UserBasicInformation, UserProfile} from '../../models/user.model';
10 10
import {MatSnackBar} from '@angular/material';
11 11
import {TranslateService} from '@ngx-translate/core';
12
import { Config } from '../util/config.service';
12 13

  
13 14
@Injectable({
14 15
  providedIn: 'root'
......
16 17
export class UsersService extends BasicService {
17 18
  private _usersUrl = this.baseUrl + '/api/users';
18 19

  
19
  constructor(protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
20
    super(http, snackBar, translateService);
20
  constructor(protected config: Config, protected http: HttpClient, protected snackBar: MatSnackBar, protected translateService: TranslateService) {
21
    super(config, http, snackBar, translateService);
21 22
  }
22 23

  
23 24
  /**
webapp/src/app/services/util/config.service.ts
1
import {Injectable} from '@angular/core';
2

  
3
@Injectable({
4
  providedIn: 'root'
5
})
6
export class Config {
7
  baseUrl: string;
8
  redirectUrl: string;
9

  
10
  get loginUrl(): string {
11
    return this.baseUrl + '/api/login/google?target=' + this.redirectUrl;
12
  }
13
}
webapp/src/assets/config/config.json
1
{
2
	"baseUrl": "http://localhost:9080",
3
	"redirectUrl": "http://localhost"
4
}
webapp/src/environments/environment.prod.ts
1 1
export const environment = {
2 2
  production: true,
3
  apiUrl: 'http://localhost:9080/' // TODO Change to production url
4 3
};
webapp/src/environments/environment.ts
1 1
export const environment = {
2 2
  production: false,
3
  apiUrl: 'http://localhost:9080'
4 3
};

Také k dispozici: Unified diff