generated from Spelaya-melon-X/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Saving submissions #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
840feb1
add s3Repository, redisRepository for submissions
toximu 228d8f9
.gitignore
toximu de98401
delete test
toximu 6e43221
change workflow
toximu 74ca9ba
refactor s3 integration, change submission class
toximu 9ec1b41
refactor s3 integration, change submission class
toximu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ build/ | |
| .classpath | ||
| .factorypath | ||
| .project | ||
| .settings | ||
| .authSettings | ||
| .springBeans | ||
| .sts4-cache | ||
| bin/ | ||
|
|
||
This file contains hidden or 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 hidden or 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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/codzilla/backend/S3/S3Configuration.java
This file contains hidden or 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,40 @@ | ||
| package com.codzilla.backend.S3; | ||
|
|
||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
| import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
|
|
||
| import java.net.URI; | ||
|
|
||
|
|
||
| @Slf4j | ||
| @Configuration | ||
| @EnableConfigurationProperties(S3Settings.class) | ||
| public class S3Configuration { | ||
|
|
||
| @Autowired | ||
| S3Settings settings; | ||
|
|
||
| @Bean | ||
| public S3Client s3Client() { | ||
| log.info(settings.toString()); | ||
| return S3Client.builder() | ||
| .endpointOverride(URI.create(settings.endpoint())) | ||
| .credentialsProvider(StaticCredentialsProvider.create( | ||
| AwsBasicCredentials.create( | ||
| settings.accessKey(), | ||
| settings.secretKey()))) | ||
| .region(Region.of(settings.region())) | ||
| .forcePathStyle(true) | ||
| .build(); | ||
| } | ||
|
|
||
|
|
||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/codzilla/backend/S3/S3Initialization.java
This file contains hidden or 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,34 @@ | ||
| package com.codzilla.backend.S3; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.stereotype.Component; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.model.CreateBucketRequest; | ||
| import software.amazon.awssdk.services.s3.model.HeadBucketRequest; | ||
| import software.amazon.awssdk.services.s3.model.NoSuchBucketException; | ||
|
|
||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class S3Initialization { | ||
|
|
||
| @Autowired | ||
| S3Settings settings; | ||
|
|
||
| @Autowired | ||
| S3Client s3Client; | ||
|
|
||
| @EventListener(ApplicationReadyEvent.class) | ||
| public void initBucket() { | ||
| try { | ||
| s3Client.headBucket(HeadBucketRequest.builder().bucket(settings.bucketName()).build()); | ||
| log.info("Already have bucket: " + settings.bucketName()); | ||
| } catch (NoSuchBucketException e) { | ||
| s3Client.createBucket(CreateBucketRequest.builder().bucket(settings.bucketName()).build()); | ||
| log.info("Create bucket: " + settings.bucketName()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,15 @@ | ||
| package com.codzilla.backend.S3; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| @ConfigurationProperties(prefix = "app.s3") | ||
| public record S3Settings( | ||
| String endpoint, | ||
| String accessKey, | ||
| String secretKey, | ||
| String region, | ||
| String bucketName | ||
|
|
||
| ) { | ||
| } | ||
|
|
31 changes: 31 additions & 0 deletions
31
src/main/java/com/codzilla/backend/Submissions/RedisSubmissionRepository.java
This file contains hidden or 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,31 @@ | ||
| package com.codzilla.backend.Submissions; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.stereotype.Repository; | ||
| import tools.jackson.databind.ObjectMapper; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| @Repository | ||
| public class RedisSubmissionRepository implements SubmissionRepository { | ||
|
|
||
| @Autowired | ||
| protected ObjectMapper objectMapper; | ||
|
|
||
| @Autowired | ||
| RedisTemplate<String, Object> redisTemplate; | ||
|
|
||
| @Override | ||
| public void save(Submission submission) { | ||
| redisTemplate.opsForValue().set("submission:" + submission.id(), submission); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Submission> get(UUID id) { | ||
| Object value = redisTemplate.opsForValue().get("submission:"+id); | ||
| if (value == null) return Optional.empty(); | ||
| return Optional.of(objectMapper.convertValue(value, Submission.class)); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/codzilla/backend/Submissions/S3SubmissionRepository.java
This file contains hidden or 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,55 @@ | ||
| package com.codzilla.backend.Submissions; | ||
|
|
||
| import com.codzilla.backend.S3.S3Settings; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Repository; | ||
| import software.amazon.awssdk.core.sync.RequestBody; | ||
| import software.amazon.awssdk.core.sync.ResponseTransformer; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
| import software.amazon.awssdk.services.s3.model.NoSuchKeyException; | ||
| import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
|
|
||
| @Repository | ||
| public class S3SubmissionRepository implements SubmissionRepository { | ||
| @Autowired | ||
| S3Client s3Client; | ||
|
|
||
| @Autowired | ||
| S3Settings settings; | ||
|
|
||
| @Override | ||
| public void save(Submission submission) { | ||
| s3Client.putObject( | ||
| PutObjectRequest.builder() | ||
| .contentType("text/plain") | ||
| .bucket(settings.bucketName()) | ||
| .key("submissions/" + submission.id() + ".cpp") | ||
| .build(), | ||
| RequestBody.fromBytes(submission.content()) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<Submission> get(UUID id) { | ||
| try { | ||
| String code = s3Client.getObject( | ||
| GetObjectRequest.builder() | ||
| .bucket(settings.bucketName()) | ||
| .key("submissions/" + id + ".cpp") // todo: cpp? | ||
| .build(), | ||
| ResponseTransformer.toBytes() | ||
|
|
||
| ).asUtf8String(); | ||
| Submission result = new Submission(id, UUID.fromString(""), "".getBytes(), Language.CPP); | ||
| return Optional.of(result); | ||
| } catch (NoSuchKeyException ex) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/codzilla/backend/Submissions/Submission.java
This file contains hidden or 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 @@ | ||
| package com.codzilla.backend.Submissions; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public record Submission( | ||
| UUID id, | ||
| UUID userId, | ||
| byte[] content, | ||
| Language language | ||
| ) { | ||
| } | ||
|
|
||
| enum Language { CPP, PY, JAVA } | ||
33 changes: 33 additions & 0 deletions
33
src/main/java/com/codzilla/backend/Submissions/SubmissionConfiguration.java
This file contains hidden or 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,33 @@ | ||
| package com.codzilla.backend.Submissions; | ||
|
|
||
|
|
||
| import com.codzilla.backend.S3.S3Settings; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.data.redis.serializer.JacksonJsonRedisSerializer; | ||
| import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
|
||
|
|
||
| @Slf4j | ||
| @Configuration | ||
| public class SubmissionConfiguration { | ||
|
|
||
| @Bean | ||
| RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
| var redisTemplate = new RedisTemplate<String, Object>(); | ||
| redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
|
|
||
| redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
|
|
||
| JacksonJsonRedisSerializer<Object> serializer = new JacksonJsonRedisSerializer<>(Object.class); | ||
|
|
||
| redisTemplate.setValueSerializer(serializer); | ||
| return redisTemplate; | ||
| } | ||
|
|
||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/codzilla/backend/Submissions/SubmissionRepository.java
This file contains hidden or 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,11 @@ | ||
| package com.codzilla.backend.Submissions; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| public interface SubmissionRepository { | ||
|
|
||
|
|
||
| void save(Submission submission); | ||
| Optional<Submission> get(UUID id); | ||
| } |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/codzilla/backend/Submissions/SubmissionService.java
This file contains hidden or 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,4 @@ | ||
| package com.codzilla.backend.Submissions; | ||
|
|
||
| public class SubmissionService { | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
лучше сделать
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а для юзеров сделать
id uuid primary keyи потом повесить индекс на полеemail