-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Informatik-Projekt-Kurs/dev
Dev
- Loading branch information
Showing
20 changed files
with
335 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Build the application | ||
FROM maven:3.8.5-openjdk-17 as builder | ||
WORKDIR /app | ||
COPY pom.xml . | ||
RUN mvn dependency:go-offline | ||
COPY src/ ./src/ | ||
RUN mvn clean package -DskipTests=true | ||
|
||
# Run the application | ||
FROM openjdk:17 | ||
WORKDIR /app | ||
COPY --from=builder /app/target/MeetMate.jar /app/MeetMate.jar | ||
CMD ["java", "-jar", "MeetMate.jar"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
version: '3' | ||
services: | ||
|
||
meet-mate: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
ports: | ||
- "8081:8080" | ||
depends_on: | ||
- postgres | ||
environment: | ||
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/test | ||
SPRING_DATASOURCE_USERNAME: postgres | ||
SPRING_DATASOURCE_PASSWORD: 1234 | ||
|
||
postgres: | ||
image: postgres:16 | ||
restart: always | ||
environment: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: 1234 | ||
POSTGRES_DB: test | ||
ports: | ||
- "5432:5432" | ||
volumes: | ||
- ./data:/var/lib/postgresql/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
# Backend Repo | ||
# Backend Repository of MeetMate | ||
|
||
Das Reich von Tim :D | ||
## Setting up the application | ||
|
||
All the following commands are to be executed in the root directory of the project. | ||
|
||
### Creating the Docker image | ||
Make the jar file with<br> | ||
> mvn clean package | ||
--- | ||
### Running the application with Docker Compose | ||
Start the application using | ||
>docker-compose up | ||
--- | ||
### Stopping the application | ||
To stop the running application use either `Ctrl + C` or | ||
>docker-compose stop | ||
or delete the created containers with | ||
>docker-compose down |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/MeetMate/experiments/AuthenticationHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.MeetMate.experiments; | ||
|
||
public @interface AuthenticationHeader {} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/MeetMate/experiments/Experimentational.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package com.MeetMate.experiments; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.SOURCE) | ||
public @interface Experimentational {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.MeetMate.experiments; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping(path = "test") | ||
public class Test { | ||
//Link in SecurityConfig.java | ||
@PostMapping(path = "test") | ||
public String getUser(){ | ||
return "asdasdasd"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
public enum Role { | ||
ADMIN, | ||
COMPANY, | ||
CLIENT; | ||
CLIENT, | ||
COMPANY_OWNER, | ||
COMPANY_MEMBER; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/MeetMate/throttle/GlobalRateLimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.MeetMate.throttle; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedList; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GlobalRateLimiter extends OncePerRequestFilter { | ||
|
||
private final LinkedList<Long> requests = new LinkedList<>(); | ||
private final int maxRequests = 500; | ||
private final long refreshTime = 1000 * 1; // 1 second | ||
|
||
@Override | ||
protected void doFilterInternal( | ||
@NotNull HttpServletRequest request, | ||
@NotNull HttpServletResponse response, | ||
@NotNull FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
requests.addLast(System.currentTimeMillis()); | ||
|
||
clearRequests(); | ||
|
||
if (requests.size() > maxRequests) { | ||
response.setStatus(429); | ||
response.getWriter().write("Too many requests"); | ||
return; | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private void clearRequests() { | ||
while (!requests.isEmpty() | ||
&& System.currentTimeMillis() - requests.getFirst() > refreshTime) | ||
requests.remove(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.MeetMate.throttle; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class IPRateLimiter extends OncePerRequestFilter { | ||
|
||
private final HashMap<String, LinkedList<Long>> requests = new HashMap<>(); | ||
private final int maxRequests = 2; | ||
private final long refreshTime = 1000 * 10; // 10 seconds | ||
|
||
@Override | ||
protected void doFilterInternal( | ||
@NotNull HttpServletRequest request, | ||
@NotNull HttpServletResponse response, | ||
@NotNull FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
String ip = request.getRemoteAddr(); | ||
|
||
if (requests.containsKey(ip)) | ||
requests.get(ip).addLast(System.currentTimeMillis()); | ||
else | ||
requests.put(ip, new LinkedList<Long>(Collections.singleton(System.currentTimeMillis()))); | ||
|
||
clearRequests(ip); | ||
|
||
if (requests.get(ip).size() > maxRequests) { | ||
response.setStatus(429); | ||
response.getWriter().write("Too many requests"); | ||
return; | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
private void clearRequests(String ip) { | ||
while (!requests.isEmpty() | ||
&& System.currentTimeMillis() - requests.get(ip).getFirst() > refreshTime) | ||
requests.get(ip).remove(); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.