Skip to content

Commit

Permalink
chore: using bun (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
adeherysh authored Apr 4, 2024
1 parent 1acbbfa commit f4ccd93
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 2,591 deletions.
File renamed without changes.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CORS_ALLOWED_HEADERS='*'
CORS_ALLOWED_METHOD='GET, POST, PATCH, PUT, DELETE, OPTIONS'
CORS_ALLOWED_ORIGIN='*'
SMOCKER_SECRET_KEY='secret'
1 change: 1 addition & 0 deletions .github/assets/logo-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions .github/assets/logo-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion .github/workflows/deploy-stg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ jobs:
build-push:
runs-on: ktbs-build-deploy-runner
steps:
- name: Setup bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest

- name: Run build & push
uses: kitabisa/composite-actions/frontend/build@v2
env:
CORS_ALLOWED_HEADERS: '*'
CORS_ALLOWED_METHOD: 'GET, POST, PATCH, PUT, DELETE, OPTIONS'
CORS_ALLOWED_ORIGIN: '*'
SMOCKER_SECRET_KEY: ${{ secrets.SMOCKER_SECRET_KEY }}
with:
project_id: ${{ secrets.GCP_PROJECT_ID_STG }}
credentials_json: ${{ secrets.GCP_SA_KEY_STG }}
artifact_registry_host: ${{ secrets.ARTIFACT_REGISTRY_HOST }}
artifact_registry_project_id: ${{ secrets.GCP_PROJECT_ID_INFRA }}
setup_pnpm: true
# setup_bun: true
using_nextjs: true
config: false

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ yarn-debug.log*
yarn-error.log*

# local env files
.env
.env*.local

# vercel
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:lts-slim
FROM oven/bun:1

ARG GIT_COMMIT
ARG VERSION
Expand All @@ -21,4 +21,4 @@ RUN adduser --disabled-password --gecos '' smocker

USER smocker

CMD ["node", "server.js"]
CMD ["bun", "server.js"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ help:
.PHONY: install
install:
@echo "Installing dependencies for ${APP_NAME} ${VERSION}"
CI=true pnpm install --frozen-lockfile
CI=true bun install --frozen-lockfile

.PHONY: build
build:
Expand All @@ -34,7 +34,7 @@ build:
CI_ENV=${ENV} \
NODE_ENV=production \
VERSION=${VERSION} \
pnpm build
bun build

.PHONY: package
package:
Expand Down
35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<h1 align="center">Smocker</h1>
<p align="center">
<a href="https://kitabisa.com" target="_blank">
<picture>
<source media="(prefers-color-scheme: dark)" srcset=".github/assets/logo-dark.svg">
<source media="(prefers-color-scheme: light)" srcset=".github/assets/logo-light.svg">
<img alt="Smocker" src=".github/assets/logo-light.svg" width="350" height="70" style="max-width: 100%;">
</picture>
</a>
</p>

<p align="center">
Supple mock server with <a href="https://github.com/faker-js/faker">Faker.js</a>
Supple mock server with random fake data using <a href="https://github.com/faker-js/faker">Faker.js</a>
</p>

<p align="center">
Expand All @@ -12,32 +20,42 @@

## Features

* Using [Bun](https://github.com/oven-sh/bun)
* Using [Next.js](https://github.com/vercel/next.js)
* Written in [Typescript](https://github.com/microsoft/TypeScript)
* Using [Next.js](https://github.com/vercel/next.js) (with catch all api route)
* Get random fake data using [Faker.js](https://github.com/faker-js/faker)
* Flexible on-demand response api (can modify body, status, headers, and delayed response)

## Getting Started

Setup Node.js:
Setup Bum (Bun Version Manager):
Bum is supported on Linux x86_64 and Darwin x86_64 (Mac OS)
You can enter `uname -ms` command in your terminal to see yours

```
$ brew install nvm
$ nvm install
$ curl -fsSL https://github.com/owenizedd/bum/raw/main/install.sh | bash
$ source ~/.zshrc
$ bum use
```

## Quick Start

Install dependencies:

```
$ pnpm i
$ bun install
```

Setup environment:

```
$ cp .env.example .env (and modify your env)
```

Run in local:

```
$ pnpm dev
$ bun dev
```

## How to use?
Expand All @@ -49,6 +67,7 @@ async function getUsers() {
const response = await fetch("https://geni.kitabisa.xyz/smocker/users", {
method: "GET",
headers: {
"X-Smocker-Secret": process.env.SMOCKER_SECRET_KEY,
"X-Smocker-Body": JSON.stringify({
api_code: 101000,
data: [
Expand Down
14 changes: 11 additions & 3 deletions app/[[...slug]]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ export async function DELETE(request: NextRequest) {

async function smocker(request: NextRequest) {
const requestHeaders = new Headers(request.headers)

if (requestHeaders.get('X-Smocker-Secret') !== process.env.SMOCKER_SECRET_KEY) {
return Response.json({
message: 'your secret key is missing or not allowed'
})
}

const body = requestHeaders.get('X-Smocker-Body')
? faker.helpers.fake(`${requestHeaders.get('X-Smocker-Body')}`)
: undefined
const status = requestHeaders.get('X-Smocker-Status')
? Number(requestHeaders.get('X-Smocker-Status'))
: 200
const headers = requestHeaders.get('X-Smocker-Headers')
? JSON.parse(`${requestHeaders.get('X-Smocker-Headers')}`)
: undefined
const headers = {
'Content-Type': 'application/json',
...JSON.parse(`${requestHeaders.get('X-Smocker-Headers')}`),
}
const delay = requestHeaders.get('X-Smocker-Delay')
? Number(requestHeaders.get('X-Smocker-Delay'))
: 0
Expand Down
5 changes: 5 additions & 0 deletions app/health_check/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export async function GET() {
return Response.json({
health_check: 'up'
})
}
Binary file added bun.lockb
Binary file not shown.
9 changes: 3 additions & 6 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ const nextConfig = {
headers: [
{
key: "Access-Control-Allow-Origin",
value: "*",
// value: process.env.ALLOWED_ORIGIN,
value: process.env.CORS_ALLOWED_ORIGIN,
},
{
key: "Access-Control-Allow-Methods",
value: "GET, POST, PATCH, PUT, DELETE, OPTIONS",
// value: process.env.ALLOWED_METHOD,
value: process.env.CORS_ALLOWED_METHOD,
},
{
key: "Access-Control-Allow-Headers",
value: "*",
// value: process.env.ALLOWED_HEADERS,
value: process.env.CORS_ALLOWED_HEADERS,
},
],
},
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"version": "0.1.0",
"private": true,
"scripts": {
"preinstall": "npx only-allow pnpm",
"preinstall": "bunx only-allow bun",
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "14.1.3",
"next": "14.1.4",
"react": "^18",
"react-dom": "^18"
},
Expand All @@ -20,7 +20,10 @@
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.1.3",
"typescript": "^5"
}
}
"eslint-config-next": "14.1.4",
"typescript": "^5",
"@types/bun": "latest"
},
"module": "index.ts",
"type": "module"
}
Loading

0 comments on commit f4ccd93

Please sign in to comment.