lapeh 2.4.4 → 2.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/doc/ARCHITECTURE_GUIDE.md +73 -0
- package/doc/CHANGELOG.md +97 -0
- package/doc/CHEATSHEET.md +94 -0
- package/doc/CLI.md +139 -0
- package/doc/CONTRIBUTING.md +105 -0
- package/doc/DEPLOYMENT.md +245 -0
- package/doc/FAQ.md +81 -0
- package/doc/FEATURES.md +165 -0
- package/doc/GETTING_STARTED.md +108 -0
- package/doc/INTRODUCTION.md +60 -0
- package/doc/PACKAGES.md +66 -0
- package/doc/PERFORMANCE.md +91 -0
- package/doc/ROADMAP.md +93 -0
- package/doc/SECURITY.md +93 -0
- package/doc/STRUCTURE.md +90 -0
- package/doc/TUTORIAL.md +192 -0
- package/package.json +2 -1
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# Deployment Guide
|
|
2
|
+
|
|
3
|
+
Panduan ini akan membantu Anda membawa aplikasi Lapeh dari `localhost` ke Server Production (VPS/Cloud).
|
|
4
|
+
|
|
5
|
+
## Persiapan Pra-Deploy
|
|
6
|
+
|
|
7
|
+
Sebelum deploy, pastikan:
|
|
8
|
+
|
|
9
|
+
1. **Environment Variables**: Siapkan nilai `.env` untuk production (DB URL asli, JWT Secret yang kuat).
|
|
10
|
+
2. **Build**: TypeScript harus dikompilasi ke JavaScript.
|
|
11
|
+
|
|
12
|
+
## Strategi 1: VPS (Ubuntu/Debian) dengan PM2
|
|
13
|
+
|
|
14
|
+
Ini adalah cara paling umum dan hemat biaya.
|
|
15
|
+
|
|
16
|
+
### 1. Setup Server
|
|
17
|
+
|
|
18
|
+
Pastikan Node.js, NPM, dan Database (PostgreSQL/MySQL) sudah terinstall di server.
|
|
19
|
+
|
|
20
|
+
### 2. Clone & Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
git clone https://github.com/username/repo-anda.git
|
|
24
|
+
cd repo-anda
|
|
25
|
+
npm install --production=false # Install devDependencies juga untuk build
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 3. Build Aplikasi
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm run build
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Ini akan menghasilkan folder `dist/`.
|
|
35
|
+
|
|
36
|
+
### 4. Setup Database Production
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# Setup .env dulu
|
|
40
|
+
cp .env.example .env
|
|
41
|
+
nano .env # Isi dengan config production
|
|
42
|
+
|
|
43
|
+
# Jalankan migrasi (Hanya deploy schema, jangan reset!)
|
|
44
|
+
npm run prisma:deploy
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 5. Jalankan dengan PM2
|
|
48
|
+
|
|
49
|
+
Lapeh kini menyertakan konfigurasi otomatis PM2 (`ecosystem.config.js`).
|
|
50
|
+
|
|
51
|
+
1. **Install PM2 Global**:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install -g pm2
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
2. **Jalankan Aplikasi**:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pm2 start ecosystem.config.js
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Perintah ini akan:
|
|
64
|
+
|
|
65
|
+
- Menjalankan aplikasi dalam mode **Cluster** (menggunakan semua core CPU yang tersedia).
|
|
66
|
+
- Mengatur `NODE_ENV` ke `production`.
|
|
67
|
+
- Mengaktifkan auto-restart jika aplikasi crash atau penggunaan memori melebihi 1GB.
|
|
68
|
+
|
|
69
|
+
3. **Cek Status**:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pm2 status
|
|
73
|
+
pm2 logs
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
4. **Simpan Config Startup (Agar jalan saat server reboot)**:
|
|
77
|
+
```bash
|
|
78
|
+
pm2 save
|
|
79
|
+
pm2 startup
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### ❓ FAQ: Mengapa Aplikasi Saya Muncul Ganda di PM2?
|
|
83
|
+
|
|
84
|
+
Jika Anda menjalankan `pm2 list` dan melihat nama aplikasi Anda muncul lebih dari satu kali (misal: `my-app` ada 2 atau 4 baris), **JANGAN KHAWATIR**. Ini adalah fitur, bukan bug.
|
|
85
|
+
|
|
86
|
+
- **Penyebab**: Konfigurasi `instances: "max"` dan `exec_mode: "cluster"` di `ecosystem.config.js`.
|
|
87
|
+
- **Fungsi**: PM2 mendeteksi jumlah inti CPU (Core) di VPS Anda dan membuat 1 proses worker untuk setiap core.
|
|
88
|
+
- Jika VPS punya 2 vCPU -> Muncul 2 proses.
|
|
89
|
+
- Jika VPS punya 4 vCPU -> Muncul 4 proses.
|
|
90
|
+
- **Keuntungan**: Aplikasi Anda menjadi **Multi-Threaded**. Request yang masuk akan dibagi rata ke semua proses, meningkatkan performa 2x-4x lipat dibanding mode biasa.
|
|
91
|
+
|
|
92
|
+
**Cara mengubah ke Single Instance (Hemat RAM):**
|
|
93
|
+
Jika RAM server Anda terbatas (misal 512MB/1GB) dan ingin menghemat resource, ubah `ecosystem.config.js`:
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
module.exports = {
|
|
97
|
+
apps: [
|
|
98
|
+
{
|
|
99
|
+
name: "my-app",
|
|
100
|
+
// ...
|
|
101
|
+
instances: 1, // Ubah "max" menjadi 1
|
|
102
|
+
// ...
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
};
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Lalu jalankan `pm2 reload ecosystem.config.js`.
|
|
109
|
+
|
|
110
|
+
### 7. Advanced: Menjalankan Beberapa Aplikasi (Multi-App)
|
|
111
|
+
|
|
112
|
+
Jika Anda memiliki beberapa aplikasi Node.js dalam satu VPS (misalnya: Backend API, Frontend React SSR, dan API Lapeh), Anda bisa menggabungkannya dalam satu file `ecosystem.config.js`.
|
|
113
|
+
|
|
114
|
+
Berikut adalah contoh konfigurasi **Real World** untuk menjalankan 3 aplikasi sekaligus:
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
module.exports = {
|
|
118
|
+
apps: [
|
|
119
|
+
// 1. APLIKASI LAIN (Contoh: Backend MERN)
|
|
120
|
+
{
|
|
121
|
+
name: "api-mern-news",
|
|
122
|
+
cwd: "/var/www/html/node/api-mern-news",
|
|
123
|
+
script: "dist/src/index.js",
|
|
124
|
+
env: {
|
|
125
|
+
NODE_ENV: "production",
|
|
126
|
+
PORT: 4000,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
// 2. APLIKASI LAIN (Contoh: Frontend React/Next.js)
|
|
131
|
+
{
|
|
132
|
+
name: "web-mern-news",
|
|
133
|
+
cwd: "/var/www/html/node/web-mern-news",
|
|
134
|
+
script: "npm",
|
|
135
|
+
args: "start", // Menjalankan 'npm start'
|
|
136
|
+
env: {
|
|
137
|
+
NODE_ENV: "production",
|
|
138
|
+
PORT: 3001,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
// 3. APLIKASI LAPEH FRAMEWORK
|
|
143
|
+
{
|
|
144
|
+
name: "api-lapeh-project",
|
|
145
|
+
cwd: "/var/www/html/node/my-lapeh-project",
|
|
146
|
+
|
|
147
|
+
// PENTING: Gunakan binary Lapeh dari node_modules lokal
|
|
148
|
+
script: "./node_modules/lapeh/bin/index.js",
|
|
149
|
+
|
|
150
|
+
// Argument 'start' untuk mode produksi
|
|
151
|
+
args: "start",
|
|
152
|
+
|
|
153
|
+
// Mode Cluster (Gunakan semua Core CPU)
|
|
154
|
+
instances: "max",
|
|
155
|
+
exec_mode: "cluster",
|
|
156
|
+
|
|
157
|
+
// Restart jika memori bocor > 1GB
|
|
158
|
+
max_memory_restart: "1G",
|
|
159
|
+
|
|
160
|
+
// Matikan watch di production
|
|
161
|
+
watch: false,
|
|
162
|
+
|
|
163
|
+
env: {
|
|
164
|
+
NODE_ENV: "production",
|
|
165
|
+
PORT: 8001,
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
};
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**Tips:**
|
|
173
|
+
|
|
174
|
+
1. Sesuaikan `cwd` (Current Working Directory) dengan lokasi folder proyek Anda di VPS.
|
|
175
|
+
2. Pastikan port tidak bentrok antar aplikasi (contoh di atas: 4000, 3001, 8001).
|
|
176
|
+
3. Simpan file ini di root folder proyek utama atau di folder khusus konfigurasi server Anda.
|
|
177
|
+
4. Jalankan semua aplikasi sekaligus dengan: `pm2 start ecosystem.config.js`.
|
|
178
|
+
|
|
179
|
+
### 8. Reverse Proxy (Nginx)
|
|
180
|
+
|
|
181
|
+
Jangan expose port 4000 langsung. Gunakan Nginx di depannya.
|
|
182
|
+
Config Nginx block:
|
|
183
|
+
|
|
184
|
+
```nginx
|
|
185
|
+
server {
|
|
186
|
+
server_name api.domain-anda.com;
|
|
187
|
+
location / {
|
|
188
|
+
proxy_pass http://localhost:4000;
|
|
189
|
+
proxy_http_version 1.1;
|
|
190
|
+
proxy_set_header Upgrade $http_upgrade;
|
|
191
|
+
proxy_set_header Connection 'upgrade';
|
|
192
|
+
proxy_set_header Host $host;
|
|
193
|
+
proxy_cache_bypass $http_upgrade;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Strategi 2: Docker (Container)
|
|
199
|
+
|
|
200
|
+
Lapeh sudah menyertakan `Dockerfile` (jika belum, buat sederhana saja).
|
|
201
|
+
|
|
202
|
+
**Dockerfile Minimal:**
|
|
203
|
+
|
|
204
|
+
```dockerfile
|
|
205
|
+
FROM node:18-alpine
|
|
206
|
+
|
|
207
|
+
WORKDIR /app
|
|
208
|
+
|
|
209
|
+
COPY package*.json ./
|
|
210
|
+
RUN npm install
|
|
211
|
+
|
|
212
|
+
COPY . .
|
|
213
|
+
RUN npm run build
|
|
214
|
+
|
|
215
|
+
EXPOSE 4000
|
|
216
|
+
CMD ["npm", "run", "start:prod"]
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
**Deploy:**
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
docker build -t my-lapeh-app .
|
|
223
|
+
docker run -p 4000:4000 --env-file .env my-lapeh-app
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Strategi 3: PaaS (Railway / Render / Vercel)
|
|
227
|
+
|
|
228
|
+
Platform seperti Railway.app sangat mudah karena mendeteksi `package.json`.
|
|
229
|
+
|
|
230
|
+
1. Push kode ke GitHub.
|
|
231
|
+
2. Connect repo di Railway/Render.
|
|
232
|
+
3. Set Environment Variables di dashboard mereka.
|
|
233
|
+
4. Set **Build Command**: `npm run build`.
|
|
234
|
+
5. Set **Start Command**: `npm run start:prod`.
|
|
235
|
+
|
|
236
|
+
**Catatan Khusus Vercel (Serverless):**
|
|
237
|
+
Lapeh didesain sebagai _Long-Running Server_ (Express). Deploy ke Vercel dimungkinkan tapi butuh wrapper serverless (seperti `vercel-express`). Untuk performa terbaik, disarankan menggunakan VPS atau Container (Railway/Fly.io).
|
|
238
|
+
|
|
239
|
+
## Checklist Keamanan Production
|
|
240
|
+
|
|
241
|
+
- [ ] `NODE_ENV=production` harus diset.
|
|
242
|
+
- [ ] `JWT_SECRET` harus panjang dan acak.
|
|
243
|
+
- [ ] Database tidak boleh terekspos ke publik (gunakan private network atau firewall).
|
|
244
|
+
- [ ] Rate Limiting aktif (default Lapeh sudah aktif).
|
|
245
|
+
- [ ] Gunakan HTTPS (SSL) via Nginx atau Cloudflare.
|
package/doc/FAQ.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Frequently Asked Questions (FAQ)
|
|
2
|
+
|
|
3
|
+
Kumpulan pertanyaan umum dan solusi untuk masalah yang sering dihadapi.
|
|
4
|
+
|
|
5
|
+
## Database
|
|
6
|
+
|
|
7
|
+
### Q: Bagaimana cara mengganti database dari PostgreSQL ke MySQL?
|
|
8
|
+
**A:**
|
|
9
|
+
1. Buka `prisma/base.prisma.template`.
|
|
10
|
+
2. Ubah `provider = "postgresql"` menjadi `provider = "mysql"`.
|
|
11
|
+
3. Di `.env`, ubah `DATABASE_URL` formatnya menjadi format MySQL (`mysql://user:pass@host:3306/db`).
|
|
12
|
+
4. Hapus folder `prisma/migrations` (jika baru mulai) atau siapkan strategi migrasi.
|
|
13
|
+
5. Jalankan `npm run prisma:migrate`.
|
|
14
|
+
|
|
15
|
+
### Q: Bagaimana cara membuat relasi antar tabel?
|
|
16
|
+
**A:**
|
|
17
|
+
Definisikan relasi di file `.prisma` masing-masing. Karena file akan digabung, Anda bisa mereferensikan model yang ada di file lain.
|
|
18
|
+
**File `user.prisma`:**
|
|
19
|
+
```prisma
|
|
20
|
+
model User {
|
|
21
|
+
id Int @id
|
|
22
|
+
posts Post[] // Relasi ke Post
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
**File `post.prisma`:**
|
|
26
|
+
```prisma
|
|
27
|
+
model Post {
|
|
28
|
+
id Int @id
|
|
29
|
+
authorId Int
|
|
30
|
+
author User @relation(fields: [authorId], references: [id]) // Relasi ke User
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Redis & Caching
|
|
35
|
+
|
|
36
|
+
### Q: Saya tidak ingin pakai Redis di local, ribet installnya.
|
|
37
|
+
**A:**
|
|
38
|
+
Tenang, Lapeh otomatis mendeteksi jika Redis tidak berjalan dan akan menggunakan **In-Memory Mock** (simulasi Redis di RAM). Aplikasi tetap jalan normal tanpa error. Anda tidak perlu config apa-apa.
|
|
39
|
+
|
|
40
|
+
### Q: Bagaimana cara clear cache Redis?
|
|
41
|
+
**A:**
|
|
42
|
+
Jika punya akses CLI Redis: `redis-cli FLUSHALL`.
|
|
43
|
+
Atau via kode:
|
|
44
|
+
```typescript
|
|
45
|
+
import { redis } from "@/core/redis";
|
|
46
|
+
await redis.flushall();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Fitur & Kustomisasi
|
|
50
|
+
|
|
51
|
+
### Q: Bagaimana cara handle File Upload?
|
|
52
|
+
**A:**
|
|
53
|
+
Gunakan `multer`. Framework sudah menyiapkannya di `src/routes/auth.ts` sebagai contoh (upload avatar).
|
|
54
|
+
Anda bisa copy logic konfigurasi `multer` tersebut ke route lain.
|
|
55
|
+
|
|
56
|
+
### Q: Bagaimana cara menambah middleware global baru?
|
|
57
|
+
**A:**
|
|
58
|
+
Buka `src/core/server.ts`. Di sana ada bagian `// Global Middlewares`. Tambahkan middleware Express Anda di situ:
|
|
59
|
+
```typescript
|
|
60
|
+
app.use(myCustomMiddleware);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Q: Saya ingin mengubah port server.
|
|
64
|
+
**A:**
|
|
65
|
+
Cukup ubah variabel `PORT` di file `.env`. Defaultnya adalah `4000`.
|
|
66
|
+
|
|
67
|
+
## Troubleshooting
|
|
68
|
+
|
|
69
|
+
### Q: Error `EADDRINUSE: address already in use :::4000`
|
|
70
|
+
**A:**
|
|
71
|
+
Artinya port 4000 sedang dipakai program lain (atau instance Lapeh sebelumnya yang nyangkut).
|
|
72
|
+
**Solusi:**
|
|
73
|
+
1. Matikan terminal.
|
|
74
|
+
2. Jalankan perintah kill (framework biasanya memberi saran commandnya saat error muncul).
|
|
75
|
+
- Windows: `taskkill /F /IM node.exe` (Hati-hati ini mematikan semua node process).
|
|
76
|
+
- Atau cari PID nya: `netstat -ano | findstr :4000`.
|
|
77
|
+
|
|
78
|
+
### Q: Error `Unique constraint failed` saat seeding
|
|
79
|
+
**A:**
|
|
80
|
+
Data yang mau dimasukkan sudah ada (misal email `sa@sa.com`).
|
|
81
|
+
Jalankan `npm run db:reset` untuk menghapus semua data dan mulai dari nol.
|
package/doc/FEATURES.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Fitur & Konsep Inti
|
|
2
|
+
|
|
3
|
+
Dokumen ini menjelaskan fitur-fitur utama Lapeh Framework dan cara penggunaannya secara mendalam.
|
|
4
|
+
|
|
5
|
+
## 1. Validasi Data (Laravel-Style)
|
|
6
|
+
|
|
7
|
+
Framework ini menyediakan utility `Validator` yang terinspirasi dari Laravel, menggunakan `zod` di belakang layar namun dengan API yang lebih string-based dan mudah dibaca.
|
|
8
|
+
|
|
9
|
+
**Lokasi:** `@lapeh/utils/validator`
|
|
10
|
+
|
|
11
|
+
### Penggunaan Dasar
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Validator } from "@lapeh/utils/validator";
|
|
15
|
+
|
|
16
|
+
export async function createProduct(req: Request, res: Response) {
|
|
17
|
+
const validator = await Validator.make(req.body, {
|
|
18
|
+
name: "required|string|min:3",
|
|
19
|
+
price: "required|number|min:1000",
|
|
20
|
+
email: "required|email|unique:user,email", // Cek unik di tabel user kolom email
|
|
21
|
+
category_id: "required|exists:category,id", // Cek exist di tabel category kolom id
|
|
22
|
+
photo: "required|image|max:2048", // Validasi file upload (Max 2MB)
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (validator.fails()) {
|
|
26
|
+
return sendError(res, 400, "Validation failed", validator.errors());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const data = validator.validated();
|
|
30
|
+
// Lanjut proses simpan...
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Aturan Tersedia (Rules)
|
|
35
|
+
|
|
36
|
+
- `required`: Wajib diisi.
|
|
37
|
+
- `string`, `number`, `boolean`: Tipe data.
|
|
38
|
+
- `email`: Format email valid.
|
|
39
|
+
- `min:X`, `max:X`: Panjang string atau nilai number.
|
|
40
|
+
- `unique:table,column`: Pastikan nilai belum ada di database (Async).
|
|
41
|
+
- `exists:table,column`: Pastikan nilai ada di database (Async).
|
|
42
|
+
- `image`: File harus berupa gambar (jpg, png, webp, dll).
|
|
43
|
+
- `mimes:types`: File harus tipe tertentu (misal: `mimes:pdf,docx`).
|
|
44
|
+
|
|
45
|
+
## 2. High Performance Response (Fastify-Style)
|
|
46
|
+
|
|
47
|
+
Untuk endpoint yang membutuhkan performa tinggi (misalnya list data besar), gunakan serialisasi berbasis schema. Ini jauh lebih cepat daripada `res.json` standar Express.
|
|
48
|
+
|
|
49
|
+
**Lokasi:** `@/utils/response`, `@/core/serializer`
|
|
50
|
+
|
|
51
|
+
### Langkah Implementasi
|
|
52
|
+
|
|
53
|
+
1. **Definisikan Schema Output**
|
|
54
|
+
Sesuaikan dengan field yang ingin ditampilkan ke user.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
const productSchema = {
|
|
58
|
+
type: "object",
|
|
59
|
+
properties: {
|
|
60
|
+
id: { type: "string" }, // BigInt otomatis jadi string
|
|
61
|
+
name: { type: "string" },
|
|
62
|
+
price: { type: "number" },
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
2. **Buat Serializer (Cached)**
|
|
68
|
+
Simpan di luar handler function agar dicompile sekali saja.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { getSerializer, createResponseSchema } from "@/core/serializer";
|
|
72
|
+
|
|
73
|
+
const productSerializer = getSerializer(
|
|
74
|
+
"product-single",
|
|
75
|
+
createResponseSchema(productSchema)
|
|
76
|
+
);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
3. **Kirim Response**
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { sendFastSuccess } from "@lapeh/utils/response";
|
|
83
|
+
|
|
84
|
+
// Di dalam controller
|
|
85
|
+
sendFastSuccess(res, 200, productSerializer, {
|
|
86
|
+
status: "success",
|
|
87
|
+
message: "Data retrieved",
|
|
88
|
+
data: productData,
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## 3. Authentication & Authorization (RBAC)
|
|
93
|
+
|
|
94
|
+
Sistem autentikasi menggunakan JWT (JSON Web Token) dan mendukung Role-Based Access Control.
|
|
95
|
+
|
|
96
|
+
### Middleware Auth
|
|
97
|
+
|
|
98
|
+
- `requireAuth`: Memastikan user login (mengirim header `Authorization: Bearer <token>`).
|
|
99
|
+
- `requireAdmin`: Memastikan user login DAN memiliki role `admin` atau `super_admin`.
|
|
100
|
+
|
|
101
|
+
**Contoh di Route:**
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { requireAuth, requireAdmin } from "@/middleware/auth";
|
|
105
|
+
|
|
106
|
+
router.get("/profile", requireAuth, getProfile); // Login only
|
|
107
|
+
router.delete("/users/:id", requireAuth, requireAdmin, deleteUser); // Admin only
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Helper RBAC (Role Based Access Control)
|
|
111
|
+
|
|
112
|
+
Anda bisa mengecek permission secara granular di dalam controller:
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
// (Contoh implementasi, logic ada di AuthController/RbacController)
|
|
116
|
+
if (req.user.role !== "manager") {
|
|
117
|
+
return sendError(res, 403, "Forbidden");
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## 4. Caching & Redis
|
|
122
|
+
|
|
123
|
+
Framework ini memiliki integrasi Redis "Zero-Config".
|
|
124
|
+
|
|
125
|
+
- Jika `REDIS_URL` ada di `.env` dan server Redis berjalan, framework akan connect.
|
|
126
|
+
- Jika tidak ada atau gagal connect, framework otomatis fallback ke **In-Memory Mock**. Ini membuat development di local tidak wajib install Redis.
|
|
127
|
+
|
|
128
|
+
**Mengakses Redis:**
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { redis } from "@/core/redis";
|
|
132
|
+
|
|
133
|
+
// Set cache (1 jam)
|
|
134
|
+
await redis.set("my-key", "value", "EX", 3600);
|
|
135
|
+
|
|
136
|
+
// Get cache
|
|
137
|
+
const val = await redis.get("my-key");
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## 5. Keamanan (Security)
|
|
141
|
+
|
|
142
|
+
Secara default, framework sudah menerapkan:
|
|
143
|
+
|
|
144
|
+
- **Helmet**: Mengamankan HTTP headers.
|
|
145
|
+
- **CORS**: Mengizinkan akses lintas domain (configurable).
|
|
146
|
+
- **Rate Limiting**: Membatasi jumlah request per IP untuk mencegah DDoS/Brute Force.
|
|
147
|
+
- Konfigurasi ada di `src/middleware/rateLimit.ts`.
|
|
148
|
+
- Default: 100 request / 15 menit.
|
|
149
|
+
|
|
150
|
+
## 6. Import Path Aliases
|
|
151
|
+
|
|
152
|
+
Gunakan `@/` untuk import module agar kode lebih bersih.
|
|
153
|
+
|
|
154
|
+
- `@/core` -> `src/core`
|
|
155
|
+
- `@/controllers` -> `src/controllers`
|
|
156
|
+
- `@/utils` -> `src/utils`
|
|
157
|
+
- `@/middleware` -> `src/middleware`
|
|
158
|
+
|
|
159
|
+
**Contoh:**
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { prisma } from "@/core/database"; // ✅ Rapi
|
|
163
|
+
// vs
|
|
164
|
+
import { prisma } from "../../../core/database"; // ❌ Berantakan
|
|
165
|
+
```
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Getting Started with Lapeh Framework
|
|
2
|
+
|
|
3
|
+
Selamat datang di dokumentasi resmi **Lapeh Framework**. Panduan ini akan membantu Anda memulai instalasi, konfigurasi, dan pemahaman dasar tentang struktur proyek.
|
|
4
|
+
|
|
5
|
+
## Persyaratan Sistem
|
|
6
|
+
|
|
7
|
+
Sebelum memulai, pastikan sistem Anda memenuhi persyaratan berikut:
|
|
8
|
+
|
|
9
|
+
- **Node.js**: Versi 18.x atau lebih baru.
|
|
10
|
+
- **Database**: PostgreSQL (Recommended) atau MySQL/MariaDB.
|
|
11
|
+
- **Package Manager**: NPM (bawaan Node.js).
|
|
12
|
+
|
|
13
|
+
## Instalasi
|
|
14
|
+
|
|
15
|
+
Cara termudah untuk memulai adalah menggunakan CLI generator `npx`.
|
|
16
|
+
|
|
17
|
+
### 1. Buat Project Baru
|
|
18
|
+
|
|
19
|
+
Jalankan perintah berikut di terminal Anda:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npx lapeh@latest nama-project-anda
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Atau untuk setup lengkap (dengan data dummy user & role):
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npx lapeh@latest nama-project-anda --full
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 2. Setup Awal
|
|
32
|
+
|
|
33
|
+
Setelah project dibuat, masuk ke direktori project dan jalankan setup wizard:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
cd nama-project-anda
|
|
37
|
+
npm run first
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Script ini akan melakukan hal-hal berikut secara otomatis:
|
|
41
|
+
|
|
42
|
+
1. Menyalin `.env.example` ke `.env`.
|
|
43
|
+
2. Menginstall semua dependency (`npm install`).
|
|
44
|
+
3. Membuat **JWT Secret** yang aman.
|
|
45
|
+
4. Menjalankan migrasi database (membuat tabel).
|
|
46
|
+
5. Menjalankan seeder (mengisi data awal).
|
|
47
|
+
|
|
48
|
+
### 3. Jalankan Server Development
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm run dev
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Server akan berjalan di `http://localhost:4000` (atau port yang Anda tentukan di `.env`).
|
|
55
|
+
|
|
56
|
+
## Struktur Direktori
|
|
57
|
+
|
|
58
|
+
Berikut adalah struktur folder standar Lapeh Framework:
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
my-app/
|
|
62
|
+
├── bin/ # Script CLI untuk npx
|
|
63
|
+
├── doc/ # Dokumentasi proyek
|
|
64
|
+
├── prisma/ # Konfigurasi Database & Schema
|
|
65
|
+
│ ├── migrations/ # File history migrasi database
|
|
66
|
+
│ ├── base.prisma.template # Template konfigurasi database
|
|
67
|
+
│ ├── schema.prisma # File schema gabungan (Auto-generated)
|
|
68
|
+
│ └── seed.ts # Script untuk mengisi data awal
|
|
69
|
+
├── scripts/ # Script utility (Generator, Compiler)
|
|
70
|
+
├── src/ # Source code utama aplikasi
|
|
71
|
+
│ ├── controllers/ # Logika bisnis (Handler request)
|
|
72
|
+
│ ├── core/ # Konfigurasi inti (DB, Redis, Server)
|
|
73
|
+
│ ├── middleware/ # Middleware Express (Auth, RateLimit)
|
|
74
|
+
│ ├── models/ # Definisi Schema Prisma per-fitur
|
|
75
|
+
│ ├── routes/ # Definisi routing API
|
|
76
|
+
│ ├── utils/ # Helper function (Response, Validator)
|
|
77
|
+
│ └── index.ts # Entry point aplikasi
|
|
78
|
+
├── .env # Variabel lingkungan (Rahasia)
|
|
79
|
+
├── package.json # Dependensi & Script NPM
|
|
80
|
+
└── tsconfig.json # Konfigurasi TypeScript
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Konfigurasi Environment (.env)
|
|
84
|
+
|
|
85
|
+
File `.env` menyimpan konfigurasi penting. Berikut adalah variabel kunci:
|
|
86
|
+
|
|
87
|
+
```ini
|
|
88
|
+
# Server
|
|
89
|
+
PORT=4000
|
|
90
|
+
NODE_ENV=development
|
|
91
|
+
|
|
92
|
+
# Database (Ganti sesuai kredensial Anda)
|
|
93
|
+
DATABASE_URL="postgresql://user:password@localhost:5432/mydb?schema=public"
|
|
94
|
+
|
|
95
|
+
# Security
|
|
96
|
+
JWT_SECRET="rahasia-super-panjang-dan-acak"
|
|
97
|
+
ACCESS_TOKEN_EXPIRES_IN=3600 # 1 jam
|
|
98
|
+
|
|
99
|
+
# Redis (Opsional - otomatis mock jika tidak ada)
|
|
100
|
+
REDIS_URL="redis://localhost:6379"
|
|
101
|
+
NO_REDIS=false # Set true untuk memaksa mode mock
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Langkah Selanjutnya
|
|
105
|
+
|
|
106
|
+
- Pelajari cara menggunakan **[CLI Tools](CLI.md)** untuk mempercepat development.
|
|
107
|
+
- Pahami **[Fitur & Konsep Inti](FEATURES.md)** framework.
|
|
108
|
+
- Ikuti **[Tutorial Studi Kasus](TUTORIAL.md)** untuk membangun fitur nyata.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Pengenalan Lapeh Framework
|
|
2
|
+
|
|
3
|
+
## Apa itu Lapeh?
|
|
4
|
+
|
|
5
|
+
**Lapeh** adalah framework Backend untuk Node.js yang dibangun di atas **Express** dan **TypeScript**.
|
|
6
|
+
|
|
7
|
+
Jika Anda pernah menggunakan **Laravel** (PHP) atau **NestJS** (Node.js), Anda akan merasa sangat familiar. Lapeh mengambil filosofi kemudahan & struktur rapi dari Laravel, namun tetap mempertahankan fleksibilitas dan kecepatan Express.
|
|
8
|
+
|
|
9
|
+
Nama "Lapeh" diambil dari bahasa Minang yang berarti "Lepas" atau "Bebas", melambangkan kebebasan developer untuk membangun aplikasi dengan cepat tanpa terbebani konfigurasi yang rumit.
|
|
10
|
+
|
|
11
|
+
## Mengapa Lapeh Dibuat?
|
|
12
|
+
|
|
13
|
+
Di ekosistem Node.js, developer sering mengalami "Decision Fatigue" (Kelelahan memilih):
|
|
14
|
+
- "Pakai ORM apa? Prisma, TypeORM, atau Drizzle?"
|
|
15
|
+
- "Validasi pakai Joi, Zod, atau express-validator?"
|
|
16
|
+
- "Struktur foldernya gimana? MVC? Clean Architecture?"
|
|
17
|
+
- "Auth-nya gimana?"
|
|
18
|
+
|
|
19
|
+
Lapeh menjawab semua itu dengan **Opinionated Defaults**:
|
|
20
|
+
1. **ORM**: Prisma (Standar industri saat ini).
|
|
21
|
+
2. **Validasi**: Zod (dengan wrapper syntax ala Laravel).
|
|
22
|
+
3. **Struktur**: MVC Modular (Controller, Model, Route terpisah tapi kohesif).
|
|
23
|
+
4. **Auth**: JWT + RBAC (Role Based Access Control) siap pakai.
|
|
24
|
+
|
|
25
|
+
## Perbandingan dengan Framework Lain
|
|
26
|
+
|
|
27
|
+
| Fitur | Express (Raw) | NestJS | Lapeh Framework |
|
|
28
|
+
| :--- | :--- | :--- | :--- |
|
|
29
|
+
| **Learning Curve** | Rendah (tapi bingung struktur) | Tinggi (Angular-style, Decorators) | **Sedang** (Express + Struktur Jelas) |
|
|
30
|
+
| **Boilerplate** | Kosong | Sangat Banyak | **Pas (Ready to use)** |
|
|
31
|
+
| **Type Safety** | Manual | Strict | **Strict (TypeScript Native)** |
|
|
32
|
+
| **Kecepatan Dev** | Lambat (setup manual) | Sedang | **Cepat (CLI Generator)** |
|
|
33
|
+
| **Fleksibilitas** | Sangat Tinggi | Kaku | **Tinggi** |
|
|
34
|
+
|
|
35
|
+
## Filosofi "The Lapeh Way"
|
|
36
|
+
|
|
37
|
+
1. **Developer Experience (DX) First**: CLI tools, error message yang jelas, dan hot-reload adalah prioritas.
|
|
38
|
+
2. **Performance by Default**: Serialisasi JSON cepat (Fastify-style) dan Redis caching terintegrasi.
|
|
39
|
+
3. **Explicit is Better than Implicit**: Tidak ada "sihir" yang terlalu gelap. Kode controller Anda adalah kode Express biasa yang Anda mengerti.
|
|
40
|
+
4. **Production Ready**: Security (Helmet, Rate Limit) dan Scalability (Docker, Cluster) bukan pikiran belakangan, tapi bawaan.
|
|
41
|
+
|
|
42
|
+
## Siklus Hidup Request (Request Lifecycle)
|
|
43
|
+
|
|
44
|
+
Bagaimana Lapeh menangani satu permintaan dari user?
|
|
45
|
+
|
|
46
|
+
1. **Request Masuk** (`GET /api/users`)
|
|
47
|
+
2. **Security Middleware**: Helmet (Headers), CORS, Rate Limiter.
|
|
48
|
+
3. **Global Middleware**: Request Logger, Body Parser (JSON).
|
|
49
|
+
4. **Routing**: Mencocokkan URL di `src/routes/`.
|
|
50
|
+
5. **Auth Middleware** (Opsional): Cek token JWT & Role.
|
|
51
|
+
6. **Validator** (Opsional): Validasi input body/query.
|
|
52
|
+
7. **Controller**: Logika bisnis utama dijalankan.
|
|
53
|
+
- Panggil Database (Prisma).
|
|
54
|
+
- Panggil Cache (Redis).
|
|
55
|
+
8. **Serializer**: Data diformat & disanitasi (misal: hide password).
|
|
56
|
+
9. **Response**: JSON dikirim kembali ke user.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
**Selanjutnya:** Pelajari struktur folder di [Struktur Proyek](STRUCTURE.md).
|