diff --git a/src/handlers/home.tsx b/src/handlers/home.tsx index 5f5ccb8..b01cd92 100644 --- a/src/handlers/home.tsx +++ b/src/handlers/home.tsx @@ -2,16 +2,16 @@ import { Hono } from "hono"; import { Home } from "@blog/templates/Pages/Home"; import { PostMeta } from "@blog/models/PostMeta"; import { Post } from '@blog/models/Post'; -import { PostFileService } from '@blog/services/post-file'; +import { PostService } from '@blog/services/post-file'; type Posts = { - postService: PostFileService + postService: PostService }; const app = new Hono<{ Variables: Posts }>(); app.get("/", async (c) => { - const postService: PostFileService = c.get('postService'); + const postService: PostService = c.get('postService'); const postList: PostMeta[] = Array.from(postService.getPublishedPosts()) .map((p: Post) => p.meta); diff --git a/src/handlers/posts.tsx b/src/handlers/posts.tsx index 60b5b3e..1c7ba87 100644 --- a/src/handlers/posts.tsx +++ b/src/handlers/posts.tsx @@ -2,17 +2,17 @@ import { Hono } from 'hono'; import { PostPage } from '@blog/templates/Pages/PostPage'; import { FourOhFour } from '@blog/templates/Pages/FourOhFour'; import { SiteMeta } from '@blog/models/SiteMeta'; -import { PostFileService } from '@blog/services/post-file'; +import { PostService } from '@blog/services/post-file'; type Posts = { - postService: PostFileService + postService: PostService } const app = new Hono<{ Variables: Posts }>(); app.get('/:slug', async (c) => { const postSlug: string = c.req.param("slug"); - const postService: PostFileService = c.get('postService'); + const postService: PostService = c.get('postService'); try { const post = postService.getPost(postSlug); diff --git a/src/middleware/post-service.ts b/src/middleware/post-service.ts index 2e842ca..f118f01 100644 --- a/src/middleware/post-service.ts +++ b/src/middleware/post-service.ts @@ -1,12 +1,13 @@ import { createFactory } from 'hono/factory'; -import { PostFileService } from '@blog/services/post-file'; +import { createPostService, PostService } from '@blog/services/post-file'; +import { POST_PATH } from '@blog/config'; const factory = createFactory(); -let postFileService: PostFileService; +let postFileService: PostService; export const postFileMiddleware = factory.createMiddleware(async (c, next) => { if (!postFileService) { - postFileService = await PostFileService.create(); + postFileService = await createPostService(POST_PATH); } c.set('postService', postFileService); diff --git a/src/services/post-file.ts b/src/services/post-file.ts index 0a23281..b26b19f 100644 --- a/src/services/post-file.ts +++ b/src/services/post-file.ts @@ -47,26 +47,26 @@ export async function readPostFile(path: string): Promise { }; } -export class PostFileService { +export async function createPostService(path: string): Promise { + const posts = new Map(); + const postFiles: string[] = await readdir(path); + + for (const postFile of postFiles) { + const post = await readPostFile(POST_PATH + "/" + postFile); + const key = post.meta?.slug || postFile.slice(0, -3); + posts.set(key, post); + } + + return new PostService(posts); +} + +export class PostService { private posts: Map; public constructor(posts: Map) { this.posts = posts; } - public static async create() { - const posts = new Map(); - const postFiles: string[] = await readdir(POST_PATH); - - for (const postFile of postFiles) { - const post = await readPostFile(POST_PATH + "/" + postFile); - const key = post.meta?.slug || postFile.slice(0, -3); - posts.set(key, post); - } - - return new PostFileService(posts); - } - public getPosts(): Map { return this.posts; }