Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document #12

Merged
merged 6 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DBMS_CONNECTION=jdbc:mysql://mysql:3306/Vocabulary_KDP
DBMS_USERNAME=root
DBMS_PASSWORD=123456
SECRET_KEY=cEJXRvOIUGfe9KQfAh555TvLeOzQ30P7FxzQbZ5eDBOjOBCb26kQAMDbhZJahkJ1
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DBMS_CONNECTION=<value>
DBMS_USERNAME=<value>
DBMS_PASSWORD=<value>
SECRET_KEY=<value>
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Stage 1: build
# Start with a Maven image that includes JDK 17
FROM maven:3.9.9-amazoncorretto-17 AS build

# Copy source code and pom.xml file to /app folder
WORKDIR /app
COPY pom.xml .
COPY src ./src

# Build source code with maven
RUN mvn package -DskipTests

# Stage 2: create image
# Start with Amazon Correto JDK 17
FROM amazoncorretto:17

# Set working folder to App and copy complied file from above step
WORKDIR /app
COPY --from=build /app/target/*.jar app.jar

# Command to run the application
ENTRYPOINT ["java", "-jar", "app.jar"]
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Learn Vocabulary KDP Service

# Features

# Set up
## Requirement
- JDK 17+ ([How to download](https://www.geeksforgeeks.org/download-and-install-java-development-kit-jdk-on-windows-mac-and-linux/))
- IDE: [IntelliJ IDEA
](https://www.jetbrains.com/idea/) (Optional)
- [Lombok](https://projectlombok.org/): Install plugin in your IDE or Text Editor. [How to install Lombok](https://www.google.com/search?q=how+to+install+lombok)
- MySQL (Use with Docker)
### 1. Clone this repository
```git
https://github.com/K1ethoang/BE_Learn-Vocabulary_KDP.git
```
### 2. Go to project folder
```git
cd BE_Learn-Vocabulary_KDP
```
## .ENV
- Has 2 `.env` file: [.env.example](.env.example) for you set up with your configuration and [.env.dev](.env.dev) to run if you do not care about configuration.

## Install Docker (Required)
- Follow-up [document of Docker](https://docs.docker.com/compose/install/)

## How to run

### [Run manually Wiki](https://github.com/K1ethoang/BE_Learn-Vocabulary_KDP/wiki/Run-manually)

### [Run with Docker Wiki](https://github.com/K1ethoang/BE_Learn-Vocabulary_KDP/wiki/Run-with-Docker)

# Usage (Building)
Access http://localhost:9091/swagger-ui/index.html to see Api document, read more [Usage wiki]()
28 changes: 28 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
mysql:
image: mysql:8.3.0
container_name: mysql-container-8.3.0
environment:
MYSQL_ROOT_PASSWORD: 123456
ports:
- "6603:3306"
volumes:
- mysql_data:/var/lib/mysql # Lưu dữ liệu MySQL
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro # Chạy file SQL khi container khởi động lần đầu MySQL
restart: always

kdp-service:
build:
context: . # Thư mục hiện tại
dockerfile: Dockerfile
container_name: kdp-service-container-0.1.0
ports:
- "9091:9091"
depends_on:
- mysql # Đảm bảo MySQL khởi động trước
restart: always
env_file:
- .env.dev

volumes:
mysql_data:
8 changes: 8 additions & 0 deletions init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright (c) 2024. K1ethoang
* @Author: Kiet Hoang Gia
* @LastModified: 2024/12/19 - 00:27 AM (ICT)
*/

-- Tạo schema (database)
CREATE DATABASE IF NOT EXISTS Vocabulary_KDP;
32 changes: 30 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!--
~ Copyright (c) 2024. K1ethoang
~ @Author: Kiet Hoang Gia
~ @LastModified: 2024/12/14 - 17:24 PM (ICT)
~ @LastModified: 2024/12/18 - 16:28 PM (ICT)
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -17,7 +17,7 @@
</parent>
<groupId>org.kdp</groupId>
<artifactId>Learn_Vocabulary_KDP</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.1.0</version>
<name>Learn_Vocabulary_KDP</name>
<description>Learn_Vocabulary_KDP</description>
<url/>
Expand Down Expand Up @@ -102,6 +102,34 @@

<build>
<plugins>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<configuration>
<java>
<removeUnusedImports />
<toggleOffOn />
<trimTrailingWhitespace />
<endWithNewline />
<indent>
<tabs>true</tabs>
<spacesPerTab>4</spacesPerTab>
</indent>
<palantirJavaFormat />
<importOrder>
<order>java, jakarta, org, com, com.diffplug</order>
</importOrder>
</java>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>apply</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @Author: Kiet Hoang Gia
* @LastModified: 2024/12/14 - 19:19 PM (ICT)
************************************************/

package org.kdp.learn_vocabulary_kdp;

import org.springframework.boot.SpringApplication;
Expand All @@ -17,5 +16,4 @@ public class LearnVocabularyKdpApplication {
public static void main(String[] args) {
SpringApplication.run(LearnVocabularyKdpApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @Author: Kiet Hoang Gia
* @LastModified: 2024/12/15 - 15:51 PM (ICT)
************************************************/

package org.kdp.learn_vocabulary_kdp.Util;

import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -38,6 +37,4 @@ public String getUserIdFromContext() {
}
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @Author: Kiet Hoang Gia
* @LastModified: 2024/12/17 - 21:57 PM (ICT)
************************************************/

package org.kdp.learn_vocabulary_kdp.configuration;

import lombok.AccessLevel;
Expand Down Expand Up @@ -48,36 +47,71 @@ ApplicationRunner applicationRunner() {
if (userRepository.findByEmail("kiethoang101@gmail.com").isEmpty()) {
Role role = roleRepository.findByName(ERole.ADMIN.getName());

User user = User.builder().email("kiethoang101@gmail.com").password(passwordEncoder.encode("12345678")).fullName("admin").role(role).isBlocked(false).build();
User user = User.builder()
.email("kiethoang101@gmail.com")
.password(passwordEncoder.encode("12345678"))
.fullName("admin")
.role(role)
.isBlocked(false)
.build();

userRepository.save(user);
log.warn("Init account: Admin user has been created with: " + "'kiethoang101@gmail.com/12345678', " + "please change password");
log.warn("Init account: Admin user has been created with: " + "'kiethoang101@gmail.com/12345678', "
+ "please change password");
}

// Init data for Type entity
if (typeRepository.count() == 0) {
typeRepository.save(Type.builder().symbol("N").name("Noun").description("A person, place, thing, or idea.\nExample: cat, dog, house...").build());
typeRepository.save(Type.builder()
.symbol("N")
.name("Noun")
.description("A person, place, thing, or idea.\nExample: cat, dog, house...")
.build());
log.info("Init: Noun (N)");

typeRepository.save(Type.builder().symbol("V").name("Verb").description("An action word.\nExample: run, jump, eat, think...").build());
typeRepository.save(Type.builder()
.symbol("V")
.name("Verb")
.description("An action word.\nExample: run, jump, eat, think...")
.build());
log.info("Init: Verb (V)");

typeRepository.save(Type.builder().symbol("Adj").name("Adjective").description("A word that describes a noun.\nExample: big, small, happy, sad...").build());
typeRepository.save(Type.builder()
.symbol("Adj")
.name("Adjective")
.description("A word that describes a noun.\nExample: big, small, happy, sad...")
.build());
log.info("Init: Adjective (Adj)");

typeRepository.save(Type.builder().symbol("Prep").name("Preposition").description("A word that shows the relationship between nouns, pronouns, and other words in a sentence.\nExample: in, on, at, with...").build());
typeRepository.save(Type.builder()
.symbol("Prep")
.name("Preposition")
.description(
"A word that shows the relationship between nouns, pronouns, and other words in a sentence.\nExample: in, on, at, with...")
.build());
log.info("Init: Preposition (Prep)");

typeRepository.save(Type.builder().symbol("Det").name("Determiner").description("A word that modifies a noun.\nExample: the, a, an, this, that...").build());
typeRepository.save(Type.builder()
.symbol("Det")
.name("Determiner")
.description("A word that modifies a noun.\nExample: the, a, an, this, that...")
.build());
log.info("Init: Determiner (Det)");

typeRepository.save(Type.builder().symbol("Conj").name("Conjunction").description("A word that joins words phrases, or clauses.\nExample: and, but, or, because...").build());
typeRepository.save(Type.builder()
.symbol("Conj")
.name("Conjunction")
.description("A word that joins words phrases, or clauses.\nExample: and, but, or, because...")
.build());
log.info("Init: Conjunction (Conj)");

typeRepository.save(Type.builder().symbol("Intj").name("Interjection").description("A word or phrase that expresses strong emotion.\nExample: Wow!, Oh no!, Hey!...").build());
typeRepository.save(Type.builder()
.symbol("Intj")
.name("Interjection")
.description("A word or phrase that expresses strong emotion.\nExample: Wow!, Oh no!, Hey!...")
.build());
log.info("Init: Interjection (Intj)");
}

};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* @Author: Kiet Hoang Gia
* @LastModified: 2024/12/18 - 02:07 AM (ICT)
************************************************/

package org.kdp.learn_vocabulary_kdp.configuration;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -19,15 +20,16 @@
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
public void commence(
HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
throws IOException, ServletException {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);

ResponseEntity<ErrorResponse> apiResponse = ApiResponse.createErrorResponse(HttpStatus.UNAUTHORIZED, GlobalMessage.AUTHENTICATED);
ResponseEntity<ErrorResponse> apiResponse =
ApiResponse.createErrorResponse(HttpStatus.UNAUTHORIZED, GlobalMessage.AUTHENTICATED);

ObjectMapper mapper = new ObjectMapper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @Author: Kiet Hoang Gia
* @LastModified: 2024/12/14 - 20:17 PM (ICT)
************************************************/

package org.kdp.learn_vocabulary_kdp.configuration;

import org.kdp.learn_vocabulary_kdp.entity.User;
Expand All @@ -19,15 +18,18 @@ public class ModelMapperConfig {
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT).setSkipNullEnabled(true);
modelMapper
.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STRICT)
.setSkipNullEnabled(true);

modelMapper.addMappings(new PropertyMap<User, UserResponse>() {
@Override
protected void configure() {
map().setRole(source.getRole().getName());
}
});

return modelMapper;
}
}
Loading