/jestergeoip

Jester plugin for getting location information for each client request based on IP address.

Primary LanguageNimMIT LicenseMIT

Introduction to jestergeoip

ver 1.0.1

nimble repo.support

This is a plugin for the nim web framework Jester. It pulls location information from a database run by GeoJS using the IP address of the current web request and places it into a JSON document.

The service is currently provided for free from https://www.geojs.io/

HOW TO USE

  1. Install this library via nimble (nimble install jestermmgeoip).
  1. Include the plugin location <- getGeoIP("sqlitedbname") at the top of your main routes or primary router. This will enable the plugin for the whole web site.
  2. In every route, the location JsonNode variable created by the plugin is available.

EXAMPLE

import json
import jesterwithplugins
import jestergeoip

proc namePage(loc: JsonNode): string =
  if loc.hasKey("country_code"):
    result = "Hello person in " & loc["country_code"].getStr
  else:
    result = "Hello stranger"

routes:
  plugin location <- getGeoIP("geojs.db")
  get "/test":                  # get http://127.0.0.1/test
    resp namePage(location)

HOW IT WORKS

When ever a request reaches the jester server, the sqlite db is checked for a legacy answer. If an old entry is found or no entry is found, a live API query is made and saved. The time-to-live for an entry is 30 days.

If an error is encountered during an API lookup, the returned JsonNode will contain key of "error" and a value describing the failure.

The answer is returned as a JsonNode object.

For details about the JsonNode document, visit: https://www.geojs.io/docs/v1/endpoints/geo/

Table Of Contents

  1. Introduction to jestergeoip

  2. Appendices

    1. jestergeoip Reference