Created
June 9, 2020 17:33
-
-
Save omirobarcelo/bda16f099950824c0bda9280b9032b09 to your computer and use it in GitHub Desktop.
Service for serving posts
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
import { join, normalize } from '../../../deps.ts'; | |
import { readJson } from '../../../deps.ts'; | |
import { Request, Response, serveFile } from '../../../deps.ts'; | |
import { Post } from './interfaces/post.interface.ts'; | |
export class PostsService { | |
private CWD = '.'; | |
constructor() { | |
this.CWD = Deno.cwd(); | |
} | |
/** | |
* Reads JSON with available posts | |
*/ | |
async getPosts(): Promise<Post[]> { | |
return readJson(`${this.CWD}/post_list.json`) as Promise<Post[]>; | |
} | |
/** | |
* Serves the requested post | |
* @param id | |
* @param req Request that serves the file | |
*/ | |
async getPost(id: string, req: Request): Promise<Response> { | |
const postLocations = (await readJson(`${this.CWD}/post_loc.json`)) as { [id: string]: string }; | |
return serveFile(req.serverRequest as any, normalize(join(this.CWD, postLocations[id]))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment