Larry Price

And The Endless Cup Of Coffee

Let Me Be a Billboard for DuckDuckGo for a Few Minutes

| Comments

I would describe my job as 10% cleverness, 10% tenacity, 30% historical knowledge, and 50% searching the internet.

As you can see, a good search engine is extremely important to me. My job depends on it. Opening Chromium’s history, I can see that I have recently searched for microwaves, activesupport compatibility in ruby 2.1.0, deleting branches in git, and glibc backwards compatibility.

I don’t use Lord Google as my default search engine. Instead I use DuckDuckGo. From Wikipedia:

DuckDuckGo is an Internet search engine that emphasizes protecting searchers' privacy and avoiding the “filter bubble” of personalized search results. DuckDuckGo distinguishes itself from other search engines by not profiling its users and by deliberately showing all users the same search results for a given search term. DuckDuckGo also emphasizes getting information from the best sources rather than the most sources….

The gist is that DuckDuckGo does not personalize your search results based on demographics or search history. It doesn’t store your IP address and it doesn’t log your information. You cannot “log in” to the DuckDuckGo website.

Getting down to brass tacks: How does it do search?

Searching on DuckDuckGo for queries such as “Will Ferrell” or “Markdown” will give you a blurb at the top of your search results from Wikipedia about that matching query. Searching programming topics (like “ruby random array” or “asp.net create table”) will give you a near-matching StackOverflow answer. Searching for a single word will usually give you the definition.

A query that seems mistyped will ask you if you want to correct it. Search speed is comparable to Google, I’ve noticed no difference.

Looking at the result of your query, you get your search results middle-aligned. Middle. Aligned I know you probably don’t think that’s a big deal, but go do a Google search right now. It’s left-aligned. Same with Bing. Do you see what’s on the right half of your monitor? Nothing. When running a Google search with my 22" monitor (~19" of horizontal space), there are 13" of wasted space. About 9" on Bing.

So, you’ve just done a search on DuckDuckGo. Now you can use the Up/Down arrows on your keyboard to navigate the search results. Pressing Enter takes you to the website. I vaguely remember that Google used to allow this, but it seems to have disappeared from the default search results.

Don’t like being able to see the combined search results of 50+ search engines? !Bang syntax is the answer.

What is !bang syntax? Let’s say you’re me and you’re looking for over-the range microwaves. I type into my DuckDuckGo search microwaves !a and DuckDuckGo takes me straight to Amazon, showing me search results for all those sweet, sweet microwaves. !g takes you to Google, !w takes you to Wikipedia, !gi takes you to Google Images, !yt takes you to Youtube, !lowes takes you to the Lowe’s search results, !gog takes you to Good Ol' Games, and on and on and on and on and on.

There are 100s of other goodies as well. Do math using an “=” sign, like “=128*256”. Find “Hello, World” examples by searching for “hello world perl” or “hello world clojure”. Do a Twitter hashtag search to find out what “#yolo” means. Currency conversions. Unit conversions. Convert between timezones.

Okay, enough shameless plugging for my favorite search engine. Give it a chance. Let those Google servers cool down for a few microseconds, they won’t mind.

Convincing Rvm to Let You Use Ruby 2.1.0

| Comments

Ruby 2.1.0 went stable a few months ago, and Ruby 1.9.3 support will end in a just over a year.

You know what that means: Warplanes in the sky falling to the ground, dogs and cats getting along like old pals, and people wandering aimlessly through the streets trying to remember the last time they saw a green build.

Believe it or not, all of these things can be prevented. Once upon a time, I wrote about configuring and using rvm to control individual ruby environments for each of your projects. If it’s been a while since you installed your copy of rvm, you might have some trouble installing and using newer version of ruby. Lucky for us, those clever rvm developers made it easy to get around this.

In my case, I want to upgrade a project to use ruby 2.1.0. The first time I tried to run rvm install ruby-2.1.0, I ended up installing ruby-2.1.0-preview1. I realized that I had installed rvm on this machine around October 2013, and ruby 2.1.0 was released in December 2013, so rvm had no idea that ruby 2.1.0 was stable. Updating rvm (in the root of the project directory):

/home/lrp/Projects/2014/projNeedingRuby210
1
$ rvm get stable

There will be some amount of text on the screen if your system needs to be updated. Note that you must be connected to the internet if you want rvm to update. Now we do the install of our brand new ruby:

/home/lrp/Projects/2014/projNeedingRuby210
1
$ rvm install ruby-2.1.0

Again, text on the screen from fetching of data from the internet. But hopefully you see something that tells you the operation was successful. You can also verify which rubies you have installed using the list command:

/home/lrp/Projects/2014/projNeedingRuby210
1
2
3
4
5
6
7
8
$ rvm list rubies
   ruby-1.9.3-p448 [ x86_64 ]
=> ruby-2.0.0-p247 [ x86_64 ]
 * ruby-2.1.0 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

Now we tell our current project to use ruby 2.1.0:

/home/lrp/Projects/2014/projNeedingRuby210
1
2
3
4
5
6
7
8
9
10
$ rm .ruby-version .ruby-gemset
$ rvm 2.1.0@projNeedingRuby210 --create --ruby-version
ruby-2.1.0 - #gemset created /home/lrp/.rvm/gems/ruby-2.1.0@projNeedingRuby210
ruby-2.1.0 - #generating projNeedingRuby210 wrappers.
$ rvm gemset copy 2.0.0-p247@projNeedingRuby210 2.1.0@projNeedingRuby210
Copying gemset from 2.0.0-p247@projNeedingRuby210 to 2.1.0@projNeedingRuby210
Generating gemset wrappers ruby-2.1.0@projNeedingRuby210.
Making gemset 2.1.0@projNeedingRuby210 pristine.
$ which ruby
/home/lrp/.rvm/rubies/ruby-2.1.0/bin/ruby

Alright! Crisis averted. If you’re using bundler with this project, be sure to change your ruby version (usually located near the top of the Gemfile).

What about setting up a new project using ruby 2.1.0? Easy! Switch to the project directory and:

/home/lrp/Projects/2014/newRubyProject
1
$ rvm 2.1.0@newRubyProject --create --ruby-version

Oh, rvm, you make life too easy sometimes.

Letting the Browser Decide Betwen HTTP and HTTPS

| Comments

While working on SEP Labs' Health2Wealth app, I got my first taste of setting up a website to use secure connections. I’ve been using the browser extension HTTPS Everywhere for quite some time now, so all of the web pages that I visit attempt to using HTTPSinstead of the standard HTTP. When I inadvertently started testing our app with HTTPS, things got a little weird.

My machine was able to open most of a page, but it wouldn’t load some of the graphics or javascript we had embedded in the page. I was the only one able to reproduce it, so we chalked it up to a wonky machine. Then I suddenly remembered that I was using HTTPS Everywhere. I found the area in FireFox that warns you about insecure connections and found that our page was only partially loading on my browser because some of the content was insecure. From there I enabled an option to reveal the insecure elements, which forced my page to load everything correctly. So now we needed to find what was insecure about our page. I trekked through the codebase and found that some of our graphs and links were hard-coded to use HTTP. My initial thought was just to force them to use HTTPS, which is almost certainly the impending standard. With a quick search, I found that there exists a real solution to this problem.

Let’s say I have a very normal web application, so I want to include jQuery embedded in my page. I initially copy-paste <script src='http://code.jquery.com/jquery.js'></script> right into my <head> node, which normally seems fine. Unfortunately, this is where issues arise. I’m now forcing the user to make a connection which may or may not be secure to fetch data from an insecure page.

By removing the http: from the http://, the issue of deciding whether to fetch the file using HTTP or HTTPS is left to the web browser. So my script include becomes <script src='//code.jquery.com/jquery.js'></script>. This also works for links, images, and web fonts. In fact, every link included in this post uses the same principal, where I leave the protocol out of what becomes the href tag.

Custom Domain With Github Pages

| Comments

Update April 2022: I no longer own the domain larry-price.com and have removed the links here

As of today, I’m the proud owner of larry-price.com. On the recommendation from a friend, I used domainmonster.com for it’s cost effectiveness and usable toolset. I wanted to link my Github Pages blog (this blog!) to my new site. In case I ever want to do this again, I’ve provided myself (and you!) with a step-by-step guide below. Note that Github has a page that explains how to do this, but there’s some fluff and ordering issues that turned me off from using those instructions as any more than a reference.

Step 0: Buy your domain

All sites where you can purchase domains have the same domains available, but at different prices. From the sites I perused, GoDaddy had the best search for domain names, suggesting different variations on the searched domain name. I found that domainmonster.com had the ugliest UI but the best prices. Hover.com looked really modern and had moderate prices. Shop around and spend money.

Step 0.5: Exercise restraint

At this point in the process, don’t go to your site.

My guess is you already have. If you don’t have the ability to go back in time, don’t sweat it, just know that you probably won’t be able to verify the fruits of your labor for a few hours.

Step 1: Setting up an ‘A Record’

Find the ip address where your site is currently hosted. For me (replace URL with your own):

1
2
3
4
5
6
7
$ nslookup larryprice.github.io
Server:   127.0.1.1
Address:  127.0.1.1#53

Non-authoritative answer:
Name: larryprice.github.io
Address: 199.27.74.133

You should be able to “manage” your domain on your domain provider’s website. In the “management” area, you should be able to find a section about “DNS”. Find something labeled “A records.” There will likely already be one or two entries here - one for the IP address of your new domain and one with the IP address of your domain with “www.” prepended.

Modify the entry (or entries) with the IP address found using nslookup (“199.27.74.133” from the example above). Save your site settings.

Step 2: Mucking with the “www” instance

While we’re modifying these settings, we might as well move the ‘www’ case out of the “A records”. Go ahead and delete it.

Look for something named “CNAME records”, or figure out how to add records in general. We’re going to add a CNAME record where the “Alias” is “www” and the “Address” is your site’s name. In my case, my “Alias” field is “www” and my “Address” field is “larry-price.com”. Add the record and cross your fingers for success.

Step 3: Telling Github Pages what to do

Alhtough Github Pages is magical in its own right, it doesn’t know what to do when receiving a request from the “A record” we created in Step 2 without further instruction.

If you’re using Github Pages for a user page, we are going to make this change on the master branch. If this is a project page, do it on the gh-pages branch.

All we have to do is put our new domain into a file in the root folder called CNAME. Let’s do it in one fell swoop of the command line.

echo "larry-price.com" > CNAME

Push that to Github and wait 0 to 10 minutes for Github to refresh your page.

Note: If you’re one of the few, the proud using the brilliant Octopress to generate your blog, you want to make sure to add the CNAME file to the root of your source/ directory. That way, when you call deploy your CNAME file will end up in the root of your _deploy directory.

Step 4: Behold

Check your Github repository’s “Settings” section. In the “GitHub Pages” section, you should now see something along the lines of “Your site is published at http://larry-price.com”. Go, young one, and visit your new site; and behold the glory you have brought to your family.

I’ll note that this is the point where not obeying Step 0.5 will cause issues. Once your internets knows that your page hit a certain DNS, it takes a few hours to refresh. In my case it only took about 2 hours, but it may take between 12 and 48 hours to refresh. A workaround to see if everything is working is to pull out your smart phone and use your carrier’s network to check your new site, since that area of the internets is probably still untainted. (All of this paragraph is a simplified thought process on how DNS works, considering that’s the level I understand it.)

Now fly, my children, and spread yourself over the internet with dozens of silly domain names!

Finishing the Google Go Writing Web Applications Tutorial

| Comments

A golang web app tutorial

I did some work with Google Go recently and had the chance to follow their great tutorial Writing Web Applications. The tutorial is pretty simple: use the Go http library to create a very simple wiki-style site. I like this tutorial a lot because there’s not too much hand-holding, but they do eventually hand you the final code listing. Then the good people at Google give you the tall task of completing the following ‘Other tasks’ without solutions:

  • Store templates in tmpl/ and page data in data/.
  • Add a handler to make the web root redirect to /view/FrontPage.
  • Spruce up the page templates by making them valid HTML and adding some CSS rules.
  • Implement inter-page linking by converting instances of [PageName] to &lt;a href="/view/PageName"&gt;PageName&lt;/a&gt;. (hint: you could use regexp.ReplaceAllFunc to do this)

This is what I’d like to go over. I scoured the web and didn’t have much luck finding solutions to these issues. That would be okay if they were all trivial, but the final step is not straightforward. I’m going to assume you’ve already gone over the tutorial. You can see my repository on Github, and I have included links to the appropriate commits in the code sections of this blog post.

Store templates in tmpl/ and page data in data/

The tutorial originally has the developer store all pages in the project directory. Every time a user made a new wiki page, a new file would creep into the project directory. All HTML templates were also stored in the project directory.

Moving templates is quite trivial. In the global scope:

wiki.golink
1
2
-var templates = template.Must(template.ParseFiles("edit.html", "view.html"))
+var templates = template.Must(template.ParseFiles("tmpl/edit.html", "tmpl/view.html"))

I found moving the page data to data/ was a little trickier, especially if the directory didn’t already exist. You may not have the same issue, but I remedied this by creating the directory if it doesn’t exist. My save function differences:

wiki.golink
1
2
3
4
5
6
7
func (p *Page) save() error {
-    filename := p.Title + ".txt"
-    return ioutil.WriteFile(filename, p.Body, 0600)
+  os.Mkdir("data", 0777)
+  filename := "data/" + p.Title + ".txt"
+  return ioutil.WriteFile(filename, p.Body, 0600)
}

Add a handler to make the web root redirect to /view/FrontPage

All we’re going to do is create a simple handler called rootHandler that redirects to a new page called FrontPage. We then add it in the main function. The tutorial had us wrap out handlers in a function call to take some special actions, but that wrapper would mess up our handler in its current form. So I just Redirect to the view handler, which will then decide whether to view or create the FrontPage.

wiki.golink
1
2
3
4
5
6
7
8
9
10
11
+ func rootHandler(w http.ResponseWriter, r *http.Request) {
+   http.Redirect(w, r, "/view/FrontPage", http.StatusFound)
+ }

...

func main() {
+  http.HandleFunc("/", rootHandler)
  http.HandleFunc("/view/", makeHandler(viewHandler))
  http.HandleFunc("/edit/", makeHandler(editHandler))
  http.HandleFunc("/save/", makeHandler(saveHandler))

Spruce up the page templates by making them valid HTML and adding some CSS rules.

I took my old .html files and put them through a validator. Making them valid only involved adding DOCTYPE, html, and head tags. The head tag needed meta, and title tags and we were valid. I’ve shown view.html below.

view.htmllink
1
2
3
4
5
6
7
8
9
10
11
12
+<!DOCTYPE html>
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+<title>Wiki made using Golang</title>
+</head>
 <h1></h1>

 <p>[<a href="/edit/">edit</a>]</p>

 <div></div>
+</html>

Implement inter-page linking by converting instances of [PageName]

Converting [PageName] to a hyperlink was a bit more complicated than expected. I originally just tried to run the string through ReplaceAllFunc and replace all instance of [PageName] with an equivalent hyperlink. This does not work because we use Go’s ExecuteTemplate method to render our template. ExecuteTemplate escapes any HTML that we give it to prevent us from displaying unwanted HTML. Getting around this was the fun part, because I want the benefit of escaped HTML while still having the ability to substitute my own HTML.

As it turns out, ExecuteTemplate will not escape variables of the type template.HTML. So I added another variable onto the Page struct called DisplayBody.

wiki.golink
1
2
3
4
5
type Page struct {
     Title string
     Body  []byte
+    DisplayBody template.HTML
}

Next, I create a regular expression to find instances of [PageName] and I put the defintion above the main method.

wiki.golink
1
+var linkRegexp = regexp.MustCompile("\\[([a-zA-Z0-9]+)\\]")

In my viewHandler, I escape Body and then set DisplayBody to that escaped string with the links substituted.

wiki.golink
1
2
3
4
5
6
7
8
9
+  escapedBody := []byte(template.HTMLEscapeString(string(p.Body)))
+
+  p.DisplayBody = template.HTML(linkRegexp.ReplaceAllFunc(escapedBody, func(str []byte) []byte {
+      matched := linkRegexp.FindStringSubmatch(string(str))
+      out := []byte("<a href=\"/view/"+matched[1]+"\">"+matched[1]+"</a>")
+      return out
+    }))
  renderTemplate(w, "view", p)
}

To finish up, I modify the view.html to show DisplayBody. I don’t use printf, because that would turn DisplayBody back into a string and ExecuteTemplate would escape it.

wiki.golink
1
2
-<div></div>
+<div></div>

And that completes the extra exercises for Google’s Writing Web Applications tutorial. Hopefully one day this helps someone who gets stuck.