Curl test your http server

Web

Basic usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# get /index
curl http://localhost

# Post data
curl -d "param1=value1&param2=value2" -X POST http://localhost/data

# Post file
curl -d "@data.txt" -X POST http://localhost/data

# -H set header
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost/data

# When you got error, try http1.1
# curl: (92) HTTP/2 stream 1 was not closed cleanly: REFUSED_STREAM (err 7)
curl --http1.1 http://localhost

# When you got this, try -k
# curl: (60) SSL certificate problem: self signed certificate
curl -k https://localhost

# login
curl --http1.1 -k -d '{"username":"abc@abc.com","password":"123456","role":"user","type":"json"}' -X POST -H "Content-Type: application/json" https://localhost/auth

Reference

https://gist.github.com/subfuzion/08c5d85437d5d4f00e58




Google Serverless with Firebase quick start

Firebase

Basic usage

1
2
3
4
5
6
7
8
9
10
11
# install firebase-cli
yarn global add firebase-tool

# login
firebase login

# create project, that will generate files
firebase init

# deploy your app
firebase deploy

API

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
// cdn
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-firestore.js"></script>

// js
var db = firebase.firestore();
firebase.initializeApp({
apiKey: 'AIzaSyAaJ-xa21TAN8f8PbEubfMTh1234567890',
authDomain: 'helloworld-11594.firebaseapp.com',
projectId: 'helloworld-11594'
});

// read data
db.collection("device").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(doc.data());
});
});

// write data
db.collection("user").add({
first: "Ada",
last: "Lovelace",
born: 1815
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});

Reference

https://console.firebase.google.com
https://firebase.google.com/docs/firestore/query-data/listen


Deploy your docker container to AWS

Docker

1. Installation and Configure AWS

1.1 install aws-cli

1
sudo pip3 install awscli

1.2 Create IAM at aws-console

1.3 Configure your IAM token

1
2
3
4
5
6
7
8
9
10
aws configure

AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: ap-southeast-1
Default output format [None]: ENTER

# that will generate file in
~/.aws/config
~/.aws/credentials

2. Create Docker-Machine

The VPC-ID and Subnet-ID should copy from AWS-console.
2.1 Create a docker-machine

1
2
3
4
5
6
7
8
9
docker-machine create \
--driver amazonec2 \
--amazonec2-access-key AKIAIOSFODNN7EXAMPLE \
--amazonec2-secret-key wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY \
--amazonec2-region ap-southeast-1 \
--amazonec2-subnet-id subnet-16b8e272 \
--amazonec2-vpc-id vpc-b263fdd6 \
--amazonec2-zone a \
ap-southeast-1-docker-machine-1

2.2 Active it

1
2
3
docker-machine ls
docker-machine env ap-southeast-1-docker-machine-1
eval $(docker-machine env ap-southeast-1-docker-machine-1)

2.3 Build and run your project

1
2
3
4
5
6
cd /your/project/root
docker build -t ap-southeast-1/project .
docker run ap-southeast-1/project

docker ps
docker-machine ls

2.4 DONT FORGET TO CONFIGT AWS SECURITY GROUP FOR YOUR APPLICATION

Reference

https://docs.docker.com/machine/drivers/aws/#options


Deploy container to other host througt docker-machine

Docker

Prepare

You can depoly container to AWS or DigitalOcean through docker-machine.

Create

1
2
3
4
5
6
docker-machine create \
--driver generic \
--generic-ssh-user=ec2-user \
--generic-ip-address=192.168.1.200 \
--generic-ssh-key ~/.ssh/id_rsa \
vm-192-168-1-200-sandbox

List

1
docker-machine ls

Switch / Active

1
2
3
4
5
6
7
8
# (optional) get the env of machine
docker-machine env vm-192-168-1-200-sandbox

# set active (do any docker command you want)
eval $(docker-machine env vm-192-168-1-200-sandbox)

# unset (back to localhost mode)
eval $(docker-machine env -u)

Deploy

1
2
3
cd /your/project/directory
docker build -t project-name .
docker run -p 80:80 project-name

Reference

https://docs.docker.com/machine/drivers/generic/#options




Pug template syntax

Pug

Basic Element

pug

1
a(class='button' href='google.com') Google

html

1
<a class="button" href="google.com">Google</a>

HTML

input
pug

1
2
3
4
5
6
7
8
9
10
doctype html
html(lang="en")
head
title= pageTitle
link(href='https://unpkg.com/sakura.css/css/sakura.css', rel='stylesheet')
body
h1 Pug - node template engine
#container.col
Pug is a terse and simple templating language with a
strong focus on performance and powerful features.

html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pug</title>
</head>
<body>
<h1>Pug - node template engine</h1>
<div id="container" class="col">
<p>Pug is a terse and simple templating language with a strong focus on performance and powerful features.</p>
</div>
</body>
</html>

Varriable

pug

1
2
url = 'https://example.com/'
a(href=url) Another link

html

1
<a href="https://example.com/">Another link</a>

Escape

pug

1
div(data="<hello_world>")

html

1
<div data="&lt;hello_world&gt;"></div>

Inline style css

pug

1
a(style={color: 'red', background: 'green'})

html

1
<a style="color:red;background:green;"></a>

Mixin (function)

pug

1
2
3
4
5
6
mixin list(id, ...items)
ul(id=id)
each item in items
li= item

+list('my-list', 1, 2, 3, 4)

html

1
2
3
4
5
6
<ul id="my-list">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>

Cli

pug-cli

Reference

https://pugjs.org/api/getting-started.html


Node js template engine quick search

Node

Name Star Github Remark
Mustache 2457 https://github.com/mustache/mustache login-free, Haml like
Pug 16045 https://github.com/pugjs/pug That was formerly known as “Jade”, Haml like
olado/doT 3779 https://github.com/olado/doT fastest + concise
linkedin/dustjs 2694 https://github.com/linkedin/dustjs Maintained by LinkedIn
Template 7 459 https://github.com/nolimits4web/template7/ Support Framework 7
EJS 2138 https://github.com/mde/ejs Client-side support, complies with Express