Skip to main content

Posts

Showing posts from 2017

Serving static files in a directory.

A few different ways to serve static files in a directory on the fly for development.

IIS Express
C:\> "C:\Program Files (x86)\IIS Express\iisexpress.exe" /path:C:\MyWeb /port:8000
NodeJS $ npm install -g http-server
$ http-server -p 8000
PHP $ php -S localhost:8000
Python 2 $ python -m SimpleHTTPServer 8000
Python 3 $ python3 -m http.server 8000
Ruby $ ruby -run -e httpd . -p 8000

Reverse a number without converting to a string.

Sometimes I like to work on coding problems just for fun. Things like Project Euler, Code Eval Hackerrank etc. Depending on what language you decide to do things in it might be easier and more efficient to think a little different.
Some languages like Ruby, for example, converting a number to a string with to_str() and then reversing is rather trivial. But what if I want to solve it by keeping the number a number?
Currently, I am playing around with Rust so my example will be in that language however you can use this same formula in whatever language you like.
pub fn num_reverse(number: i32) -> i32 { let mut num = number; let mut rev = 0; while num > 0 { rev = rev * 10 + (num % 10); num = num / 10; } return rev; } Now that we have the code lets map out a little bit of how it works. Let us take the number 1234.
num = 1234; rev = 0; Grab the first digit.
rev = rev * 10 + (num % 10)
Which can be written as.
rev = 0 * 10 + (1234 % 10…

Remote API useful tricks for Docker on Mac OS X

Install SocatSocat allow you to forward ports to a variety of things. For Docker forwarding a port to the docker.sock file makes a quick way to get access to the Docker remote api. Docker also allow a way to open up that TCP port however on Mac it is temperamental and hard to usebrew install socat socat -d TCP-LISTEN:2375,reuseaddr,fork UNIX:/var/run/docker.sock & Create an IP aliasRunning Docker containers locally is a fairly common thing. Interconnectivity between containers works naturally. However, sometimes you may also want a container to connect back to the Docker Remote API. If you are constantly on the go changing networks you do not want to have to change that IP address every time. A great use case for this is running Jenkins and using docker slavessudo ifconfig lo0 alias 192.168.99.50 Remote API There are also some interesting endpoints you can use for informationcurl http://localhost:2375 curl http://localhost:2375/containers/json curl http://localhost:2375/image…

Mock HTTP calls with Angular and Protractor

One of the benefits using AngularJS is also using some test tools like Protractor. Protractor is a tool that was made for Angular testing. Many times when testing you will need to mock some data so that you can see if your application responds in the correct manner. Let us assume that we have the following HTML file running our application.
<!doctype html> <html ng-app="demo"> <head> <title>Hello</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-mocks.js"></script> <script src="hello.js"></script> </head> <body> <div ng-controller="Hello"> <p>Hello <span id="gname">{{exampleResponse.name}}</span></p> </div> …

Build your own expectation library.

In the world of impending deadlines, testing is a key a vital role with any project. When building software you can test many functions and processes in a matter of minutes depending on how you setup your tests and how well the code was written to be testable. A key element for that tests is what you expect. Javascript for example, there are many expectation frameworks out there. Jasmine, Chai, Mocha to name a few. Let's build a simple expectation without their input. var expect = function(obj) { return {} } This will be our starting point for what we will expect. Very simply it is a function that will accept an argument obj and then it returns some object back that will be defined in more detail soon. You may be thinking one object? That doesn't seem quite right. For an expect statement to work shouldn't we have something like "expect(a, b, 'equal')". Yes, we could do something like that. However, for this example, we will attempt to make the co…