Route all Angular Universal requests to nginx, remove simon's hacks

This commit is contained in:
wiz 2020-11-27 23:01:47 +09:00
parent dee9fa2d9a
commit b6150a3237
No known key found for this signature in database
GPG Key ID: A394E332255A6173
5 changed files with 52 additions and 57 deletions

View File

@ -5,60 +5,56 @@ import { Observable } from 'rxjs';
import { StateService } from './state.service'; import { StateService } from './state.service';
import { WebsocketResponse } from '../interfaces/websocket.interface'; import { WebsocketResponse } from '../interfaces/websocket.interface';
const API_BASE_URL = '{network}/api/v1';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class ApiService { export class ApiService {
private apiBaseUrl: string; private apiBaseUrl: string; // base URL is protocol, hostname, and port
private apiBasePath: string; // network path is /testnet, etc. or '' for mainnet
constructor( constructor(
private httpClient: HttpClient, private httpClient: HttpClient,
private stateService: StateService, private stateService: StateService,
) { ) {
this.apiBaseUrl = ''; // use relative URL by default
if (!stateService.isBrowser) { // except when inside AU SSR process
this.apiBaseUrl = this.stateService.env.NGINX_PROTOCOL + '://' + this.stateService.env.NGINX_HOSTNAME + ':' + this.stateService.env.NGINX_PORT;
}
this.apiBasePath = ''; // assume mainnet by default
this.stateService.networkChanged$.subscribe((network) => { this.stateService.networkChanged$.subscribe((network) => {
if (network === 'bisq' && !this.stateService.env.BISQ_SEPARATE_BACKEND) { if (network === 'bisq' && !this.stateService.env.BISQ_SEPARATE_BACKEND) {
network = ''; network = '';
} }
this.apiBaseUrl = API_BASE_URL.replace('{network}', network ? '/' + network : ''); this.apiBasePath = network ? '/' + network : '';
if (!stateService.isBrowser) {
this.apiBaseUrl = this.stateService.env.BACKEND_URL + this.apiBaseUrl;
}
}); });
this.apiBaseUrl = API_BASE_URL.replace('{network}', '');
if (!stateService.isBrowser) {
this.apiBaseUrl = this.stateService.env.BACKEND_URL + this.apiBaseUrl;
}
} }
list2HStatistics$(): Observable<OptimizedMempoolStats[]> { list2HStatistics$(): Observable<OptimizedMempoolStats[]> {
return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + '/statistics/2h'); return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/statistics/2h');
} }
list24HStatistics$(): Observable<OptimizedMempoolStats[]> { list24HStatistics$(): Observable<OptimizedMempoolStats[]> {
return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + '/statistics/24h'); return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/statistics/24h');
} }
list1WStatistics$(): Observable<OptimizedMempoolStats[]> { list1WStatistics$(): Observable<OptimizedMempoolStats[]> {
return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + '/statistics/1w'); return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/statistics/1w');
} }
list1MStatistics$(): Observable<OptimizedMempoolStats[]> { list1MStatistics$(): Observable<OptimizedMempoolStats[]> {
return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + '/statistics/1m'); return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/statistics/1m');
} }
list3MStatistics$(): Observable<OptimizedMempoolStats[]> { list3MStatistics$(): Observable<OptimizedMempoolStats[]> {
return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + '/statistics/3m'); return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/statistics/3m');
} }
list6MStatistics$(): Observable<OptimizedMempoolStats[]> { list6MStatistics$(): Observable<OptimizedMempoolStats[]> {
return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + '/statistics/6m'); return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/statistics/6m');
} }
list1YStatistics$(): Observable<OptimizedMempoolStats[]> { list1YStatistics$(): Observable<OptimizedMempoolStats[]> {
return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + '/statistics/1y'); return this.httpClient.get<OptimizedMempoolStats[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/statistics/1y');
} }
getTransactionTimes$(txIds: string[]): Observable<number[]> { getTransactionTimes$(txIds: string[]): Observable<number[]> {
@ -66,7 +62,7 @@ export class ApiService {
txIds.forEach((txId: string) => { txIds.forEach((txId: string) => {
params = params.append('txId[]', txId); params = params.append('txId[]', txId);
}); });
return this.httpClient.get<number[]>(this.apiBaseUrl + '/transaction-times', { params }); return this.httpClient.get<number[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/transaction-times', { params });
} }
requestDonation$(amount: number, orderId: string): Observable<any> { requestDonation$(amount: number, orderId: string): Observable<any> {
@ -74,14 +70,14 @@ export class ApiService {
amount: amount, amount: amount,
orderId: orderId, orderId: orderId,
}; };
return this.httpClient.post<any>(this.apiBaseUrl + '/donations', params); return this.httpClient.post<any>(this.apiBaseUrl + this.apiBasePath + '/api/v1/donations', params);
} }
getDonation$(): Observable<any[]> { getDonation$(): Observable<any[]> {
return this.httpClient.get<any[]>(this.apiBaseUrl + '/donations'); return this.httpClient.get<any[]>(this.apiBaseUrl + this.apiBasePath + '/api/v1/donations');
} }
getInitData$(): Observable<WebsocketResponse> { getInitData$(): Observable<WebsocketResponse> {
return this.httpClient.get<WebsocketResponse>(this.apiBaseUrl + '/init-data'); return this.httpClient.get<WebsocketResponse>(this.apiBaseUrl + this.apiBasePath + '/api/v1/init-data');
} }
} }

View File

@ -16,13 +16,13 @@ export class AssetsService {
private httpClient: HttpClient, private httpClient: HttpClient,
private stateService: StateService, private stateService: StateService,
) { ) {
let baseApiUrl = ''; let apiBaseUrl = '';
if (!this.stateService.isBrowser) { if (!this.stateService.isBrowser) {
baseApiUrl = this.stateService.env.STATIC_WEBSERVER_URL; apiBaseUrl = this.stateService.env.NGINX_PROTOCOL + '://' + this.stateService.env.NGINX_HOSTNAME + ':' + this.stateService.env.NGINX_PORT;
} }
this.getAssetsJson$ = this.httpClient.get(baseApiUrl + '/resources/assets.json').pipe(shareReplay()); this.getAssetsJson$ = this.httpClient.get(apiBaseUrl + '/resources/assets.json').pipe(shareReplay());
this.getAssetsMinimalJson$ = this.httpClient.get(baseApiUrl + '/resources/assets.minimal.json').pipe(shareReplay()); this.getAssetsMinimalJson$ = this.httpClient.get(apiBaseUrl + '/resources/assets.minimal.json').pipe(shareReplay());
this.getMiningPools$ = this.httpClient.get(baseApiUrl + '/resources/pools.json').pipe(shareReplay()); this.getMiningPools$ = this.httpClient.get(apiBaseUrl + '/resources/pools.json').pipe(shareReplay());
} }
} }

View File

@ -4,87 +4,87 @@ import { Observable } from 'rxjs';
import { Block, Transaction, Address, Outspend, Recent, Asset } from '../interfaces/electrs.interface'; import { Block, Transaction, Address, Outspend, Recent, Asset } from '../interfaces/electrs.interface';
import { StateService } from './state.service'; import { StateService } from './state.service';
let API_BASE_URL = '{network}/api';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class ElectrsApiService { export class ElectrsApiService {
private apiBaseUrl: string; private apiBaseUrl: string; // base URL is protocol, hostname, and port
private apiBasePath: string; // network path is /testnet, etc. or '' for mainnet
constructor( constructor(
private httpClient: HttpClient, private httpClient: HttpClient,
private stateService: StateService, private stateService: StateService,
) { ) {
if (!stateService.isBrowser) { this.apiBaseUrl = ''; // use relative URL by default
API_BASE_URL = this.stateService.env.ELECTRS_URL; if (!stateService.isBrowser) { // except when inside AU SSR process
this.apiBaseUrl = this.stateService.env.NGINX_PROTOCOL + '://' + this.stateService.env.NGINX_HOSTNAME + ':' + this.stateService.env.NGINX_PORT;
} }
this.apiBaseUrl = API_BASE_URL.replace('{network}', ''); this.apiBasePath = ''; // assume mainnet by default
this.stateService.networkChanged$.subscribe((network) => { this.stateService.networkChanged$.subscribe((network) => {
if (network === 'bisq') { if (network === 'bisq') {
network = ''; network = '';
} }
this.apiBaseUrl = API_BASE_URL.replace('{network}', network ? '/' + network : ''); this.apiBasePath = network ? '/' + network : '';
}); });
} }
getBlock$(hash: string): Observable<Block> { getBlock$(hash: string): Observable<Block> {
return this.httpClient.get<Block>(this.apiBaseUrl + '/block/' + hash); return this.httpClient.get<Block>(this.apiBaseUrl + this.apiBasePath + '/api/block/' + hash);
} }
listBlocks$(height?: number): Observable<Block[]> { listBlocks$(height?: number): Observable<Block[]> {
return this.httpClient.get<Block[]>(this.apiBaseUrl + '/blocks/' + (height || '')); return this.httpClient.get<Block[]>(this.apiBaseUrl + this.apiBasePath + '/api/blocks/' + (height || ''));
} }
getTransaction$(txId: string): Observable<Transaction> { getTransaction$(txId: string): Observable<Transaction> {
return this.httpClient.get<Transaction>(this.apiBaseUrl + '/tx/' + txId); return this.httpClient.get<Transaction>(this.apiBaseUrl + this.apiBasePath + '/api/tx/' + txId);
} }
getRecentTransaction$(): Observable<Recent[]> { getRecentTransaction$(): Observable<Recent[]> {
return this.httpClient.get<Recent[]>(this.apiBaseUrl + '/mempool/recent'); return this.httpClient.get<Recent[]>(this.apiBaseUrl + this.apiBasePath + '/api/mempool/recent');
} }
getOutspend$(hash: string, vout: number): Observable<Outspend> { getOutspend$(hash: string, vout: number): Observable<Outspend> {
return this.httpClient.get<Outspend>(this.apiBaseUrl + '/tx/' + hash + '/outspend/' + vout); return this.httpClient.get<Outspend>(this.apiBaseUrl + this.apiBasePath + '/api/tx/' + hash + '/outspend/' + vout);
} }
getOutspends$(hash: string): Observable<Outspend[]> { getOutspends$(hash: string): Observable<Outspend[]> {
return this.httpClient.get<Outspend[]>(this.apiBaseUrl + '/tx/' + hash + '/outspends'); return this.httpClient.get<Outspend[]>(this.apiBaseUrl + this.apiBasePath + '/api/tx/' + hash + '/outspends');
} }
getBlockTransactions$(hash: string, index: number = 0): Observable<Transaction[]> { getBlockTransactions$(hash: string, index: number = 0): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/block/' + hash + '/txs/' + index); return this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/block/' + hash + '/txs/' + index);
} }
getBlockHashFromHeight$(height: number): Observable<string> { getBlockHashFromHeight$(height: number): Observable<string> {
return this.httpClient.get(this.apiBaseUrl + '/block-height/' + height, {responseType: 'text'}); return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/block-height/' + height, {responseType: 'text'});
} }
getAddress$(address: string): Observable<Address> { getAddress$(address: string): Observable<Address> {
return this.httpClient.get<Address>(this.apiBaseUrl + '/address/' + address); return this.httpClient.get<Address>(this.apiBaseUrl + this.apiBasePath + '/api/address/' + address);
} }
getAddressTransactions$(address: string): Observable<Transaction[]> { getAddressTransactions$(address: string): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/address/' + address + '/txs'); return this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/address/' + address + '/txs');
} }
getAddressTransactionsFromHash$(address: string, txid: string): Observable<Transaction[]> { getAddressTransactionsFromHash$(address: string, txid: string): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/address/' + address + '/txs/chain/' + txid); return this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/address/' + address + '/txs/chain/' + txid);
} }
getAsset$(assetId: string): Observable<Asset> { getAsset$(assetId: string): Observable<Asset> {
return this.httpClient.get<Asset>(this.apiBaseUrl + '/asset/' + assetId); return this.httpClient.get<Asset>(this.apiBaseUrl + this.apiBasePath + '/api/asset/' + assetId);
} }
getAssetTransactions$(assetId: string): Observable<Transaction[]> { getAssetTransactions$(assetId: string): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/asset/' + assetId + '/txs'); return this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/asset/' + assetId + '/txs');
} }
getAssetTransactionsFromHash$(assetId: string, txid: string): Observable<Transaction[]> { getAssetTransactionsFromHash$(assetId: string, txid: string): Observable<Transaction[]> {
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + '/asset/' + assetId + '/txs/chain/' + txid); return this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/asset/' + assetId + '/txs/chain/' + txid);
} }
getAddressesByPrefix$(prefix: string): Observable<string[]> { getAddressesByPrefix$(prefix: string): Observable<string[]> {
return this.httpClient.get<string[]>(this.apiBaseUrl + '/address-prefix/' + prefix); return this.httpClient.get<string[]>(this.apiBaseUrl + this.apiBasePath + '/api/address-prefix/' + prefix);
} }
} }

View File

@ -35,9 +35,6 @@ export class HttpCacheInterceptor implements HttpInterceptor {
.pipe(tap((event: HttpEvent<any>) => { .pipe(tap((event: HttpEvent<any>) => {
if (!this.isBrowser && event instanceof HttpResponse) { if (!this.isBrowser && event instanceof HttpResponse) {
let keyId = request.url.split('/').slice(3).join('/'); let keyId = request.url.split('/').slice(3).join('/');
if (keyId.indexOf('api/') === -1) {
keyId = 'api/' + keyId;
}
this.transferState.set(makeStateKey('/' + keyId), event); this.transferState.set(makeStateKey('/' + keyId), event);
} }
})); }));

View File

@ -21,10 +21,9 @@ export interface Env {
SPONSORS_ENABLED: boolean; SPONSORS_ENABLED: boolean;
ELECTRS_ITEMS_PER_PAGE: number; ELECTRS_ITEMS_PER_PAGE: number;
KEEP_BLOCKS_AMOUNT: number; KEEP_BLOCKS_AMOUNT: number;
BACKEND_URL?: string; NGINX_PROTOCOL?: string;
ELECTRS_URL?: string; NGINX_HOSTNAME?: string;
ELECTRS_URL_SERVER?: string; NGINX_PORT?: string;
STATIC_WEBSERVER_URL?: string;
} }
const defaultEnv: Env = { const defaultEnv: Env = {
@ -35,6 +34,9 @@ const defaultEnv: Env = {
'SPONSORS_ENABLED': false, 'SPONSORS_ENABLED': false,
'ELECTRS_ITEMS_PER_PAGE': 25, 'ELECTRS_ITEMS_PER_PAGE': 25,
'KEEP_BLOCKS_AMOUNT': 8, 'KEEP_BLOCKS_AMOUNT': 8,
'NGINX_PROTOCOL': 'http',
'NGINX_HOSTNAME': '127.0.0.1',
'NGINX_PORT': '81',
}; };
@Injectable({ @Injectable({