ArcGIS Proxy

ArcGIS Proxy

หน้าที่ของ resource proxy นิยามไว้ดังนี้

  • เข้าถึงทรัพยากรข้ามโดเมน (CORS)
  • ส่ง Request ที่มีความยาวเกิน 2048 ตัวอักษร หรือ Request แบบ POST ที่ต้องการส่งข้อมูลจำนวนมาก
  • การเข้าถึง Secure Map Service ด้วย Token Based Authentication

AtlasX API Gateway สามารถใช้งานเป็น ArcGIS Proxy ได้ โดยคุณสามารถตั้งค่าได้ที่ไฟล์ Configurations/ArcGisProxy/arcgis-proxy.[Environment].json ตาม Environment ที่คุณต้องการ (โดย config ของ Production จะไม่มี .[Environment]) แสดงได้ดังตัวอย่างต่อไปนี้

{
  "Proxy": {
    "Path": "/arcgis-proxy",
    "TokenPath": "/arcgis-token",
    "AllowedReferers": [
      // "*",
      "http://localhost", // For local development only
      "https://portal-atlasx.cdg.co.th"
    ]
  },
  "ArcGIS": [
    // Public services (No authentication required)
    {
      "Url": "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services"
    },
    // ArcGIS Enterprise - Token-based authentication
    {
      "Url": "https://enterprise.example.com/arcgis/rest/services/Admin/MapServer",
      "Username": "admin",
      "Password": "adminpass"
    },
    ...
    // ArcGIS Online - organization-specific subdomain
    {
      "Host": "yourorg.maps.arcgis.com",
      "ClientId": "your-client-id",
      "ClientSecret": "your-client-secret",
      "OAuth2TokenEndpoint": "https://www.arcgis.com/sharing/rest/oauth2/token"
    }
  ]
}

การตั้งค่าพื้นฐาน

คุณสามารถตั้งค่าพื้นฐานของ ArcGIS Proxy ภายใต้ Object Proxy รายละเอียดการตั้งค่าแต่ละ Property มีดังนี้

  • Path : ระบุชื่อ path สำหรับใช้เป็น Proxy (default: /arcgis-proxy)
  • TokenPath : ระบุชื่อ path สำหรับการขอ ArcGIS Token เพื่อ Authenticate (default: /arcgis-token)
  • AllowedReferers : ระบุ URL ของ Referer ที่อนุญาตให้สามารถเรียกใช้งาน ArcGIS Proxy ได้ สามารถใส่ “*” เพื่อ allow ทุก Host ให้เรียกได้ (ไม่แนะนำให้ทำบน Production)

การตั้งค่า Server ต่างๆ

คุณสามารถระบุรายละเอียดของ Map Server ต่างๆ ที่สามารถเข้าถึงได้ผ่าน Proxy ภายใต้ Array “ArcGIS” ซึ่งรองรับการตั้งค่า 2 ลักษณะใหญ่ๆ ดังนี้

  1. ArcGIS Enterprise สำหรับเข้าถึง REST Services ต่างๆ บน ArcGIS Server เพื่อเข้าถึงแบบ Token-based (ใช้ Username/Password) โดย property ต่างๆ ที่ต้องระบุมีดังนี้
  • Url : ระบุ URL ของ Map Server มักอยู่ในรูปแบบ https://[domain]/arcgis/rest/services
  • Username : ระบุ Username สำหรับเข้าถึง Map Server ** สามารถละไว้ได้ถ้าเป็น Public Service
  • Password : ระบุ Password สำหรับเข้าถึง Map Server ** สามารถละไว้ได้ถ้าเป็น Public Service

ตัวอย่าง

{
  ...
  "ArcGIS": [
    // Public services (No authentication required)
    {
      "Url": "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services"
    },
    // ArcGIS Enterprise - Token-based authentication
    {
      "Url": "https://enterprise.example.com/arcgis/rest/services/Admin/MapServer",
      "Username": "admin",
      "Password": "adminpass"
    },
    {
      "Url": "https://enterprise.example.com/arcgis/rest/services/Viewer/MapServer",
      "Username": "viewer",
      "Password": "viewerpass"
    },
    ...
  ]
}
  1. OAuth2 สำหรับเข้าถึง Service ต่างๆ บน ArcGIS Online หรือ ArcGIS Enterprise ขององค์กร โดยใช้ Client ID และ Secret โดย property ต่างๆ ที่ต้องระบุมีดังนี้
  • Host : ระบุ URL ของ ArcGIS Online หรือ ArcGIS Enterprise
  • ClientId : ระบุ Client ID
  • ClientSecret : ระบุ Secret
  • OAuth2TokenEndpoint : ระบุ URL สำหรับขอ OAuth2 Token

ตัวอย่าง

{
  ...
  "ArcGIS": [
    // ArcGIS Enterprise - OAuth2
    {
      "Host": "enterprise1.example.com",
      "ClientId": "client-id",
      "ClientSecret": "client-secret",
      "OAuth2TokenEndpoint": "https://enterprise1.example.com/portal/sharing/rest/oauth2/token"
    },
    // ArcGIS Online (www.arcgis.com) - multiple apps
    {
      "Host": "www.arcgis.com",
      "ClientId": "client-id-1",
      "ClientSecret": "client-secret-1",
      "OAuth2TokenEndpoint": "https://www.arcgis.com/sharing/rest/oauth2/token"
    },
    {
      "Host": "www.arcgis.com",
      "ClientId": "client-id-2",
      "ClientSecret": "client-secret-2",
      "OAuth2TokenEndpoint": "https://www.arcgis.com/sharing/rest/oauth2/token"
    },
    // ArcGIS Online - organization-specific subdomain
    {
      "Host": "yourorg.maps.arcgis.com",
      "ClientId": "your-client-id",
      "ClientSecret": "your-client-secret",
      "OAuth2TokenEndpoint": "https://www.arcgis.com/sharing/rest/oauth2/token"
    }
    ...
  ]
}

วิธีการเรียกใช้งาน ArcGIS Proxy

ทำการ Request มาที่ http://[Gateway_HostAddress]/[Path]?[MapServiceURL] เพื่อเรียกใช้งาน Map Service ผ่าน Proxy

ตัวอย่าง

curl --location --request GET 'http://localhost:5220/arcgis-proxy?https://gisserv2.cdg.co.th/arcgis/rest/services/AtlasX/City/MapServer'

ทำการ Request มาที่ http://[Gateway_HostAddress]/[TokenPath]?[MapServiceURL] เพื่อ Generate ArcGIS Token

ตัวอย่าง

curl --location --request GET 'http://localhost:5220/arcgis-token?https://gisserv2.cdg.co.th/arcgis/rest/services'

การใช้งานร่วมกับ Frontend ด้วย ArcGIS Maps SDK for JavaScript

ที่ฝั่ง Frontend คุณสามารถใช้ Proxy ร่วมกับ utility methods urlUtils หรือร่วมกับ Property esriConfig.request.proxyRules ได้ โดยการตั้งค่า Proxy Rule ดังตัวอย่างต่อไปนี้

const urlUtils = await $arcgis.import("@arcgis/core/core/urlUtils.js");

urlUtils.addProxyRule({
  urlPrefix: "map-service-url",
  proxyUrl: "http://localhost:5220/arcgis-proxy",
});

ด้วยการตั้งค่านี้จะทำให้ทุก Request ที่เรียกไปยัง Map Service ที่ขึ้นต้นด้วย map-service-url ที่ระบุ จะทำการเรียกผ่าน Proxy http://localhost:5220/arcgis-proxy?[map-service-url] เสมอ