-
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.
feat: added anchors to the articles + local testing setup
- Loading branch information
1 parent
9edfaf5
commit 23dc644
Showing
8 changed files
with
77 additions
and
5 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
9 changes: 8 additions & 1 deletion
9
src/main/java/com/buyallmemes/blogapi/github/GitHubConfiguration.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,22 +1,29 @@ | ||
package com.buyallmemes.blogapi.github; | ||
|
||
import com.buyallmemes.blogapi.domain.dependencies.PostQueryRepository; | ||
import org.kohsuke.github.GitHub; | ||
import org.kohsuke.github.GitHubBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
@Configuration | ||
@ConditionalOnProperty(name = "blog.posts.fetch-from-github", havingValue = "true") | ||
public class GitHubConfiguration { | ||
|
||
@Bean | ||
public GitHub gitHub(@Value("${github.token}") Optional<String> token) throws IOException { | ||
GitHub gitHub(@Value("${github.token}") Optional<String> token) throws IOException { | ||
GitHubBuilder gitHubBuilder = new GitHubBuilder(); | ||
token.ifPresent(gitHubBuilder::withOAuthToken); | ||
return gitHubBuilder.build(); | ||
} | ||
|
||
@Bean | ||
PostQueryRepository gitHubPostRepository(GitHub gitHub) { | ||
return new GitHubPostRepository(gitHub); | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
src/main/java/com/buyallmemes/blogapi/local/LocalPostRepository.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,63 @@ | ||
package com.buyallmemes.blogapi.local; | ||
|
||
import com.buyallmemes.blogapi.domain.Post; | ||
import com.buyallmemes.blogapi.domain.dependencies.PostQueryRepository; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* This class is responsible for fetching blog posts from the local file system. | ||
* Intended to be used when the property `blog.posts.fetch-from-github` is set to `false`. | ||
* Mainly used for development. | ||
*/ | ||
@Component | ||
@ConditionalOnProperty(name = "blog.posts.fetch-from-github", havingValue = "false", matchIfMissing = true) | ||
class LocalPostRepository implements PostQueryRepository { | ||
|
||
private final static String POSTS_PATH = "posts"; | ||
|
||
@Override | ||
public List<Post> getAllPosts() { | ||
File[] files = findBlogPostFiles(); | ||
return Arrays.stream(files) | ||
.map(File::toPath) | ||
.map(this::buildPost) | ||
.sorted(this::reverseFileName) | ||
.toList(); | ||
} | ||
|
||
private File[] findBlogPostFiles() { | ||
return Paths.get("./" + POSTS_PATH) | ||
.normalize() | ||
.toAbsolutePath() | ||
.toFile() | ||
.listFiles(); | ||
} | ||
|
||
private int reverseFileName(Post p1, Post p2) { | ||
return p2.filename() | ||
.compareTo(p1.filename()); | ||
} | ||
|
||
private Post buildPost(Path path) { | ||
try { | ||
String fileName = path.getFileName() | ||
.toString(); | ||
String content = Files.readString(path); | ||
return Post.builder() | ||
.filename(fileName) | ||
.content(content) | ||
.build(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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,2 @@ | ||
spring.application.name=blog-api | ||
blog.posts.fetch-from-github=false |
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,2 +1,3 @@ | ||
spring.application.name=blog-api | ||
blog.posts.fetch-from-github=true | ||
github.token=${GITHUB_TOKEN:#{null}} |
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 @@ | ||
blog.posts.fetch-from-github=false |