diff --git a/.env.sample b/.env.sample new file mode 100644 index 00000000..2fe438c3 --- /dev/null +++ b/.env.sample @@ -0,0 +1,14 @@ +DB_NAME=skilltree +MYSQL_DB_USERNAME=testuser +MYSQL_DB_PASSWORD=testpassword +MYSQL_ROOT_PASSWORD=password +DB_DDL_POLICY=update +RDS_PUBLIC_KEY="-----BEGIN PUBLIC KEY----- + MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAg5HrGgKFmH485DXLG4fG + 1mKNXceWthXgAozsUGHxLIM3Fa5wU+tLi7tLDZ6LRKo4ZZV2gJJdDqFSNsvn1Uvr + XygvkCCRD/DYjxpG/qNiq9kgv7G8gjVI394oJWgx9uWTmDp4DmEVi7rIviyqVimE + V1nzdlKfGeLckagsetPS28rqULVGXaIAOL52vzrk3O74MnvCSVkjH/+cxEHE7XGg + ryy1fcH9CXvgrXxOLG2xrIdHrV39WBaMC4KOt5piCXgz2tAfhy47x6tIaEj5URx/ + jxQoSPZJ5TAf27pjPvUkGVs726RlO0tBLteNzTAWCVZ0QeMoMzBwtdloouC+Rzj2 + 8QIDAQAB + -----END PUBLIC KEY-----" \ No newline at end of file diff --git a/Makefile b/Makefile index a8e5ceb6..62e3ca5e 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +copy-env: + cp .env.sample .env + # Setup MySql Database docker-run: @if command -v docker > /dev/null; then \ @@ -21,5 +24,8 @@ setup: @echo "--- Setting up docker ---" @make docker-run + @echo "--- Copying env ---" + @make copy-env + @echo "\n" @echo "Setup complete!" \ No newline at end of file diff --git a/README.md b/README.md index 3ac54c6d..bc7132d7 100644 --- a/README.md +++ b/README.md @@ -1,197 +1,65 @@ # 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. + +### 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) -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: - -Screenshot 2023-12-26 at 9 33 17 PM - -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 @@ -212,10 +80,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 - ---- diff --git a/public/create-new-application.png b/public/create-new-application.png new file mode 100644 index 00000000..625fc9f7 Binary files /dev/null and b/public/create-new-application.png differ diff --git a/public/highlight-add-new-env-cta.png b/public/highlight-add-new-env-cta.png new file mode 100644 index 00000000..b5bd64d9 Binary files /dev/null and b/public/highlight-add-new-env-cta.png differ diff --git a/public/highlight-check-env.png b/public/highlight-check-env.png new file mode 100644 index 00000000..fc25934e Binary files /dev/null and b/public/highlight-check-env.png differ diff --git a/public/highlight-edit-config.png b/public/highlight-edit-config.png new file mode 100644 index 00000000..7f362182 Binary files /dev/null and b/public/highlight-edit-config.png differ diff --git a/public/highlight-env-list.png b/public/highlight-env-list.png new file mode 100644 index 00000000..adac3344 Binary files /dev/null and b/public/highlight-env-list.png differ diff --git a/public/highlight-java-version-main-class.png b/public/highlight-java-version-main-class.png new file mode 100644 index 00000000..742a0e8b Binary files /dev/null and b/public/highlight-java-version-main-class.png differ diff --git a/public/highlight-run-application.png b/public/highlight-run-application.png new file mode 100644 index 00000000..0c729b38 Binary files /dev/null and b/public/highlight-run-application.png differ