~/blog/membuat-service-dan-route-kong-gateway
Published on

Membuat Service dan Route Kong Gateway

book2 minutes read

Apa itu service? Service yaitu entitas untuk layanan API yang mendefinisikan upstream service kita. Sedangkan route yaitu endpoint untuk masuk ke kong, yang kemudian akan mengarahkan ke service tertentu. Jadi misalnya kita punya server API yg endpoint nya adalah http://localhost:3000/users kita bisa membuat service dengan nama “pengguna” dan route nya yaitu http://localhost:8000/v1/users yang jika diakses akan mengarah ke http://localhost port 3000 dan endpoint /users

Okee, mungkin kita bisa langsung praktek saja biar makin paham hehe

Sebelum lanjut, disini saya menggunakan tools httpie untuk mengakses endpoint admin dari kong.

Langkah 1: Buat Service

Buat service API untuk pengguna yang beralamat di http://localhost:3000/users

$ http :8001/services name=users url=http://localhost:3000/users

HTTP/1.1 201 Created
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 254
Content-Type: application/json; charset=utf-8
Date: Wed, 21 Nov 2018 06:36:55 GMT
Server: kong/0.14.1

{
    "connect_timeout": 60000,
    "created_at": 1542782215,
    "host": "localhost",
    "id": "626c2ffa-3034-41b1-a2f0-b506c0139b2c",
    "name": "users",
    "path": "/users",
    "port": 3000,
    "protocol": "http",
    "read_timeout": 60000,
    "retries": 5,
    "updated_at": 1542782215,
    "write_timeout": 60000
}

Langkah 2: Buat Route

Kita ambil id service yang kita buat tadi untuk membuat route

$ http :8001/routes service:='{"id":"626c2ffa-3034-41b1-a2f0-b506c0139b2c"}' paths:='["/v1/users"]' methods:='["GET","POST","PATCH","PUT"]'

HTTP/1.1 201 Created
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 314
Content-Type: application/json; charset=utf-8
Date: Sun, 25 Nov 2018 10:51:59 GMT
Server: kong/0.14.1

{
    "created_at": 1543143119,
    "hosts": null,
    "id": "cdeeede8-75b8-44dc-9ee6-c8e808a680c8",
    "methods": [
        "GET",
        "POST",
        "PATCH",
        "PUT"
    ],
    "paths": [
        "/v1/users"
    ],
    "preserve_host": false,
    "protocols": [
        "http",
        "https"
    ],
    "regex_priority": 0,
    "service": {
        "id": "626c2ffa-3034-41b1-a2f0-b506c0139b2c"
    },
    "strip_path": true,
    "updated_at": 1543143119
}

Langkah 3: Cek Konfigurasi

Setelah berhasil membuat service dan route kita coba untuk mengakses nya

$ http :8000/v1/users apikey:AisbenXBYMH7rFXhnOoaArn27lGrZOXb

HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 53
Content-Type: application/json; charset=utf-8
Date: Sun, 25 Nov 2018 11:55:13 GMT
ETag: W/"35-D378nm5shd12kuL9ZrIOCXfvqaY"
Via: kong/0.14.1
X-Kong-Proxy-Latency: 0
X-Kong-Upstream-Latency: 30
X-Powered-By: Express

{
    "data": [],
    "message": "Request success",
    "status": true
}

Contoh request diatas saya memakai apikey untuk mengakses API, untuk menambahkan apikey authentication pada kong saya sudah menulis tutorialnya disini

dah gitu aja, see ya~