Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
# IDE | ||
.idea | ||
.vscode | ||
|
||
# Mac OS | ||
.DS_Store | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
const test = require('ava') | ||
|
||
test('ava works ok', t => { | ||
t.true(true) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}) |