Skip to content

Commit

Permalink
Merge pull request #156 from Real-Dev-Squad/develop
Browse files Browse the repository at this point in the history
Dev to main merge
  • Loading branch information
prakashchoudhary07 authored Sep 2, 2024
2 parents 6264763 + a7c39ec commit 1d09920
Show file tree
Hide file tree
Showing 114 changed files with 1,472 additions and 3,564 deletions.
17 changes: 17 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
DB_NAME=skilltree
MYSQL_DB_USERNAME=testuser
MYSQL_DB_PASSWORD=testpassword
MYSQL_ROOT_PASSWORD=password
DB_DDL_POLICY=update
RDS_BACKEND_BASE_URL=http://localhost:3000
SKILL_TREE_FRONTEND_BASE_URL=https://localhost
SKILL_TREE_BACKEND_BASE_URL=http://localhost:8080
RDS_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAg5HrGgKFmH485DXLG4fG
1mKNXceWthXgAozsUGHxLIM3Fa5wU+tLi7tLDZ6LRKo4ZZV2gJJdDqFSNsvn1Uvr
XygvkCCRD/DYjxpG/qNiq9kgv7G8gjVI394oJWgx9uWTmDp4DmEVi7rIviyqVimE
V1nzdlKfGeLckagsetPS28rqULVGXaIAOL52vzrk3O74MnvCSVkjH/+cxEHE7XGg
ryy1fcH9CXvgrXxOLG2xrIdHrV39WBaMC4KOt5piCXgz2tAfhy47x6tIaEj5URx/
jxQoSPZJ5TAf27pjPvUkGVs726RlO0tBLteNzTAWCVZ0QeMoMzBwtdloouC+Rzj2
8QIDAQAB
-----END PUBLIC KEY-----"
10 changes: 9 additions & 1 deletion .github/workflows/deploy-to-ec2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
branches:
- main
- develop
- feat/aws-deploy # Remove before merting this PR to dev

jobs:
build-and-push:
Expand Down Expand Up @@ -53,5 +52,14 @@ jobs:
--name ${{ github.event.repository.name }}-${{ vars.ENV }} \
--network=${{ vars.DOCKER_NETWORK }} \
-e RDS_PUBLIC_KEY="${{ secrets.RDS_PUBLIC_KEY }}" \
-e MYSQL_HOST="${{ secrets.MYSQL_HOST }}" \
-e MYSQL_PORT="${{ secrets.MYSQL_PORT }}" \
-e DB_NAME="${{ vars.DB_NAME }}" \
-e MYSQL_DB_USERNAME="${{ secrets.MYSQL_DB_USERNAME }}" \
-e MYSQL_DB_PASSWORD="${{ secrets.MYSQL_DB_PASSWORD }}" \
-e RDS_BACKEND_BASE_URL="${{ vars.RDS_BACKEND_BASE_URL }}" \
-e SKILL_TREE_BACKEND_BASE_URL="${{ vars.SKILL_TREE_BACKEND_BASE_URL }}" \
-e SKILL_TREE_FRONTEND_BASE_URL="${{ vars.SKILL_TREE_FRONTEND_BASE_URL }}" \
-e SPRING_PROFILES_ACTIVE="${{ vars.SPRING_PROFILES_ACTIVE }}" \
-e API_V1_PREFIX=/api/v1 \
${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }}
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
copy-env:
cp .env.sample .env

# Setup MySql Database
docker-run:
@if command -v docker > /dev/null; then \
Expand All @@ -21,5 +24,8 @@ setup:
@echo "--- Setting up docker ---"
@make docker-run

@echo "--- Copying env ---"
@make copy-env

@echo "\n"
@echo "Setup complete!"
254 changes: 64 additions & 190 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,197 +1,78 @@
# Skill Tree Backend

## Steps to Run the Service Locally

### Required Tools

- [Maven](https://maven.apache.org/download.cgi) version 3.9.6 or higher

Installing Maven on macOS using Homebrew:

1. Open your terminal.
2. Type the following command and press Enter to install Homebrew (if not installed):

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```

3. Once Homebrew is installed, run the following command to install Maven:
## Required Tools
- [Homebrew](https://brew.sh/) (If you're using macOS)
- [Maven](https://maven.apache.org/download.cgi) (version 3.9.6 or higher)
- [Java](https://www.oracle.com/in/java/technologies/downloads/#java17) (version 17)
- [IntelliJ Community or Ultimate Edition](https://www.jetbrains.com/idea/download/other.html)
- [Docker Desktop](https://www.docker.com/products/docker-desktop/)

## Development Setup
### Install Maven
#### macOS
1. To install maven type the following command in your terminal:
```bash
brew install maven

4. Wait for the installation process to complete.

5. Verify the Maven installation by typing:
```
2. Verify your installation using the following command:
```bash
mvn -version
```
If maven is installed correctly you should see the maven version in the terminal:
```bash
Apache Maven 3.9.7
```

This should display information about the installed Maven version.

Installing Maven on Windows:

#### Windows
1. To install Maven on Windows, we head over to the Apache Maven site to download the latest version and select the Maven zip file, for example, apache-maven-3.9.6-bin.zip.

2. Adding Maven to the Environment Path
We add both M2_HOME and MAVEN_HOME variables to the Windows environment using system properties and point them to our Maven folder.

3. Verify the Maven installation by typing:
mvn -version
4. Then, we update the PATH variable by appending the Maven bin folder — %M2_HOME%\bin — so that we can run the Maven command everywhere.

### Setup database and configure env
- To setup the database & create a `.env` file run the following command in your terminal:
```bash
make setup
```
- To edit your environment variables edit the `.env` file.

#### NOTE:
- If you do not have `make` installed, run the `docker-run` and `copy-env` commands from the `Makefile` manually.

### Install EnvFile Plugin in IntelliJ
- To read the values from your `.env` you will need install the `EnvFile` plugin.
- To Install the plugin to go `settings` > `Plugins` > `Marketplace` > EnvFile.
![img.png](public/highlight-env-file-plugin.png)

### Create a configuration in IntelliJ
- Press the Edit configurations button on the top right of your screen
![img.png](public/highlight-edit-config.png)
- Create a new application
![img.png](public/create-new-application.png)
- Select `Java version 17`, `Skill tree` and `SkillTreeApplication`
![img.png](public/highlight-java-version-main-class.png)
- Check `EnvFile` and `Substitute Environment Variables`
![img.png](public/highlight-check-env.png)
- Add path to the `.env` file you created above
![img.png](public/highlight-add-new-env-cta.png)
- You should a new entry like so:
![img.png](public/highlight-env-list.png)
- Click on `Apply` and then `Ok`

### Running your application
- Press the `Run` button on the top right of your screen to run the application.
![img.png](public/highlight-run-application.png)

## Steps to authenticate a user during development
- Run RDS backend on your local machine
- Copy the `public key` from `local.js` (if not in `local.js` copy it from `development.js`) and add it in your `.env` file.
- Run skill tree frontend, click on `signin with github` button
- Once authenticated you will be redirected to `localhost:4000` (skill tree frontend homepage) and a `rds-session-v2-development` cookie will be set in the browser which will be used for authentication when making API calls.
- NOTE:
- make sure that `incompleteUserDetails` is false for the user in firestore, else the redirect will not work as intended.

Then, we update the PATH variable by appending the Maven bin folder — %M2_HOME%\bin — so that we can run the Maven command everywhere.

Then, we unzip it to the folder where we want Maven to live.

- Java (version 17 or higher) [Link](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html)
- Any text editor of your choice (Preferred - IntelliJ Community or Ultimate Edition).
- Docker Desktop: [Installation steps](https://docs.docker.com/engine/install/)
- MySQL Docker Image: [Installation steps](https://hub.docker.com/_/mysql)

### Steps to Login to MySQL

1. Go to Docker Desktop

2. You'll see skill-tree-backed (If the process is running)

3. skill-tree-backend > skill-tree-backend-db-1 > open in terminal

To login to MySQL

```
mysql -u root -p
```

(in terminal)
password : password

Reference Screenshots:

If the project is started with `docker compose up`, this can be seen once you open Docker Desktop:

<img width="1680" alt="Screenshot 2023-12-26 at 9 33 17 PM" src="https://github.com/ashifkhn/skill-tree-backend/assets/54736284/57b90473-ae22-45b2-8a19-3377bfbcf1b9">

Terminal needs to be opened here:

![image](https://github.com/ashifkhn/skill-tree-backend/assets/54736284/d66166ae-b931-40ab-914f-f42615323a32)

## Steps for Creating the Database (To be executed in order)

Semicolon is important in the commands

Username: testuser
Password: testpassword

```
CREATE DATABASE skilltree;
SHOW DATABASES;
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'testpassword';
GRANT ALL ON skilltree.* TO testuser;
```

## Steps for setting up skill-tree in Intellij

1. After creating the database project needs to be compiled.
2. Open skill-tree-backend in intellij.
3. Java_Home path needs to be added here.
4. You can either add the existing path and JDK 17 can be downloaded inside intellij.
5. If adding from the existing path, go to Settings > Project Structure > Choose the earlier installed Java 17 SDK.
6. Install Lombok plug-in, if you see annotation errors

## Creating Run/Debug Configuration

1. Create a .env file inside the skill-tree folder with content mentioned in the Additional Configuration Steps below
2. Click on "Edit Configurations" > Add new "Application" Configuration
3. Choose "Java 17" and "skill-tree" folder in the dropdown
4. Choose com.RDS.skilltree.SkillTreeApplication as the Main class
5. Add the .env file you created in the first step for the environment variables and click "OK"

(Below steps are not required as of now.)

### Steps to Connect the Service to MySQL Running in Docker

Refer to this [link](https://find10archived.medium.com/how-to-connect-a-mysql-docker-container-with-a-local-spring-boot-application-9366707dce0d) for detailed steps.

1. Create a database user with full access for table creation, deletion, and modification (for development only in local MySQL).
2. After creating the database user, enter the credentials in the Skill Tree Spring Boot application.
3. Attempt to connect to the database using the URL "jdbc:mysql://${MYSQL_HOST:localhost}:3306/${DB_NAME}".
4. Create a simple API to test the database connection and data retrieval. For example, create a `/test` route in `skill-tree/src/main/java/com/RDS/skilltree/User/UserController.java`.

```java
package com.RDS.skilltree.User;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
@GetMapping("/test")
public void test(){
System.out.println("test123");
}
}
```

### Steps to Generate a JWT Token (Similar to [Website Backend Repo](https://github.com/Real-Dev-Squad/website-backend/issues))

1. If you've already generated the public and private keys during the website backend setup, you can proceed directly to step 5.
2. Ensure the website-backend is running locally.
3. Generate Public and Private keys using openssl with the following commands:
- `openssl genrsa -out ./private.key 4096`
- `openssl rsa -in private.key -pubout -outform PEM -out public.key`
4. Obtain a user token (private and public key stored in `local.js` file).
5. After calling `localhost:3000/auth/github/login`, the backend will generate the JWT token.
6. Validate the token using [jwt.io](https://jwt.io/) by entering the public and private keys stored in the website backend.
7. Use the public key in the Skill Tree repo to decrypt the JWT token passed for authentication.

## Steps for connecting mysql workbench to run mysql inside docker

1. `docker exec -it skill-tree-db-1 bin/bash`
2. bash-4.4# `mysql -u root -p -A`

By default after deployment MySQL has following connection restrictions:

```
mysql> select host, user from mysql.user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | healthchecker |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)
```

Apparently, for security purposes, you will not be able to connect to it from outside of the docker container. If you need to change that to allow root to connect from any host (say, for development purposes), do the following:

3. update mysql.user set host='%' where user='root';
4. Quit the mysql client.
5. Restart the docker container

Now you can connect to the mysql running in the docker container, also to connect to it on the terminal type `mysql -utestuser -p -P3306 -h127.0.0.1`

## Additional Configuration Steps

1. Download the EnvFile plugin from the marketplace.
2. Create a new Env file with the provided content and fill in the RDS public key value.
```env
DB_NAME=${DB_NAME}
MYSQL_DB_USERNAME=testuser
MYSQL_DB_PASSWORD=testpassword
MYSQL_ROOT_PASSWORD=password
DB_DDL_POLICY=update
RDS_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END PUBLIC KEY-----"
```
> Note : Publickey in both [RDS backend](https://github.com/Real-Dev-Squad/website-backend) and skilltree backend should be the same.
3. Click "Edit Configurations" -> Create a new application.
4. Give it a name instead of "Unnamed".
5. In "Build and Run", select Java 17.
6. In the class , check "Enable Env file" and "Substitute env var" checkboxes.
7. Click "Apply" and "OK".
8. Click "Build and Run".
9. Retrieve the Bearer token by accessing `http://localhost:3000/auth/github/login` and locating the key `rds-session-development` in the application. The value associated with this key is the `Bearer token`.
10. Click the green "Run" button or "Shift + F10" to start the application
11. After starting the Tomcat server on port `8080`, attempt to access the dummy route `http://localhost:8080/test` using the `GET` method in Postman or ThunderClient while providing the `bearer token`. If the terminal displays `test123`, it indicates that the setup has been successful.

## To Authenticate Yourself

Expand All @@ -212,10 +93,3 @@ Please build using `mvn compile` in local or run `mvn spotless:apply` before pus
To check if the codebase is formatted, you can explicitly use `mvn spotless:check`

The Continuous Integration build for pushed commits may fail when a Pull Request is created if your code doesn't follow project's formatting guideline.

## Known Issues Faced by Other Developers

1. Port 8080 Conflict: Make sure there is no other process running on the 8080 port where we are going to run our server check this with lsof -p PID (PID - port id)
2. Local MySQL Conflict: Make sure there is no local Mysql running on the local machine

---
Binary file added public/create-new-application.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/highlight-add-new-env-cta.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/highlight-check-env.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/highlight-edit-config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/highlight-env-file-plugin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/highlight-env-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/highlight-java-version-main-class.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/highlight-run-application.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion skill-tree/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<version>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.RDS</groupId>
Expand Down Expand Up @@ -114,6 +114,16 @@
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>10.17.1</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
<version>10.17.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Loading

0 comments on commit 1d09920

Please sign in to comment.