VSCOde regex

Find functions which don’t have $credits variable

GetSQLValueString((?!\$credits)(\$.*),

Add the $credits variable to the function

GetSQLValueString($credits, $1,
mysqli_real_escape_string\((?!$credits)(\$.*)\);
mysqli_real_escape_string($credits, $1);

Convert MP3 to m3u8 for streaming

Check the video resolution

ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 video1.mp4

CONVERT TO M3u8

ffmpeg -i video1.mp4 -profile:v baseline -level 3.0 -s 712x480 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls index.m3u84

STREAM WITH jwplayer

<div id="player">Loading the player...</div>
<script>
// Setup the player
const player = jwplayer('player').setup({
  file: 'HLS/test/index.m3u8',
  image: 'HLS/test/index.png'
});

// Call the API
const bumpIt = () => {
  const vol = player.getVolume();
  player.setVolume(vol + 10);
}
bumpIt();
</script>

Advertisement

5 JavaScript features to know

JavaScript brings tons of new features and it is hard to track of everything. I would like to share 5 features that incredibly useful.

1. Nullish Coalescing : Check undefined or null using ??

function calculatePrice(price, taxes, description) {
  taxes = taxes ?? 0.05
  description = description ?? "Default item"
  const total = price * (1 + taxes)
  console.log(`${description} with tax: CAD${total}`)
}



calculatePrice(100, 0.05, "First item") /* First item with tax: CAD105 */
calculatePrice(100, 0, "Second item") /* Second item with tax CAD100 */
calculatePrice(100, undefined, undefined)/* Default item with tax CAD105 */

2. Styling Console Log: add styles using %c

console.log(`%c${description} with tax: %cCAD${total}`, "font-weight: bold; color: green;", "color: red")

3. Optional Chaining: using ?

class Person {
  contructor(name, address, hobbies) {
    this.name = name
    this.address = address
    this.hobbies = hobbies

  }

  print() {
    console.log(this)
  }
}


function printPersonStreet(person) {
  /* check whether person is defined, then person.address is defined and person.address.street is defined */
  console.log(person?.address?.street)
}

const sugith = new Person("Sugith", {street: "1234 Edmonton St", city: "Edmonton"}, ["Cricket", "Badminton"])

sugith.print?.() /* print function will execute if it is available */

console.log(sugith.hobbies?.[0]) /*this will console log only if there is a hobbies array and the array has values*/

console.log(sugith.hobbies?.[0].toLowerCase())

printPersonStreet(sugith)

4. Object Shorthand: If you want to use the same key as the value variable.

const name = "Sugith"
const favouriteFood = "Rice"

const sugith = {
  name, favouriteFood
}

console.log(sugith)

5. Defer/Async Loading: add defer to your script tag and you can place it in the head instead of the end of the body

<script src="script.js" defer></script>
This will run the script after the page renders

Laravel 8 with auth

laravel new laravel-vue

cd laravel-vue

--update the .env file with the database connection info 
-- update below settings to make sure the session is saving on a database.

CACHE_DRIVER=database
SESSION_DRIVER=database

php artisan migrate

composer require laravel/breeze --dev

php artisan breeze:install

npm install

npm run dev

php artisan session:table

php artisan cache:table 

php artisan migrate

php artisan serve






TURN ON debugger bar

composer require barryvdh/laravel-debugbar --dev


.env file

APP_DEBUG=true




php artisan make:model Company -m

Nodejs deployment with PM2, NGINX and Let’s Encrypt

Install pm2

npm install -g pm2
#start the service with pm2
pm2 start [FILE_NAME]
#check the status 
pm2 status
#restart the service using pm2
pm2 restart [FILE_NAME]
#check your logs
pm2 logs
#flush/clear the logs
pm2 flush
#start the app when the server restarts
pm2 startup ubuntu

Before using NGINX

Turn on the firewall

:~# ufw status
Status inactive

#enable the firewall
ufw enable
ufw allow ssh

#Every port will be closed now. Because we activated the firewall
#We are going to turn on port 80 and 443

ufw allow http
ufw allow https

#Check the firewall status
ufw status

#WE COMPLETED THE BASIC FIREWALL
#Setup NGINX

#install NGINX
sudo apt install nginx

#configure
sudo nano /etc/nginx/sites-availble/default

Add the following to the location part of the server block

server_name yourdomain.com www.yourdomain.com;

location / {
  proxy_pass http://localhost:5000;
  proxy_http_version 1.1;
  proxy_set_header Upgrade #http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}

#Check NGINX config
sudo nginx -t

# Restart NGINX
sudo service nginx restart

Add domain in Digital Ocean

In digital ocean, go to networking and add a domain

Add an A record for @ and for www your droplet

Register and/or setup domain from registrar

Go to your preferred domain registrar

Choose “custom namservers” and add these 3

ns1.digitalocean.com
ns2.digitalocean.com
ns3.digitalocean.com

Add SSL with LetsEncrypt

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

#Only valid for 90 days, test the renewal process with
certbot renew --dry-run

#Renew automatically- LetsEncrypt recommends that subscribers renew every sixty days.

#Add line into crontab -e
@monthly certbot renew

.Net Core MVC identity with PostgresSQL

Create the MVC project with authentication using visual studio.

Install these two libraries using NuGet

  • Npgsql.EntityFrameworkCore.PostgreSQL
  • Npgsql.EntityFrameworkCore.PostgreSQL.Design

Change the Providers in Startup.cs file

options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

Change the connection string in appsettings.json

"DefaultConnection":"Server=localhost;Port=5432;Database=example;Username=postgres;Password=postgres;Pooling=true"

Run DB Migration

PM>Update-Database

When trying to run the migration, cause you an error like

System.NullReferenceException: Object reference not set to an instance of an object. System.InvalidOperationException: No mapping to a relational type can be found for property ‘Microsoft.AspNetCore.Identity.IdentityUser.TwoFactorEnabled’ with the CLR type ‘bool’.

Delete the entire migration directory & regenerate a new initial migration

PM>Add-Migration Initial

This will create the migration table in your database. Then run

PM>Update-Database