-
Notifications
You must be signed in to change notification settings - Fork 19
Get geolocation about a given IP address #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
28779ba
23df1d1
fc08d73
0b966f4
7224f68
e70054b
97c4ec6
f7246fc
5a3506c
37271a3
4473889
5a97097
77ca319
6f4f8be
84bd996
3cd7cf2
1dc6920
5ec7b1b
9616015
2a83ae9
9c5177e
629c057
665dd7d
44347b6
d9f1270
f284d67
262f214
9d67bc5
bbafc46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Deploy Compute@Edge GeoIp | ||
on: | ||
push: | ||
# branches: [main] | ||
## Note: by removing the branches value, the workflow will run when a push is made to any branch. This is to serve for testing. | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Install project dependencies | ||
run: npm install | ||
working-directory: fastly-compute/geoip | ||
|
||
- name: Deploy to Compute@Edge | ||
uses: fastly/compute-actions@v5 | ||
with: | ||
project_directory: ./fastly-compute/geoip | ||
env: | ||
FASTLY_API_TOKEN: ${{ secrets.FASTLY_API_TOKEN }} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules | ||
/bin | ||
/pkg |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"sup3rs3cr3t" : "true" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Get Geolocation Information About a Given IP Address | ||
|
||
This service using [Fastly's Compute@Edge](https://developer.fastly.com/learning/compute/) to get specific geolcation information about a given IP address, and deploys automagically using Github Actions. | ||
|
||
## Getting started | ||
|
||
- Be sure to install [Fastly's CLI](https://developer.fastly.com/learning/tools/cli/) | ||
- As well as the dependencies: `npm install` | ||
|
||
## Try it out locally | ||
|
||
`fastly compute serve --watch` | ||
- This application should now be running on http://localhost:7676 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# This file describes a Fastly Compute@Edge package. To learn more visit: | ||
# https://developer.fastly.com/reference/fastly-toml/ | ||
|
||
authors = ["[email protected]"] | ||
description = "geoip" | ||
language = "javascript" | ||
manifest_version = 3 | ||
name = "geoip" | ||
service_id = "CnsNqIdpSj1tZNLTl6PUi4" | ||
|
||
[scripts] | ||
build = "npm run build" | ||
|
||
[local_server] | ||
[local_server.config_stores] | ||
[local_server.config_stores.geoip_auth] | ||
file = ".secret.json" | ||
format = "json" | ||
|
||
ewdurbin marked this conversation as resolved.
Show resolved
Hide resolved
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "get-geolocation-for-ip", | ||
"version": "0.4.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/pypi/infra.git" | ||
}, | ||
"type": "module", | ||
"author": "chloe@python.org", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/pypi/infra/issues" | ||
}, | ||
"homepage": "https://developer.fastly.com/solutions/starters/compute-starter-kit-javascript-empty", | ||
"dependencies": { | ||
"@fastly/js-compute": "^2.0.1" | ||
}, | ||
"scripts": { | ||
"build": "js-compute-runtime ./src/index.js ./bin/main.wasm", | ||
"deploy": "fastly compute publish" | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: overall, run the js code through Prettier to get consistent style. There's a live playground here: https://prettier.io/playground/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for this resource! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be useful to rerun prettier again. (Also looks like per-file comments can't be resolved. FYI @github ) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/// <reference types="@fastly/js-compute" /> | ||
|
||
import { getGeolocationForIpAddress } from "fastly:geolocation"; | ||
import { ConfigStore } from "fastly:config-store"; | ||
|
||
async function app(event) { | ||
const configStore = new ConfigStore("geoip_auth"); | ||
const secret = event.request.headers.get("X-Secret"); | ||
const token = await configStore.get(secret); | ||
|
||
if (!token) { | ||
let respBody = JSON.stringify({ Error: "Unauthorized" }); | ||
return new Response(respBody, { | ||
status: 401, | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
} | ||
let ip = | ||
new URL(event.request.url).searchParams.get("ip") || event.client.address; | ||
let geo = getGeolocationForIpAddress(ip); | ||
let respBody = JSON.stringify({ | ||
geo: { | ||
city: geo.city, | ||
continent: geo.continent, | ||
country_code: geo.country_code, | ||
country_code3: geo.country_code3, | ||
country_name: geo.country_name, | ||
region: geo.region, | ||
}, | ||
}); | ||
|
||
return new Response(respBody, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} | ||
|
||
addEventListener("fetch", (event) => event.respondWith(app(event))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We likely want to restore this behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed 👍