Sunday, 11 March 2018

AngularJS Routing Using UI-Router

Background

In the last post, we saw AngularJS Hello World example. In this post, we will see how we can create an angular JS app with routing using UI-Router module. If you have not read the previous post I would highly recommend you read that first.


 Setup

There is a small change in the file structure than our previous example. Create files as shown in the following screenshot -



Following are file details -
  • helloWorld.html : Our main HTML file. Starting page similar to the last example.
  • helloworld.module.js : Declares angular module and it's dependencies
  • helloWorld.controller.js : Controller for the module
  • helloworld.config.js : Config for the module.  
  • login.html : login page to route to
  • setting.html : setting page to route to
 Also make sure you install http-server module to host your angular app and test it -
  • npm install -g http-server


Then you can simply run in your project directory as -
  • http-server -o 
NOTE : -o option is to open browser.

AngularJS Routing Using UI-Router


Now let's see one by one content of each file. Let's start with  helloWorld.html -

<html>
    <head>
        <title>Hello World!</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
            <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
            <script type="text/javascript" src="helloworld.module.js"></script>
        <script type="text/javascript" src="helloWorld.controller.js"></script>
        <script type="text/javascript" src="helloworld.config.js"></script>
    </head>
    <body ng-app="helloWorldApp">
        <div ng-controller="helloWorldAppController" ng-init="init()">
                  <p>Enter Message here : <input type="text" ng-model="message"></p>
                   <p>Entered Message :  {{ message }}!</p>

                   <a href="/login">Login</a> <br/>
                   <a href="/setting">Setting</a>

                <div class="content-wrapper">
                    <ui-view></ui-view>
                </div>    
        </div>
    </body>
</html>


In this base HTML file we have just referenced other angular JS files that we need. An interesting thing to note here is the ui-view tag. This is where the injected code goes. But we will see that in a moment.


Let's see out module and controller files next -


helloworld.module.js

(function() {
    'use strict';
    console.log("In hello world module");
    angular.module('helloWorldApp', ['ui.router']);
})();


This code just declares your module as we did in last example. Only change is that it has an additional dependency on ui.router module. Note we have included a new script in helloWorld.html to get code for this module.


helloWorld.controller.js

(function(){
    'use strict';
    console.log("In hello world controller");
    angular.module("helloWorldApp").controller("helloWorldAppController", function($scope) {
        $scope.init = function() {
            console.log("Init method called");
            $scope.message = "Hello World!";
        }
    });

})();


This is a controller of the module we defined. This is again same as we did in last post. No new changes here.


Now let's see our new logic - helloworld.config.js


(function() {
    'use strict';
    console.log("In hello world config");
    angular.module('helloWorldApp').config([
        '$stateProvider',
        '$urlRouterProvider',
        function($stateProvider, $urlRouterProvider) {
            $stateProvider.state('login',{
                     url: "/login",
                     templateUrl: "login.html"
                })
            .state('settings',{
                    url: "/setting",
                    templateUrl: "setting.html"
            });
        }
    ]);
})();






 

This is actually the routing logic. For eg. if it encounters URL "/login" it will render page "login.html" that we have in same folder. Same for setting.html.


Finally, let's see out login.html and setting.html files -


login.html -


<h1>This is a login page!</h1>
<a href="/helloWorld.html">Back</a>



setting.html -


<h1>This is a login page!</h1>
<a href="/helloWorld.html">Back</a>


 Once you have all files in place just run http-server as follows -




And you should see following behavior - 


 






But wait what happened? I thought the code was supposed to be injected at tag ui-view.

This is because we do not actually use angular routing per say to change the state. Let's do some changes to see how we can do that. Change your login href as follows - 

<a href="" ng-click="replaceLoginPage()">Login</a>


and now add this method to the controller -


(function(){
    'use strict';
    console.log("In hello world controller");
    angular.module("helloWorldApp").controller("helloWorldAppController", function($scope, $state) {
        $scope.init = function() {
            console.log("Init method called");
            $scope.message = "Hello World!";
        }

        $scope.replaceLoginPage = function() {
            console.log("In replaceLoginPage");
            $state.go("login");
        }
    });

})();



And now it should replace ui-view tag.









Related Links



Tuesday, 27 February 2018

AngularJS Hello World Example

Background

AngularJS is a client-side javascript framework developed by Google which can interact with HTML. It helps to easily create and maintain SPA (Single page applications)

Some of its features are -
  1. Helps create responsive applications
  2. Provides MVC capabilities
  3. Dependency injection
  4. Powerful and flexible with less code to write
NOTE: AngularJS is a very old framework. The first version is called AngularJS. Later versions are just called angular. You can see the detailed release history - https://github.com/angular/angular.js/releases. This post will try to explain a simple Hello World program in Angular JS. This is mainly for projects that are already on it. If you are starting a new project I would highly recommend to go with later angular versions. You can visit https://angular.io/  for the same.


 AngularJS Hello World Example

Let's start by writing our HTML file. Create a folder where you can store your code files. Now in this folder create two files -
  1. helloWorld.html
  2. helloWorld.js
And add following content to it -

helloWorld.html:

 <html>
    <head>
        <title>Hello World!</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>       
        <script type="text/javascript" src="helloWorld.js"></script>
    </head>
    <body ng-app="helloWorldApp">

        <div ng-controller="helloWorldAppController" ng-init="init()">
                  <p>Enter Message here : <input type="text" ng-model="message"></p>
                   <p>Entered Message :  {{ message }}!</p>
        </div>

    </body>
</html>



helloWorld.js  :

 (function(){
    'use strict';
    console.log("In JS");
    var helloWorldApp = angular.module("helloWorldApp", []);
    helloWorldApp.controller("helloWorldAppController", function($scope) {
        $scope.init = function() {
            console.log("Init method called");
            $scope.message = "Hello World!";
        }
    });

})();



Now you can simply open the helloWorld.html file and see the changes. 

 

Understanding the code

Now that we saw the behavior let's try to understand the code.

In our code we have initialized a angular module using ng-app directive. In the javascript we get this module using andular.module() function.  Inside a module you can have multiple controllers controlling various parts of your html. In our case we have defined a controller called helloWorldAppController that controls the div element we have defined. In the javascript we are getting this controller from our module.


Once you have the controller you can define varibles and functions that you can access in the HTML controller by this particular controller. You can also notice the variable called "message" that is defined in the controller is accessible in the HTML using {{message}} syntax. You would also see the binding exist using the ng-model syntax. This is called two way binding. So value changed in the input field is immediately visible in same HTML using {{message}} syntax as well as the controller using $scope.message. In the html where we have declared controller we have also specified init method using ng-init and you can see this menthod defined in controller using $scope.init. This is called when controller is initialized and this function in the controller script initialized messages variable to "Hello World!" that is reflected in HTML page.



Lastly we have injected our controller script (helloWorld.js ) and  angular js script in the html for our angular js code to work. Make sure angular script is the 1st one added.

All keywords starting with ng- are angular directived.
  • ng-app
  • ng-controller
  • ng-init
  • ng-model 

Create a gif from a video using ffmpeg with good quality

Background

In the last post, we saw to Resizing videos and images using ffmpeg. In this post, we will see how we can convert a video into a gif with good quality.



 Create a gif from a video using FFmpeg

You can use the following shell script to convert your video to Gif -

#!/bin/bash
#----------------------------------------------------
#  Author : athakur
#  Version : 1.0
#  Create Date : 27/02/2018
#  Update Date : 27/02/2018
#  Description : Create gif from a video
#    Sample usage : ./creategif.sh input.mp4 output.gif
# ffmpeg static build can be downloaded from https://johnvansickle.com/ffmpeg/
#----------------------------------------------------
echo "Converting $1 to $2"

if [ -z "$1" ] || [ -z "$2" ]
  then
  echo "Incorrect arguments supplied. Format - ./creategif.sh input.mp4 output.gif"
  exit
fi

palette="/tmp/palette.png"

filters="fps=10,scale=1024:-1:flags=lanczos"

ffmpeg -v warning -i "$1" -vf "$filters,palettegen" -y "$palette"
ffmpeg -v warning -i "$1" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$2"

echo "Completed gif creation" 


You can find this code in my GitHub gists section-
 To run you can simply execute following command - 
  • ./creategif.sh input.mp4 output.gif
Input need not be an mp4. It can be an .avi or a .webm too.

Sample video and gif are as follows -

Video :




Gif :




That's it your high-quality gif is ready.


You can do more with FFmpeg like scaling and cropping of video before converting to gig. Eg.

./ffmpeg -loglevel warning -y -i input.mp4 -i palette.png -filter_complex "crop=800:500:0:0[x];[x][1:v]paletteuse=dither=bayer:bayer_scale=3[tl];[tl]fps=10,scale=1024:-1:flags=lanczos"  target.gif



Related Links

Friday, 23 February 2018

Resizing videos and images using ffmpeg

Background

If you are a developer and a geek and need to work on the images/videos formatting in terms of cropping, scaling or resizing then you can use FFmpeg for that. Static build for FFmpeg is available on -
which you can download and store it on your local machine to use.



I have even added this to my Linux PATH so that I can access it from anywhere. If you are not aware how to do that refer -
Once you have added FFmpeg in your PATH you can simply run ffmpeg to see the help content -




Specifically, if you are working on making Android or iOS apps or chrome plugin and you have an icon of standard size and you want to resize it to fit other aspect ratios supported by the corresponding platforms then FFmpeg really comes in handy.


Rescaling images with FFmpeg

I have a simple icon with size 256*256 to be used as replay button.



 I want it in 128*128 size.  You can do this with the following command -
  • ffmpeg -i icon-256.png -vf scale="128:128" icon-128.png
And if you want 48*48 you can similarly do -
  • ffmpeg -i icon-256.png -vf scale="48:48" icon-48.png



If you want to want to retain the aspect ration you can do -
  • ffmpeg -i icon-256.png -vf scale="128:-1" icon-128.png

You can do similar resizing for a video instead of an image -
  • ffmpeg -i input.avi -vf scale="320:240" output.avi
If you want your image to be based on the actual image size then you can do that as well. For eg. You want the image to be double size of what the size it actually is -

  • ffmpeg -i icon-256.png -vf scale="iw*2:ih*2" icon-double.png


Since this is double this would be 512*512 since original was 256*256.
Similarly, if you want half you can do -

  •  ffmpeg -i icon-256.png -vf scale="iw/2:ih/2" icon-half.png






 Since this is half this would be 128*128 since original was 256*256.

NOTE :
  • iw : input width
  • ih : input height


Related Links

Saturday, 10 February 2018

Shell script to update lambda code and environment variables to multiple regions and environments using AWS CLI

Background

If you are using AWS stack in your project then you must have used Lambda service some or the other time. It is what we call the serverless architecture. You do not care about the hardware or the  operating system. You just provide the Lambda with the code you wish to run which can be triggered from multiple sources like an S3, API gateway etc.

Since AWS services are region specific you may have same lambda code running in multiple regions. You will have to go to each region and deploy your lambda code either by directly uploading the zip file or uploading from S3 bucket in same region. Either ways this way is time consuming and repetitive. Also you might have same code running under different names corresponding to different execution environments like dev,qa,production etc. Also each lambda may have environment variables like database configuration settings or other custom setting like memory and timeout.

In this post I will show you a simple shell script that uses AWS CLI to do this from your local machine. You can just run this command and it will take care of deploying your code, changing environment variables and setting custom configurations for each region you wish to deploy lambda to.




Assumptions and Setup

This deployment script assumes you have installed AWS Cli and configure a profile in it. If you have not done it already refer -
NOTE : If you are not explicitly providing a profile name to aws configure it is by default set as "default"

Next this script also assumes you have local zip file that has your lambda code. This script also takes an env variables as the input which expects your lambda function name to have it as a suffix. So if your base lambda function name is "my-lambda" then your actual lambda function name is different environments should be -
  • test : my-lambda-test
  • dev : my-lambda-dev
  • qa : my-lambda-qa
  • prod : my-lambda-prod
Script has the base name of the lambda and some environment variables that are defined globally and per region. Script also has array of regions the lambda should be updated to. These  things you can change as per your usecase.


Shell script code

You can find the code on my Github gist -
I am also adding it below to explain it how it works. However to see the most recent version refer to the gist link above.

Code is as follows -

#!/bin/bash
#----------------------------------------------------
#  Author : athakur
#  Version : 1.0
#  Date : 10/02/2018
#  Description : Deployment script to update lambda code and env variables
#  Sample usage :  
#    Local : ./update-lambda.sh test aws-admin fileb://../lambda.zip
#    Dev : ./update-lambda.sh dev aws-admin fileb://../lambda.zip
#    QA : ./update-lambda.sh qa aws-admin fileb://../lambda.zip
#    Prod : ./update-lambda.sh prod aws-admin fileb://../lambda.zip
#----------------------------------------------------
echo "Updating lambda code for ENV : $1 PROFILE : $2 ZIP_FILE_PATH : $3"

ENV=$1
PROFILE=$2
ZIP_FILE_PATH=$3

if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]
  then
  echo "Incorrect arguments supplied. Format - ./update-lambda.sh ENV PROFILE ZIP_FILE_PATH"
  exit
fi

FUNCTION_NAME="my-lambda-$ENV"

ENV_VAR_1=1991
ENV_VAR_2="password"


SNS_ENDPOINT=''

SUPPORTED_REGIONS=("us-east-1" "ap-northeast-1" "ap-southeast-1" "ap-southeast-2")

for REGION in ${SUPPORTED_REGIONS[@]}
do
    echo 'Region : $REGION'
    case "$REGION" in
        "us-east-1")
            SNS_ENDPOINT="arn:aws:sns:us-east-1:123456789123:my-sns-notification"
        ;;
        "ap-northeast-1")
            SNS_ENDPOINT="arn:aws:sns:ap-northeast-1:123456789123:my-sns-notification"
        ;;
        "ap-southeast-1")
            SNS_ENDPOINT="arn:aws:sns:ap-southeast-1:123456789123:my-sns-notification"
        ;;
        "ap-southeast-2")
            SNS_ENDPOINT="arn:aws:sns:ap-southeast-2:123456789123:my-sns-notification"
        ;;                    
        *)
            echo "Environment not provided"
            exit 1
        ;;
    esac

    env_variables="{\"Variables\":{\"ENV_VAR_1\":\"${ENV_VAR_1}\",\"ENV_VAR_2\":\"${ENV_VAR_2}\",\"SNS_ENDPOINT\":\"${SNS_ENDPOINT}\",\"ENV\":\"${ENV}\"}}"
    echo "Env variables :  $env_variables"        
    lambda_update_env_command="aws lambda update-function-configuration --function-name $FUNCTION_NAME --region $REGION --profile $PROFILE --environment '$env_variables' --timeout 300 --memory-size 3008"    
    echo "Executing command : $lambda_update_env_command"
    eval $lambda_update_env_command
    lambda_update_code_command="aws lambda update-function-code --function-name $FUNCTION_NAME --region $REGION --zip-file $ZIP_FILE_PATH --profile $PROFILE"    
    echo "Executing command : $lambda_update_code_command"
    eval $lambda_update_code_command    
    echo "Completed Lambda function update for region $REGION"
done


Now let's try to understand what we are doing in above shell script.

Understanding the shell script

Above shell script takes 3 arguments -
  1. Env : This is the env. Eg. test, dev, qa, prod etc
  2. Profile name : This is the aws profile name configured. If you have not done so this will just be "default"
  3. zip file path : Path to lambda zip file

 First part of the script validates that you provided arguments needed for script to run. Next we define some environment variables that we need to set for our lambda. Next we set a environment variable called SNS_ENDPOINT that changes per region. You can do a similar code snippet per environment as well.

Next we have an array of aws regions we need to deploy our lambda code in. You can add / remove as per your use case. Finally we run 2 aws commands for each region -
  1. update-function-configuration : This updates the environment variables and other configurations needed by your lambda
  2. update-function-code : This updates the actual lambda code that gets deployed to the lamdba.
NOTE :  We are also setting --timeout 300 --memory-size 3008 which basically sets lambda timeout and memory to maximum available i.e 5 mins and 3 GB respectively.

NOTE :  Lambda is charged as the amount of time it runs * memory it uses. So change above configurations as per your need and budget.



Related Links


t> UA-39527780-1 back to top