Simply Ian

The personal blog of Ian Macalinao

Getting Django to Work With Vagrant

| Comments

I was having problems getting Django working with Vagrant, then I stumbled upon this StackOverflow answer.

The problem with the port forwarding is not with Vagrant, but with Django itself. You need to bind to 0.0.0.0, not 127.0.0.1.

So you'd run this command:

1
python migrate.py runserver 0.0.0.0:8000

That will fix all of your issues.

The Coffeescript Keyword Nobody’s Heard of: ‘By’

| Comments

Coffeescript is an amazing language. As a matter of fact, it’s one of my favorite languages, second only to JavaScript. Here’s some recent code I used to split up one array into a multidimensional array with a certain number of columns.

1
matrix.push arr[i..(i + cols - 1)] for i in [0..arr.length - 1] by cols

Notice the second to last keyword: by.

The by keyword basically changes the increment of the generated for loop. Without the by, you’d have i++ as the increment of your for loop. However, with the by, you’d instead have i += 3 if you had by 3.

Here’s a simple example where I am getting money out of my bank account in the form of 5 dollar bills.

1
console.log "I have #{i} dollars." for i in [0..100] by 5

This translates to the following Javascript:

1
2
3
4
var i;
for (i = 0; i <= 100; i += 5) {
  console.log("I have " + i + " dollars.");
}

As you can see, the increment of the loop is now 5 rather than just 1.

Now the question is, are there any other hidden features of Coffeescript like this?

JSON Backends Are the Next Big Thing

| Comments

AngularJS is an awesome web framework made by Google to create "structured web apps". In this framework, templating is done client-side, so all the server needs to do is send data to the client via HTTP. Thus, no templating needs to be done on the backend and the web browser handles all of the data. All the backend needs to do is send JSON.

This is awesome.

Why? Because data is now separate from the view. Model-View-Controller, or MVC for short, has long been a standard of developing web applications as it organizes code in a way that just makes sense. A controller takes a request and serves a model, the data, and a view, which formats the data into a nice format.

However, since the introduction of the iPhone, web isn't all we have to worry about. Mobile apps are becoming almost necessary for services. Everyone does things from their phone now, as not everyone wants to bring a laptop around everywhere they go.

Websites are not built for mobile devices. Yes, there is responsive web design, but that does not beat the usefulness of an app that you can just click from your home screen. Thus, apps are a necessity.

So let's say you built a backend specific for your app. Great! But how about if you need to deal with other platforms? For example, smart watches or smart TVs. You need to build a whole new app that may have a completely different interface. To make this easier, you can create an API which will make it easy to interface with your website. You can even expose this API to developers to increase adoption of your product.

However, now you have this website that you simultaneously have to maintain. Every new feature of the website has its own rendering mechanism completely separate from the REST API. The features of the REST API aren't synchronized with your website.

Here's where frameworks like Angular come in. Angular can be like a mobile app, dealing directly with your API. When you build a new feature on your website, you can also build the logic into your API, causing you to only have to write your code ONCE. Now you don't have to worry about supporting certain operations on multiple platforms, as you have one backend that handles all of the platforms.

JSON-based backends are awesome. Logic is written once, and all you have to worry about is how your data is displayed. You can still support multiple platforms, but you only need to write your backend logic once, as JSON is ubiquitous.

How to Have Language-Specific Settings in Vim

| Comments

I wanted to set up Vim so that Coffeescript files would use 2 spaces for indentation. This was very simple:

  1. Create a file at ~/.vim/ftplugin/<language>.vim where <language> is the language you want to modify. For Coffeescript, this is coffee. The language name you should use is just the file extension.
  2. In this file, add your settings. In my coffee.vim, I have setlocal tabstop=2 and setlocal shiftwidth=2 to make my tabs 2 spaces.
  3. Open up a file in that language and enjoy your language specific settings!

Setting Up Virtual Hosts in Apache

| Comments

Today, I wanted to put my downloads on a different domain from my screenshots. Not wanting to manage multiple servers for no reason, I set up virtual hosts, also known as vhosts. Basically, depending on what domain you visit my web server from, you will get a different website. This is actually very simple to set up.

First, navigate to your apache2 directory and go to the sites-available directory within it. On my Debian system, this is at /etc/apache2/sites-available/. In this directory, you'll see a bunch of files. Each one of these files is a config file that can be enabled or disabled individually; this is called a site.

To set up vhosting, you should first disable the default website. Use the command sudo a2dissite default to do this.

Next, add the rest of your websites. Here is the very simple config file I use:

1
2
3
4
5
6
7
8
9
<VirtualHost *:80>
    ServerName domain.you.want.to.use.com
    DocumentRoot /var/www/sitefiles/
    <Directory /var/www/sitefiles/>
        Options Indexes FollowSymLinks MultiViews
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Save this to a file with the name of the site, no extension. I named my site screenshots for example.

Obviously, replace domain.you.want.to.use.com with the domain you want to use for the website. (For my screenshot website, this is s.giza.us.) The document root is the folder that contains the files at the root of your website. For me that's /var/www/screenshots.

Lastly, type sudo a2ensite sitename where sitename is whatever you named that file. Then, restart apache with sudo service apache2 restart, and all is well. For any additional domains, create more config files with that information, replacing all of the relevant stuff. Enjoy your awesome new vhosted website!

Including Dependencies in Your Gradle Build Script’s Classpath

| Comments

In a recent project, I wanted to use SnakeYAML in my Gradle build script. This is pretty easy to do; all you have to do is add the following to your script:

1
2
3
4
5
6
7
8
9
10
11
buildscript {
    repositories {
        mavenCentral()
        // ...etc
    }

    dependencies {
        classpath group: 'org.yaml', name: 'snakeyaml', version: '1.5'
        // ..etc
    }
}

This is the same as the repositories {} and dependencies {} sections of the build script. After doing this, feel free to use your libraries anywhere in your build script. Don't forget to import the classes you use!

Adding a Provided JAR as a Dependency in a Gradle Project

| Comments

Adding a JAR as a dependency is simple in Gradle. In your dependencies {}, add the following line:

1
compile files('file.jar')

Where file.jar is the path to the JAR from the root directory of the repository. For example, if I had Dependency.jar at ./libs/Dependency.jar, I would use compile files('libs/Dependency.jar').

Up - a Useful File Sharing Tool Using SCP

| Comments

I created a tool today called up. Although it's really simple, it gets the job done and it's really useful.

Basically, to share a file, you just type up MyFile and it will upload that file to your remote server, returning the URL it is hosted at.

How to Run a Command on Startup on Linux

| Comments

I recently purchased a VPS to run my IRC client in. I wanted to start my IRC client in tmux on startup. The answer is simple: crontabs.

Run the following command on the user you want to run the command on:

1
crontab -e

This will open up the crontab of your current user. A crontab is basically a file stating a bunch of tasks that you want to run on some sort of schedule.

Next, add the following text to the file:

1
@reboot <command>

where <command> is, of course, the command you want to run. For example, on boot, I run a shell script that starts up my tmux stuff, so I have @reboot /usr/ian/tmux_start.sh.

Pretty simple, right? Crontabs have a lot more uses that you can probably find on Google.

Source: man cron and man crontab.