// geoGreeting — a localized greeting based on the caller's country. // // Demonstrates: reading the raw request via `ctx.request.headers` (the geo // header the edge host injects), plus an optional GET query param. The whole // point of running at the edge is being close to the user — this reacts to // where they are. // // curl 'http://localhost:8910/?name=Ada' -H 'cf-ipcountry: DE' import { Effect, Schema } from 'effect' import { defineServerless } from '@voltro/serverless' const HELLO: Record = { DE: 'Hallo', FR: 'Bonjour', ES: 'Hola', IT: 'Ciao', JP: 'こんにちは', US: 'Hi', GB: 'Hello', } export default defineServerless({ name: 'geo-greeting', method: 'GET', input: Schema.Struct({ name: Schema.optional(Schema.String) }), output: Schema.Struct({ greeting: Schema.String, country: Schema.String, }), handler: ({ name }, ctx) => Effect.sync(() => { // Cloudflare sets `cf-ipcountry`; other proxies use varied headers. We // read whatever the host injected — the function code stays the same. const country = ( ctx.request.headers.get('cf-ipcountry') ?? ctx.request.headers.get('x-vercel-ip-country') ?? ctx.request.headers.get('x-forwarded-country') ?? 'XX' ).toUpperCase() const hello = HELLO[country] ?? 'Hello' return { greeting: `${hello}, ${name ?? 'there'}!`, country } }), })