@odvi/create-dtt-framework 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/template.js +14 -1
- package/dist/utils/template.js.map +1 -1
- package/package.json +1 -1
- package/template/.env.example +117 -106
- package/template/DESIGN.md +1052 -0
- package/template/LICENSE +21 -0
- package/template/docs/framework/environment-variables.md +52 -0
- package/template/drizzle/0000_awesome_argent.sql +18 -0
- package/template/drizzle/meta/0000_snapshot.json +129 -0
- package/template/drizzle/meta/_journal.json +13 -0
- package/template/eslint.config.js +61 -0
- package/template/src/config/env.ts +5 -0
- package/template/src/features/health-check/config.ts +7 -0
- package/template/src/server/api/routes/health/aws-s3.ts +59 -0
- package/template/src/server/api/routes/health/index.ts +7 -3
- package/template/start-database.sh +88 -0
package/template/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 DTT Framework
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -97,6 +97,19 @@ SNOWFLAKE_SCHEMA=PUBLIC
|
|
|
97
97
|
# Snowflake role name
|
|
98
98
|
SNOWFLAKE_ROLE=ANALYST
|
|
99
99
|
|
|
100
|
+
# ============================================
|
|
101
|
+
# AWS S3 (Optional)
|
|
102
|
+
# ============================================
|
|
103
|
+
|
|
104
|
+
# AWS Access Key ID
|
|
105
|
+
AWS_ACCESS_KEY_ID=AKIA...
|
|
106
|
+
|
|
107
|
+
# AWS Secret Access Key
|
|
108
|
+
AWS_ACCESS_SECRET_KEY=wJalr...
|
|
109
|
+
|
|
110
|
+
# AWS Region (e.g., us-east-1, ap-southeast-1)
|
|
111
|
+
AWS_REGION=us-east-1
|
|
112
|
+
|
|
100
113
|
# ============================================
|
|
101
114
|
# NextBank (Placeholder - Optional)
|
|
102
115
|
# ============================================
|
|
@@ -418,6 +431,45 @@ SNOWFLAKE_ROLE=ANALYST
|
|
|
418
431
|
|
|
419
432
|
---
|
|
420
433
|
|
|
434
|
+
### AWS S3 Variables (Optional)
|
|
435
|
+
|
|
436
|
+
#### AWS_ACCESS_KEY_ID
|
|
437
|
+
|
|
438
|
+
**Purpose:** AWS Access Key ID for S3 access
|
|
439
|
+
|
|
440
|
+
**Where to find:** AWS IAM Console → Users → Security credentials
|
|
441
|
+
|
|
442
|
+
**Example:**
|
|
443
|
+
```bash
|
|
444
|
+
AWS_ACCESS_KEY_ID=AKIA...
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
#### AWS_ACCESS_SECRET_KEY
|
|
448
|
+
|
|
449
|
+
**Purpose:** AWS Secret Access Key for S3 access
|
|
450
|
+
|
|
451
|
+
**Where to find:** AWS IAM Console → Users → Security credentials (only visible when created)
|
|
452
|
+
|
|
453
|
+
**Example:**
|
|
454
|
+
```bash
|
|
455
|
+
AWS_ACCESS_SECRET_KEY=wJalr...
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
**Notes:**
|
|
459
|
+
- Never commit to version control
|
|
460
|
+
- Ensure the user has appropriate S3 permissions (e.g., `AmazonS3ReadOnlyAccess` or custom policy)
|
|
461
|
+
|
|
462
|
+
#### AWS_REGION
|
|
463
|
+
|
|
464
|
+
**Purpose:** AWS Region where your S3 buckets are located
|
|
465
|
+
|
|
466
|
+
**Example:**
|
|
467
|
+
```bash
|
|
468
|
+
AWS_REGION=us-east-1
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
---
|
|
472
|
+
|
|
421
473
|
### NextBank Variables (Placeholder)
|
|
422
474
|
|
|
423
475
|
**Note:** NextBank is a placeholder integration. These variables are optional and not currently used.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
CREATE TABLE "users" (
|
|
2
|
+
"id" text PRIMARY KEY NOT NULL,
|
|
3
|
+
"email" varchar(255) NOT NULL,
|
|
4
|
+
"first_name" varchar(255),
|
|
5
|
+
"last_name" varchar(255),
|
|
6
|
+
"image_url" text,
|
|
7
|
+
"clerk_org_id" text,
|
|
8
|
+
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
9
|
+
"updated_at" timestamp DEFAULT now() NOT NULL,
|
|
10
|
+
CONSTRAINT "users_email_unique" UNIQUE("email")
|
|
11
|
+
);
|
|
12
|
+
--> statement-breakpoint
|
|
13
|
+
CREATE TABLE "health_check_tests" (
|
|
14
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
15
|
+
"test_key" text NOT NULL,
|
|
16
|
+
"test_value" text,
|
|
17
|
+
"created_at" timestamp DEFAULT now() NOT NULL
|
|
18
|
+
);
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "8ef2966f-b7b3-4fd4-adfc-84bfe2fdbf38",
|
|
3
|
+
"prevId": "00000000-0000-0000-0000-000000000000",
|
|
4
|
+
"version": "7",
|
|
5
|
+
"dialect": "postgresql",
|
|
6
|
+
"tables": {
|
|
7
|
+
"public.users": {
|
|
8
|
+
"name": "users",
|
|
9
|
+
"schema": "",
|
|
10
|
+
"columns": {
|
|
11
|
+
"id": {
|
|
12
|
+
"name": "id",
|
|
13
|
+
"type": "text",
|
|
14
|
+
"primaryKey": true,
|
|
15
|
+
"notNull": true
|
|
16
|
+
},
|
|
17
|
+
"email": {
|
|
18
|
+
"name": "email",
|
|
19
|
+
"type": "varchar(255)",
|
|
20
|
+
"primaryKey": false,
|
|
21
|
+
"notNull": true
|
|
22
|
+
},
|
|
23
|
+
"first_name": {
|
|
24
|
+
"name": "first_name",
|
|
25
|
+
"type": "varchar(255)",
|
|
26
|
+
"primaryKey": false,
|
|
27
|
+
"notNull": false
|
|
28
|
+
},
|
|
29
|
+
"last_name": {
|
|
30
|
+
"name": "last_name",
|
|
31
|
+
"type": "varchar(255)",
|
|
32
|
+
"primaryKey": false,
|
|
33
|
+
"notNull": false
|
|
34
|
+
},
|
|
35
|
+
"image_url": {
|
|
36
|
+
"name": "image_url",
|
|
37
|
+
"type": "text",
|
|
38
|
+
"primaryKey": false,
|
|
39
|
+
"notNull": false
|
|
40
|
+
},
|
|
41
|
+
"clerk_org_id": {
|
|
42
|
+
"name": "clerk_org_id",
|
|
43
|
+
"type": "text",
|
|
44
|
+
"primaryKey": false,
|
|
45
|
+
"notNull": false
|
|
46
|
+
},
|
|
47
|
+
"created_at": {
|
|
48
|
+
"name": "created_at",
|
|
49
|
+
"type": "timestamp",
|
|
50
|
+
"primaryKey": false,
|
|
51
|
+
"notNull": true,
|
|
52
|
+
"default": "now()"
|
|
53
|
+
},
|
|
54
|
+
"updated_at": {
|
|
55
|
+
"name": "updated_at",
|
|
56
|
+
"type": "timestamp",
|
|
57
|
+
"primaryKey": false,
|
|
58
|
+
"notNull": true,
|
|
59
|
+
"default": "now()"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"indexes": {},
|
|
63
|
+
"foreignKeys": {},
|
|
64
|
+
"compositePrimaryKeys": {},
|
|
65
|
+
"uniqueConstraints": {
|
|
66
|
+
"users_email_unique": {
|
|
67
|
+
"name": "users_email_unique",
|
|
68
|
+
"nullsNotDistinct": false,
|
|
69
|
+
"columns": [
|
|
70
|
+
"email"
|
|
71
|
+
]
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"policies": {},
|
|
75
|
+
"checkConstraints": {},
|
|
76
|
+
"isRLSEnabled": false
|
|
77
|
+
},
|
|
78
|
+
"public.health_check_tests": {
|
|
79
|
+
"name": "health_check_tests",
|
|
80
|
+
"schema": "",
|
|
81
|
+
"columns": {
|
|
82
|
+
"id": {
|
|
83
|
+
"name": "id",
|
|
84
|
+
"type": "uuid",
|
|
85
|
+
"primaryKey": true,
|
|
86
|
+
"notNull": true,
|
|
87
|
+
"default": "gen_random_uuid()"
|
|
88
|
+
},
|
|
89
|
+
"test_key": {
|
|
90
|
+
"name": "test_key",
|
|
91
|
+
"type": "text",
|
|
92
|
+
"primaryKey": false,
|
|
93
|
+
"notNull": true
|
|
94
|
+
},
|
|
95
|
+
"test_value": {
|
|
96
|
+
"name": "test_value",
|
|
97
|
+
"type": "text",
|
|
98
|
+
"primaryKey": false,
|
|
99
|
+
"notNull": false
|
|
100
|
+
},
|
|
101
|
+
"created_at": {
|
|
102
|
+
"name": "created_at",
|
|
103
|
+
"type": "timestamp",
|
|
104
|
+
"primaryKey": false,
|
|
105
|
+
"notNull": true,
|
|
106
|
+
"default": "now()"
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
"indexes": {},
|
|
110
|
+
"foreignKeys": {},
|
|
111
|
+
"compositePrimaryKeys": {},
|
|
112
|
+
"uniqueConstraints": {},
|
|
113
|
+
"policies": {},
|
|
114
|
+
"checkConstraints": {},
|
|
115
|
+
"isRLSEnabled": false
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
"enums": {},
|
|
119
|
+
"schemas": {},
|
|
120
|
+
"sequences": {},
|
|
121
|
+
"roles": {},
|
|
122
|
+
"policies": {},
|
|
123
|
+
"views": {},
|
|
124
|
+
"_meta": {
|
|
125
|
+
"columns": {},
|
|
126
|
+
"schemas": {},
|
|
127
|
+
"tables": {}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { FlatCompat } from "@eslint/eslintrc";
|
|
2
|
+
import tseslint from "typescript-eslint";
|
|
3
|
+
// @ts-ignore -- no types for this plugin
|
|
4
|
+
import drizzle from "eslint-plugin-drizzle";
|
|
5
|
+
|
|
6
|
+
const compat = new FlatCompat({
|
|
7
|
+
baseDirectory: import.meta.dirname,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export default tseslint.config(
|
|
11
|
+
{
|
|
12
|
+
ignores: [".next"],
|
|
13
|
+
},
|
|
14
|
+
...compat.extends("next/core-web-vitals"),
|
|
15
|
+
{
|
|
16
|
+
files: ["**/*.ts", "**/*.tsx"],
|
|
17
|
+
plugins: {
|
|
18
|
+
drizzle,
|
|
19
|
+
},
|
|
20
|
+
extends: [
|
|
21
|
+
...tseslint.configs.recommended,
|
|
22
|
+
...tseslint.configs.recommendedTypeChecked,
|
|
23
|
+
...tseslint.configs.stylisticTypeChecked,
|
|
24
|
+
],
|
|
25
|
+
rules: {
|
|
26
|
+
"@typescript-eslint/array-type": "off",
|
|
27
|
+
"@typescript-eslint/consistent-type-definitions": "off",
|
|
28
|
+
"@typescript-eslint/consistent-type-imports": [
|
|
29
|
+
"warn",
|
|
30
|
+
{ prefer: "type-imports", fixStyle: "inline-type-imports" },
|
|
31
|
+
],
|
|
32
|
+
"@typescript-eslint/no-unused-vars": [
|
|
33
|
+
"warn",
|
|
34
|
+
{ argsIgnorePattern: "^_" },
|
|
35
|
+
],
|
|
36
|
+
"@typescript-eslint/require-await": "off",
|
|
37
|
+
"@typescript-eslint/no-misused-promises": [
|
|
38
|
+
"error",
|
|
39
|
+
{ checksVoidReturn: { attributes: false } },
|
|
40
|
+
],
|
|
41
|
+
"drizzle/enforce-delete-with-where": [
|
|
42
|
+
"error",
|
|
43
|
+
{ drizzleObjectName: ["db", "ctx.db"] },
|
|
44
|
+
],
|
|
45
|
+
"drizzle/enforce-update-with-where": [
|
|
46
|
+
"error",
|
|
47
|
+
{ drizzleObjectName: ["db", "ctx.db"] },
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
linterOptions: {
|
|
53
|
+
reportUnusedDisableDirectives: true,
|
|
54
|
+
},
|
|
55
|
+
languageOptions: {
|
|
56
|
+
parserOptions: {
|
|
57
|
+
projectService: true,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
);
|
|
@@ -30,6 +30,11 @@ const envSchema = z.object({
|
|
|
30
30
|
NEXTBANK_API: z.string().url().optional(),
|
|
31
31
|
NEXTBANK_API_USERNAME: z.string().optional(),
|
|
32
32
|
NEXTBANK_API_PASSWORD: z.string().optional(),
|
|
33
|
+
|
|
34
|
+
// AWS S3 (optional)
|
|
35
|
+
AWS_ACCESS_KEY_ID: z.string().optional(),
|
|
36
|
+
AWS_ACCESS_SECRET_KEY: z.string().optional(),
|
|
37
|
+
AWS_REGION: z.string().optional(),
|
|
33
38
|
})
|
|
34
39
|
|
|
35
40
|
export const env = envSchema.parse(process.env)
|
|
@@ -52,6 +52,13 @@ export const SERVICES = [
|
|
|
52
52
|
{ name: 'Ping API', endpoint: '/nextbank/ping' },
|
|
53
53
|
],
|
|
54
54
|
},
|
|
55
|
+
{
|
|
56
|
+
name: 'AWS S3',
|
|
57
|
+
icon: 'cloud',
|
|
58
|
+
checks: [
|
|
59
|
+
{ name: 'Connect', endpoint: '/aws-s3/connect' },
|
|
60
|
+
],
|
|
61
|
+
},
|
|
55
62
|
{
|
|
56
63
|
name: 'Framework',
|
|
57
64
|
icon: 'code',
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Hono } from 'hono'
|
|
2
|
+
import { S3Client, ListBucketsCommand } from '@aws-sdk/client-s3'
|
|
3
|
+
import { env } from '@/config/env'
|
|
4
|
+
|
|
5
|
+
export const awsS3HealthRoutes = new Hono()
|
|
6
|
+
|
|
7
|
+
awsS3HealthRoutes.get('/connect', async (c) => {
|
|
8
|
+
const start = performance.now()
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
// Check if credentials are configured
|
|
12
|
+
if (!env.AWS_ACCESS_KEY_ID || !env.AWS_ACCESS_SECRET_KEY || !env.AWS_REGION) {
|
|
13
|
+
return c.json({
|
|
14
|
+
status: 'warning',
|
|
15
|
+
responseTimeMs: Math.round(performance.now() - start),
|
|
16
|
+
message: 'AWS S3 credentials not configured - skipping connection check',
|
|
17
|
+
data: {
|
|
18
|
+
skipped: true,
|
|
19
|
+
reason: 'Missing AWS_ACCESS_KEY_ID, AWS_ACCESS_SECRET_KEY, or AWS_REGION',
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const s3Client = new S3Client({
|
|
25
|
+
region: env.AWS_REGION,
|
|
26
|
+
credentials: {
|
|
27
|
+
accessKeyId: env.AWS_ACCESS_KEY_ID,
|
|
28
|
+
secretAccessKey: env.AWS_ACCESS_SECRET_KEY,
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// Try to list buckets to verify connectivity and credentials
|
|
33
|
+
const command = new ListBucketsCommand({})
|
|
34
|
+
const response = await s3Client.send(command)
|
|
35
|
+
|
|
36
|
+
return c.json({
|
|
37
|
+
status: 'healthy',
|
|
38
|
+
responseTimeMs: Math.round(performance.now() - start),
|
|
39
|
+
message: 'Successfully connected to AWS S3',
|
|
40
|
+
data: {
|
|
41
|
+
region: env.AWS_REGION,
|
|
42
|
+
bucketCount: response.Buckets?.length ?? 0,
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
} catch (error) {
|
|
46
|
+
return c.json(
|
|
47
|
+
{
|
|
48
|
+
status: 'error',
|
|
49
|
+
responseTimeMs: Math.round(performance.now() - start),
|
|
50
|
+
error: error instanceof Error ? error.message : 'Failed to connect to AWS S3',
|
|
51
|
+
data: {
|
|
52
|
+
details: error instanceof Error ? error.stack : undefined,
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
500
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
@@ -6,6 +6,7 @@ import { edgeFunctionsHealthRoutes } from './edge-functions'
|
|
|
6
6
|
import { snowflakeHealthRoutes } from './snowflake'
|
|
7
7
|
import { nextbankHealthRoutes } from './nextbank'
|
|
8
8
|
import { frameworkHealthRoutes } from './framework'
|
|
9
|
+
import { awsS3HealthRoutes } from './aws-s3'
|
|
9
10
|
|
|
10
11
|
export const healthRoutes = new Hono()
|
|
11
12
|
|
|
@@ -16,6 +17,7 @@ healthRoutes.route('/edge', edgeFunctionsHealthRoutes)
|
|
|
16
17
|
healthRoutes.route('/snowflake', snowflakeHealthRoutes)
|
|
17
18
|
healthRoutes.route('/nextbank', nextbankHealthRoutes)
|
|
18
19
|
healthRoutes.route('/framework', frameworkHealthRoutes)
|
|
20
|
+
healthRoutes.route('/aws-s3', awsS3HealthRoutes)
|
|
19
21
|
|
|
20
22
|
healthRoutes.get('/all', async (c) => {
|
|
21
23
|
const start = performance.now()
|
|
@@ -38,6 +40,7 @@ healthRoutes.get('/all', async (c) => {
|
|
|
38
40
|
{ name: 'Snowflake Query', url: `${baseUrl}/api/health/snowflake/query` },
|
|
39
41
|
{ name: 'NextBank Ping', url: `${baseUrl}/api/health/nextbank/ping` },
|
|
40
42
|
{ name: 'NextBank Auth', url: `${baseUrl}/api/health/nextbank/auth` },
|
|
43
|
+
{ name: 'AWS S3 Connect', url: `${baseUrl}/api/health/aws-s3/connect` },
|
|
41
44
|
]
|
|
42
45
|
|
|
43
46
|
// Run all checks in parallel
|
|
@@ -46,7 +49,7 @@ healthRoutes.get('/all', async (c) => {
|
|
|
46
49
|
const checkStart = performance.now()
|
|
47
50
|
try {
|
|
48
51
|
const response = await fetch(check.url, {
|
|
49
|
-
method: check.method
|
|
52
|
+
method: check.method ?? 'GET',
|
|
50
53
|
headers: {
|
|
51
54
|
'Content-Type': 'application/json',
|
|
52
55
|
// Forward auth header if present
|
|
@@ -60,7 +63,7 @@ healthRoutes.get('/all', async (c) => {
|
|
|
60
63
|
|
|
61
64
|
return {
|
|
62
65
|
name: check.name,
|
|
63
|
-
status: response.ok ? data.status
|
|
66
|
+
status: response.ok ? data.status ?? 'healthy' : 'error',
|
|
64
67
|
responseTimeMs: Math.round(performance.now() - checkStart),
|
|
65
68
|
httpStatus: response.status,
|
|
66
69
|
data,
|
|
@@ -85,12 +88,13 @@ healthRoutes.get('/all', async (c) => {
|
|
|
85
88
|
edge: results.filter((r) => r.status === 'fulfilled' && r.value.name.includes('Edge')),
|
|
86
89
|
snowflake: results.filter((r) => r.status === 'fulfilled' && r.value.name.includes('Snowflake')),
|
|
87
90
|
nextbank: results.filter((r) => r.status === 'fulfilled' && r.value.name.includes('NextBank')),
|
|
91
|
+
awsS3: results.filter((r) => r.status === 'fulfilled' && r.value.name.includes('AWS S3')),
|
|
88
92
|
}
|
|
89
93
|
|
|
90
94
|
// Determine overall status
|
|
91
95
|
const allResults = results.map((r) => (r.status === 'fulfilled' ? r.value : null))
|
|
92
96
|
const hasErrors = allResults.some((r) => r && (r.status === 'error' || r.httpStatus >= 400))
|
|
93
|
-
const allHealthy = allResults.every((r) => r
|
|
97
|
+
const allHealthy = allResults.every((r) => r?.status === 'healthy')
|
|
94
98
|
|
|
95
99
|
return c.json({
|
|
96
100
|
status: hasErrors ? 'unhealthy' : allHealthy ? 'healthy' : 'partial',
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Use this script to start a docker container for a local development database
|
|
3
|
+
|
|
4
|
+
# TO RUN ON WINDOWS:
|
|
5
|
+
# 1. Install WSL (Windows Subsystem for Linux) - https://learn.microsoft.com/en-us/windows/wsl/install
|
|
6
|
+
# 2. Install Docker Desktop or Podman Deskop
|
|
7
|
+
# - Docker Desktop for Windows - https://docs.docker.com/docker-for-windows/install/
|
|
8
|
+
# - Podman Desktop - https://podman.io/getting-started/installation
|
|
9
|
+
# 3. Open WSL - `wsl`
|
|
10
|
+
# 4. Run this script - `./start-database.sh`
|
|
11
|
+
|
|
12
|
+
# On Linux and macOS you can run this script directly - `./start-database.sh`
|
|
13
|
+
|
|
14
|
+
# import env variables from .env
|
|
15
|
+
set -a
|
|
16
|
+
source .env
|
|
17
|
+
|
|
18
|
+
DB_PASSWORD=$(echo "$DATABASE_URL" | awk -F':' '{print $3}' | awk -F'@' '{print $1}')
|
|
19
|
+
DB_PORT=$(echo "$DATABASE_URL" | awk -F':' '{print $4}' | awk -F'\/' '{print $1}')
|
|
20
|
+
DB_NAME=$(echo "$DATABASE_URL" | awk -F'/' '{print $4}')
|
|
21
|
+
DB_CONTAINER_NAME="$DB_NAME-postgres"
|
|
22
|
+
|
|
23
|
+
if ! [ -x "$(command -v docker)" ] && ! [ -x "$(command -v podman)" ]; then
|
|
24
|
+
echo -e "Docker or Podman is not installed. Please install docker or podman and try again.\nDocker install guide: https://docs.docker.com/engine/install/\nPodman install guide: https://podman.io/getting-started/installation"
|
|
25
|
+
exit 1
|
|
26
|
+
fi
|
|
27
|
+
|
|
28
|
+
# determine which docker command to use
|
|
29
|
+
if [ -x "$(command -v docker)" ]; then
|
|
30
|
+
DOCKER_CMD="docker"
|
|
31
|
+
elif [ -x "$(command -v podman)" ]; then
|
|
32
|
+
DOCKER_CMD="podman"
|
|
33
|
+
fi
|
|
34
|
+
|
|
35
|
+
if ! $DOCKER_CMD info > /dev/null 2>&1; then
|
|
36
|
+
echo "$DOCKER_CMD daemon is not running. Please start $DOCKER_CMD and try again."
|
|
37
|
+
exit 1
|
|
38
|
+
fi
|
|
39
|
+
|
|
40
|
+
if command -v nc >/dev/null 2>&1; then
|
|
41
|
+
if nc -z localhost "$DB_PORT" 2>/dev/null; then
|
|
42
|
+
echo "Port $DB_PORT is already in use."
|
|
43
|
+
exit 1
|
|
44
|
+
fi
|
|
45
|
+
else
|
|
46
|
+
echo "Warning: Unable to check if port $DB_PORT is already in use (netcat not installed)"
|
|
47
|
+
read -p "Do you want to continue anyway? [y/N]: " -r REPLY
|
|
48
|
+
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
49
|
+
echo "Aborting."
|
|
50
|
+
exit 1
|
|
51
|
+
fi
|
|
52
|
+
fi
|
|
53
|
+
|
|
54
|
+
if [ "$($DOCKER_CMD ps -q -f name=$DB_CONTAINER_NAME)" ]; then
|
|
55
|
+
echo "Database container '$DB_CONTAINER_NAME' already running"
|
|
56
|
+
exit 0
|
|
57
|
+
fi
|
|
58
|
+
|
|
59
|
+
if [ "$($DOCKER_CMD ps -q -a -f name=$DB_CONTAINER_NAME)" ]; then
|
|
60
|
+
$DOCKER_CMD start "$DB_CONTAINER_NAME"
|
|
61
|
+
echo "Existing database container '$DB_CONTAINER_NAME' started"
|
|
62
|
+
exit 0
|
|
63
|
+
fi
|
|
64
|
+
|
|
65
|
+
if [ "$DB_PASSWORD" = "password" ]; then
|
|
66
|
+
echo "You are using the default database password"
|
|
67
|
+
read -p "Should we generate a random password for you? [y/N]: " -r REPLY
|
|
68
|
+
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
69
|
+
echo "Please change the default password in the .env file and try again"
|
|
70
|
+
exit 1
|
|
71
|
+
fi
|
|
72
|
+
# Generate a random URL-safe password
|
|
73
|
+
DB_PASSWORD=$(openssl rand -base64 12 | tr '+/' '-_')
|
|
74
|
+
if [[ "$(uname)" == "Darwin" ]]; then
|
|
75
|
+
# macOS requires an empty string to be passed with the `i` flag
|
|
76
|
+
sed -i '' "s#:password@#:$DB_PASSWORD@#" .env
|
|
77
|
+
else
|
|
78
|
+
sed -i "s#:password@#:$DB_PASSWORD@#" .env
|
|
79
|
+
fi
|
|
80
|
+
fi
|
|
81
|
+
|
|
82
|
+
$DOCKER_CMD run -d \
|
|
83
|
+
--name $DB_CONTAINER_NAME \
|
|
84
|
+
-e POSTGRES_USER="postgres" \
|
|
85
|
+
-e POSTGRES_PASSWORD="$DB_PASSWORD" \
|
|
86
|
+
-e POSTGRES_DB="$DB_NAME" \
|
|
87
|
+
-p "$DB_PORT":5432 \
|
|
88
|
+
docker.io/postgres && echo "Database container '$DB_CONTAINER_NAME' was successfully created"
|