Skip to content

Commit

Permalink
chore: add noti in values.yaml, merge from main
Browse files Browse the repository at this point in the history
  • Loading branch information
amber committed Oct 23, 2024
2 parents 2abacb0 + 3f6e277 commit 9cc2a7e
Show file tree
Hide file tree
Showing 50 changed files with 866 additions and 405 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gitops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:

- name: Update Image Tag Values
run: |
chart_dir="helm"
chart_dir="helm/nshm"
new_image_tag=${{ github.sha }}
sed -i -E "s|(repository: z1yoon/nshm-${{ matrix.service }}[[:space:]]+tag: )[^\"]*|\1$new_image_tag|g" "$chart_dir/values.yaml"
Expand Down
6 changes: 0 additions & 6 deletions decrypt-run.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/bin/bash

DOCKER_COMPOSE_FILE=docker-compose.dev.yaml
ENVS_DIR="./envs"
PASSPHRASE_FILE="nshm.passphrase"

# Check if the passphrase file exists, if not, download it from S3
if [[ ! -f "$PASSPHRASE_FILE" ]]; then
echo "Passphrase file not found. Downloading from S3..."
Expand All @@ -13,7 +11,6 @@ if [[ ! -f "$PASSPHRASE_FILE" ]]; then
exit 1
fi
fi

# Decrypt each .env.gpg file inside the envs directory
for env_file_gpg in "$ENVS_DIR"/*.env.gpg; do
if [[ -f "$env_file_gpg" ]]; then
Expand Down Expand Up @@ -47,15 +44,12 @@ for env_file_gpg in "$ENVS_DIR"/*.env.gpg; do
fi
fi
done

# Check if the rabbitmq container is running, stop all containers if necessary
if [[ "$(docker ps -q -f name=rabbitmq)" ]]; then
echo "Stopping running containers..."
docker compose -f "$DOCKER_COMPOSE_FILE" down
fi

# Start the Docker Compose services
echo "Starting Docker Compose services..."
docker compose -f "$DOCKER_COMPOSE_FILE" up --build -d

echo "Docker Compose services started."
12 changes: 8 additions & 4 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ services:
networks:
- bridge
depends_on:
- account-postgres
- rabbitmq
account-postgres:
condition: service_started
rabbitmq:
condition: service_healthy
restart: always

account-postgres:
Expand Down Expand Up @@ -92,8 +94,10 @@ services:
networks:
- bridge
depends_on:
- wishlist-mongo
- rabbitmq
wishlist-mongo:
condition: service_started
rabbitmq:
condition: service_healthy
restart: always

wishlist-mongo:
Expand Down
5 changes: 0 additions & 5 deletions encrypt.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/bin/bash

# Check if the passphrase file exists
PASSPHRASE_FILE="nshm.passphrase"
S3_BUCKET="s3://nus-backend-terraform"
SERVICES_DIR="services"

if [ ! -f "$PASSPHRASE_FILE" ]; then
echo "Passphrase file not found, downloading from S3..."
aws s3 cp "${S3_BUCKET}/${PASSPHRASE_FILE}" .
Expand All @@ -16,10 +14,8 @@ if [ ! -f "$PASSPHRASE_FILE" ]; then
else
echo "Passphrase file already exists."
fi

# Get a list of service directories
service_dirs=($(ls -d "$SERVICES_DIR"/*))

# Loop through each service directory and encrypt its .env file
for service_dir in "${service_dirs[@]}"; do
SERVICE_NAME=$(basename "$service_dir")
Expand All @@ -38,6 +34,5 @@ for service_dir in "${service_dirs[@]}"; do
echo "No .env file found in $service_dir, skipping..."
fi
done

echo "All services processed."

Binary file modified envs/account.env.gpg
Binary file not shown.
Binary file modified envs/notification.env.gpg
Binary file not shown.
Binary file modified envs/wishlist.env.gpg
Binary file not shown.
13 changes: 13 additions & 0 deletions helm/nshm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ web:
requests:
cpu: 250m
memory: 256Mi
# Notification service values
notification:
image:
repository: z1yoon/nshm-notification
tag: latest
replicas: 1
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 250m
memory: 256Mi
7 changes: 7 additions & 0 deletions services/account/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
envs/currency.env.gpg
envs/item.env.gpg
envs/notification.env.gpg
envs/web.env.gpg
envs/account.env.gpg


HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
Expand Down
42 changes: 42 additions & 0 deletions services/account/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Account Service

Account service is responsible for account-related operations, such as registration, profile management, log in/out, etc.

## Events

### Exchange

- Name: account
- Type: topic
- Durable: true

### Topic: updated

- Description: A user has updated the profile with new information.
- Message format: JSON-serialized item.

```json
{
{
"id": 1,
"email": "[email protected]",
"nickname": "mrcaidev",
"avatar_url": "https://avatars.githubusercontent.com/u/78269445?v=4",
"department_id": 1,
"phone_code": "65",
"phone_number": "80843976",
"preferred_currency": "CNY",
"created_at": "2024-09-23 12:19:10.415264+00",
"deleted_at": null
}
}
```

### Topic: deleted

- Description: A user has deleted the account.
- Message format: Account ID.

```
1
```
10 changes: 10 additions & 0 deletions services/account/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@
<version>4.0.0</version> <!-- 根据最新版本调整 -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-amqp</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>



</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package edu.nus.market.config;

import jakarta.annotation.Resource;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
// use Jackson2JsonMessageConverter to transfer the message JSON format
@Bean
public Jackson2JsonMessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}

@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(jsonMessageConverter());
return template;
}

// define account Topic exchange
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("account");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,32 @@ public ResponseEntity<Object> register(@Valid @RequestBody RegisterReq registerR
return accountService.registerService(registerReq);
}
@DeleteMapping("/{id}")
public ResponseEntity<Object> deleteAccount(@PathVariable int id, @RequestHeader(value = "Cookie", required = false) String token){
public ResponseEntity<Object> deleteAccount(@PathVariable int id, @CookieValue(value = "access_token", required = false) String token){
if (token == null || token.isEmpty()){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorMsg(ErrorMsgEnum.NOT_LOGGED_IN.ErrorMsg));
}
if (!JwtTokenManager.validateCookie(token)) {
if (!JwtTokenManager.validateToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorMsg(ErrorMsgEnum.UNAUTHORIZED_ACCESS.ErrorMsg));
}
int userId = JwtTokenManager.decodeCookie(token).getId();
int userId = JwtTokenManager.decodeAccessToken(token).getId();
if (userId != id){
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ErrorMsg(ErrorMsgEnum.ACCESS_FORBIDDEN.ErrorMsg));
}
return accountService.deleteAccountService(id);
}

@PatchMapping("/{id}")
public ResponseEntity<Object> updateProfile(@PathVariable int id, @Valid @RequestBody UpdateProfileReq req, BindingResult bindingResult, @RequestHeader(value = "Cookie", required = false) String token){
public ResponseEntity<Object> updateProfile(@PathVariable int id, @Valid @RequestBody UpdateProfileReq req, BindingResult bindingResult, @CookieValue(value = "access_token", required = false) String token){
if (token == null || token.isEmpty()){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorMsg(ErrorMsgEnum.NOT_LOGGED_IN.ErrorMsg));
}
if (!JwtTokenManager.validateCookie(token)) {
}
if (!JwtTokenManager.validateToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorMsg(ErrorMsgEnum.UNAUTHORIZED_ACCESS.ErrorMsg));
}
if (bindingResult.hasErrors()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMsg(ErrorMsgEnum.INVALID_DATA_FORMAT.ErrorMsg));
}
int userId = JwtTokenManager.decodeCookie(token).getId();
int userId = JwtTokenManager.decodeAccessToken(token).getId();
if (userId != id){
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ErrorMsg(ErrorMsgEnum.ACCESS_FORBIDDEN.ErrorMsg));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class AuthController {

// Get Account Info
@GetMapping("/me")
public ResponseEntity<Object> getAccount(@RequestHeader(value = "Cookie", required = false) String token){
public ResponseEntity<Object> getAccount(@CookieValue(value = "access_token", required = false) String token){
if (token == null || token.isEmpty())
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorMsg(ErrorMsgEnum.NOT_LOGGED_IN.ErrorMsg));
if (!JwtTokenManager.validateCookie(token)) {
if (!JwtTokenManager.validateToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorMsg(ErrorMsgEnum.UNAUTHORIZED_ACCESS.ErrorMsg));
}
return accountService.getAccountService(JwtTokenManager.decodeCookie(token).getId());
return accountService.getAccountService(JwtTokenManager.decodeAccessToken(token).getId());
}

// Login and Logout
Expand Down Expand Up @@ -61,17 +61,17 @@ public ResponseEntity<Object> resetPassword(@Valid @RequestBody ForgetPasswordRe

//Updating Account
@PutMapping("/me/password")
public ResponseEntity<Object> updateAccountPsw(@Valid @RequestBody UpdPswReq req, BindingResult bindingResult, @RequestHeader(value = "Cookie", required = false) String token){
public ResponseEntity<Object> updateAccountPsw(@Valid @RequestBody UpdPswReq req, BindingResult bindingResult, @CookieValue(value = "access_token", required = false) String token){
if (token == null || token.isEmpty()){
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorMsg(ErrorMsgEnum.NOT_LOGGED_IN.ErrorMsg));
}
if (!JwtTokenManager.validateCookie(token)) {
if (!JwtTokenManager.validateToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorMsg(ErrorMsgEnum.UNAUTHORIZED_ACCESS.ErrorMsg));
}
if (bindingResult.hasErrors()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMsg(ErrorMsgEnum.INVALID_DATA_FORMAT.ErrorMsg));
}
return accountService.updatePasswordService(req, JwtTokenManager.decodeCookie(token).getId());
return accountService.updatePasswordService(req, JwtTokenManager.decodeAccessToken(token).getId());
}

@GetMapping("/healthz")
Expand All @@ -80,3 +80,4 @@ public String checkHealth(){
}

}

11 changes: 11 additions & 0 deletions services/account/src/main/java/edu/nus/market/pojo/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ public Account(RegisterReq registerReq){
this.preferredCurrency = null;
}

public Account(int id, String email){
this.id = id;
this.email = email;
this.nickname = null;
this.avatarUrl = null;
this.departmentId = 0;
this.phoneCode = null;
this.phoneNumber = null;
this.preferredCurrency = null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package edu.nus.market.pojo.ResEntity;

import edu.nus.market.pojo.Account;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UpdateMessage {
int id;

String nickname;

String avatarUrl;


@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("UpdateMessage{")
.append("id=").append(id)
.append(", nickname='").append(nickname).append('\'')
.append(", avatarUrl='").append(avatarUrl).append('\'')
.append('}');
return sb.toString();
}

// all args constructor, input an Account object, and convert to this UpdateMessage object
public UpdateMessage(Account account) {
this.id = account.getId();
this.nickname = account.getNickname();
this.avatarUrl = account.getAvatarUrl();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import edu.nus.market.pojo.ReqEntity.*;
import edu.nus.market.pojo.ResEntity.JWTPayload;
import edu.nus.market.pojo.ResEntity.ResAccount;
import edu.nus.market.pojo.ResEntity.UpdateMessage;
import edu.nus.market.security.CookieManager;
import edu.nus.market.security.JwtTokenManager;
import edu.nus.market.security.PasswordHasher;
Expand Down Expand Up @@ -35,6 +36,9 @@ public class AccountServiceImpl implements AccountService{
@Resource
CookieManager cookieManager;

@Resource
MQService mqService;

/**
*
* @param id
Expand Down Expand Up @@ -135,7 +139,7 @@ public ResponseEntity<Object> updateProfileService(UpdateProfileReq updateProfil
}

account = accountDao.updateProfile(updateProfileReq, id);

mqService.sendUpdateMessage(new UpdateMessage(account));
return ResponseEntity.status(HttpStatus.OK).body(new ResAccount(account));

}
Expand All @@ -148,6 +152,7 @@ public ResponseEntity<Object> deleteAccountService(int id) {
//delete account
accountDao.softDeleteAccount(id);
ResponseCookie cookie = cookieManager.deleteCookie();
mqService.sendDeleteMessage(String.valueOf(id));
return ResponseEntity.status(HttpStatus.NO_CONTENT).header("Set-Cookie", cookie.toString()).build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package edu.nus.market.service;

import edu.nus.market.pojo.ResEntity.UpdateMessage;

public interface MQService {
public void sendUpdateMessage(UpdateMessage message);
public void sendDeleteMessage(String message);
}
Loading

0 comments on commit 9cc2a7e

Please sign in to comment.