Skip to content

Commit f81265b

Browse files
committed
add environment support
1 parent 775e5db commit f81265b

File tree

8 files changed

+158
-105
lines changed

8 files changed

+158
-105
lines changed

.vscode/settings.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"deno.enable": true,
3+
"deno.unstable": true
4+
}

manager/.env.sample

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ GRAPHQL_SERVER_PORT=
1111
INFLUX_URL=
1212
INFLUX_TOKEN=
1313
INFLUX_ORG=
14-
INFLUX_BUCKET=
14+
INFLUX_BUCKET=
15+
ENVIRONMENT= # dev | staging | prod

manager/deno.lock

+88-73
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manager/main.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ if (import.meta.main) {
3939
},
4040
})
4141
logger('manager').info('Starting Arkiver...')
42-
const dev = Deno.env.get('DEV') !== undefined
43-
const manager = new ArkiveManager({ dev })
42+
const environment = Deno.env.get('ENVIRONMENT')
43+
if (!environment) throw new Error('ENVIRONMENT not set')
44+
const manager = new ArkiveManager({ environment })
4445
await manager.init()
4546
}

manager/packages/manager/manager.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ export class ArkiveManager {
1717
private arkives: { arkive: arkiverTypes.Arkive; worker: Worker }[] = []
1818
private rpcUrls: Record<string, string>
1919

20-
constructor(params: { dev: boolean }) {
20+
constructor(params: { environment: string }) {
2121
this.removeAllArkives = this.removeAllArkives.bind(this)
2222
this.addNewArkive = this.addNewArkive.bind(this)
2323

24-
this.arkiveProvider = params.dev
24+
const environment = params.environment.toLowerCase()
25+
this.arkiveProvider = environment === "dev"
2526
? new LocalArkiveProvider()
26-
: new SupabaseProvider()
27+
: new SupabaseProvider({ environment })
2728
this.dataProvider = new MongoDataProvider()
2829
this.graphQLServer = new GraphQLServer(this.arkiveProvider) // TODO implement GraphQL server
2930
this.rpcUrls = collectRpcUrls()
@@ -151,7 +152,7 @@ export class ArkiveManager {
151152
// check if previous version is an older major version
152153
if (
153154
previousVersion.arkive.deployment.major_version <
154-
arkive.deployment.major_version
155+
arkive.deployment.major_version
155156
) {
156157
logger('manager').info(
157158
'removing old major version',
@@ -198,9 +199,9 @@ export class ArkiveManager {
198199
a.arkive.id === arkive.id && // same id
199200
(a.arkive.deployment.major_version < arkive.deployment.major_version || // older major version
200201
(a.arkive.deployment.major_version === // same major version but older minor version
201-
arkive.deployment.major_version &&
202+
arkive.deployment.major_version &&
202203
a.arkive.deployment.minor_version <
203-
arkive.deployment.minor_version)),
204+
arkive.deployment.minor_version)),
204205
)
205206
}
206207

manager/packages/providers/supabase.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ArkiveProvider } from './interfaces.ts'
22
import {
3-
arkiver,
43
arkiverTypes,
54
path as denoPath,
65
supabase,
76
} from '../../deps.ts'
87
import { getEnv, getSupabaseClient, rm, unpack } from '../utils.ts'
98
import { arkivesDir } from '../manager/manager.ts'
9+
import { logger } from '../logger.ts'
1010

1111
interface RawArkive extends Omit<arkiverTypes.Arkive, 'deployment'> {
1212
deployments: arkiverTypes.Deployment[]
@@ -16,15 +16,18 @@ export class SupabaseProvider implements ArkiveProvider {
1616
private supabase: supabase.SupabaseClient
1717
private newArkiveListener?: supabase.RealtimeChannel
1818
private deletedArkiveListener?: supabase.RealtimeChannel
19+
private environment: string
1920

20-
constructor() {
21+
constructor(params: { environment: string }) {
2122
this.supabase = getSupabaseClient()
23+
this.environment = params.environment
2224
}
2325

2426
public async getArkives(): Promise<arkiverTypes.Arkive[]> {
2527
const arkivesRes = await this.supabase
2628
.from(getEnv('SUPABASE_ARKIVE_TABLE'))
2729
.select<'*, deployments(*)', RawArkive>('*, deployments(*)')
30+
.eq('environment', this.environment)
2831

2932
if (arkivesRes.error) {
3033
throw arkivesRes.error
@@ -82,19 +85,20 @@ export class SupabaseProvider implements ArkiveProvider {
8285
)
8386
.select<'*', Omit<arkiverTypes.Arkive, 'deployment'>>('*')
8487
.eq('id', payload.new.arkive_id)
85-
.single()
88+
.eq('environment', this.environment)
8689
if (e) {
8790
const error = {
8891
...e,
8992
name: 'SupabaseProvider.listenNewArkive',
9093
} satisfies Error
91-
arkiver.logger().error(error, {
94+
logger('manager').error(error, {
9295
source: 'SupabaseProvider.listenNewArkive',
9396
})
9497
return
9598
}
99+
if (data.length === 0) return
96100
const newArkive = {
97-
...data,
101+
...data[0],
98102
deployment: payload.new,
99103
}
100104
await callback(newArkive)
@@ -189,6 +193,6 @@ export class SupabaseProvider implements ArkiveProvider {
189193
if (this.deletedArkiveListener) {
190194
this.deletedArkiveListener.unsubscribe()
191195
}
192-
arkiver.logger().info('closed')
196+
logger('manager').info('closed')
193197
}
194198
}

manager/packages/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const collectRpcUrls = () => {
3939
for (const chain of Object.keys(arkiver.supportedChains)) {
4040
try {
4141
rpcUrls[chain] = getEnv(`${chain.toUpperCase()}_RPC_URL`)
42-
} catch (e) {}
42+
} catch (e) { }
4343
}
4444
return rpcUrls
4545
}

supabase/functions/arkives/post.ts

+43-16
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ type PostParams = Partial<
1111
isPublic: string;
1212
update: "major" | "minor";
1313
manifest: any;
14+
env?: string;
1415
}
1516
>;
1617

1718
export const post = async (
1819
supabase: SupabaseClient,
1920
params: PostParams,
2021
) => {
21-
const { userId, name, pkg, isPublic, update, manifest } = params;
22+
const { userId, name, pkg, isPublic, update, manifest, env } = params;
2223
const parsedManifest = JSON.parse(manifest);
2324
// check params
2425
if (!userId || !name || !pkg) {
@@ -29,12 +30,13 @@ export const post = async (
2930
const selectRes = await supabase
3031
.from(getEnv("ARKIVE_TABLE"))
3132
.select<
32-
"id, deployments(major_version, minor_version)",
33+
"id, env, deployments(major_version, minor_version)",
3334
{
3435
id: string;
36+
env: string;
3537
deployments: { major_version: number; minor_version: number }[];
3638
}
37-
>("id, deployments(major_version, minor_version)")
39+
>("id, env, deployments(major_version, minor_version)")
3840
.eq("user_id", userId)
3941
.eq("name", name);
4042

@@ -54,16 +56,20 @@ export const post = async (
5456
userId,
5557
update,
5658
manifest: parsedManifest,
59+
env
5760
},
5861
);
5962
} else {
6063
return await createDeployment(
61-
supabase,
62-
userId,
63-
name,
64-
pkg,
65-
isPublic,
66-
parsedManifest,
64+
{
65+
supabase,
66+
userId,
67+
name,
68+
pkg,
69+
isPublic,
70+
manifest: parsedManifest,
71+
env
72+
}
6773
);
6874
}
6975
};
@@ -73,16 +79,18 @@ const updateDeployment = async (
7379
arkive: {
7480
id: string;
7581
deployments: { major_version: number; minor_version: number }[];
82+
env: string;
7683
},
7784
params: {
7885
userId: string;
7986
pkg: File;
8087
update: "major" | "minor";
8188
manifest: any;
89+
env?: string;
8290
},
8391
) => {
8492
// check params
85-
const { userId, pkg, update, manifest } = params;
93+
const { userId, pkg, update, manifest, env } = params;
8694
if (
8795
(update !== "major" && update !== "minor")
8896
) {
@@ -147,24 +155,43 @@ const updateDeployment = async (
147155
throw insertRes.error;
148156
}
149157

158+
if (env && env !== arkive.env) {
159+
const updateRes = await supabase
160+
.from(getEnv("ARKIVE_TABLE"))
161+
.update<{ env: string }>({
162+
env,
163+
})
164+
.eq("id", arkive.id);
165+
166+
if (updateRes.error) {
167+
throw updateRes.error;
168+
}
169+
}
170+
150171
return insertRes.data;
151172
};
152173

153174
const createDeployment = async (
154-
supabase: SupabaseClient,
155-
userId: string,
156-
name: string,
157-
pkg: File,
158-
isPublic: string | undefined,
159-
manifest: any,
175+
params: {
176+
supabase: SupabaseClient,
177+
userId: string,
178+
name: string,
179+
pkg: File,
180+
isPublic: string | undefined,
181+
manifest: any,
182+
env?: string
183+
}
160184
) => {
185+
const { supabase, userId, name, pkg, isPublic, manifest, env } = params;
186+
161187
// insert new row to arkive table
162188
const insertArkiveRes = await supabase
163189
.from(getEnv("ARKIVE_TABLE"))
164190
.insert({
165191
user_id: userId,
166192
name,
167193
public: isPublic !== undefined,
194+
env: env ?? "staging",
168195
})
169196
.select<"id", { id: string }>("id");
170197

0 commit comments

Comments
 (0)