Go Language Setup for Multiple Projects

When working with Go language you must setup the GOPATH environment variable, but soon you will face two problems:

  • Each project should have its own Go dependencies and its own Git code repo, so putting your source under GOPATH would be problematic.
  • When working with “Atom” with “Go Plus” plugin, it needs to install several Go packages which would pollute your own source.

To solve both problems I added the following to my “.bash_login”:

export GOPATH=$HOME/go
alias atom='PATH=$PATH:$HOME/go/bin GOPATH=$HOME/go:$GOPATH atom'
gpp() {
        export GOPATH=`pwd`
}

It perform the following:

  • Set a default GOPATH to $HOME/go_sandbox for testing small Go projects
  • Set an alias for “atom” to add an extra dir in GOPATH and PATH, so whatever GO Plus plugin add won’t be add to your current GOPATH
  • Setup a “gpp” function to quickly change GOPATH to current directory

Implementing Login/Logout in Django

Update: add names and namespace to URLs

Implementing user authentication is fairly easy job in Django, many functionalities are already included in the standard Django installation, you can manage users using the default “admin” app the comes with Django.

Here I will show how implement Login/Logout feature by relying on Django built-in views.

Steps:

  • Create “accounts” app: we will put all “accounts” related files in this app.
  • Create URL mapping for login and logout pages.
  • Include “accounts” URL in the main URL mapping.
  • Create login page HTML template
  • Add small template code to show current user in all pages (changing base template)

# Create "accounts" app
$ ./manage.py startapp accounts
# Add to INSTALLED_APPS
$ vi project_name/settings.py 
INSTALLED_APPS = (
  :
  'accounts',
  :
)


# Create URL mapping for login and logout pages
$ vi accounts/urls.py 
from django.conf.urls import patterns, url
urlpatterns = patterns('',
    url(
        r'^login/$',
        'django.contrib.auth.views.login',
        name='login',
        kwargs={'template_name': 'accounts/login.html'}
    ),
    url(
        r'^logout/$',
        'django.contrib.auth.views.logout',
        name='logout',
        kwargs={'next_page': '/'}
    ),
)

# Include "accounts" URL in the main URL mapping
$ vi project_name/urls.py 
urlpatterns = patterns('',
    :
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),
    :
)


# Create login page HTML template
$ vi templates/accounts/login.html
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url 'accounts:login' %}">
{% csrf_token %}
<table>
{{ form }}
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="/" />
</form>
{% endblock %}


# Add small template code to show current user
$ vi template/base.html
:
{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}  <a href="{% url 'accounts:logout' %}">[logout]</a></p>
{% else %}
    <p>Welcome, Guest <a href="{% url 'accounts:login' %}">[login]</a></p>
{% endif %}
:

For more on the subject:
Using the Django authentication system

Bonus: Password Change form

Continue reading Implementing Login/Logout in Django

My First Makefile

The following is a sample Makefile for simple project, here I am building a simple “Bloom Filter” library, and “main” program to use it. The library will have the following files:
* hash.h and hash.c
* bloom.h and bloom.c and we should get “libbloom.a” out of it. The “main” program will use:

  • main.c
  • bloom.h
  • libbloom.a and we should get “main” executable. So our make file should look like this:
$ cat Makefile
CFLAGS=-Wall -O3
LDFLAGS= -L.
LDLIBS=-lbloom

CFLAGS += `pkg-config --cflags libpcre`
LDFLAGS += `pkg-config --libs libpcre`


OBJS=main.o other.o libbloom.a
BLOOM_OBJS=hash.o bloom.o

all: main libbloom.a

main: $(OBJS)
        $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

libbloom.a: $(BLOOM_OBJS)
        ar rcs $@ $^


.PHONY: clean
clean:
        -rm main libbloom.a *.o 

Makefile use TAB not spaces for indentation. To execute it just type “make” in the same directory where “Makefile” reside. Macros Explained: http://www.cs.duke.edu/~ola/courses/programming/Makefiles/node7.html