Skip to content

Go solution to provide a fast way to consult Costa Rican citizens basic information

License

Notifications You must be signed in to change notification settings

greivinlopez/citizens

Repository files navigation

citizens-logo

A Go solution to provide a fast way to consult Costa Rican citizens basic information

API Documentation

The Citizens API uses the REST architectural style. The API is organized into resources and each resource should be reachable using an unique URI. Different actions occurs depending on the HTTP verb you use to access the resource. Documentation related to the API can be found here: Citizens REST API documentation

Ubuntu Server Setup

Install MongoDB

Install MongoDB following this instructions: Installing MongoDB on Ubuntu

Run the mongo shell:

mongo

Add admin user to mongo:

use admin

db.createUser(
  {
    user: "<adminuser>",
    pwd: "<adminpassword>",
    roles:
    [
      {
        role: "userAdminAnyDatabase",
        db: "admin"
      }
    ]
  }
)

Substitute the values between the <> with the ones you want to use for MongoDB admin user

Edit mongodb.conf and add auth=true

http://stackoverflow.com/questions/6235808/how-can-i-restart-mongodb-with-auth-option-in-ubuntu-10-04

Restart mongodb service

sudo service mongodb restart

Run the MongoDB shell

mongo

Create a user for people database

use admin
db.auth('<adminuser>', '<adminpassword>')
use people
db.createUser(
	{
	    user: "<dbuser>",
	    pwd: "<dbpassword>",
	    roles : [
			{
				role : "readWrite",
				db : "people"
			},
			{
				role : "dbAdmin",
				db : "people"
			}
		]
	}
)
db.auth('<dbuser>', '<dbpassword>')

Again remember to substitute the values between <> with your own credentials.

Create a dummy document to save the new database permanently

db.users.save( {username:"glopez"} )

There are other security measures you can follow regarding the MongoDB server:

https://www.digitalocean.com/community/tutorials/how-to-securely-configure-a-production-mongodb-server

Installing Go

cd ~
wget https://storage.googleapis.com/golang/go1.3.1.linux-amd64.tar.gz
tar -zxvf go1.3.1.linux-amd64.tar.gz
rm go1.3.1.linux-amd64.tar.gz

Binary distributions assume they will be installed at /usr/local/go Otherwise, you must set the GOROOT environment variable

sudo mv ~/go /usr/local
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile
source ~/.profile

Install Dependencies

apt-get install git
apt-get install bzr

Server Configuration

The server application follows the recomendation of the twelve-factor app regarding configuration. Store the configuration in environment variables. To set the variables use the "export" command. For instance:

sudo nano ~/.bash_profile

Add the following lines to the end of the file

export GOPATH=/root/packages/
export CZ_DB_ADDRESS="localhost"
export CZ_DB_USER="<dbuser>"
export CZ_DB_PASS="<dbpassword>"
export CZ_API_KEY="<apikey>"

Save the file and return to the command line. Run the new configuration

source ~/.bash_profile

The values you use for dbuser and dbpassword must be the same you use when creating the database user in the previous steps. The value for apikey will hold the API key the clients will use to make requests to the server. Ensure to use a long key value that includes numbers, letters, symbols and to keep it secured.

Running Server

Install package dependencies

go get gopkg.in/mgo.v2
go get gopkg.in/martini.v1
go get github.com/greivinlopez/skue

Download source code

mkdir citizens
cd citizens
git init
git remote add --track master origin https://github.com/greivinlopez/citizens.git
git pull

Fill database

Download the complete "Padron Electoral":

cd filldb
curl -O http://www.tse.go.cr/zip/padron/padron_completo.zip

Install zip and unzip facilities to the server:

sudo apt-get install zip unzip

Unzip the archived file:

unzip padron_completo.zip

Compile and fill database

go build filldb.go
./filldb

The filldb command will extract the data from the "padron electoral" files and create the documents on the database. The process will run for 5-10 minutes.

Compile and run the Server

cd ..
cd server
go build server.go
./server

Test the API

We are using curl here but you can use the REST client that you prefer. Replace "localhost" with the actual host address and "citizensApi" with your own API Key.

curl --request GET http://localhost:3020/citizens/205480941 --header 'X-API-KEY:citizensApi'

You should receive a response like this one:

{
  "Identification": "205480941",
  "FirstName": "Greivin",
  "LastName": "Lopez",
  "SurName": "Paniagua",
  "Gender": "M",
  "Address": {
   "Province": "Alajuela",
   "City": "Central",
   "District": "Villa Bonita (Parte Sur)"
  }
}

License

About

Go solution to provide a fast way to consult Costa Rican citizens basic information

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages