Skip to content

Commit

Permalink
use environment variable to swap
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhouweling committed Nov 12, 2024
1 parent 2f43834 commit c73dac6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ jobs:
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
environment: ${{ github.event.workflow_run.head_branch == 'main' && 'production' || 'staging' }}
vars: |
KV_NAMESPACE
DISCORD_APP_ID
DISCORD_PUBLIC_KEY
XBOX_USERNAME
secrets: |
DISCORD_TOKEN
XBOX_PASSWORD
env:
KV_NAMESPACE: ${{ vars.KV_NAMESPACE }}
DISCORD_APP_ID: ${{ vars.DISCORD_APP_ID }}
DISCORD_PUBLIC_KEY: ${{ vars.DISCORD_PUBLIC_KEY }}
DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }}
Expand Down
6 changes: 5 additions & 1 deletion src/services/install.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { DiscordService } from "./discord/discord.mjs";
import { HaloService } from "./halo/halo.mjs";
import { KvService } from "./kv/kv.mjs";
import { XboxService } from "./xbox/xbox.mjs";

export interface Services {
kvService: KvService;
discordService: DiscordService;
xboxService: XboxService;
haloService: HaloService;
Expand All @@ -13,11 +15,13 @@ interface InstallServicesOpts {
}

export function installServices({ env }: InstallServicesOpts): Services {
const kvService = new KvService({ env });
const discordService = new DiscordService({ env });
const xboxService = new XboxService({ env });
const xboxService = new XboxService({ env, kvService });
const haloService = new HaloService({ xboxService });

return {
kvService,
discordService,
xboxService,
haloService,
Expand Down
19 changes: 19 additions & 0 deletions src/services/kv/kv.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
interface KvServiceOpts {
env: Env;
}

export class KvService {
private readonly env: Env;

constructor({ env }: KvServiceOpts) {
this.env = env;
}

get kv() {
const kv = this.env[this.env.KV_NAMESPACE as keyof Env];

Check failure on line 13 in src/services/kv/kv.mts

View workflow job for this annotation

GitHub Actions / Run Build

Property 'KV_NAMESPACE' does not exist on type 'Env'.
if (!kv) {
throw new Error(`KV namespace ${this.env.KV_NAMESPACE} not found`);

Check failure on line 15 in src/services/kv/kv.mts

View workflow job for this annotation

GitHub Actions / Run Build

Property 'KV_NAMESPACE' does not exist on type 'Env'.

Check failure on line 15 in src/services/kv/kv.mts

View workflow job for this annotation

GitHub Actions / Run linters

Invalid type "any" of template literal expression
}
return kv as KVNamespace;
}
}
12 changes: 8 additions & 4 deletions src/services/xbox/xbox.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { authenticate, CredentialsAuthenticateInitialResponse } from "@xboxreplay/xboxlive-auth";
import { KvService } from "../kv/kv.mjs";

enum TokenInfoKey {
XSTSToken,
Expand All @@ -7,14 +8,17 @@ enum TokenInfoKey {

interface XboxServiceOpts {
env: Env;
kvService: KvService;
}

export class XboxService {
private readonly env: Env;
private readonly kvService: KvService;
private tokenInfoMap = new Map<TokenInfoKey, string>();

constructor({ env }: XboxServiceOpts) {
constructor({ env, kvService }: XboxServiceOpts) {
this.env = env;
this.kvService = kvService;

void this.loadCredentials();
}
Expand All @@ -33,11 +37,11 @@ export class XboxService {

clearToken() {
this.tokenInfoMap.clear();
void this.env.SERVICE_API_TOKENS.delete("xbox");
void this.kvService.kv.delete("xbox");
}

private async loadCredentials() {
const tokenInfo = await this.env.SERVICE_API_TOKENS.get("xbox");
const tokenInfo = await this.kvService.kv.get("xbox");
console.log("loading token info", tokenInfo);
if (tokenInfo) {
try {
Expand All @@ -57,7 +61,7 @@ export class XboxService {
this.tokenInfoMap.set(TokenInfoKey.expiresOn, credentialsResponse.expires_on);

console.log("updating token info...");
await this.env.SERVICE_API_TOKENS.put("xbox", JSON.stringify(Array.from(this.tokenInfoMap.entries())), {
await this.kvService.kv.put("xbox", JSON.stringify(Array.from(this.tokenInfoMap.entries())), {
expirationTtl: Math.floor((new Date(credentialsResponse.expires_on).getTime() - new Date().getTime()) / 1000),
});
console.log("updated token info");
Expand Down
5 changes: 4 additions & 1 deletion worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Generated by Wrangler by running `wrangler types --experimental-include-runtime`

interface Env {
SERVICE_API_TOKENS: KVNamespace;
SERVICE_API_TOKENS_DEV: KVNamespace;
SERVICE_API_TOKENS_STAGING: KVNamespace;
SERVICE_API_TOKENS_PRODUCTION: KVNamespace;
DISCORD_APP_ID: string;
DISCORD_TOKEN: string;
DISCORD_PUBLIC_KEY: string;
XBOX_USERNAME: string;
XBOX_PASSWORD: string;
KV_NAMESPACE: string;
}
12 changes: 8 additions & 4 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ upload_source_maps = true
enabled = true
head_sampling_rate = 1 # optional. default = 1.

[[kv_namespaces]]
binding = "SERVICE_API_TOKENS_DEV"
id = "09cd8999e6974145bd11b533f9f8afaa"

[env.staging]
name = "guilty-spark-staging"
[[kv_namespaces]]
binding = "SERVICE_API_TOKENS"
id = "ced62798d67d44e89104646327a0b44c"
binding = "SERVICE_API_TOKENS_STAGING"
id = "0bda29a4f4f046c18495f498dc3ff926"

[env.production]
name = "guilty-spark"
[[kv_namespaces]]
binding = "SERVICE_API_TOKENS"
id = "7332bc767e3e43738ec3bdbd6d22db15"
binding = "SERVICE_API_TOKENS_PRODUCTION"
id = "627e6ea829d347ff848dc11cebc43923"

0 comments on commit c73dac6

Please sign in to comment.