Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
adeiskandarzulkarnaen committed Dec 7, 2024
1 parent 621cad4 commit 4a634c8
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 11 deletions.
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
coverage*
node_modules
dist
Dockerfile*
docker-compose*
.dockerignore
.editorconfig
.env
.git
.gitignore
.DS_Store
.vscode
.idea
8 changes: 4 additions & 4 deletions .github/workflows/bunhono_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
bun run lint
bun run test
env:
APP_PORT: ${{ variables.APP_PORT }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
ACCESS_TOKEN_AGE: ${{ variables.ACCESS_TOKEN_AGE }}
ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }}
APP_PORT: ${{ variables.TEST_APP_PORT }}
DATABASE_URL: ${{ variables.TEST_DATABASE_URL }}
ACCESS_TOKEN_AGE: ${{ variables.TEST_ACCESS_TOKEN_AGE }}
ACCESS_TOKEN_SECRET: ${{ variables.TEST_ACCESS_TOKEN_SECRET }}
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM oven/bun:alpine

ENV PORT=
ENV DATABASE_URL=
ENV ACCESS_TOKEN_AGE=
ENV ACCESS_TOKEN_SECRET=

LABEL maintainer="adeiskandarzulkarnaen@gmail.com"
LABEL description="A Docker image for a Bun HONO application"
LABEL version="1.0"

WORKDIR /app

COPY . .

RUN bun install
RUN bunx prisma generate

EXPOSE $APP_PORT/tcp

CMD ["sh", "-c", "bun run migrate:up && bun run start:dev"]
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
"dependencies": {
"@prisma/client": "6.0.1",
"hono": "^4.6.12",
"instances-container": "^2.0.6"
"instances-container": "^2.0.6",
"redis": "^4.7.0"
},
"devDependencies": {
"@eslint/js": "^9.15.0",
"@types/bun": "latest",
"@types/redis": "^4.0.11",
"eslint": "^9.15.0",
"globals": "^15.12.0",
"prisma": "^6.0.1",
Expand Down
7 changes: 7 additions & 0 deletions src/Applications/cache/CacheServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
abstract class CacheServer {
abstract set(key: string, value: string): Promise<void>;
abstract get(key: string): Promise<string>;
abstract delete(key:string): Promise<unknown>;
};

export default CacheServer;
2 changes: 1 addition & 1 deletion src/Applications/use_case/UserLoginUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { eNewAuth, NewAuth } from '@domains/authentications/NewAuth';
import { eNewAuth, NewAuth } from '@domains/authentications/entities/NewAuth';
import { eUserLogin, UserLogin } from '@domains/users/entities/UserLogin';
import type UserRepository from '@domains/users/UserRepository';
import type PasswordHash from '@applications/security/PasswordHash';
Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions src/Infrastructures/cache/redis/RedisCacheServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createClient, RedisClientType } from 'redis';
import CacheServer from '@applications/cache/CacheServer';



class RedisCacheServer implements CacheServer{
private client: RedisClientType;
constructor() {
this.client = createClient({
socket: {
host: process.env.REDIS_HOST,
}
});

this.client.on('error', (error) => console.error(error));
this.client.connect();
}

public async set(key: string, value: string, expirationInSecond = 1800) : Promise<void> {
await this.client.set(key, value, {
EX: expirationInSecond,
});
}

public async get(key: string): Promise<string> {
const result = await this.client.get(key);
if (!result) throw new Error('Cache tidak ditemukan');
return result;
}

public async delete(key: string): Promise<number> {
return this.client.del(key);
}
};

export default RedisCacheServer;
Empty file.
Empty file.
34 changes: 34 additions & 0 deletions src/Infrastructures/storage/LocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// const fs = require('fs');

// class StorageService {
// constructor(folder) {
// this._folder = folder;

// if (!fs.existsSync(folder)) {
// fs.mkdirSync(folder, { recursive: true });
// }
// }

// writeFile(file, meta) {
// const filename = +new Date() + meta.filename;
// const path = `${this._folder}/${filename}`;

// const fileStream = fs.createWriteStream(path);

// return new Promise((resolve, reject) => {
// fileStream.on('error', (error) => reject(error));
// file.pipe(fileStream);
// file.on('end', () => resolve(filename));
// });
// }

// deleteFile(url) {
// const filename = url.split('/').pop();
// const path = `${this._folder}/${filename}`;
// fs.unlink(path, () => {
// console.log('gagal menghapus, file tidak ditemukan');
// });
// }
// }

// module.exports = StorageService;
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { bearerAuth } from 'hono/bearer-auth';
import { bearerAuth as bearerAuthMiddleware } from 'hono/bearer-auth';
import { MiddlewareHandler } from 'hono';


function bearerMiddleware(): MiddlewareHandler {
function bearerAuth(): MiddlewareHandler {
const token: string | undefined = process.env.BEARER_TOKEN;

if (!token) {
throw new Error('Environment variable BEARER_TOKEN is not set');
}

return bearerAuth({
return bearerAuthMiddleware({
token,
noAuthenticationHeaderMessage: {
status: 'fail',
Expand All @@ -26,5 +26,4 @@ function bearerMiddleware(): MiddlewareHandler {
});
};

export default bearerMiddleware;

export default bearerAuth;

0 comments on commit 4a634c8

Please sign in to comment.