Skip to content

Commit 7f2be3c

Browse files
committed
Cleanup + read vars from .env.local
1 parent 816bcd9 commit 7f2be3c

File tree

8 files changed

+73
-67
lines changed

8 files changed

+73
-67
lines changed

‎.env.example‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
# THE FOLLOWING ARE LOCAL ONLY VALUES. IN PRODUCTION, THESE SHOULD BE SET IN THE GITHUB SECRETS.
1+
# WARNING!!! - WARNING!!! - WARNING!!
2+
3+
# Some of the following values are for use LOCAL ONLY and are not needed in GitHub secrets.
4+
# MYSQL_* variables are only used with docker-compose.yml and are not needed in GitHub secrets.
5+
# APP_NAME, APP_TOKEN, and CAPROVER_URL are only used with caprover deploy bash script and useful for directly deploying to CapRover.
6+
# JWT_SECRET shoudl always be generated separately for production than what is used for local development.
27

38
# REQUIRED for Prisma
49
DATABASE_URL="mysql://dbuser:dbpass@localhost:3306/dbname"
510

611
# Required for JWT authentication
712
# Generate one with `openssl rand -base64 32`. Generate a seperate secret for production.
8-
VITE_JWT_PRIVATE_KEY="secret key"
13+
JWT_SECRET="secret key"
914

1015
# ONLY REQUIRED FOR LOCAL DEVELOPMENT!!! DO NOT ADD THESE TO GITHUB SECRETS.
1116
MYSQL_DATABASE="dbname"
1217
MYSQL_USER="dbuser"
1318
MYSQL_PASSWORD="dbpass"
1419
MYSQL_ROOT_PASSWORD="dbrootpass"
1520

16-
SENDGRID_API_KEY="sendgrid api key"
17-
EMAIL_FROM="no-reply@your-domain.com"
21+
# REQUIRED - FOR CONTACT FORM. (For local development only. For production, use GitHub secrets.)
22+
SENDGRID_API_KEY="SGXXXXXXXXXXXXXXXXXXXXXXXXXXX"
23+
EMAIL_DESTINATION="nick@example.com" # Where contact form emails will be sent to.
24+
EMAIL_REPLY_TO="no-reply@example.com" # Must be verified email or domain in SendGrid
25+
26+
# OPTIONAL - REMOVE IF NOT NEEDED. (For direct deployment to CapRover using caprover deploy bash script.)
27+
APP_NAME="my-app"
28+
APP_TOKEN="XXXXXXXXXXXXXXXXXXX"
29+
CAPROVER_URL="https://captain.example.com"
30+
31+
# Google ReCaptcha - Up next...

‎.github/workflows/production.yml‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ on:
55
branches:
66
- "main"
77

8-
#env:
9-
#DATABASE_URL: ${{ secrets.DATABASE_URL }}
10-
#SENDGRID_API_KEY: ${{ secrets.SENDGRID_API_KEY }}
8+
env:
9+
DATABASE_URL: ${{ secrets.DATABASE_URL }} # Used by Prisma
10+
SENDGRID_API_KEY: ${{ secrets.SENDGRID_API_KEY }} # Used by SendGrid
11+
JWT_SECRET": ${{ secrets.JWT_SECRET }} # Used by authentication/authorization/session management
12+
13+
# Email configuration
14+
EMAIL_DESTINATION: "you@example.com" # Where contact form emails will be sent to.
15+
EMAIL_REPLY_TO: "no-reply@example.com" # Must be verified email or domain in SendGrid
1116

1217
jobs:
1318
build-and-deploy:

‎.vscode/settings.json‎

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
{
2-
"editor.formatOnSave": true,
3-
"workbench.editor.labelFormat": "short",
42
"editor.codeActionsOnSave": {
53
"source.fixAll.eslint": true
64
},
5+
"editor.formatOnSave": true,
76
"emmet.includeLanguages": {
87
"postcss": "css"
98
},
109
"files.associations": {
1110
"*.css": "sass"
1211
},
1312
"files.exclude": {
14-
// "**/.git": true,
15-
// "**/.svn": true,
16-
// "**/.hg": true,
17-
// "**/CVS": true,
18-
// "**/.DS_Store": true,
19-
// "**/Thumbs.db": true,
20-
// "**/node_modules": false,
21-
// "**/build": true,
22-
// ".svelte-kit": true,
23-
// ".eslint*": true,
24-
// ".npmrc": true,
25-
// ".prettier*": true,
26-
// "Dockerfile": false,
27-
// "package-lock.json": true,
28-
// "playwright.config.ts": true,
29-
// "postcss.config.cjs": true,
30-
// "tailwind.config.cjs": true,
31-
// "tsconfig.json": false,
32-
// "svelte.config.js": true,
33-
// "vite.config.ts": true,
34-
// "npm.sh": true,
35-
// "prisma.sh": true,
36-
// "yarn.lock": true,
37-
// ".gitignore": true,
38-
}
13+
"**/.DS_Store": true,
14+
"**/.git": true,
15+
"**/.hg": true,
16+
"**/.svn": true,
17+
"**/CVS": true,
18+
"**/Thumbs.db": true,
19+
"**/build": true,
20+
"**/node_modules": false,
21+
".eslint*": true,
22+
".gitignore": true,
23+
".npmrc": true,
24+
".prettier*": true,
25+
".svelte-kit": true,
26+
"Dockerfile": false,
27+
"npm.sh": true,
28+
"package-lock.json": true,
29+
"playwright.config.ts": true,
30+
"postcss.config.cjs": true,
31+
"prisma.sh": true,
32+
"svelte.config.js": true,
33+
"tailwind.config.cjs": true,
34+
"tsconfig.json": true,
35+
"vite.config.ts": true,
36+
"yarn.lock": true
37+
},
38+
"workbench.editor.labelFormat": "short",
39+
"workbench.iconTheme": "vs-minimal",
40+
"workbench.colorTheme": "Tomorrow Night Blue"
3941
}

‎deploy.sh‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#!/bin/bash
22

3-
APP_NAME="my-app"
4-
APP_TOKEN="45a0fc928aa4c3756251b81709da45abb4183c53a0bab31976bf79f5a2a47a9a"
5-
CAPROVER_URL="https://captain.example.com"
6-
73
# Run yarn build
84
yarn build
95

‎src/lib/variables.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export const variables = {
2-
private_key: import.meta.env.VITE_JWT_PRIVATE_KEY,
2+
private_key: import.meta.env.JWT_SECRET,
33
}

‎src/routes/+page.svelte‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@
1111
<style lang="postcss">
1212
:global(html) {
1313
background-color: theme(colors.gray.100);
14-
height: 100%;
1514
}
1615
</style>

‎src/routes/contact/+page.server.ts‎

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
1+
// This file is a SvelteKit action that is called when the contact form is submitted.
22
import type { Actions } from "@sveltejs/kit";
33
import { fail } from "@sveltejs/kit"
44

5-
5+
// Form specific imports
66
import z from "zod"
77
import sgMail from "@sendgrid/mail/index";
8-
// THE FOLLOWING TWO LINES ARE NOT WORKING. STILL WORKING ON THIS.
9-
// import { env } from '$env/dynamic/private';
10-
// const { SENDGRID_API_KEY, SENDGRID_EMAIL_FROM, VITE_CONTACT_EMAIL } = env;
118

12-
// This needs to be picked up from the .env file. I'm not sure how to do this. Seems broken at the moment.
13-
const SENDGRID_API_KEY = "<EXAMPLE_KEY>";
14-
const SENDGRID_EMAIL_FROM = "no-reply@example.com";
15-
const VITE_CONTACT_EMAIL = "username+contactform@example.com";
9+
// Import the SendGrid API key and email addresses from the private environment variables.
10+
import { SENDGRID_API_KEY, EMAIL_DESTINATION, EMAIL_REPLY_TO } from '$env/static/private';
1611

17-
// Refer to https://zod.dev/ for building a desired data schema for the data we want to parse and optionally.
18-
const contactSchema = z.object({
12+
// Refer to https://zod.dev/ for details about how to setup a validation schema.
13+
const contactFormSchema = z.object({
1914
fname: z.string({ required_error: "First name is required" }).min(1),
2015
lname: z.string({ required_error: "Last name is required" }).min(1),
2116
email: z.string({ required_error: "Valid email address required" }).email(),
@@ -25,20 +20,13 @@ const contactSchema = z.object({
2520
export const actions: Actions = {
2621
default: async ({ request }) => {
2722

28-
sgMail.setApiKey(SENDGRID_API_KEY);
29-
3023
const formData = Object.fromEntries(await request.formData());
31-
32-
// Remove empty fields from the form data.
33-
Object.keys(formData).forEach(key => formData[key] === '' && delete formData[key]);
34-
35-
const form = contactSchema.safeParse(formData);
36-
const { ...rest } = formData;
24+
const form = contactFormSchema.safeParse(formData);
3725

3826
// If there is a problem with the form data, return the error.
39-
if (!form.success) {
27+
if (!form.success) { // If not successful, return the error.
4028
return fail(400, {
41-
data: rest,
29+
data: formData,
4230
errors: form.error.flatten().fieldErrors,
4331
body: {
4432
message: 'Please review the form and try again.',
@@ -48,10 +36,12 @@ export const actions: Actions = {
4836
}
4937

5038
try {
39+
sgMail.setApiKey(SENDGRID_API_KEY);
40+
5141
await sgMail.send({
52-
to: VITE_CONTACT_EMAIL, // Destination email address (Who is responsible responding to emails)
53-
replyTo: form.data.email, // Email of visitor requesting contact.
54-
from: SENDGRID_EMAIL_FROM, // Email of sender (verified email address or domain at SendGrid)
42+
to: EMAIL_DESTINATION, // SendGrid verified email or domain where email is send "from". This could be a reply-to address, or a sales, support, or other address.
43+
from: EMAIL_REPLY_TO, // The email address that will be displayed as the sender.
44+
replyTo: form.data.email, // The email provided provided by the visitor.
5545
subject: 'Contat form submission',
5646
text: 'First Name: ' + form.data.fname + '\n' +
5747
'Last Name: ' + form.data.lname + '\n' +
@@ -64,7 +54,7 @@ export const actions: Actions = {
6454
if (err.response) {
6555
console.error(err.response.body?.errors)
6656
return fail(400, {
67-
data: rest,
57+
data: formData,
6858
body: {
6959
message: "Upstream Error: " + err.response.body?.errors[0]?.message,
7060
classes: 'text-2xl text-red-500'

‎src/routes/contact/+page.svelte‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
/**
55
* @type {{ errors: { fname: any[]; lname: any[]; email: any[]; message: any[]; }; fname: any; data: { lname: any; email: any; message: any; }; }}
66
*/
7-
export let form;
7+
export let form;
88
/**
99
* @type {{ message: any; classes: any; } | null}
1010
*/
11-
export let placeholder = null;
11+
export let placeholder = null;
1212
</script>
1313

1414
<div class=" mx-auto my-20 w-1/3 border border-purple-500 bg-white">

0 commit comments

Comments
 (0)