From 838b2a6a3424aec989dcb37d38eeae7adb32d8d3 Mon Sep 17 00:00:00 2001 From: Jesse Lucas Date: Sun, 15 Mar 2020 12:33:21 -0400 Subject: [PATCH] add mock in-memory data service --- README.md | 4 + package-lock.json | 5 + package.json | 1 + src/app/api-utils.ts | 4 +- src/app/app.module.ts | 8 +- src/app/in-memory-config-data.service.spec.ts | 16 +++ src/app/in-memory-config-data.service.ts | 114 ++++++++++++++++++ src/app/system-config.service.ts | 9 +- 8 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 src/app/in-memory-config-data.service.spec.ts create mode 100644 src/app/in-memory-config-data.service.ts diff --git a/README.md b/README.md index 55099411a..184df7d0b 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ This project was generated with [Angular CLI](https://github.com/angular/angular ## Development server +Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. + +## Production server + Run `ng build --prod --base-href /tech-ui/` and build [syncthing tech-ui branch](https://github.com/kastelo/syncthing/tree/tech-ui). Run `export STTECHUIDIR=/path/to/angular/build/; ./bin/syncthing` and navigate to `http://localhost:8384/tech-ui/`. ## Code scaffolding diff --git a/package-lock.json b/package-lock.json index f0d9e9b0d..2d1cb0b4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1790,6 +1790,11 @@ "resolved": "https://registry.npmjs.org/amdefine/-/amdefine-1.0.1.tgz", "integrity": "sha1-SlKCrBZHKek2Gbz9OtFR+BfOkfU=" }, + "angular-in-memory-web-api": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/angular-in-memory-web-api/-/angular-in-memory-web-api-0.10.0.tgz", + "integrity": "sha512-GzY4lHPR8suREYfgiYiJSBrgq0bdCiFw/LuqW5IsOQfRx98AvS69A5Ehz12oMLy9ABzoGpqmNC5fcFtClDAPqQ==" + }, "ansi-colors": { "version": "3.2.4", "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.4.tgz", diff --git a/package.json b/package.json index 6741c9099..ebf89a84b 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "@angular/platform-browser": "~9.0.5", "@angular/platform-browser-dynamic": "~9.0.5", "@angular/router": "~9.0.5", + "angular-in-memory-web-api": "^0.10.0", "component": "^1.1.0", "rxjs": "~6.5.4", "tslib": "^1.10.0", diff --git a/src/app/api-utils.ts b/src/app/api-utils.ts index 44432b967..19d6d0675 100644 --- a/src/app/api-utils.ts +++ b/src/app/api-utils.ts @@ -1,5 +1,7 @@ +import { environment } from '../environments/environment' + export const deviceID = (): String => { - const dID: String = globalThis.metadata['deviceID']; + const dID: String = environment.production ? globalThis.metadata['deviceID'] : '12345'; return dID.substring(0, 5) } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 392e5df9d..c185ec010 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -15,8 +15,10 @@ import { FolderListComponent } from './folder-list/folder-list.component'; import { DeviceListComponent } from './device-list/device-list.component'; import { StatusToggleComponent } from './status-toggle/status-toggle.component'; import { DeviceListDataSource } from './device-list/device-list-datasource'; +import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api'; +import { InMemoryConfigDataService } from './in-memory-config-data.service'; import { deviceID } from './api-utils'; - +import { environment } from '../environments/environment' @NgModule({ declarations: [ @@ -38,7 +40,9 @@ import { deviceID } from './api-utils'; HttpClientXsrfModule.withOptions({ headerName: 'X-CSRF-Token-' + deviceID(), cookieName: 'CSRF-Token-' + deviceID(), - }) + }), + environment.production ? + [] : HttpClientInMemoryWebApiModule.forRoot(InMemoryConfigDataService) ], providers: [], bootstrap: [AppComponent] diff --git a/src/app/in-memory-config-data.service.spec.ts b/src/app/in-memory-config-data.service.spec.ts new file mode 100644 index 000000000..4650f227e --- /dev/null +++ b/src/app/in-memory-config-data.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { InMemoryConfigDataService } from './in-memory-config-data.service'; + +describe('InMemoryDataService', () => { + let service: InMemoryConfigDataService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(InMemoryConfigDataService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/in-memory-config-data.service.ts b/src/app/in-memory-config-data.service.ts new file mode 100644 index 000000000..33ee0986a --- /dev/null +++ b/src/app/in-memory-config-data.service.ts @@ -0,0 +1,114 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class InMemoryConfigDataService { + createDb() { + const config = { + "version": 15, + "folders": [ + { + "id": "GXWxf-3zgnU", + "label": "MyFolder", + "path": "...", + "type": "sendreceive", + "devices": [ + { + "deviceID": "..." + } + ], + "rescanIntervalS": 60, + "ignorePerms": false, + "autoNormalize": true, + "minDiskFreePct": 1, + "versioning": { + "type": "simple", + "params": { + "keep": "5" + } + }, + "copiers": 0, + "pullers": 0, + "hashers": 0, + "order": "random", + "ignoreDelete": false, + "scanProgressIntervalS": 0, + "pullerSleepS": 0, + "pullerPauseS": 0, + "maxConflicts": 10, + "disableSparseFiles": false, + "disableTempIndexes": false, + "fsync": false, + "invalid": "" + } + ], + "devices": [ + { + "deviceID": "...", + "name": "Laptop", + "addresses": [ + "dynamic", + "tcp://192.168.1.2:22000" + ], + "compression": "metadata", + "certName": "", + "introducer": false + } + ], + "gui": { + "enabled": true, + "address": "127.0.0.1:8384", + "user": "Username", + "password": "$2a$10$ZFws69T4FlvWwsqeIwL.TOo5zOYqsa/.TxlUnsGYS.j3JvjFTmxo6", + "useTLS": false, + "apiKey": "pGahcht56664QU5eoFQW6szbEG6Ec2Cr", + "insecureAdminAccess": false, + "theme": "default" + }, + "options": { + "listenAddresses": [ + "default" + ], + "globalAnnounceServers": [ + "default" + ], + "globalAnnounceEnabled": true, + "localAnnounceEnabled": true, + "localAnnouncePort": 21027, + "localAnnounceMCAddr": "[ff12::8384]:21027", + "maxSendKbps": 0, + "maxRecvKbps": 0, + "reconnectionIntervalS": 60, + "relaysEnabled": true, + "relayReconnectIntervalM": 10, + "startBrowser": false, + "natEnabled": true, + "natLeaseMinutes": 60, + "natRenewalMinutes": 30, + "natTimeoutSeconds": 10, + "urAccepted": -1, + "urUniqueId": "", + "urURL": "https://data.syncthing.net/newdata", + "urPostInsecurely": false, + "urInitialDelayS": 1800, + "restartOnWakeup": true, + "autoUpgradeIntervalH": 12, + "keepTemporariesH": 24, + "cacheIgnoredFiles": false, + "progressUpdateIntervalS": 5, + "limitBandwidthInLan": false, + "minHomeDiskFreePct": 1, + "releasesURL": "https://upgrades.syncthing.net/meta.json", + "alwaysLocalNets": [], + "overwriteRemoteDeviceNamesOnConnect": false, + "tempIndexMinBlocks": 10 + }, + "ignoredDevices": [], + "ignoredFolders": [] + } + console.log("in mem?!?!?", config) + return { config }; + } + constructor() { } +} diff --git a/src/app/system-config.service.ts b/src/app/system-config.service.ts index c01dfe1db..26644b211 100644 --- a/src/app/system-config.service.ts +++ b/src/app/system-config.service.ts @@ -8,6 +8,7 @@ import { Folder } from './folder'; import { Device } from './device'; import { FOLDERS, DEVICES } from './mock-config-data'; import { CookieService } from './cookie.service'; +import { environment } from '../environments/environment' import { apiURL } from './api-utils' @Injectable({ @@ -19,13 +20,15 @@ export class SystemConfigService { private devices: Device[]; private foldersSubject: Subject = new Subject(); private devicesSubject: Subject = new Subject(); - private systemConfigUrl = apiURL + '/rest/system/config'; // URL to web api + private systemConfigUrl = environment.production ? apiURL + '/rest/system/config' : 'api/config'; - httpOptions = { headers: new HttpHeaders(this.cookieService.getCSRFHeader()) }; + private httpOptions; constructor(private http: HttpClient, private cookieService: CookieService) { } - ngOnInit(): void { } + ngOnInit(): void { + this.httpOptions = { headers: new HttpHeaders(this.cookieService.getCSRFHeader()) }; + } getSystemConfig(): Observable { return this.http