├── .vscode └── settings.json ├── Dockerfile ├── Dockerfile.web ├── LICENSE.md ├── Makefile ├── README.md ├── app.ts ├── dem.json ├── public └── app.css ├── src ├── domain │ └── post.ts ├── repositories │ └── posts.ts ├── server.ts └── usecases │ └── posts.ts ├── tsconfig.json ├── vendor └── https │ ├── deno.land │ ├── std │ │ └── uuid │ │ │ └── mod.ts │ └── x │ │ └── dejs │ │ └── mod.ts │ └── denopkg.com │ └── syumai │ └── dinatra │ ├── mod.ts │ ├── params.ts │ └── response.ts └── views └── showPosts.ejs /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "deno.enable": true 3 | } -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2019- Andy Hayden 2 | # https://github.com/hayd/deno_docker 3 | 4 | FROM frolvlad/alpine-glibc:alpine-3.11_glibc-2.31 5 | 6 | ENV DENO_VERSION=1.0.0 7 | 8 | RUN apk add --virtual .download --no-cache curl \ 9 | && curl -fsSL https://github.com/denoland/deno/releases/download/v${DENO_VERSION}/deno-x86_64-unknown-linux-gnu.zip \ 10 | --output deno.zip \ 11 | && unzip deno.zip \ 12 | && rm deno.zip \ 13 | && chmod 777 deno \ 14 | && mv deno /bin/deno \ 15 | && apk del .download 16 | 17 | RUN addgroup -g 1993 -S deno \ 18 | && adduser -u 1993 -S deno -G deno \ 19 | && mkdir /deno-dir/ \ 20 | && chown deno:deno /deno-dir/ 21 | 22 | ENV DENO_DIR /deno-dir/ 23 | 24 | COPY app.ts /app/ 25 | COPY src /app/src 26 | COPY vendor /app/vendor 27 | 28 | WORKDIR /app 29 | 30 | RUN deno cache app.ts 31 | 32 | COPY . /app 33 | 34 | CMD deno run --allow-net --allow-read app.ts -p $PORT 35 | -------------------------------------------------------------------------------- /Dockerfile.web: -------------------------------------------------------------------------------- 1 | Dockerfile -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2019-present [syumai](https://github.com/syumai/) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SHELL=/bin/bash 2 | TARGET_SRC=$(shell shopt -s globstar && ls ./**/*.ts) 3 | 4 | lint: 5 | deno fmt --check $(TARGET_SRC) 6 | 7 | fmt: 8 | deno fmt $(TARGET_SRC) 9 | 10 | deploy: 11 | heroku container:push -a denoboard web --recursive 12 | heroku container:release -a denoboard web 13 | 14 | .PHONY: lint fmt deploy -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Denoboard 2 | 3 | - message board application implemented in Deno. 4 | 5 | ## Demo 6 | 7 | https://denoboard.herokuapp.com/posts 8 | 9 | ## Usage 10 | 11 | ```ts 12 | deno run -A app.ts 13 | ``` 14 | 15 | ### Using Docker 16 | 17 | ```console 18 | docker build . -t denoboard:latest 19 | docker run -p 8080:8080 -e PORT=8080 denoboard:latest 20 | ``` 21 | 22 | ## Deployment 23 | 24 | - This workflow deploys app to Heroku. 25 | 26 | ```console 27 | heroku login 28 | heroku container:login 29 | make deploy 30 | ``` 31 | 32 | ## Development 33 | 34 | - This app is using [dem](https://github.com/syumai/dem) as module manager. 35 | - If new module is needed as dependency, please use `dem add` and `dem link` to manage module. 36 | 37 | ## Status 38 | 39 | WIP 40 | -------------------------------------------------------------------------------- /app.ts: -------------------------------------------------------------------------------- 1 | import { 2 | app, 3 | get, 4 | post, 5 | } from "./vendor/https/denopkg.com/syumai/dinatra/mod.ts"; 6 | import { PostsMemoryRepository } from "./src/repositories/posts.ts"; 7 | import { Server } from "./src/server.ts"; 8 | 9 | const postsRepo = new PostsMemoryRepository(); 10 | const server = new Server(postsRepo); 11 | 12 | app( 13 | get("/", () => ``), 14 | get("/posts", () => server.showPosts()), 15 | post("/posts", ({ params }) => server.createPost(params)), 16 | ); 17 | -------------------------------------------------------------------------------- /dem.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.1.0", 3 | "modules": [ 4 | { 5 | "protocol": "https", 6 | "path": "deno.land/std", 7 | "version": "v0.51.0", 8 | "files": [ 9 | "/uuid/mod.ts" 10 | ] 11 | }, 12 | { 13 | "protocol": "https", 14 | "path": "deno.land/x/dejs", 15 | "version": "0.6.0", 16 | "files": [ 17 | "/mod.ts" 18 | ] 19 | }, 20 | { 21 | "protocol": "https", 22 | "path": "denopkg.com/syumai/dinatra", 23 | "version": "0.12.0", 24 | "files": [ 25 | "/mod.ts", 26 | "/params.ts", 27 | "/response.ts" 28 | ] 29 | } 30 | ] 31 | } -------------------------------------------------------------------------------- /public/app.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 16px; 3 | } 4 | 5 | a { 6 | color: #000; 7 | text-decoration: none; 8 | } 9 | 10 | a:hover { 11 | opacity: 0.5; 12 | } 13 | 14 | .form-container { 15 | display: flex; 16 | align-items: center; 17 | } 18 | 19 | .notice { 20 | color: #999; 21 | font-size: 12px; 22 | margin-bottom: 2px; 23 | } 24 | 25 | .github a { 26 | font-weight: bold; 27 | font-size: 12px; 28 | } 29 | 30 | .deno-logo { 31 | width: 64px; 32 | height: 64px; 33 | } 34 | 35 | .post-form { 36 | display: flex; 37 | } 38 | 39 | .post-form button, 40 | .post-form input { 41 | margin: 4px; 42 | } 43 | 44 | .post { 45 | padding: 6px; 46 | } 47 | 48 | .post .post-header { 49 | display: flex; 50 | align-items: flex-end; 51 | margin-bottom: 3px; 52 | } 53 | 54 | .post .post-header .post-header-name { 55 | font-weight: bold; 56 | margin-right: 6px; 57 | font-size: 16px; 58 | } 59 | 60 | .post .post-header .post-header-date { 61 | cursor: default; 62 | color: #999; 63 | font-size: 14px; 64 | } 65 | 66 | .post .post-body { 67 | font-size: 18px; 68 | } 69 | -------------------------------------------------------------------------------- /src/domain/post.ts: -------------------------------------------------------------------------------- 1 | export type Post = { 2 | id: string; 3 | name: string; 4 | body: string; 5 | createdAt: Date; 6 | }; 7 | -------------------------------------------------------------------------------- /src/repositories/posts.ts: -------------------------------------------------------------------------------- 1 | import { Post } from "../domain/post.ts"; 2 | import { v4 } from "../../vendor/https/deno.land/std/uuid/mod.ts"; 3 | 4 | export type PostsRepository = { 5 | getPosts(minutesLimit: number): Post[]; 6 | createPost(name: string, body: string, postsLimit: number): Post; 7 | }; 8 | 9 | export class PostsMemoryRepository { 10 | store: { 11 | posts: Post[]; 12 | } = { 13 | posts: [], 14 | }; 15 | 16 | getPosts(minutesLimit: number): Post[] { 17 | const filterDate = new Date(); 18 | filterDate.setMinutes(new Date().getMinutes() - minutesLimit); 19 | this.store.posts = this.store.posts.filter( 20 | (post) => filterDate.getTime() < post.createdAt.getTime(), 21 | ); 22 | return [...this.store.posts]; 23 | } 24 | 25 | createPost(name: string, body: string, postsLimit: number): Post { 26 | const post: Post = { 27 | id: v4.generate(), 28 | name, 29 | body, 30 | createdAt: new Date(), 31 | }; 32 | this.store.posts.push(post); 33 | if (this.store.posts.length > postsLimit) { 34 | this.store.posts = this.store.posts.slice( 35 | this.store.posts.length - postsLimit, 36 | ); 37 | } 38 | return post; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/server.ts: -------------------------------------------------------------------------------- 1 | import { PostsRepository } from "./repositories/posts.ts"; 2 | import { GetPosts, CreatePost } from "./usecases/posts.ts"; 3 | import { renderFile } from "../vendor/https/deno.land/x/dejs/mod.ts"; 4 | import { Response } from "../vendor/https/denopkg.com/syumai/dinatra/response.ts"; 5 | import { Params } from "../vendor/https/denopkg.com/syumai/dinatra/params.ts"; 6 | 7 | export class Server { 8 | constructor(private postsRepo: PostsRepository) {} 9 | 10 | async showPosts(): Promise { 11 | const getPosts = new GetPosts(this.postsRepo); 12 | const posts = getPosts.invoke(); 13 | return await renderFile("./views/showPosts.ejs", { 14 | name: "", 15 | posts, 16 | }); 17 | } 18 | 19 | async createPost({ name, body }: Params): Promise { 20 | const createPost = new CreatePost(this.postsRepo); 21 | createPost.invoke(name, body); 22 | const getPosts = new GetPosts(this.postsRepo); 23 | const posts = getPosts.invoke(); 24 | return [ 25 | 201, 26 | await renderFile("./views/showPosts.ejs", { 27 | name, 28 | posts, 29 | }), 30 | ]; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/usecases/posts.ts: -------------------------------------------------------------------------------- 1 | import { Post } from "../domain/post.ts"; 2 | import { PostsRepository } from "../repositories/posts.ts"; 3 | 4 | const nameMax = 20; 5 | const bodyMax = 100; 6 | 7 | const minutesLimit = 30; 8 | const postsLimit = 50; 9 | 10 | export class CreatePost { 11 | constructor(private repo: PostsRepository) {} 12 | 13 | invoke(name: string, body: string): Post { 14 | if (!name || name.length > nameMax) { 15 | // TODO: Add error msg 16 | throw 400; 17 | } 18 | if (!body || body.length > bodyMax) { 19 | // TODO: Add error msg 20 | throw 400; 21 | } 22 | return this.repo.createPost(name, body, postsLimit); 23 | } 24 | } 25 | 26 | export class GetPosts { 27 | constructor(private repo: PostsRepository) {} 28 | 29 | invoke(): Post[] { 30 | const posts: Post[] = [...this.repo.getPosts(minutesLimit)]; 31 | posts.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()); 32 | return posts; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "paths": { 5 | "deno": ["../../../../.deno/deno.d.ts"], 6 | "http://*": ["../../../../.deno/deps/http/*"], 7 | "https://*": ["../../../../.deno/deps/https/*"] 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /vendor/https/deno.land/std/uuid/mod.ts: -------------------------------------------------------------------------------- 1 | export * from "https://deno.land/std@v0.51.0/uuid/mod.ts"; 2 | -------------------------------------------------------------------------------- /vendor/https/deno.land/x/dejs/mod.ts: -------------------------------------------------------------------------------- 1 | export * from "https://deno.land/x/dejs@0.6.0/mod.ts"; 2 | -------------------------------------------------------------------------------- /vendor/https/denopkg.com/syumai/dinatra/mod.ts: -------------------------------------------------------------------------------- 1 | export * from "https://denopkg.com/syumai/dinatra@0.12.0/mod.ts"; 2 | -------------------------------------------------------------------------------- /vendor/https/denopkg.com/syumai/dinatra/params.ts: -------------------------------------------------------------------------------- 1 | export * from "https://denopkg.com/syumai/dinatra@0.12.0/params.ts"; 2 | -------------------------------------------------------------------------------- /vendor/https/denopkg.com/syumai/dinatra/response.ts: -------------------------------------------------------------------------------- 1 | export * from "https://denopkg.com/syumai/dinatra@0.12.0/response.ts"; 2 | -------------------------------------------------------------------------------- /views/showPosts.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | Denoboard 13 | 14 | 15 |

16 | 17 | Denoboard 18 | 19 |

20 |
21 | 22 |
23 | <% if (name) { %> 24 | 33 | 42 | <% } else { %> 43 | 51 | 59 | <% } %> 60 | 61 |
62 |
63 |
64 | Post disappears after 30 minutes. 65 |
66 |
67 | GitHub 68 |
69 |
70 |
71 | <% for (let post of posts) { %> 72 |
73 |
74 |
<%= post.name %>
75 |
<%= post.createdAt %>
76 |
77 |
<%= post.body %>
78 |
79 | <% } %> 80 |
81 | 82 | 83 | --------------------------------------------------------------------------------