Simply Ian

The personal blog of Ian Macalinao

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.

How to Change the Name of the JAR Produced in Gradle

| Comments

I don’t like the traditional JAR name assigned in Gradle. In Bukkit development, you usually make the JAR name the same as the plugin name. Here’s how you set the name of the JAR:

1
jar.baseName = 'JarName'

Where JarName would be the name of the jar generated in build/libs/.

Source: StackOverflow

How to Run a Specific Class Inside of a JAR

| Comments

My friend wanted me to test out his program, but it didn’t have a main class specified in the manifest. Here’s the solution:

1
java  -cp yourjar.jar com.yourpackage.YourClass

This will run the specified class as the main class.

Source: StackOverflow

Using Tmux Remotely Within a Local Tmux Session

| Comments

I often SSH to remote servers, and those servers usually have tmux installed. (tmux is better than Screen in every way) However, conflicts arise when you want to manipulate a remote tmux session within a local one. Ctrl-B refers to the local tmux session, not the remote one, and you have to press Ctrl-B twice to manipulate the remote one. This is pretty annoying. Fortunately, there is a solution to this.

There is one line you need to add to your ~/.tmux.conf (if this file doesn’t exist, create it):

1
bind-key -n C-a send-prefix

This binds the command send-prefix to Ctrl-A. Basically, you are sending a Ctrl-B (assuming you’ve left tmux at its defaults) directly to the server when you press Ctrl-A. This will let you manipulate the remote session with Ctrl-A and still use your local session with Ctrl-B. Pretty nice, eh?

Source: StackOverflow