Skip to content

Instantly share code, notes, and snippets.

View alvises's full-sized avatar

Alvise Susmel alvises

View GitHub Profile
var express = require('express');
var mongodb = require('mongodb');
var app = express();
//ENABLE CORS
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.get('/locate', function(req, res) {
//getting the ip of the client from the request headers or remoteAddress
var ip = req.param("ip");
ip = ip || req.headers['x-forwarded-for'] || req.connection.remoteAddress;
var net = ip.split(".").slice(0,3).join(".")+".0"; //aaa.bbb.ccc.0
mongodb.connect("mongodb://127.0.0.1:27017/poetic_geoips",function(err,db){
if(err) throw err;
var ips = db.collection("ips");
function GeoIP(forcedIP) {
var locateUrl = "http://geoip.poeticoding.com/locate";
//locate function, success or fail callbacks will be triggered
//once the request to the server is finished
this.locate = function(successCallback,failCallback) {
var xhr = new XMLHttpRequest();
if(forcedIP) locateUrl += "?ip="+forcedIP;
xhr.open("GET", locateUrl, true);
xhr.timeout = 60000;
xhr.onload = function (e) {
<HTML>
<head>
<title>GeoIP test</title>
<script src="http://geoip.poeticoding.com/geoip.js" type="text/javascript"></script>
</head>
<body>
<div id="details">Loading...</div>
</body>
<script>
var geoip = new GeoIP();
-(void)presentPopoverFromView:(UIView *)fromView
onModalViewController:(UIViewController*)modalViewController {
[self presentPopoverFromView:fromView];
 
//converting the origin of the popover from the old parent view to the new modal view
self.origin = [modalViewController.view convertPoint:self.origin fromView:_parentView];
 
//the new parent view is the modal view
[self.view removeFromSuperview];
@alvises
alvises / ghost_main.css
Last active November 13, 2018 22:50
Poeticoding Ghost blog style
.site-header {
background: white !important;
background-color: white !important;
}
@alvises
alvises / client.ex
Created December 19, 2018 19:51
Realtime market-data updates - full channel - calculating the rate
defmodule Coinbase.Client do
use WebSockex
@url "wss://ws-feed.pro.coinbase.com"
def start_link(products \\ []) do
{:ok, pid} = WebSockex.start_link(@url, __MODULE__, %{})
subscribe(pid, products)
{:ok, pid}
end
@alvises
alvises / lazy_csv_bench.exs
Created January 13, 2019 20:47
Benchee benchmark of csv processing
lazy_csv_fn = fn ->
File.stream!("large.csv")
|> Stream.map(&String.trim(&1))
|> Stream.map(&String.split(&1, ","))
|> Stream.filter(fn
["Timestamp" | _] -> false
[_, "NaN" | _] -> false
[timestamp | _] ->
true
end) |> Enum.find(fn
@alvises
alvises / start.sh
Created February 25, 2019 10:56
How to run a Docker Container
docker container run -p 8000:80 nginx
@alvises
alvises / example.exs
Created March 18, 2019 13:17
async callback
defmodule Example do
use GenServer
def start_link(_) do
GenServer.start_link __MODULE__, :ok, []
end
def init(:ok) do
{:ok, []}
end