|
1 | | -The repository contains an example Go application that will be deployed to AWS. |
2 | | - |
3 | | -## goals Application |
4 | | - |
5 | | -This application is a social media platform for setting and sharing life goals and aspirations. |
6 | | - |
7 | | -### Features |
8 | | - |
9 | | -- User authentication with Google OAuth2 |
10 | | -- Create and edit personal profiles (username, display name, bio, bio link, life aspirations, things I like to do) |
11 | | -- Share aspiration updates (create, edit, and delete) |
12 | | -- Leave nested comments on aspiration updates |
13 | | -- Like and unlike updates |
14 | | -- Follow and unfollow other users |
15 | | -- Browse recent users and updates |
16 | | -- User banning system (admin functionality) |
17 | | - |
18 | | -### Prerequisites for Running the Application |
19 | | - |
20 | | -- Go 1.24.2 or later |
21 | | -- Docker and Docker Compose |
22 | | -- PostgreSQL (if not using Docker) |
23 | | -- Google Cloud Console account for OAuth2 setup |
24 | | - |
25 | | -### Docker Setup |
26 | | - |
27 | | -1. Ensure Docker and Docker Compose are installed |
28 | | -2. Run `docker-compose up --detach` to start both the PostgreSQL database |
29 | | - |
30 | | -### Google OAuth2 Setup |
31 | | - |
32 | | -1. Go to the Google Cloud Console: https://console.cloud.google.com/ |
33 | | -2. Create a new project or select an existing one |
34 | | -3. Navigate to "APIs & Services" > "Credentials" |
35 | | -4. Click on "Create Credentials" and select "OAuth client ID" |
36 | | -5. Set up the OAuth consent screen if prompted |
37 | | -6. Choose "Web application" as the application type |
38 | | -7. Set the name for your OAuth 2.0 client |
39 | | -8. Add http://localhost:8080/auth/google/callback to "Authorized redirect URIs" |
40 | | -9. Click "Create" and note down the Client ID and Client Secret |
41 | | -10. Keep note of credentials to use in `.env` file later |
42 | | - |
43 | | -### Database Setup |
44 | | - |
45 | | -```bash |
46 | | -docker compose exec postgres psql -U postgres -d postgres |
47 | | -``` |
48 | | - |
49 | | -```sql |
50 | | -CREATE TABLE users ( |
51 | | - id SERIAL PRIMARY KEY, |
52 | | - email VARCHAR(255) UNIQUE NOT NULL, |
53 | | - bio TEXT, |
54 | | - bio_link VARCHAR(255), |
55 | | - username VARCHAR(50) UNIQUE NOT NULL, |
56 | | - display_name VARCHAR(100), |
57 | | - profile_image_url TEXT, |
58 | | - life_aspirations TEXT, |
59 | | - things_i_like_to_do TEXT, |
60 | | - created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
61 | | - is_logged_in BOOLEAN DEFAULT FALSE, |
62 | | - is_banned BOOLEAN DEFAULT FALSE |
63 | | -); |
64 | | - |
65 | | -CREATE TABLE administrators ( |
66 | | - id SERIAL PRIMARY KEY, |
67 | | - email VARCHAR(255) UNIQUE NOT NULL, |
68 | | - username VARCHAR(255) UNIQUE NOT NULL |
69 | | -); |
70 | | - |
71 | | -CREATE TABLE aspiration_updates ( |
72 | | - id SERIAL PRIMARY KEY, |
73 | | - user_id INTEGER REFERENCES users(id), |
74 | | - content TEXT NOT NULL, |
75 | | - created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP |
76 | | -); |
77 | | - |
78 | | -CREATE TABLE likes ( |
79 | | - id SERIAL PRIMARY KEY, |
80 | | - user_id INTEGER REFERENCES users(id), |
81 | | - update_id INTEGER REFERENCES aspiration_updates(id), |
82 | | - created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
83 | | - UNIQUE (user_id, update_id) |
84 | | -); |
85 | | - |
86 | | -CREATE TABLE followers ( |
87 | | - follower_id INTEGER REFERENCES users(id), |
88 | | - followed_id INTEGER REFERENCES users(id), |
89 | | - created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
90 | | - PRIMARY KEY (follower_id, followed_id) |
91 | | -); |
92 | | - |
93 | | -CREATE TABLE comments ( |
94 | | - id SERIAL PRIMARY KEY, |
95 | | - update_id INTEGER REFERENCES aspiration_updates(id), |
96 | | - user_id INTEGER REFERENCES users(id), |
97 | | - parent_id INTEGER REFERENCES comments(id), |
98 | | - content TEXT NOT NULL, |
99 | | - created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP |
100 | | -); |
101 | | -``` |
102 | | - |
103 | | -Then once you login to the app, add yourself as admin |
104 | | - |
105 | | -```sql |
106 | | -INSERT INTO administrators (email, username) |
107 | | -SELECT email, username |
108 | | -FROM users |
109 | | -WHERE email = 'user@example.com'; |
110 | | -``` |
111 | | - |
112 | | -### Development Environment |
113 | | - |
114 | | -1. Copy `.env.example` to create new `.env` file |
115 | | -2. Update `.env` file with OAuth credentials |
116 | | -3. Source `.env` with `source .env` |
117 | | -4. Start server with `go run main.go` |
118 | | -5. Navigate to http://localhost:8080 |
119 | | - |
120 | | - |
121 | | -## Course Resources |
122 | | - |
123 | | -The resources and code snippets below are reference throughout the [Fullstack Deployment: From Containers to Production AWS]() course. |
124 | | - |
125 | | -### General Setup Instructions |
126 | | - |
127 | | -To follow along with the course, you will need: |
128 | | - |
129 | | -1. Go version 1.24.2 or later |
130 | | - - [Download and install Go](https://go.dev/doc/install) |
131 | | -1. Docker Desktop |
132 | | - - [Download and install Docker Desktop](https://www.docker.com/products/docker-desktop/) |
133 | | - - **Important:** Check "Enable host networking" under `Settings > Resources > Network` |
134 | | -1. Google Cloud Console |
135 | | - - Log into the [Google Cloud Console](https://console.cloud.google.com/auth/clients) |
136 | | - - You'll create an OAuth Client during the course |
137 | | -1. AWS |
138 | | - - Create an AWS Root User account and log into the [AWS Console](https://us-east-1.console.aws.amazon.com/console/home) |
139 | | - - Install the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) |
140 | | - - In the course, you'll create an Administrator User in IAM for the CLI and [set environment variables](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html) in your console to authenticate the CLI |
141 | | -1. Terraform |
142 | | - - Install the [Terraform CLI](https://developer.hashicorp.com/terraform/install) |
143 | | -1. Supabase |
144 | | - - Create a [Supabase account]() |
145 | | -1. |
146 | | - |
147 | | - |
148 | | -### App Runner IAM Policy |
149 | | - |
150 | | -In the **AWS Parameter Store** lesson, copy and paste this JSON code when you are creating the IAM policy for the AWS App Runner: |
151 | | - |
152 | | -```json |
153 | | -{ |
154 | | - "Statement": [ |
155 | | - { |
156 | | - "Action": ["ssm:GetParameters"], |
157 | | - "Effect": "Allow", |
158 | | - "Resource": ["arn:aws:ssm:us-west-2:<ACCOUNT_ID>:parameter/fem-fd-service/*"] |
159 | | - } |
160 | | - ], |
161 | | - "Version": "2012-10-17" |
162 | | -} |
163 | | -``` |
164 | | - |
165 | | -### App Runner IAM Role |
166 | | - |
167 | | -In the **App Runner IAM Role** lesson, copy and paste this JSON code when you are creating the IAM Role for the AWS App Runner: |
168 | | - |
169 | | -```json |
170 | | -{ |
171 | | - "Version": "2012-10-17", |
172 | | - "Statement": [ |
173 | | - { |
174 | | - "Effect": "Allow", |
175 | | - "Principal": { |
176 | | - "Service": ["tasks.apprunner.amazonaws.com"] |
177 | | - }, |
178 | | - "Action": ["sts:AssumeRole"], |
179 | | - } |
180 | | - ], |
181 | | -} |
182 | | -``` |
183 | | - |
184 | | -### Adding Database Migration |
185 | | - |
186 | | -The application uses [goose for database migrations](https://github.com/pressly/goose). Install goose: |
187 | | - |
188 | | -```bash |
189 | | -go install github.com/pressly/goose/v3/cmd/goose@latest |
190 | | -``` |
191 | | - |
192 | | -Then verify the installation by running `goose -version` |
193 | | - |
194 | | -**Note:** If you see a `command not found: goose` error when trying to run goose, it's because the `$HOME/go/bin` directory is not added to your PATH. You can fix this temporarily by running export `PATH=$HOME/go/bin:$PATH`, but this will not persist if you close your terminal. A permanent fix would require adding export `PATH=$HOME/go/bin:$PATH` to your .zshrc or .bashrc. |
195 | | - |
196 | | - |
197 | | -### Deploying the Service |
198 | | - |
199 | | -When you are deploying the fm-fd-service with Terraform, Erik covers some troubleshooting tips throughout the lesson. Here are some additional troubleshooting tips: |
200 | | - |
201 | | -**Clean up your local docker images and push up a fresh image to ECR** |
202 | | -```bash |
203 | | -# You first may need to log (Go to ECR > Click on your image > View Push Command) |
204 | | -# aws ecr get-login-password...... |
205 | | - |
206 | | -# Clean up your existing image builds |
207 | | -`docker system prune --all && docker buildx prune --all` |
208 | | - |
209 | | -# Build a new image for staging and push it to ECR |
210 | | -make build-image |
211 | | -make BUILD_TAG="staging" build-image-promote |
212 | | -``` |
213 | | - |
214 | | -**Destroy and Reapply the Terraform configuration** |
215 | | -If you visit ECS and your staging cluster has 0 Container Instances (or there are any other issues you can't resolve), you likely have an issue with your Terraform configuration. Firstly, tear down the existing infrastructure: |
216 | | - |
217 | | -```bash |
218 | | -terraform destroy |
219 | | -``` |
220 | | - |
221 | | -Confirm your terraform configuration matches [this commit on Erik's workshop branch](https://github.com/ALT-F4-LLC/fem-fd-service/tree/94ee588f97cc00f5a13aec486df08a5bc04deb22). Then redeploy the infrastructure with the init, plan and apply commands: |
222 | | - |
223 | | -```bash |
224 | | -terraform init |
225 | | -terraform plan -out "terraform.tfplan" |
226 | | -terraform apply "terraform.tfplan" |
227 | | -``` |
228 | | - |
229 | | -## Deleting AWS Resources |
230 | | - |
231 | | -Once you complete the course, you'll need to remove all the AWS resources to avoid changes: |
232 | | - |
233 | | -1. Run `terraform destroy` to remove all resources created by Terraform |
234 | | -1. Navigate to [AWS App Runner](https://us-west-2.console.aws.amazon.com/apprunner/home?region=us-west-2), click on your App Runner instance and choose `Actions > Delete` |
235 | | -1. Navigate to [AWS Parameter Store](https://us-west-2.console.aws.amazon.com/systems-manager/parameters/?region=us-west-2) and delete the `fm-fd-service` parameters (the others should have been removed by running `terraform destroy`) |
236 | | -1. Navigate [to ECR](https://us-west-2.console.aws.amazon.com/ecr/private-registry/repositories?region=us-west-2) and delete your container |
237 | | -1. Delete your [Supabase database](https://supabase.com/dashboard/) |
238 | | - |
239 | | -> You can monitor your AWS changes in the [Cost Explorer](https://us-east-1.console.aws.amazon.com/costmanagement/home?region=us-west-2#/home) |
240 | | -
|
241 | | -## License |
242 | | - |
243 | | -This project is proprietary and closed source. All rights reserved. Unauthorized use, reproduction, or distribution of this software is strictly prohibited. |
| 1 | +### go + html deployment from start to scale. |
0 commit comments