get GraphQl schema through Curl

Cli

Get graphql schema through curl

1
2
3
4
5
curl 'https://gitlab.com/api/graphql' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
--data-binary '{"query":"# Write your query or mutation here\nquery {\n __schema {\n mutationType {\n fields {\n name\n }\n }\n queryType {\n fields {\n name\n }\n }\n\t\ttypes{\n name\n }\n directives{\n name\n }\n }\n}"}' \
| jq

GraphQL query

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
query {
__schema {
mutationType {
fields {
name
}
}
queryType {
fields {
name
}
}
types{
name
}
directives{
name
}
}
}

https://gitlab.com/-/graphql-explorer



espeak - text to speech

Cli

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# install
brew install espeak

espeak "Nice to meet u?"

# female #5
espeak -v f5 "Nice to meet u?"

# male #2
espeak -v m2 "Nice to meet u?"

# 中文
espeak -v zh "中文"

# slow
espeak -s 80 -v m2 "Nice to meet u?"

# read and write a file
espeak -f foo.txt -w foo.wav

How to use telnet to test your web server

Cli

Usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# telnet ${YOUR_SERVER} ${PORT_NUMBER}
telnet localhost 80
telnet_mode>_

# example 1: telnet www.google.com 80
GET / HTTP/1.1
Host: www.google.ccom
[ENTER]
[...HTTP_RESPONSE]

# example 2: telnet localhost 8080
GET /controller/action?a=b HTTP/2
Host: localhost
User-Agent: Mozilla/5.0 AppleWebKit/500.0 Chrome/60.0
[ENTER]
[...HTTP_RESPONSE]

Docker for PHP Laravel (Dockerfile and docker-compose.yaml)

Php

Dockerfile

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
FROM php:7-apache

WORKDIR /var/www/html

# git: for php package barryvdh/laravel-debugbar
# zip, unzip: for composer download from dist
# libmagickwand-dev: for intervention/image and php7-imagick
# ssh: ssh client for git-ssh
RUN apt-get update && apt-get install -y \
$PHPIZE_DEPS \
ca-certificates \
curl \
xz-utils \
git \
zip unzip\
libzip-dev \
libmagickwand-dev \
ssh \
--no-install-recommends && rm -r /var/lib/apt/lists/*

# imagick: for intervention/image
RUN pecl install imagick

# pdo_mysql, bcmath: officail laravel requirement
# fileinfo, imagick: for php package intervention/image
RUN docker-php-ext-install pdo_mysql bcmath zip
RUN docker-php-ext-enable imagick


RUN a2enmod rewrite

COPY apache2.000-default.conf /etc/apache2/sites-enabled/000-default.conf

# xdebug
#RUN apt-get update &&\
# apt-get install --no-install-recommends --assume-yes --quiet ca-certificates curl git &&\
# rm -rf /var/lib/apt/lists/*
#
#RUN pecl install xdebug && docker-php-ext-enable xdebug

docker-compose.yaml

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
version: '3'

services:
# docker run --rm -it --net myNetwork -v $(pwd):/var/www/html -p 10080:80 --name localhost-cms-backend-v2 localhost/cms_backend_v2
api:
# container_name: localhost-cms-backend-v2
build: .
ports:
- "10080:80"
volumes:
- .:/var/www/html
- ~/.ssh:/root/.ssh
db:
# container_name: db
image: mariadb:10.3.15-bionic
ports:
- "13306:3306"
# volumes:
# - ../../Storage/mysql:/var/lib/mysql
environment:
- MYSQL_DATABASE=my_db
- MYSQL_ROOT_PASSWORD=123456!

# docker run --name redis --net myNetwork -d -t -p 6379:6379 redis
redis:
# container_name: redis
image: redis:5.0.5-stretch
ports:
- "6379:6379"

apache2.000-default.conf

1
2
3
4
5
6
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/html/public # set default doccument root to `/var/www/html/public`
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


PHP Blue-Green deployment tools - deployer

Php

Requirement of deployment server (production server)

1
apt-get install -y git php

Deployer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# install via web
curl -LO https://deployer.org/deployer.phar
chmod +x deployer.phar

# or install via composer
composer global require deployer/deployer
composer require deployer/dist --dev
php vendor/bin/dep

# minor update and major upgrade
./deployer.phar self-update
./deployer.phar self-update --upgrade

# simple usage
./deployer.phar init # create config file
./deployer.phar deploy server-production # deploy to production server
./deployer.phar run 'ifconfig' # run ad-hoc command at remote server

Sample of deployer.php

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
<?php
namespace Deployer;
require 'recipe/common.php';

set('repository', 'https://github.com/calvinlam0124/cron-mysqldump.git');
set('deploy_path', '~/deployer/' . basename(get('repository'), '.git'));
set('default_stage', 'server-test');


// Hosts
host('localhost')
->user('root')
->port(22000)
->stage('server-test')
->forwardAgent();

host('server2.localhost')
->user('root')
->port(22000)
->stage('server-production')
->forwardAgent();

// Tasks
desc('Deploy your project');
task('deploy', [
'deploy:info',
'deploy:prepare',
'deploy:lock',
'deploy:release',
'deploy:update_code',
'deploy:shared',
'deploy:writable',
'deploy:clear_paths',
'deploy:symlink',
'deploy:unlock',
'cleanup',
'success'
]);

// [Optional] If deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

reference

https://deployer.org/docs/examples/inventory.html


Create a private composer package repo

Php

Source code

touch “src/HelloWorld.php”

1
2
3
4
5
6
7
<?php
namespace CalvinLam;
class HelloWorld{
public function say(){
echo "Hello world!";
}
}

touch “composer.json”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"license": "proprietary",
"name": "calvinlam/hello-world",
"description": "calvinlam's test composer package",
"minimum-stability": "dev",
"require": {
},
"autoload": {
"psr-4": {
"CalvinLam\\": "src/"
}

}
}

Develop your package locally

1
composer dump-autoload # or composer install
1
2
3
4
<?php
require("vendor/autoload.php");
\CalvinLam\HelloWorld::say();
echo "done";

Download your private package

  • config your private repo OAuth access
  • touch composer.json
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    {
    "repositories": [
    {
    "url": "https://bitbucket.org/calvin-lam/php-composer-module-hello-world.git",
    "type": "git"
    }
    ],
    "require": {
    "calvinlam/hello-world": "dev-master"
    },
    "config": {
    "bitbucket-oauth": {
    "consumer-key": "xxxxx",
    "consumer-secret": "yyyyyy"
    }
    }
    }

git usage

part-1

General

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
# add new file
git add [YOUR_FILE] [YOUR_FILE] [YOUR_FILE]
# add new files
git add .
# commit
git commit -m "[YOUR_COMMIT_MESSAGE]"
# reset a commit
git reset HEAD
# push your commit(s) to repo
git push
# pull files from repo
git pull


# stash
git stash
git stash list
git stash pop
git stash drop

# discard the changes to one file in the repo
git checkout -- path/to/the/file.txt

# show file changes
git diff path/to/the/file.txt

git amend

1
2
3
# update last commit message
git add add-to-last-commit.file
git commit --amend

Branch

1
2
3
4
5
6
7
8
9
10
11
12
13
# list branch
git branch
# switch branch
git checkout [BRANCH_NAME]
# create branch
git checkout -b [NEW_BRANCH_NAME]

# rename
git branch -m rename-from rename-to

# remove
git branch -d the_local_branch
git push origin --delete the_remove_branch

Export / Archive

1
2
git checkout-index --prefix=git-export-dir/ -a
git archive --format zip --output zipfile.zip master

git log

1
2
3
git log
git reflog
git reset HEAD@{index}

Merge

1
2
git checkout Dev
git merge --no-ff -m 'Merged in feature/Malls-Controller (pull request #1)' remotes/origin/feature/Malls-Controller

git flow

1
2
3
4
5
6
7
# install
brew install git-flow

git flow init
git flow feature start some_awesome_feature
git flow feature finish some_awesome_feature
git flow feature publish some_awesome_feature

git branch up to date

1
2
3
4
5
git checkout newBranch
git fetch
git merge origin/master
# reference
https://stackoverflow.com/questions/19758915/keeping-a-branch-up-to-date-with-master