Appychip

Saturday, 17 February 2018

Django API

Create REST API Using Core Django

Django is a popular web framework in the Python world for rapid development. Here we will learn, how to quickly make REST API (Representational State Transfer Application Programming Interface) using Django.

# Prerequisites:

# End Result:

We will make a small application that will keep track of all my friends with their respective details.

API to list all friends.

API to get detail of a particular friend.

API to add a friend in the list.

API to delete a friend from the list.

After installing prerequisites, follow the below steps:

# Create Project

django-admin startproject my_project

Here the project name is my_project. This command will create a directory called my_project in your current working directory, with some pre populated directory and files. This directory is root directory for you project.

# Create App

cd my_project
python manage.py startapp my_app

The app name is my_app. You can create as many app you want inside one project. This command has created my_app directory inside the project's root directory.

# Database Creation and Setup

Let's use Sqlite as our database in this tutorial. If you are planning to use database other than Sqlite, you need to do appropriate settings in settings.py file of project.

To create tables, edit the models.py file of the app.

vim my_app/models.py

Here, we will create a table called, MyFriendList. To do this paste the following code in the opened file.

First command, will make the migration files (a file that contains the equivalent SQL statements for Database) and second command will execute it. After executing migrate command, you should see migration of files that are not created by us. These are some required tables by Django.

# Create Views

Views are used to define business logic. Here we will make three views, to list, create and delete the friend list. First open the views.py file of my_app application.

We have defined empty pattern for MyFreind class, so that our API will have structure like <server_name:port>/friends/. (Note: friends/ came from my_app application route that we defined in project wide urls.py)

For class MyFriendList, we have defined integer pattern for the key pk. Here API will have structure like <server_name:port>/friends/1/.

So, we are done with all the coding. We have model, views and urls in place. Now start the development server.

python manage.py runserver
Output:
Performing system checks...
System check identified no issues (0 silenced).
February 17, 2018-18:57:06
Django version 2.0.2, using settings 'my_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

How To Setup Fluentd With High AvailabilitySetup Fluentd With Forwarder-Aggregator Architecture

Fluentd is one of the most popular alternative to logstash because of its features which are missing in logstash. So before setting up fluentd let's have a look and compare both:

Fluentd has builtin architecture for high availability (There could be more than one aggregator)

Fluentd consumes less memory as compare to logstash

Log parsing, tagging is more easy

Tag based log routing is possible

Let's start building our centralized logging system using Elasticsearch, Fluentd and Kibana (EFK).

We will be following the architecture which has fluentd-forwarder(td-agent), fluentd-aggregator, Elasticsearch and Kibana. Fluentd-forwarder (the agent) reads the logs from a file and forward the logs to aggregator. Aggregator decides what should be the index_name and which host of Elasticsearch to send logs. The Elasticsearch is on a separate instance to receive logs and it also have kibana setup to visualise elasticsearch data.

# Architecture:

Following is the architecture for High Availability. Here there are multiple log-forwarder on each application node which are forwarding logs to log-aggregators. There are two aggregators shown in below architecture, if one fails then forwarders start sending logs to the second one.

Now after restarting td-agent on both forwarder and aggregator, you can see data being stored to elasticsearch. When elasticsearch start receiving data from aggregator, you can make index pattern in kibana and start visualising the logs.

# Create Index Pattern In Kibana

Once you start getting logs in Elasticsearch, You can create an index pattern in kibana to visualise the logs. We have specified the index_name in fluentd to be of format fluentd-myapp-2018.02.12, so we will create an index pattern fluentd-* Follow the below steps shown in pictures to create an index pattern.

Saturday, 10 February 2018

How To Use AWS Lambda Function With AWS RDS MySQL

If you ever want to have small service that access database and returns some result then you should go for AWS Lambda as it costs you only when it gets invoked. So You will be paying less then an ec2 instance.

Let's go ahead and see how we can create a lambda function which will interact with a database build on AWS RDS MySQL. You can choose your own database like postgresql hosted on an ec2 instance, but here for the sake of simplicity we will be using RDS MySQL.

First get your database Instance Up and running. You will need to have its endpoint, username, password and database name.

The point here to note is that we are using pymysql library which we need to install in our current working directory where our main.py file is present. To install it you can run the following command:

pip install pymysql -t .

Now, since we have the required library in place, we need to create the zip of our code and upload it on the lambda function. To make a zip run the following command:

zip -r lambdaRDS.zip `ls`

Save the lambda function and go ahead running the script by passing a test event to the function which looks like below:

{
"id":"1",
"name":"appychip"
}

Now go ahead running the lambda function. On successful execution, this will insert a record into mysql.

Be careful, while providing, Common Name for your certificate, as it certificate will work for that Common Name only. You can provide IP or domain name here. If you want to create single certificate for all your sub domains then you can put entry like "*.your_domain.com". Certificate created with this domain name will be valid for all your subdomains that has domain your_domain.com

Monday, 20 November 2017

Creating A Chrome Extension To Show Notification

If you are a chrome user then you must have seen some cool chrome extension which are very useful in daily life but did you ever wondered, how they are build? Are you curios to know? If yes, then this post if for you to get started on how to write your own chrome extension.

Here we will be building a chrome extension to show a popup which will have a button and on the click of which a notification will appear.

video Tutorial:

The basic structure of our application looks like following:

Manifest file (manifest.json) - It is the main component of chrome extension. It includes information about versioning, permissions, and other useful metadata for extension. It should be placed in the root of project folder.

popup page ( popup.html) - This page will open up a popup on clicking the extension icon. The popup will have a button clicking on which will show a notification.

popup javascript file (popup.js) - The javascript file which contains the logic of showing notification on the click of button in popup.

Favicon image (appychip.png) - An Image to be displayed on the chrome extension bar