Skip to content

Commit de89d54

Browse files
committed
added docker compose
1 parent 326cce6 commit de89d54

File tree

6 files changed

+148
-41
lines changed

6 files changed

+148
-41
lines changed

.env.local

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DATABASE_CONTAINER_NAME=go-api-tech-challenge-db
2+
DATABASE_NAME=goAPITechChallengeDB
3+
DATABASE_USER=user
4+
DATABASE_PASSWORD=password
5+
DATABASE_HOST=localhost
6+
DATABASE_PORT=5432
7+

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ employees.db
1818
# Dependency directories (remove the comment below to include it)
1919
# vendor/
2020
.DS_Store
21-
app.env
21+
app.env
22+
23+
**/.env

internal/database/database_postgres_setup.sql renamed to database_postgres_setup.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ CREATE TABLE "users" (
1111
);
1212

1313
-- Create blog table
14-
CREATE TABLE blogs (
14+
CREATE TABLE "blogs" (
1515
id BIGSERIAL PRIMARY KEY,
1616
author_id INTEGER NOT NULL,
1717
title TEXT NOT NULL,

db_seed.sql

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
DROP TABLE IF EXISTS person_course;
2+
DROP TABLE IF EXISTS course;
3+
DROP TABLE IF EXISTS person;
4+
5+
-- person
6+
CREATE TABLE person
7+
(
8+
id SERIAL PRIMARY KEY,
9+
first_name TEXT NOT NULL,
10+
last_name TEXT NOT NULL,
11+
type TEXT CHECK (type IN ('professor', 'student')) NOT NULL,
12+
age INTEGER NOT NULL
13+
);
14+
15+
INSERT INTO person (first_name, last_name, type, age)
16+
VALUES ('Steve', 'Jobs', 'professor', 56),
17+
('Jeff', 'Bezos', 'professor', 60),
18+
('Larry', 'Page', 'student', 51),
19+
('Bill', 'Gates', 'student', 67),
20+
('Elon', 'Musk', 'student', 52);
21+
22+
-- course
23+
CREATE TABLE course
24+
(
25+
id SERIAL PRIMARY KEY,
26+
name TEXT NOT NULL
27+
);
28+
29+
INSERT INTO course (name)
30+
VALUES ('Programming'),
31+
('Databases'),
32+
('UI Design');
33+
34+
-- person_course
35+
CREATE TABLE person_course
36+
(
37+
person_id INTEGER NOT NULL,
38+
course_id INTEGER NOT NULL,
39+
PRIMARY KEY (person_id, course_id),
40+
FOREIGN KEY (person_id) REFERENCES person (id),
41+
FOREIGN KEY (course_id) REFERENCES course (id)
42+
);
43+
44+
INSERT INTO person_course (person_id, course_id)
45+
VALUES (1, 1),
46+
(1, 2),
47+
(1, 3),
48+
(2, 1),
49+
(2, 2),
50+
(2, 3),
51+
(3, 1),
52+
(3, 2),
53+
(3, 3),
54+
(4, 1),
55+
(4, 2),
56+
(4, 3),
57+
(5, 1),
58+
(5, 2),
59+
(5, 3);

docker-compose.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
services:
2+
postgres:
3+
image: postgres:alpine
4+
container_name: ${DATABASE_CONTAINER_NAME}
5+
restart: always
6+
networks:
7+
- app
8+
env_file:
9+
- .env
10+
environment:
11+
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
12+
POSTGRES_USER: ${DATABASE_USER}
13+
POSTGRES_DB: ${DATABASE_NAME}
14+
PGUSER: postgres
15+
ports:
16+
- "5432:5432"
17+
volumes:
18+
- ./database_postgres_setup.sql:/docker-entrypoint-initdb.d/init.sql
19+
- postgres-db:/var/lib/postgresql/data
20+
healthcheck:
21+
test: [ "CMD-SHELL", "pg_isready -d ${DATABASE_NAME} -U ${DATABASE_USER}" ]
22+
interval: 5s
23+
start_period: 1s
24+
timeout: 5s
25+
retries: 5
26+
27+
volumes:
28+
postgres-db:
29+
30+
networks:
31+
app:

docs/1-Technical-Setup.md

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ colima --version
7171

7272
# install docker with homebrew
7373
brew install docker
74+
75+
# install docker-compose with homebrew
76+
brew install docker-compose
7477
```
7578

7679
Once installed, you can start and stop colima using the following command:
@@ -83,12 +86,33 @@ colima start
8386
colima stop
8487
```
8588

89+
> ---
90+
>
91+
> If you run into an error such as:
92+
>
93+
>
94+
> `Cannot connect to the Docker daemon at unix:///var/run/docker.sock.
95+
> Is the docker daemon running?`
96+
>
97+
> please reference this
98+
> [help article](https://github.com/abiosoft/colima/blob/main/docs/FAQ.md#cannot-connect-to-the-docker-daemon-at-unixvarrundockersock-is-the-docker-daemon-running).
99+
>
100+
> ---
101+
102+
> ---
103+
>
86104
> If you run into an error such as:
87105
>
88-
>`Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?`
106+
> `docker: 'compose' is not a docker command.`
89107
>
90-
>please reference
91-
> this [help article](https://github.com/abiosoft/colima/blob/main/docs/FAQ.md#cannot-connect-to-the-docker-daemon-at-unixvarrundockersock-is-the-docker-daemon-running).
108+
> add the following JSON to your `~/.docker/config.json file.`
109+
> ```json
110+
> "cliPluginsExtraDirs": [
111+
> "/opt/homebrew/lib/docker/cli-plugins"
112+
> ]
113+
> ```
114+
>
115+
> ---
92116
93117
### DBeaver
94118
@@ -137,46 +161,30 @@ Once your repository is cloned locally, you are all set to move on to the next s
137161

138162
## Database Setup
139163

140-
In order to initialize the database, the following setup will be required.
141-
142-
- First, ensure that you have installed and configured Podman Desktop correctly. Then, open a
143-
terminal and run the following commands to start a podman instance:
164+
To reduce developer friction, we have provided a docker-compose file that will create a PostgreSQL
165+
databse instance for you. To start this container, we first need to create a `.env` file. To do
166+
this, running the following command to make a copy of the `.env.local` file:
144167

145-
```
146-
podman machine init
147-
podman machine start
168+
```bash
169+
# copy the .env.local file to .env
170+
cp .env.local .env
148171
```
149172

150-
- Next, pull the PostgreSQL image by running the following command:
173+
Next you can modify the username and password in the `.env` file if you would like to change it from
174+
the default. We have already added the `.env` file to the `.gitignore` file so that it will not be
175+
pushed to the repository.
151176

152-
```
153-
podman pull docker.io/library/postgres:latest
154-
```
177+
To start the database, run the following command:
155178

156-
- You are then able to create a Podman container to store the database in. Run the following command
157-
in your terminal:
158-
159-
```
160-
podman run -d --name PostgresServer -e POSTGRES_USER=user -e POSTGRES_PASSWORD=goChallenge -e POSTGRES_DB=blogs -p 5432:5432 docker.io/library/postgres:latest
179+
```bash
180+
# start the database
181+
docker command up
161182
```
162183

163-
At this point, you will notice in Podman Desktop that a container has been created and running on
164-
port 5432.
165-
166-
In order to initialize the database and provide default data, you will need to complete the
167-
following steps. Make sure that your podman container is up and running prior to completing these
168-
steps.
184+
Now that the database is running, we can connect to it using DBeaver.
169185

170186
- First, open DBeaver and add a new connection, specifying PostgreSQL as the database type. Next,
171-
make sure that the host, port, database, and credentials match as below
172-
173-
```
174-
Host: localhost
175-
Port: 5432
176-
Database: blogs
177-
Username: user
178-
Password: goChallenge
179-
```
187+
make sure that the host, port, database, and credentials match the values in the `.env` file.
180188

181189
> ---
182190
>
@@ -185,25 +193,25 @@ Password: goChallenge
185193
>
186194
> ---
187195
188-
- Once you have connected to the database, open a new sql script. Then, copy and paste the SQL
189-
commands into the sql script from the `database_postgres_setup.sql` file under the `/database`
190-
directory. At this point, you have now initialized your database and are ready to continue.
196+
- Once you have connected to the database, confirm you can see the `comments` database and the
197+
tables
198+
`blogs` and `users`.
191199

192200
## Create Go Module and Install Necessary Libraries
193201

194202
To create your go project, open a terminal in the root project directory and run the following
195203
command, replacing `[name]` with your name.
196204

197-
```
205+
```bash
198206
go mod init github.com/[name]/blog
199207
```
200208

201209
Next, run the following commands that will install the required libraries for the tech challenge.
202210

203211
```bash
204212
go get github.com/caarlos0/env/v11
213+
go get github.com/jackc/pgx/v5/stdlib
205214
go get github.com/swaggo/http-swagger
206-
go get github
207215
go install github.com/swaggo/swag/cmd/swag@latest
208216
```
209217

0 commit comments

Comments
 (0)