-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: analytics and api deployment-specific runtime configuration #2412
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9ba340f
feat: wip deployment config injection token
chrismclarke 8c3f7e3
chore: code tidying
chrismclarke 42410db
refactor: analytics module
chrismclarke a2bd9ae
refactor: http interceptors
chrismclarke 63972ae
chore: code tidying
chrismclarke 7a6d0bb
Merge branch 'master' into feat/runtime-deployment-2
chrismclarke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NgModule } from "@angular/core"; | ||
|
||
import { AnalyticsModule } from "./shared/services/analytics"; | ||
|
||
/** | ||
* Module imports required for specific deployment features | ||
* | ||
* NOTE - as angular needs all modules to be statically defined during compilation | ||
* it is not possible to conditionally load modules at runtime. | ||
* | ||
* Therefore all modules are defined and loaded as part of the core build process, | ||
* but it is still possible to override this file to create specific feature-optimised builds | ||
* | ||
* This is a feature marked for future implementation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, I can see how listing the child modules here could work |
||
*/ | ||
@NgModule({ imports: [AnalyticsModule] }) | ||
export class DeploymentFeaturesModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { NgModule } from "@angular/core"; | ||
|
||
import { | ||
MATOMO_CONFIGURATION, | ||
MatomoConfiguration, | ||
provideMatomo, | ||
withRouter, | ||
} from "ngx-matomo-client"; | ||
|
||
import { IDeploymentRuntimeConfig } from "packages/data-models"; | ||
import { DEPLOYMENT_CONFIG } from "../deployment/deployment.service"; | ||
import { environment } from "src/environments/environment"; | ||
|
||
/** When running locally can configure to target local running containing (if required) */ | ||
const devConfig: MatomoConfiguration = { | ||
disabled: true, | ||
trackerUrl: "http://localhost/analytics", | ||
siteId: 1, | ||
}; | ||
|
||
/** | ||
* When configuring the analytics module | ||
* This should be imported into the main app.module.ts | ||
*/ | ||
@NgModule({ | ||
imports: [], | ||
providers: [ | ||
provideMatomo(null, withRouter()), | ||
// Dynamically provide the configuration used by the matomo provider so that it can | ||
// access deployment config (injected from token) | ||
{ | ||
provide: MATOMO_CONFIGURATION, | ||
useFactory: (deploymentConfig: IDeploymentRuntimeConfig): MatomoConfiguration => { | ||
if (environment.production) { | ||
const { enabled, endpoint, siteId } = deploymentConfig.analytics; | ||
return { disabled: !enabled, siteId, trackerUrl: endpoint }; | ||
} else { | ||
return devConfig; | ||
} | ||
}, | ||
deps: [DEPLOYMENT_CONFIG], | ||
}, | ||
], | ||
}) | ||
export class AnalyticsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./analytics.module"; | ||
export * from "./analytics.service"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,33 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { AsyncServiceBase } from "../asyncService.base"; | ||
import { HttpClient } from "@angular/common/http"; | ||
import { catchError, map, of, firstValueFrom } from "rxjs"; | ||
import { DEPLOYMENT_RUNTIME_CONFIG_DEFAULTS, IDeploymentRuntimeConfig } from "packages/data-models"; | ||
import { Inject, Injectable, InjectionToken } from "@angular/core"; | ||
import { IDeploymentRuntimeConfig } from "packages/data-models"; | ||
import { SyncServiceBase } from "../syncService.base"; | ||
|
||
@Injectable({ providedIn: "root" }) | ||
/** | ||
* Deployment runtime config settings | ||
* Token to inject deployment config value into any service. | ||
* This is populated from json file before platform load, as part of src\main.ts | ||
* | ||
* NOTE - this is intialized using an `APP_INITIALIZER` token within | ||
* the main app.module.ts and will block all other services from loading until | ||
* it is fully initialised | ||
* Can be used directly by any service or module initialised at any time | ||
* (including app.module.ts). | ||
* | ||
* Services that access the deployment config therefore do not need to await | ||
* DeploymentService init/ready methods. | ||
* @example Inject into service | ||
* ```ts | ||
* constructor(@Inject(DEPLOYMENT_CONFIG)) | ||
* ``` | ||
* @example Inject into module | ||
* ``` | ||
* {provide: MyModule, useFactory:(config)=>{...}, deps: [DEPLOYMENT_CONFIG]`} | ||
* ``` | ||
*/ | ||
export class DeploymentService extends AsyncServiceBase { | ||
constructor(private http: HttpClient) { | ||
super("Deployment Service"); | ||
this.registerInitFunction(this.initialise); | ||
} | ||
|
||
/** Private writeable config to allow population from JSON */ | ||
private _config = DEPLOYMENT_RUNTIME_CONFIG_DEFAULTS; | ||
|
||
/** Read-only access to deployment runtime config */ | ||
public get config() { | ||
return this._config; | ||
} | ||
export const DEPLOYMENT_CONFIG: InjectionToken<IDeploymentRuntimeConfig> = | ||
new InjectionToken<IDeploymentRuntimeConfig>("Application Configuration"); | ||
|
||
/** Load active deployment configuration from JSON file */ | ||
private async initialise() { | ||
const deployment = await firstValueFrom(this.loadDeployment()); | ||
if (deployment) { | ||
this._config = deployment; | ||
} | ||
} | ||
|
||
private loadDeployment() { | ||
return this.http.get("assets/app_data/deployment.json").pipe( | ||
catchError(() => { | ||
console.warn("No deployment config available"); | ||
return of(null); | ||
}), | ||
map((v) => v as IDeploymentRuntimeConfig) | ||
); | ||
/** | ||
* The deployment service provides access to values loaded from the deployment json file | ||
* It is an alternative to injecting directly via `@Inject(DEPLOYMENT_CONFIG)` | ||
*/ | ||
@Injectable({ providedIn: "root" }) | ||
export class DeploymentService extends SyncServiceBase { | ||
constructor(@Inject(DEPLOYMENT_CONFIG) public readonly config: IDeploymentRuntimeConfig) { | ||
super("Deployment Service"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new deployment config available (previously hardcoded into environment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good, shall we add some example values to the debug deployment config, if you haven't done so already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I've just added a new siteId for storing debug analytics and will make a separate PR on the content repo to update.