Fastly_CDN/CDN_설정

패스틀리(Fastly), Compute@Edge를 사용해 보자 #3

CDN_SKY 2023. 4. 27. 10:42

이번 시간에는 간단하게 Request를 보낸 Client의 Geolocation정보를 출력하는 것을 해보도록 하겠습니다.

 

Fastly에서 제공하는 Javasript-compute  SDK에서 어떤 기능이 제공되는지 확인할 수 있습니다.

https://js-compute-reference-docs.edgecompute.app/docs/fastly:geolocation/getGeolocationForIpAddress

 

getGeolocationForIpAddress | @fastly/js-compute

The getGeolocationForIpAddress() function is used to retrieve geolocation information about the given IP address.

js-compute-reference-docs.edgecompute.app

해당 SDK에서 참고하면 Client의 IP를 기반으로 어떤 종류의 Geolocation 정보를 얻을 수 있는지 확인할 수 있습니다.

Fastly가 수신한 각 요청이 서비스로 라우팅될 때마다 FetchEvent가 발송되며, 관련 EventListener는 event.responseWith를 클라이언트로 전송하려면 동기화하여 호출해야 합니다.

addEventListener("fetch", (event) => event.respondWith(handler(event)));

async function handler(event) {
  let clientGeo = event.client.geo;  
  let respBody = JSON.stringify({
    as: {
      name: clientGeo.as_name,
      number: clientGeo.as_number,
    },
    geo: {
      city: clientGeo.city,
      country_code: clientGeo.country_code,
      conutry_name: clientGeo.conutry_name,
      gmt_offset: clientGeo.gmt_offset,
      conn_type: clientGeo.conn_type,
      postal_code: clientGeo.postal_code,
    },
  });

  return new Response(respBody, {
    headers: {
      "content-type": "application/json",
    },
  });
}

Fiddle도 아래와 같이 첨부하니 참고해주시길 바랍니다.


https://fiddle.fastly.dev/fiddle/c77ca1dd

 

Fiddle - Fastly

 

fiddle.fastly.dev

 

블로그에 방문해 주셔서 정말 감사드립니다.