Skip to content

Commit

Permalink
feat: added anchors to the articles + local testing setup
Browse files Browse the repository at this point in the history
  • Loading branch information
buyallmemes committed Apr 1, 2024
1 parent 9edfaf5 commit 23dc644
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 5 deletions.
2 changes: 1 addition & 1 deletion posts/29032024_hello_world.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Hello, world!
# <a id="lets-build" href="#lets-build"></a>Let's build

I hate frontend. But at least, I figured out how to use markdown to render content, so I don't have to struggle with
WYSIWYG editors, at least now.
Expand Down
2 changes: 1 addition & 1 deletion posts/31032024.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Let's build
# <a id="lets-build" href="#lets-build"></a>Let's build

So, the tech.
Oh yes, the most important part — the tech.
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Component
@RequiredArgsConstructor
class GitHubPostRepository implements PostQueryRepository {

Expand Down
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);
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application-local.properties
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
1 change: 1 addition & 0 deletions src/main/resources/application.properties
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}}
1 change: 1 addition & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blog.posts.fetch-from-github=false

0 comments on commit 23dc644

Please sign in to comment.