Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zrrrzzt committed Feb 23, 2017
1 parent 952c7a5 commit 8331946
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 1 deletion.
1 change: 1 addition & 0 deletions .dockerignore
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions .gitignore
@@ -1,3 +1,10 @@
# IDE
.idea
.vscode

# Mac OS
.DS_Store

# Logs
logs
*.log
Expand Down
6 changes: 6 additions & 0 deletions .travis.yml
@@ -0,0 +1,6 @@
language: node_js
node_js:
- "7"
after_success:
- npm run coveralls
- test $TRAVIS_BRANCH = "master" && npm i -g now && now -t=$NOW_TOKEN --npm && now -t=$NOW_TOKEN alias
33 changes: 33 additions & 0 deletions Dockerfile
@@ -0,0 +1,33 @@
###########################################################
#
# Dockerfile micro-geoip-country
#
###########################################################

# Setting the base to nodejs 7.6.0
FROM node:7.6.0-alpine

# Maintainer
MAINTAINER Geir Gåsodden

#### Begin setup ####

# Installs git
RUN apk add --update --no-cache git

# Bundle app source
COPY . /src

# Change working directory
WORKDIR "/src"

# Install dependencies
RUN npm install --production

ENV NODE_ENV production

# Expose 3000
EXPOSE 3000

# Startup
ENTRYPOINT npm start
46 changes: 45 additions & 1 deletion README.md
@@ -1,2 +1,46 @@
[![Build Status](https://travis-ci.org/zrrrzzt/micro-geoip-country.svg?branch=master)](https://travis-ci.org/zrrrzzt/micro-geoip-country)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
[![Greenkeeper badge](https://badges.greenkeeper.io/micro-geoip-country/micro-tania.svg)](https://greenkeeper.io/)

# micro-geoip-country
Gets country from ip
Get country from ip

## API

### GET

```bash
curl https://country.geoip.allthethings.win?ip=8.8.8.8
```

returns

```JavaScript
{
"countryCode": "US",
"countryCode3": "USA",
"countryName": "United States",
"countryEmoji": "🇺🇸"
}
```

### POST

```bash
curl -d '{"ip": "8.8.8.8"}' https://country.geoip.allthethings.win
```

```JavaScript
{
"countryCode": "US",
"countryCode3": "USA",
"countryName": "United States",
"countryEmoji": "🇺🇸"
}
```

## License

[MIT](LICENSE)

![alt text](https://robots.kebabstudios.party/micro-geoip-country.png "Robohash image of micro-geoip-country")
22 changes: 22 additions & 0 deletions index.js
@@ -0,0 +1,22 @@
'use strict'

const readFileSync = require('fs').readFileSync
const marked = require('marked')
const { parse } = require('url')
const { send, json } = require('micro')
const getCountry = require('./lib/get-country')

module.exports = async (request, response) => {
const { query } = await parse(request.url, true)
const data = request.method === 'POST' ? await json(request) : query
if (data.ip && data.ip.length > 0) {
const results = await getCountry(data.ip)
response.setHeader('Access-Control-Allow-Origin', '*')
response.setHeader('Access-Control-Allow-Methods', 'GET')
send(response, 200, results)
} else {
const readme = readFileSync('./README.md', 'utf-8')
const html = marked(readme)
send(response, 200, html)
}
}
15 changes: 15 additions & 0 deletions lib/get-country.js
@@ -0,0 +1,15 @@
'use strict'

const ip2countrify = require('ip2countrify')

module.exports = ip => {
return new Promise((resolve, reject) => {
ip2countrify.lookup(ip, (ip, results, error) => {
if (error) {
reject(error)
} else {
resolve(results)
}
})
})
}
62 changes: 62 additions & 0 deletions package.json
@@ -0,0 +1,62 @@
{
"name": "micro-geoip-country",
"description": "Get country from ip",
"version": "1.0.0",
"license": "MIT",
"private": true,
"author": {
"name": "Geir Gåsodden",
"email": "geir.gasodden@pythonia.no",
"url": "https://github.com/zrrrzzt"
},
"main": "index.js",
"engines": {
"node": ">=7.6.0"
},
"scripts": {
"test": "standard && nsp check && ava",
"test-offline": "standard && ava",
"coverage": "nyc ava",
"coveralls": "nyc ava && nyc report --reporter=lcov && cat coverage/lcov.info | coveralls",
"setup": "npm install",
"standard-fix": "standard --fix",
"start": "micro",
"now-deploy": "npm test && now --npm && now alias"
},
"keywords": [
"microservice",
"micro",
"geoip"
],
"repository": {
"type": "git",
"url": "https://github.com/zrrrzzt/micro-geoip-country.git"
},
"bugs": {
"url": "https://github.com/zrrrzzt/micro-geoip-country/issues"
},
"now": {
"alias": [
"country.geoip.allthethings.win"
],
"env": {
"NODE_ENV": "production"
},
"type": "npm"
},
"devDependencies": {
"ava": "0.18.2",
"axios": "0.15.3",
"coveralls": "2.11.16",
"nsp": "2.6.2",
"nyc": "10.1.2",
"standard": "8.6.0",
"test-listen": "1.0.1"
},
"dependencies": {
"ip2countrify": "0.2.0",
"marked": "0.3.6",
"maxmind": "2.1.0",
"micro": "7.0.6"
}
}
7 changes: 7 additions & 0 deletions test/modules/ava-test.js
@@ -0,0 +1,7 @@
'use strict'

const test = require('ava')

test('ava works ok', t => {
t.true(true)
})
12 changes: 12 additions & 0 deletions test/modules/dependencies-test.js
@@ -0,0 +1,12 @@
'use strict'

const test = require('ava')
const pkg = require('../../package.json')
const dependencies = pkg.dependencies || {}

Object.keys(dependencies).forEach((dependency) => {
test(`${dependency} loads ok`, t => {
const module = require(dependency)
t.truthy(module)
})
})
12 changes: 12 additions & 0 deletions test/modules/dev-dependencies-test.js
@@ -0,0 +1,12 @@
'use strict'

const test = require('ava')
const pkg = require('../../package.json')
const dependencies = pkg.devDependencies || {}

Object.keys(dependencies).forEach((dependency) => {
test(`${dependency} loads ok`, t => {
const module = require(dependency)
t.truthy(module)
})
})
30 changes: 30 additions & 0 deletions test/routes/routes-test.js
@@ -0,0 +1,30 @@
'use strict'

const test = require('ava')
const listen = require('test-listen')
const axios = require('axios')
const micro = require('micro')
const srv = require('../../index')

const getUrl = fn => {
const srv = micro(fn)
return listen(srv)
}

test('it returns README as frontpage', async t => {
const url = await getUrl(srv)
const result = await axios.get(url)
t.true(result.data.includes('MIT'), 'frontpage ok')
})

test('it returns json from GET', async t => {
const url = await getUrl(srv)
const result = await axios.get(`${url}?ip=8.8.8.8`)
t.is(result.data.countryCode, 'US', 'GET ok')
})

test('it returns json from POST', async t => {
const url = await getUrl(srv)
const result = await axios.post(`${url}`, {ip: '8.8.8.8'})
t.is(result.data.countryCode, 'US', 'POST ok')
})

0 comments on commit 8331946

Please sign in to comment.