Thursday, June 29, 2017

Angular cli 4 JQuery \ Other Plugins(Underscore js) setup

For this blog I will show you how to setup JQuery and Underscore js with using NPM and without using NPM

Lets create a new Angular cli project
ng new PROJECT_NAME
cd PROJECT_NAME
Replace the Highlighted red color PROJECT_NAME with your own one.

Using NPM

1. Install JQuery via NPM
npm install jquery --save
npm install --save-dev @types/jquery

Current Jquery Version 3.2.1 requires minimum Typescript version of 2.3.4 so check the package.json file and change version numbers if its necessary.
TypeScript version number in the package.json

2. Install Underscore JS via NPM
npm install underscore --save
npm install --save @types/underscore

3. Setup environment

If you open the node_modules directory locate inside the project, you will find there is folder called jquery and underscore, refer the below pictures
Node Modules directory
jquery folder
Undersocre folder

Open jquery folder then goto dist folder in here you can find the JQuery js file which is stored as jquery.js or the minified version jquery.min.js, Undersocre's JS file will be locate in the undersocre folder I hope you can find it. Now open the .angular-cli.json file locate in the root of the project
angular-cli.json file
Now copy the path of the jquery.min.js and the underscore-min.js files and paste it in the scripts section in the angular-cli.json file.
angular-cli.json file's script section after edit
Once you do this you dont need to add the Jquery and Underscore in the index.html file's head section.

4. Using the Jquery and Underscore in a component

Open the app.component.ts file which locate in the app folder. and import the Jquery and the underscore
import * as jQuery from 'jquery';
import * as _ from 'underscore';

Create a constructor and type the below code
constructor(){

    var debounced = _.debounce(function () {
     alert("Working");
    }, 400);
    $(document).ready(function () {
        debounced();
    });
  }

Final app.component.ts
import { Component } from '@angular/core';
import * as jQuery from 'jquery';
import * as _ from 'underscore';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
  constructor(){

    var debounced = _.debounce(function () {
     alert("Working");
    }, 400);
    $(document).ready(function () {
        debounced();
    });
  }
}

Thats it for the Using NPM part, lets take look at the without NPM part

Without NPM

1. Include Jquery and the Underscore JS CDN in the index.html file's head section.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Full code for index.html file
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Ex8</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
  <style>
    body{
      padding:30px;
    }
    </style>
</head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>

2. Writing Jquery and Underscore code in a component file

Open app.component.ts file which locate in the app folder. Next we need declare some variables to use Jquery code and the Underscore, other wise typescript file will not compile because for example Jquery code will always begin with the $ character but $ is not identified by the TypeScript compiler.

Add this code to the app.component.ts after the imports.
declare var jquery:any;
declare var $ :any;
declare var _:any;

Now create a constructor and add this code
constructor(){
    var debounced = _.debounce(function () {
     alert("Working");
    }, 400);
    $(document).ready(function () {
        debounced();
    });
  }

Full code for the app.component.ts

import { Component } from '@angular/core';
declare var jquery:any;
declare var $ :any;
declare var _:any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
  constructor(){
    var debounced = _.debounce(function () {
     alert("Working");
    }, 400);
    $(document).ready(function () {
        debounced();
    });
  }
}

That's it.

Saturday, May 6, 2017

Highly Bitcoin/Ethereum paying Android app 2017-05-07(yyyy/MM/dd)


BitMaker - Playstore link



Original Description ( on playstore)

Earn free Bitcoin or Ethereum simply by trying new games, apps, products, services, or watching a short video!

Bitcoin has increased over 700% in the last four years!!! 

Have you read about Bitcoin or Ethereum and wanted a piece but didn’t know how to get it? Until now risking your money to buy bitcoin or understanding complex technology to mine bitcoin were the only solutions to attaining free bitcoins. 

With BitMaker (Bitcoin Maker) earn free Bitcoin or Ethereum, not by mining, but by engaging with cool new apps, games, and products. 
Therefore, you can earn Bitcoin effortlessly without all the hassle! For Free!

You can earn free bitcoins on BitMaker every 30 minutes, simply open the app, engage, then collect your free Bitcoin!
And while waiting for your Bitcoins, you can do whatever you want, e.g. play games, study, work, watch sports, anything!!

AND, you'll get your hard-work paid back every Saturday night!

Minimum withdrawal limit is super low so you can receive your free bitcoin to your wallet and keep it to invest, save, or use however you want.

Follow us on Facebook for future updates and information: https://www.facebook.com/BitMakerApp

Note: This app uses the unit Satoshi, the smallest unit of Bitcoin, instead of Bitcoins.

If you have any questions or concerns, please check the FAQ. If your questions isn't there, please contact us at support@cakecodes.com

Download link

Proof that the app is paying ( These Images are from my phone )




Images from the Coinbase ETH Wallet


Angular2-cli and Bootstrap-3/4 setup

I am going to use the bootstrap's Collapse component for this tutorial. Finally it will something like this

Creating a angular-cli project

prerequisites


Both the CLI and generated project have dependencies that require Node 6.9.0 or higher, together with NPM 3 or higher. You can use visual studio code as the editor

Installation angular cli

Run this code in the cmd/terminal
1
npm install -g @angular/cli

Creating a new angular project

Run this code in the cmd/terminal
1
2
3
ng new PROJECT-NAME
cd PROJECT-NAME
ng serve

Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

Setup Native Angular directives for Bootstrap


Run this code in the cmd/terminal
1
npm install ngx-bootstrap --save

Add below code to your index.html file which is locate in you project's src directory.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">




















Implementation


Creating body component
ng g component body

Creating a component for the bootstrap collapse component
ng g component zippy

you can find API reference for the other bootstrap components in here

Add this code to the app.module.ts file.
import { CollapseModule } from 'ngx-bootstrap';

@NgModule({
  imports: [CollapseModule.forRoot(),...]
})


You can find the repository for this project in here

Add below code to the app.component.html
<app-body style="padding: 10px;">
</app-body>

We going to take the title of Collapse as a Input field so You have to import the Input  from @angular/core. Import Input  to the zippy component. Add or modify below line to the zippy.component.ts
import { Component, OnInit,Input } from '@angular/core';

Add isCollapsed boolean to track whether it's collapsed or not and add title string filed to store the title.
zippy.component.ts
public isCollapsed:boolean = true;
@Input() title:string = "";

Finally zippy.component.ts will be like this
import { Component, OnInit,Input } from '@angular/core';
@Component({
  selector: 'app-zippy',
  templateUrl: './zippy.component.html',
  styleUrls: ['./zippy.component.css'],
  
})
export class ZippyComponent implements OnInit {
 public isCollapsed:boolean = true;
  @Input() title:string = "";
  ngOnInit() {     
  }
}

lets add the collapse component to the zippy.component.html, please note that [collapse] in the line 6 this is coming from the ngx-bootstrap that we installed earlier.
1
2
3
4
5
6
7
8
9
<div class="panel panel-default" >
  <div class="panel-heading" (click)="isCollapsed = !isCollapsed"><b>{{title}}</b>
    <span style="float: right" class="glyphicon" [class.glyphicon-menu-down]="isCollapsed" [class.glyphicon-menu-up]="!isCollapsed"
      aria-hidden="true"></span>
  </div>
  <div [collapse]="isCollapsed" class="panel-body">
    <ng-content></ng-content>
  </div>
</div>

I am going to add some styling to to this code and finally zippy.component.html will be like this
<div class="row">
  <div class="col-sm-1"></div>
  <div class="col-sm-10">
    <div class="panel panel-default" style="margin-bottom: 0px;">
      <div class="panel-heading" (click)="isCollapsed = !isCollapsed"><b>{{title}}</b>
        <span style="float: right" class="glyphicon" [class.glyphicon-menu-down]="isCollapsed" [class.glyphicon-menu-up]="!isCollapsed"
          aria-hidden="true"></span>
      </div>
      <div [collapse]="isCollapsed" class="panel-body">
        <ng-content></ng-content>
      </div>
    </div>
  </div>
  <div class="col-sm-1"></div>
</div>

Put this code to the body.component.html. This code will give us two collapse components.
<app-zippy [title]="'Who can see my stuff?'">
    Content of who can see my stuff
</app-zippy>
<app-zippy [title]="'Who can contact me?'">
    Content of who can contact me
</app-zippy>

Now just type below command to run the Angular development server, and you can see the output from the http://localhost:4200/
ng serve

Sunday, April 30, 2017

Create a service for executable in Ubuntu 16.04 systemd



I am using a web server that I found in here as my executable, many thanks for the developer who developed this web server.

First of all we need to create a script which is capable of starting and stopping the executable and manage a PID file as we start and stop the executable, because systemd will need a PID file to find the main process id of the program.

Parameters


1. so my executable name is http_examples
2. my program is currently locate in /home/altair/Desktop/server/Simple-Web-Server/build
3. and I need to run this program as user altair
4. Start/Stop Script name server_startscript.sh

I will use above four parameters in this tutorial and you need to change them according to your environment, I will highlight them using red color so you can identify what places you need to edit



Creating the start/stop script

1
2
cd /home/altair/Desktop/server/Simple-Web-Server/build
vi server_startscript.sh

Paste the following, make sure to replace the BINARYNAME , press "I" before you paste the code because in VIM you need to press I to insert text to the file or use any other text editor like nano
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#!/bin/sh

COMMANDLINE_PARAMETERS="${2}" #add any command line parameters you want to pass here
D1=$(readlink -f "$0")
BINARYPATH="$(dirname "${D1}")"
cd "${BINARYPATH}"
LIBRARYPATH="$(pwd)"
BINARYNAME="http_examples"


case "$1" in
 start)
  if [ -e server.pid ]; then
   if ( kill -0 $(cat server.pid) 2> /dev/null ); then
    echo "The server is already running, try restart or stop"
    exit 1
   else
    echo "server.pid found, but no server running. Possibly your previously started server crashed"
    echo "Please view the logfile for details."
    rm server.pid
   fi
  fi
  if [ "${UID}" = "0" ]; then
   echo WARNING ! For security reasons we advise: DO NOT RUN THE SERVER AS ROOT
   c=1
   while [ "$c" -le 10 ]; do
    echo -n "!"
    sleep 1
    c=$(($c+1))
   done
   echo "!"
  fi
  echo "Starting the  server"
  if [ -e "$BINARYNAME" ]; then
   if [ ! -x "$BINARYNAME" ]; then
    echo "${BINARYNAME} is not executable, trying to set it"
    chmod u+x "${BINARYNAME}"
   fi
   if [ -x "$BINARYNAME" ]; then
    export LD_LIBRARY_PATH="${LIBRARYPATH}:${LD_LIBRARY_PATH}"     
    "./${BINARYNAME}" ${COMMANDLINE_PARAMETERS} > /dev/null &
     PID=`pidof ${BINARYNAME}`
    ps -p ${PID} > /dev/null 2>&1
    if [ "$?" -ne "0" ]; then
     echo "  server could not start"
    else
     echo $PID > server.pid
    fi
   else
    echo "${BINARNAME} is not exectuable, cannot start   server"
   fi
  else
   echo "Could not find binary, aborting"
   exit 5
  fi
 ;;
 stop)
  if [ -e server.pid ]; then
   echo -n "Stopping the   server"
   if ( kill -TERM $(cat server.pid) 2> /dev/null ); then
    c=1
    while [ "$c" -le 300 ]; do
     if ( kill -0 $(cat server.pid) 2> /dev/null ); then
      echo -n "."
      sleep 1
     else
      break
     fi
     c=$(($c+1)) 
    done
   fi
   if ( kill -0 $(cat server.pid) 2> /dev/null ); then
    echo "Server is not shutting down cleanly - killing"
    kill -KILL $(cat server.pid)
   else
    echo "done"
   fi
   rm server.pid
  else
   echo "No server running (server.pid is missing)"
   exit 7
  fi
 ;;
 restart)
  $0 stop && $0 start ${COMMANDLINE_PARAMETERS} || exit 1
 ;;
 status)
  if [ -e server.pid ]; then
   if ( kill -0 $(cat server.pid) 2> /dev/null ); then
    echo "Server is running"
   else
    echo "Server seems to have died"
   fi
  else
   echo "No server running (server.pid is missing)"
  fi
 ;;
 *)
  echo "Usage: ${0} {start|stop|restart|status}"
  exit 2
esac
exit 0

Save the file. Try to run and stop the executable by using the script.
1
2
chmod +x server_startscript.sh
./server_startscript.sh start

you will see a file called server.pid is created inside the directory. lets stop the executable.
1
./server_startscript.sh stop

Now the server.pid file should be removed from the files.

Creating the systemd service


1
sudo vi /lib/systemd/system/http_examples.service

add the following content and make sure to replace the highlighted areas
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[Unit]
Description=http_examples
After=network.target
 
[Service]
WorkingDirectory=/home/altair/Desktop/server/Simple-Web-Server/build/
User=altair
Group=altair
Type=forking
ExecStart=/home/altair/Desktop/server/Simple-Web-Server/build/server_startscript.sh start
ExecStop=/home/altair/Desktop/server/Simple-Web-Server/build/server_startscript.sh stop
PIDFile=/home/altair/Desktop/server/Simple-Web-Server/build/server.pid
RestartSec=15
Restart=always
 
[Install]
WantedBy=multi-user.target

Once done, save and close the editor then enable the server and startup script with this command
1
2
3
sudo systemctl --system daemon-reload
sudo systemctl enable http_examples.service
sudo service http_examples start

That's it comment if you got any problems

Thursday, April 20, 2017

Install Redmine Project Management tool in Ubuntu 16.04

Install Redmine Project Management tool on Ubuntu 16.04



First of all we need to update our system ( run these as root )

1
2
apt update
apt upgrade


Install the passenger and the Nginx web server

1
2
3
4
5
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
sudo apt-get install -y nginx-extras passenger

Setup MySql Server


1
2
3
4
5
6
sudo apt-get install mysql-server
mysql -p  #Enter the root password that you have entered at the setup of the mysql server
create database redmine; #Type in the mysql console
grant all privileges on redmine.* to 'red'@'%' identified by "123"; #Type in the mysql console
flush privileges;  #Type in the mysql console
exit;  #Type in the mysql console


Install prerequisites

1
apt install libmysqlclient-dev imagemagick libmagickwand-dev libcurl4-openssl-dev git-core subversion libssl-dev libreadline-dev


Creating a non sudo user to run the ruby project

1
2
useradd -m redmine -g www-data
passwd redmine


login to the redmine user account

1
su --login redmine


Install Ruby version 2.3.0

1
2
3
4
5
6
7
8
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.profile
echo 'eval "$(rbenv init -)"' >> ~/.profile
source ~/.profile
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenv install 2.3.0
rbenv rehash
rbenv global 2.3.0

Get the redmine current stable version ( 2017-04-20 ) from the SVN

1
2
3
4
svn co https://svn.redmine.org/redmine/branches/3.3-stable redmine-3.3
cd redmine-3.3
cp config/configuration.yml.example config/configuration.yml
cp config/database.yml.example config/database.yml


Edit the database configuration file (Change production settings  username,databasename,password,host)


1
vi config/database.yml



Configure the redmine


1
2
3
4
5
gem install bundler
bundle install --without development test
bundle exec rake generate_secret_token
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake redmine:load_default_data

Configure the nginx


1
2
exit
vi /etc/nginx/sites-available/default


change your vhost configuration like this ( change or add the highlighted fields remove any other unnecessary fields )



1
vi /etc/nginx/nginx.conf

and uncomment
include /etc/nginx/passenger.conf;



1
service nginx reload

Now browse the web server using web browser,
default username and password is admin admin


Angular cli 4 JQuery \ Other Plugins(Underscore js) setup

For this blog I will show you how to setup JQuery and Underscore js with using NPM and without using NPM Lets create a new Angular cli pro...