├── Dockerfile ├── Kong.postman_collection.json ├── README.md ├── index.js └── package.json /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:carbon 2 | 3 | # create work directory 4 | WORKDIR /usr/src/app 5 | 6 | # copy package.json 7 | COPY package.json ./ 8 | RUN npm install 9 | 10 | # copy source code 11 | COPY . . 12 | 13 | EXPOSE 10000 14 | 15 | CMD ["npm", "start"] 16 | -------------------------------------------------------------------------------- /Kong.postman_collection.json: -------------------------------------------------------------------------------- 1 | { 2 | "info": { 3 | "_postman_id": "f47dbccf-c441-2e90-353b-af218508d104", 4 | "name": "Kong", 5 | "description": "This collection is for tutorial KONG basic", 6 | "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" 7 | }, 8 | "item": [ 9 | { 10 | "name": "Services", 11 | "description": "", 12 | "item": [ 13 | { 14 | "name": "Services - List", 15 | "request": { 16 | "method": "GET", 17 | "header": [], 18 | "body": {}, 19 | "url": { 20 | "raw": "localhost:9001/services", 21 | "host": [ 22 | "localhost" 23 | ], 24 | "port": "9001", 25 | "path": [ 26 | "services" 27 | ] 28 | } 29 | }, 30 | "response": [] 31 | }, 32 | { 33 | "name": "Services - Create", 34 | "request": { 35 | "method": "POST", 36 | "header": [ 37 | { 38 | "key": "Content-Type", 39 | "value": "application/json" 40 | } 41 | ], 42 | "body": { 43 | "mode": "raw", 44 | "raw": "{\n \"name\": \"api-v1-customers\",\n \"url\": \"http://172.19.0.4:10000\"\n}" 45 | }, 46 | "url": { 47 | "raw": "localhost:9001/services/", 48 | "host": [ 49 | "localhost" 50 | ], 51 | "port": "9001", 52 | "path": [ 53 | "services", 54 | "" 55 | ] 56 | } 57 | }, 58 | "response": [] 59 | }, 60 | { 61 | "name": "Services - Delete", 62 | "request": { 63 | "method": "DELETE", 64 | "header": [], 65 | "body": {}, 66 | "url": { 67 | "raw": "localhost:9001/services/204876a1-7a78-4fb2-9c7c-8804d310d8f5", 68 | "host": [ 69 | "localhost" 70 | ], 71 | "port": "9001", 72 | "path": [ 73 | "services", 74 | "204876a1-7a78-4fb2-9c7c-8804d310d8f5" 75 | ] 76 | } 77 | }, 78 | "response": [] 79 | } 80 | ] 81 | }, 82 | { 83 | "name": "Routes", 84 | "description": "", 85 | "item": [ 86 | { 87 | "name": "Routes - List", 88 | "request": { 89 | "method": "GET", 90 | "header": [], 91 | "body": {}, 92 | "url": { 93 | "raw": "localhost:9001/routes", 94 | "host": [ 95 | "localhost" 96 | ], 97 | "port": "9001", 98 | "path": [ 99 | "routes" 100 | ] 101 | } 102 | }, 103 | "response": [] 104 | }, 105 | { 106 | "name": "Routes - Create", 107 | "request": { 108 | "method": "POST", 109 | "header": [ 110 | { 111 | "key": "Content-Type", 112 | "value": "application/json" 113 | } 114 | ], 115 | "body": { 116 | "mode": "raw", 117 | "raw": "{\n \"hosts\": [\"api.ct.id\"],\n \"paths\": [\"/api/v1/customers\"]\n}" 118 | }, 119 | "url": { 120 | "raw": "localhost:9001/services/api-v1-customers/routes/", 121 | "host": [ 122 | "localhost" 123 | ], 124 | "port": "9001", 125 | "path": [ 126 | "services", 127 | "api-v1-customers", 128 | "routes", 129 | "" 130 | ] 131 | } 132 | }, 133 | "response": [] 134 | }, 135 | { 136 | "name": "Routes - Delete", 137 | "request": { 138 | "method": "DELETE", 139 | "header": [], 140 | "body": {}, 141 | "url": { 142 | "raw": "localhost:9001/routes/034c40b4-d9d9-4e34-a8d3-94b0a2194cd6", 143 | "host": [ 144 | "localhost" 145 | ], 146 | "port": "9001", 147 | "path": [ 148 | "routes", 149 | "034c40b4-d9d9-4e34-a8d3-94b0a2194cd6" 150 | ] 151 | } 152 | }, 153 | "response": [] 154 | } 155 | ] 156 | }, 157 | { 158 | "name": "Plugin", 159 | "description": "", 160 | "item": [ 161 | { 162 | "name": "Plugins - Enabled", 163 | "request": { 164 | "method": "GET", 165 | "header": [], 166 | "body": {}, 167 | "url": { 168 | "raw": "localhost:9001/plugins/enabled", 169 | "host": [ 170 | "localhost" 171 | ], 172 | "port": "9001", 173 | "path": [ 174 | "plugins", 175 | "enabled" 176 | ] 177 | } 178 | }, 179 | "response": [] 180 | }, 181 | { 182 | "name": "Plugins - List All Plugins", 183 | "request": { 184 | "method": "GET", 185 | "header": [], 186 | "body": {}, 187 | "url": { 188 | "raw": "localhost:9001/plugins", 189 | "host": [ 190 | "localhost" 191 | ], 192 | "port": "9001", 193 | "path": [ 194 | "plugins" 195 | ] 196 | } 197 | }, 198 | "response": [] 199 | }, 200 | { 201 | "name": "Plugin - Add service to OAuth", 202 | "request": { 203 | "method": "POST", 204 | "header": [ 205 | { 206 | "key": "Content-Type", 207 | "value": "application/json" 208 | } 209 | ], 210 | "body": { 211 | "mode": "raw", 212 | "raw": "{\n \"name\": \"oauth2\",\n \"config.scopes\": [\"read\", \"write\"],\n \"config.mandatory_scope\": true,\n \"config.enable_password_grant\": true,\n \"config.accept_http_if_already_terminated\": true,\n \"config.token_expiration\": 180,\n \"config.global_credentials\": true\n}" 213 | }, 214 | "url": { 215 | "raw": "localhost:9001/services/api-v1-customers/plugins", 216 | "host": [ 217 | "localhost" 218 | ], 219 | "port": "9001", 220 | "path": [ 221 | "services", 222 | "api-v1-customers", 223 | "plugins" 224 | ] 225 | } 226 | }, 227 | "response": [] 228 | }, 229 | { 230 | "name": "Plugin - Delete service to OAuth", 231 | "request": { 232 | "method": "DELETE", 233 | "header": [], 234 | "body": {}, 235 | "url": { 236 | "raw": "localhost:9001/plugins/4f1dbb44-bb51-49b3-80d5-b0a5ac4f7caf", 237 | "host": [ 238 | "localhost" 239 | ], 240 | "port": "9001", 241 | "path": [ 242 | "plugins", 243 | "4f1dbb44-bb51-49b3-80d5-b0a5ac4f7caf" 244 | ] 245 | } 246 | }, 247 | "response": [] 248 | }, 249 | { 250 | "name": "Plugin - Add service to ACL", 251 | "request": { 252 | "method": "POST", 253 | "header": [ 254 | { 255 | "key": "Content-Type", 256 | "value": "application/json" 257 | } 258 | ], 259 | "body": { 260 | "mode": "raw", 261 | "raw": "{\n \"name\": \"acl\",\n \"config.whitelist\": \"group-api-v1-customers\"\n}" 262 | }, 263 | "url": { 264 | "raw": "localhost:9001/services/api-v1-customers/plugins", 265 | "host": [ 266 | "localhost" 267 | ], 268 | "port": "9001", 269 | "path": [ 270 | "services", 271 | "api-v1-customers", 272 | "plugins" 273 | ] 274 | } 275 | }, 276 | "response": [] 277 | } 278 | ] 279 | }, 280 | { 281 | "name": "Consumer", 282 | "description": "", 283 | "item": [ 284 | { 285 | "name": "Consumers - List", 286 | "request": { 287 | "method": "GET", 288 | "header": [], 289 | "body": {}, 290 | "url": { 291 | "raw": "localhost:9001/consumers", 292 | "host": [ 293 | "localhost" 294 | ], 295 | "port": "9001", 296 | "path": [ 297 | "consumers" 298 | ] 299 | } 300 | }, 301 | "response": [] 302 | }, 303 | { 304 | "name": "Consumers - Create", 305 | "event": [ 306 | { 307 | "listen": "prerequest", 308 | "script": { 309 | "id": "fa5431bd-b3c0-4cf5-8f2c-394a0cbf9250", 310 | "type": "text/javascript", 311 | "exec": [ 312 | "" 313 | ] 314 | } 315 | } 316 | ], 317 | "request": { 318 | "method": "POST", 319 | "header": [ 320 | { 321 | "key": "Content-Type", 322 | "value": "application/json" 323 | } 324 | ], 325 | "body": { 326 | "mode": "raw", 327 | "raw": "{\n \"username\": \"oneone@gmail.com\",\n \"custom_id\": \"11\"\n}" 328 | }, 329 | "url": { 330 | "raw": "localhost:9001/consumers", 331 | "host": [ 332 | "localhost" 333 | ], 334 | "port": "9001", 335 | "path": [ 336 | "consumers" 337 | ] 338 | } 339 | }, 340 | "response": [] 341 | }, 342 | { 343 | "name": "Consumers - Delete", 344 | "event": [ 345 | { 346 | "listen": "prerequest", 347 | "script": { 348 | "id": "fa5431bd-b3c0-4cf5-8f2c-394a0cbf9250", 349 | "type": "text/javascript", 350 | "exec": [ 351 | "" 352 | ] 353 | } 354 | } 355 | ], 356 | "request": { 357 | "method": "DELETE", 358 | "header": [ 359 | { 360 | "key": "Content-Type", 361 | "value": "application/json" 362 | } 363 | ], 364 | "body": { 365 | "mode": "raw", 366 | "raw": "" 367 | }, 368 | "url": { 369 | "raw": "localhost:9001/consumers/8f46d90a-25ab-4025-90e5-fe6538b394cc", 370 | "host": [ 371 | "localhost" 372 | ], 373 | "port": "9001", 374 | "path": [ 375 | "consumers", 376 | "8f46d90a-25ab-4025-90e5-fe6538b394cc" 377 | ] 378 | }, 379 | "description": "Notes:\n\n- Delete consumer also remove all plugins configuration associated with this consumer." 380 | }, 381 | "response": [] 382 | } 383 | ] 384 | }, 385 | { 386 | "name": "ApplicationOAuth2", 387 | "description": "", 388 | "item": [ 389 | { 390 | "name": "OAuth - Create Application", 391 | "request": { 392 | "method": "POST", 393 | "header": [ 394 | { 395 | "key": "Content-Type", 396 | "value": "application/json" 397 | } 398 | ], 399 | "body": { 400 | "mode": "raw", 401 | "raw": "{\n \"name\": \"APP 11\",\n \"client_id\": \"CLIENT_ID_11\",\n \"client_secret\": \"CLIENT_SECRET_11\",\n \"redirect_uri\": \"http://localhost:9800/cb\"\n}" 402 | }, 403 | "url": { 404 | "raw": "localhost:9001/consumers/oneone@gmail.com/oauth2", 405 | "host": [ 406 | "localhost" 407 | ], 408 | "port": "9001", 409 | "path": [ 410 | "consumers", 411 | "oneone@gmail.com", 412 | "oauth2" 413 | ] 414 | } 415 | }, 416 | "response": [] 417 | } 418 | ] 419 | }, 420 | { 421 | "name": "OAuthToken", 422 | "description": "", 423 | "item": [ 424 | { 425 | "name": "OAuth - Request Token", 426 | "request": { 427 | "method": "POST", 428 | "header": [ 429 | { 430 | "key": "Content-Type", 431 | "value": "application/json" 432 | }, 433 | { 434 | "key": "Host", 435 | "value": "api.ct.id" 436 | } 437 | ], 438 | "body": { 439 | "mode": "raw", 440 | "raw": "{\n \"client_id\": \"CLIENT_ID_11\",\n \"client_secret\": \"CLIENT_SECRET_11\",\n \"grant_type\": \"password\",\n \"provision_key\": \"kl3bUfe32WBcppmYFr1aZtXxzrBTL18l\",\n \"authenticated_userid\": \"oneone@gmail.com\",\n \"scope\": \"read\"\n}" 441 | }, 442 | "url": { 443 | "raw": "https://localhost:9443/api/v1/customers/oauth2/token/", 444 | "protocol": "https", 445 | "host": [ 446 | "localhost" 447 | ], 448 | "port": "9443", 449 | "path": [ 450 | "api", 451 | "v1", 452 | "customers", 453 | "oauth2", 454 | "token", 455 | "" 456 | ] 457 | } 458 | }, 459 | "response": [] 460 | }, 461 | { 462 | "name": "OAuth - Refresh Token", 463 | "request": { 464 | "method": "POST", 465 | "header": [ 466 | { 467 | "key": "Host", 468 | "value": "api.ct.id" 469 | }, 470 | { 471 | "key": "Content-Type", 472 | "value": "application/json" 473 | } 474 | ], 475 | "body": { 476 | "mode": "raw", 477 | "raw": "{\n \"grant_type\": \"refresh_token\",\n \"client_id\": \"CLIENT_ID_11\",\n \"client_secret\": \"CLIENT_SECRET_11\",\n \"refresh_token\": \"halEdslDD61bTvMxUGbPbu75DxdKmWdP\"\n}" 478 | }, 479 | "url": { 480 | "raw": "https://localhost:9443/api/v1/customers/oauth2/token", 481 | "protocol": "https", 482 | "host": [ 483 | "localhost" 484 | ], 485 | "port": "9443", 486 | "path": [ 487 | "api", 488 | "v1", 489 | "customers", 490 | "oauth2", 491 | "token" 492 | ] 493 | } 494 | }, 495 | "response": [] 496 | } 497 | ] 498 | }, 499 | { 500 | "name": "ACL", 501 | "description": "", 502 | "item": [ 503 | { 504 | "name": "Associate Consumer", 505 | "request": { 506 | "method": "POST", 507 | "header": [ 508 | { 509 | "key": "Content-Type", 510 | "value": "application/json" 511 | } 512 | ], 513 | "body": { 514 | "mode": "raw", 515 | "raw": "{\n \"group\": \"group-api-v1-customers\"\n}" 516 | }, 517 | "url": { 518 | "raw": "localhost:9001/consumers/oneone@gmail.com/acls", 519 | "host": [ 520 | "localhost" 521 | ], 522 | "port": "9001", 523 | "path": [ 524 | "consumers", 525 | "oneone@gmail.com", 526 | "acls" 527 | ] 528 | } 529 | }, 530 | "response": [] 531 | }, 532 | { 533 | "name": "List ACL", 534 | "request": { 535 | "method": "GET", 536 | "header": [], 537 | "body": {}, 538 | "url": { 539 | "raw": "localhost:9001/acls", 540 | "host": [ 541 | "localhost" 542 | ], 543 | "port": "9001", 544 | "path": [ 545 | "acls" 546 | ] 547 | } 548 | }, 549 | "response": [] 550 | } 551 | ] 552 | }, 553 | { 554 | "name": "Consume service", 555 | "request": { 556 | "method": "GET", 557 | "header": [ 558 | { 559 | "key": "Host", 560 | "value": "api.ct.id" 561 | }, 562 | { 563 | "key": "Authorization", 564 | "value": "bearer UYSLDGIZI8yIotLryGRp93jvF1YsYdHB" 565 | } 566 | ], 567 | "body": {}, 568 | "url": { 569 | "raw": "localhost:9000/api/v1/customers", 570 | "host": [ 571 | "localhost" 572 | ], 573 | "port": "9000", 574 | "path": [ 575 | "api", 576 | "v1", 577 | "customers" 578 | ] 579 | } 580 | }, 581 | "response": [] 582 | } 583 | ] 584 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NodeJS-API-KONG 2 | This is for tutorial Kong that need very simple and static node.js serving as API. 3 | 4 | ‘What is KONG? 5 | 6 | Kong is Orchestration Microservice API Gateway. Kong provides a flexible abstraction layer that securely manages communication between clients and microservices via API. Also known as an API Gateway, API middleware or in some cases Service Mesh. It is available as open-source project in 2015, its core values are high performance and extensibility. 7 | 8 | You can read more detail on https://medium.com/@far3ns/kong-the-microservice-api-gateway-526c4ca0cfa6 9 | 10 | 11 | A lot of Plugin on KONG, one of them is OAuth 2.0. So nowadays no need to develop byself for OAuth on your system. 12 | Here is the tutorial https://medium.com/@far3ns/kong-oauth-2-0-plugin-38faf938a468 -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const bodyParser = require('body-parser'); 3 | const app = express(); 4 | 5 | const customers = [ 6 | { 7 | id: 5, 8 | first_name: 'Dodol', 9 | last_name: 'Dargombez' 10 | }, 11 | { 12 | id: 6, 13 | first_name: 'Nyongot', 14 | last_name: 'Gonzales' 15 | } 16 | ]; 17 | 18 | const clients = [ 19 | { 20 | id: 1, 21 | first_name: 'Haha', 22 | last_name: 'Hehe' 23 | }, 24 | { 25 | id: 2, 26 | first_name: 'Lala', 27 | last_name: 'Lili' 28 | } 29 | ]; 30 | 31 | app.use(bodyParser.json()); 32 | 33 | app.get('/api/v1/customers', (req, res) => { 34 | res.json(customers); 35 | }); 36 | 37 | app.get('/api/v1/customers/:id', (req, res) => { 38 | res.json(customers[req.params.id]); 39 | }); 40 | 41 | app.get('/api/v1/clients', (req, res) => { 42 | res.json(clients); 43 | }); 44 | 45 | app.get('/api/v1/clients/:id', (req, res) => { 46 | res.json(clients[req.params.id]); 47 | }); 48 | 49 | app.listen(10000, () => { 50 | console.log(`Server started!`); 51 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "docker_node_for_kong", 3 | "version": "1.0.0", 4 | "description": "Node.js on Docker for kong", 5 | "author": "Faren", 6 | "main": "index.js", 7 | "scripts": { 8 | "start": "node index.js" 9 | }, 10 | "dependencies": { 11 | "express": "^4.16.1", 12 | "body-parser": "1.18.3" 13 | } 14 | } 15 | --------------------------------------------------------------------------------