Last active
January 18, 2017 00:42
-
-
Save davidpett/3c244117b8b6de4c2df30b4201c7c011 to your computer and use it in GitHub Desktop.
node server for fastboot with geoip
This file contains hidden or 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
server { | |
listen 80; | |
server_name foo.foo; | |
location / { | |
proxy_pass http://localhost:3000; | |
proxy_http_version 1.1; | |
proxy_set_header Upgrade $http_upgrade; | |
proxy_set_header Connection 'upgrade'; | |
proxy_set_header Host $host; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_cache_bypass $http_upgrade; | |
} | |
location ~* "^/assets/.*-([a-z0-9]{32})\.(?:css|js|gif|png|jpe?g)$" { | |
root /foo/dir; | |
expires max; | |
add_header Cache-Control public; | |
access_log off; | |
try_files $uri =404; | |
} | |
} |
This file contains hidden or 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
'use strict'; | |
const geoip = require('geoip-lite'); | |
const FSNotifier = require('fastboot-fs-notifier'); | |
const FastBootAppServer = require('fastboot-app-server'); | |
let geo = null; | |
const getGeoIP = function(req, res, next) { | |
const ip = req.header('X-Real-IP'); | |
if (!geo) { | |
geo = geoip.lookup(ip); | |
} | |
if (geo) { | |
req.headers['X-Geo-Country'] = geo.country; | |
req.headers['X-Geo-Region'] = geo.region; | |
req.headers['X-Geo-City'] = geo.city; | |
req.headers['X-Geo-Lat'] = geo.ll[0].toString(); | |
req.headers['X-Geo-Lng'] = geo.ll[1].toString(); | |
} | |
next(); | |
}; | |
const notifier = new FSNotifier({ | |
targetDir: 'app' | |
}); | |
const server = new FastBootAppServer({ | |
beforeMiddleware(app) { app.use(getGeoIP); }, | |
distPath: 'app', | |
notifier: notifier, | |
gzip: true, | |
workerCount: 2 | |
}); | |
server.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment