Kelas PBO 2025, pertemuan ke-13 (7 Nov)
git clone https://github.com/leo42night/rest-api- Install PHP & Database
- Run Database & PHP Server
php -S localhost:3000(port dapat disesuaikan)
GET / → { "message": "Koneksi success" }
GET /mahasiswa
GET /mahasiswa/1
POST /mahasiswa (body JSON)
PUT /mahasiswa/1 (body JSON)
DELETE /mahasiswa/1curl -X POST http://localhost:3000/mahasiswa \
-H "Authorization: Bearer 12345ABCDEF" \
-H "Content-Type: application/json" \
-d '{ "nama": "Andi Saputra", "jurusan": "Teknik Informatika"}'respon Berhasil:
{
"message": "Data mahasiswa berhasil ditambahkan"
}Lainnya:
curl -X GET http://localhost:3000/curl -X GET http://localhost:3000/mahasiswa \
-H "Authorization: Bearer 12345ABCDEF"curl -X GET http://localhost:3000/mahasiswa/1 \
-H "Authorization: Bearer 12345ABCDEF"curl -X PUT http://localhost:3000/mahasiswa/1 \
-H "Authorization: Bearer 12345ABCDEF" \
-H "Content-Type: application/json" \
-d '{ "nama": "Ahmad Syahroni", "jurusan": "Tata Boga"}'curl -X DELETE http://localhost:3000/mahasiswa/1 \
-H "Authorization: Bearer 12345ABCDEF"- Postman (Aplikasi)
- Thunder Client (Ekstensi VSCode)
- EchoAPI for VS Code (Ekstensi VSCode) [🌟 Disarankan]
Vercel bisa menjalankan PHP lewat custom runtime open-source bernama vercel-php
- Tambahkan file konfigurasi Vecel:
/api
├── index.php
vercel.json
- api/index.php
<?phprequire__DIR__ . "/../index.php";- vercel.json
{
"functions": {
"api/*.php": {
"runtime": "vercel-php@0.7.4"
}
},
"routes": [
{ "src": "/(.*)", "dest": "/api/index.php" }
]
}- Install Vercel CLI
Anda perlu install NPM (Node Package Manager) lebih dulu
npm install -g vercel- Login ke akunmu (pakai Github)
vercel login- Deploy proyek
vercelAkan ada konfigurasi (tekan saja enter):
? Set up and deploy “c:\oop\rest-api”? yes
? Which scope do you want to deploy to? leo42night's projects? Link to existing project? no? What’s your project’s name? rest-api? In which directory is your code located? ./? Want to modify these settings? no- Vercel akan otomatis mendeteksi file vercel.json dan menggunakan runtime @vercel/php. Setelah selesai, kamu akan dapat URL publik seperti:
https://rest-api.vercel.app
- Database Postgres
Di halaman proyek vercel kalian, buka stores (contoh: vercel.com/nama-teams/nama-proyek/stores)
- [Create Database]
- Region:
Singapore - Public Env Variabel Prefix:
PG_ - Instalation Plans: Free
- Region:
- [Continue]
- Database Name:
supabase-rest-api
- Database Name:
- [Create]
- Salin environment variabel ke
.env.local - [Open In Supabase]
- [New table]
- name: mahasiswa (id, nama [varchar], jurusan [varchar], created_at [timestamps now()])
konfigurasi seperti ini (pakai POSTGRES_URL_NON_POOLING):
DB_TYPE: pgsql
DB_HOST: aws-1-ap-southeast-1.pooler.supabase.com
DB_PORT: 5432 # pakai port NON_POOLING
DB_USER: postgres.itzxopxshpvcjotxxxxx
DB_PASS: ETnWsKT1Q9xxxxx
DB_SSLMODE: require(dalam proyek vercel) Buka Settings > Environment Variables: Tambahkan ke environment variabel proyek (letak di vercel.com/nama-teams/nama-proyek/settings/environment-variables)
- (opsional) Akses di HeidiSQL:
- Network Type: PostgreSQL (TCP/IP)
- Library: libpq-12.dll
- Test Deployment
curl -X GET https://<url-deployment>.vercel.app/
curl -X GET http://<url-deployment>.vercel.app/mahasiswa \
-H "Authorization: Bearer 12345ABCDEF"
Buat Repository di GitHub: Tempat simpan proyek
git add remote repoku https://github.com/<username>/<repo>
git add .
git commit -m "persiapan sebelum deploy"
gut push repoku main --force