Skip to content

Commit

Permalink
Merge pull request #2 from SohamRatnaparkhi/feature/db-refractor-and-…
Browse files Browse the repository at this point in the history
…docker

[upgrade][db-and-container] Version upgrade with db folder shift and dockerization
  • Loading branch information
SohamRatnaparkhi authored Jun 8, 2023
2 parents 1513da7 + c8a9c1c commit 99f5c5d
Show file tree
Hide file tree
Showing 92 changed files with 3,143 additions and 80 deletions.
62 changes: 62 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
.PHONY: docker-build-all
docker-build-all:
docker build -t api-auth:v1 ./authentication

.PHONY: docker-run-all
docker-run-all:
docker run -d \
--name db \
--network my-network \
-e POSTGRES_PASSWORD=foobarbaz \
-v pgdata:/var/lib/postgresql/data \
-p 5432:5432 \
--restart unless-stopped \
postgres:15.1-alpine

docker run -d \
--name auth \
--network my-network \
-e POSTGRES_PASSWORD=foobarbaz \
-e DB_HOST=db \
-e DB_PORT=5432 \
-e DB_USER=postgres \
-e DB_PASSWORD=foobarbaz \
-e DB_NAME=blogx \
-e PORT=8080 \
-e JWT_SECRET_KEY=foobarbaz \
-e BCRYPT_SALT_VALUE=12 \
-e DB_URL="postgres://postgres:foobarbaz@db:5432/blogx?sslmode=disable" \
-p 8080:8080 \
--restart unless-stopped \
--link db \
api-auth:v1

docker run -d \
--name blog \
--network my-network \
-e POSTGRES_PASSWORD=foobarbaz \
-e DB_HOST=db \
-e DB_PORT=5432 \
-e DB_USER=postgres \
-e DB_PASSWORD=foobarbaz \
-e DB_NAME=blogx \
-e PORT=8081 \
-e JWT_SECRET_KEY=foobarbaz \
-e BCRYPT_SALT_VALUE=12 \
-e DB_URL="postgres://postgres:foobarbaz@db:5432/blogx?sslmode=disable" \
-p 8081:8081 \
--restart unless-stopped \
--link db \
api-blog:v1

.PHONY: docker-stop-all
docker-stop-all:
docker stop db
docker stop auth
docker stop blog

.PHONY: docker-rm-all
docker-rm-all:
docker rm auth
docker rm db
docker rm blog
61 changes: 53 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,50 @@ Microservice based backend for a micro-blogging platform.

It offers four distinct API services that collectively enable various functionalities:

- Authentication: This service facilitates essential operations such as user `login`, `logout`, and `registration`, ensuring secure access to the platform.
- **Authentication**: This service facilitates essential operations such as user `login`, `logout`, and `registration`, ensuring secure access to the platform.
- **Blog**: The Blog service provides features for managing blog-related activities. Users can `create`, `update`, and `delete` their blog posts. Additionally, they have the ability to `like` or `dislike` posts, promoting user engagement and feedback.
- **User**: This service focuses on user-related interactions. It allows users to `follow` or `unfollow` other users, enabling social connections within the platform. Furthermore, users can `delete` their account if desired.
- **Feed**: The Feed service is responsible for curating and displaying relevant content to users. It offers multiple options for customizing the feed, including displaying `all posts`, posts from `followed` authors, `liked` posts, posts by a specific user, or a `particular post`.

- Blog: The Blog service provides features for managing blog-related activities. Users can `create`, `update`, and `delete` their blog posts. Additionally, they have the ability to `like` or `dislike` posts, promoting user engagement and feedback.
Try this API service in postman.

- User: This service focuses on user-related interactions. It allows users to `follow` or `unfollow` other users, enabling social connections within the platform. Furthermore, users can `delete` their account if desired.
[![Run in Postman](https://run.pstmn.io/button.svg)](https://app.getpostman.com/run-collection/25118959-d0fe8b6b-4aac-46af-b5b1-b557f0be7064?action=collection%2Ffork&source=rip_markdown&collection-url=entityId%3D25118959-d0fe8b6b-4aac-46af-b5b1-b557f0be7064%26entityType%3Dcollection%26workspaceId%3D572f4781-b67c-48e3-ae06-4fb3efc89053)

- Feed: The Feed service is responsible for curating and displaying relevant content to users. It offers multiple options for customizing the feed, including displaying `all posts`, posts from `followed` authors, `liked` posts, posts by a specific user, or a `particular post`.
## Local setup

### Code-base Setup

1. Each folder of the root directory in the codebase contains an `.env.example` file. Follow the steps below to set up the necessary environment variables:

Try this API service in postman.
* Create a file named .env in each folder.
* Open the `.env.example` file and copy its contents.
* Paste the copied contents into the corresponding `.env` file.
* Replace the placeholder values in the .env file with the suggested values provided in the `.env.example` file.

[![Run in Postman](https://run.pstmn.io/button.svg)](https://app.getpostman.com/run-collection/25118959-d0fe8b6b-4aac-46af-b5b1-b557f0be7064?action=collection%2Ffork&source=rip_markdown&collection-url=entityId%3D25118959-d0fe8b6b-4aac-46af-b5b1-b557f0be7064%26entityType%3Dcollection%26workspaceId%3D572f4781-b67c-48e3-ae06-4fb3efc89053)
2. To set up the service locally, follow the steps below:

* Clone the repository to your local machine.
* Open a terminal and navigate to the cloned repository.
* Create a network using the command `docker network create my-network`
* Run the command `docker compose up`.

> **Note**: The `docker compose up` command will automatically set up the database. However, if it fails, please refer to the "*DB-Setup*" section for further instructions.
### DB-Setup

After running `docker compose up`, perform the following steps:

1. Open a new terminal window.
2. Execute the command `docker exec -it db bash`.
3. Run the command `psql -U postgres` to access the PostgreSQL command-line interface.
4. Create a new database by executing the command `CREATE DATABASE blogx_db;`.
5. Connect to the newly created database by executing the command `\c blogx_db`.
6. Open the `init.sql` file located in the `\db` folder.
7. Copy the contents of the `init.sql` file.
8. Paste the copied contents into the terminal where the PostgreSQL command-line interface is running.
9. Press Enter to execute the commands and initialize the database.

By following these steps, you should be able to set up the local environment and initialize the necessary database for the service.

## Authentication service

Expand Down Expand Up @@ -50,7 +81,7 @@ Try this API service in postman.
| /allPosts | GET | 200 | YES |
| /allPostsPageWise?page_no= | GET | 200 | YES |
| /postWithId?post_id= | GET | 200 | YES |
| /postWithUserId?user_id= | GET | 200 | YES |
| /postWithUserId?user_id= | GET | 200 | YES |
| /myPosts | GET | 200 | YES |
| /followingUsersPosts | GET | 200 | YES |
| /myLikedPosts | GET | 200 | YES |
Expand All @@ -72,7 +103,21 @@ Try this API service in postman.
---

## Schema
## Database

Database used - PostgreSQL

### Schema

![image](https://github.com/SohamRatnaparkhi/blogx-backend-go/assets/92905626/8cda3809-23ab-498a-a9ac-4572638d9cfd)
[Database-diagram](https://drawsql.app/teams/soham-ratnaparkhi/diagrams/blog-app)

## Description

| Table name | Description |
| -------------- | ----------------------------------------------------------------------------------------------------- |
| users | Stores the details of users. The password is not stored directly; it is hashed for security purposes. |
| posts | Stores the blog posts created by users. |
| user_followers | Represents the relationship between users, where the `follower_id` follows the `following_id`. |
| user_likes | Records the likes given by users (user_id) to specific posts (post_id). |

1 change: 1 addition & 0 deletions authentication/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dockerfile
64 changes: 64 additions & 0 deletions authentication/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
FROM golang:1.20-bullseye AS build-base

WORKDIR /app

COPY go.mod go.sum ./

RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod download

######################
FROM build-base AS dev

RUN go install github.com/mitranim/gow@latest

# ENV PORT=8080
# ENV BCRYPT_SALT_VALUE=12
# ENV JWT_SECRET_KEY="blogx_secret_key"
# ENV DB_URL="postgres://postgres:Soham@123*@localhost:5432/blogx_service_go"

COPY . .

CMD [ "gow", "run", "." ]

######################
FROM build-base AS build-production

# Add non root user
RUN useradd -u 1001 nonroot

COPY . .

ENV PORT=8080
ENV BCRYPT_SALT_VALUE=12
ENV JWT_SECRET_KEY="blogx_secret_key"
# ENV DB_URL="postgres://postgres:Soham@123*@localhost:5432/blogx_service_go"

RUN go build \
-ldflags="-linkmode external -extldflags -static" \
-tags netgo \
-o main



FROM scratch

EXPOSE 8080
EXPOSE 5432

WORKDIR /
# Copy the passwd file
COPY --from=build-production /etc/passwd /etc/passwd

# Copy the app binary from the build stage
COPY --from=build-production /app/main main

# Copy .env file
COPY --from=build-production /app/.env .env

# Use nonroot user
USER nonroot


CMD ["./main"]
31 changes: 31 additions & 0 deletions authentication/db/database/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions authentication/db/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions authentication/db/database/users.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions authentication/db/sql/queries/users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- name: CreateUser :one

INSERT INTO
users (
id,
first_name,
last_name,
password,
email,
bio
)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *;

-- name: GetUserByEmail :one

SELECT * FROM users WHERE email = $1;

-- name: DeleteUser :exec

DELETE FROM users WHERE id = $1;
Loading

0 comments on commit 99f5c5d

Please sign in to comment.