Skip to main content

Posts

Showing posts from 2021

Working With Gulp and PM2

Gulp is one of the popular task runner tools which can be integrated and works well with various helper tools for code development. I've previously created a post about how Gulp can be interoperated with Nodemon . In this post, I try to show how it can work along with PM2. PM2 is a powerful process manager that can run on multiple platforms and support a variety of technologies. PM2 is quite different from Nodemon. Nodemon specifically only focuses on monitoring of Node.js application. On the other hand, PM2 is a process manager with rich features for maintaining many application processes in a time, even with support for clustering mechanism. Besides, PM2 is usually implemented as a daemon program, so we will need a different approach to integrate it with Gulp. For instance, we use Gulp to do some stuff such as linting, translating, or file copying after each code changes. After Gulp runs its main tasks, we will instruct Gulp to restart or stop the application process which is

Levi Ackerman, Nothing Left

Who is your favorite character in Attack on Titan?

Specify Different Certificates To Access Different Git Repositories

Besides HTTPS, we can make a connection to a Git repository using SSH. For authentication, we should store our public key on the remote host, and set the remote Git URL on our host to the correct address for SSH connection. For example: git remote set-url myremote ssh://git@myremotegit.com/myrepo.git Common SSH client tools provide a specific parameter to set which private key should be used for authentication. For example, the OpenSSH client provides -i parameter to specify the location of the private key that will be used. Meanwhile, common Git client tools may not provide it. When we run a Git command, the tool will look for a private key stored in the default directory which is ~/.ssh/id_rsa . We can resolve this issue by setting up a configuration file that will be stored in ~/.ssh/config . For instance, this is a sample of a configuration file. Host myremotegit HostName myremotegit.com User git IdentityFile C:\\keys1\\id_rsa IdentitiesOnly yes Host bitbucket-com

Create A GraphQL Server Using Express and Apollo Server

GraphQL can be described as a query language for API. GraphQL was initiated by Facebook developers when they tried to build a better data fetching mechanism for their mobile application. By using GraphQL, frontend developers can request any data from the backend server with specified format and properties based on their actual needs. Creating a server that has support for handling GraphQL-based requests has become easy nowadays. Express as a de-facto framework for building HTTP server has the capability to be integrated with Apollo Server which is a popular GraphQL server. Minimal modules that we need are express , graphql , and apollo-server-express . For instance, we will set up a project and build the requirements. We utilize ESM syntax for building the sample program. Firstly, we initiate the project and install the dependencies. mkdir express-graphql cd ./express-graphql yarn init -y yarn add express graphql apollo-server-express Because we use ESM syntax, we need to set the

Running Docker Command Inside A Docker Container

But, why do we need to do this? There are several occasions that make you want to perform this action, especially if you are working on the development of a continuous delivery procedure. For example: You want to set up a closed environment (like a container) for a software testing process that requires external applications which can be run as containers. You want to build an application in a container then deploy it on another host using Docker API only, without the need for shell command execution. There are two common methods to achieve this objective. The first is by binding the Unix socket of the running Docker Engine into the container. The second is by installing a specific Docker Engine inside the container. For instance, we will run a container based on an image of Docker 20.10. We can run the following command. docker run -v /var/run/docker.sock:/var/run/docker.sock -it --rm docker:20.10 Now, you can run any  docker command inside the container that you just have

Utilizing Worker Thread in Node.js

The worker thread module has already become a stable module on Node.js version 12. This module enables us to run multiple Node.js processes in parallel using thread. In the past, we couldn't do this easily. We probably ended up utilizing cluster or spawning child process. The difference in utilizing thread is that we have shareable resources (memory). The main and its child threads can communicate and pass the operation results directly with a concept of message passing. Child thread is usually used for distributing computation load in an application. The main thread may migrate a certain process to a child thread which is run a computational-expansive and asynchronous process. For instance, the following code shows how we can create a worker thread for a file reading process then send the result to the main thread. // module.js const { Worker, isMainThread, parentPort, workerData } = require('worker_threads'); if (isMainThread) { // if it is accessed as main threa

Setting Up Docker Context

When we want to run a container on a remote Docker Engine host, we can utilize the context feature of Docker. Context allows us to maintain information of several Docker Engine hosts to be remotely accessed from our local Docker Engine host. Adding the record is done by running the following command. docker context create yourContextName --docker "host=ssh://user@remote.host" The connection utilizes SSH protocol so that we need to generate keys for establishing communication with the remote host. After storing the public key value on the remote host, we can spawn a new SSH agent on the current session on our host and add the private key into the agent. eval $(ssh-agent -s) cat /path/to/private/key | ssh-add - Before we can access the remote Docker API, we need to add the remote keys information to our ~/.ssh/known_hosts file by making an SSH connection for the first time or using ssh-keyscan . Now, we can access remote Docker API by specifying the context on the lo

Levi vs Beast Titan

 ... or it is Beast Levi vs Titan

Enabling Imagick to Read or Manipulate PDF File

Imagick is one of the popular tools for manipulating image files. Some popular languages such as PHP and Node.js have provided libraries that can be used for manipulating images based on Imagick. One of the common use cases for using Imagick is for generating a thumbnail from an image or PDF file. In PHP, we can install the PHP Imagick module by running the following command. apt install php-imagick Then, we can verify the installation by running this command. php -m | grep imagick For example, we want to generate a thumbnail image for a PDF file in PHP. We can use the following script. <?php $im = new Imagick(); $im->setResolution(50, 50); // set the reading resolution before read the file $im->readImage('file.pdf[0]'); // read the first page of the PDF file (index 0) //$im = $im->flattenImages(); // @deprecated // handle transparency problem $im = $im->mergeImageLayers( Imagick::LAYERMETHOD_FLATTEN ); $im->setImageFormat('png'); $im->write

Securing Redis to Be Accessed From All Interfaces

Redis can bind to all interfaces with bind * -::* configuration. But, Redis also enables protected-mode by default in its configuration file. It will make bind * -::* configuration ineffective because the protected-mode requires both to explicitly state the binding interfaces and to define user authentication. The unsecured way is to set protected-mode no in the configuration. It will make our Redis server becomes accessible from any interfaces without authentication. It may be fine if we deploy our Redis server in a closed environment such as in a containerized one without exposing and pointing any port to the Redis service port. So that, the service can only be accessible from other services in the container's network. The recommended way is to keep protected-mode yes in the configuration. Then, we need to add a new user authentication configuration and limiting access for the default user. A default user is a user with no assigned name when the client tries to connect

Managing Node.js-based Web Project Using Gulp and Nodemon

In building a website, there are two main components which are frontend and backend components. If we build a website based on Node.js for the backend side, and of course Javascript and CSS for the frontend side, we should handle our codes in the project differently. We may perform a linter and the Typescript transpiler for our Node.js codes. While on the frontend side, we may additionally minify and bundle the project styles and scripts. The backend program needs to be restarted when there are any changes in the codes and transpiler is performed. The frontend codes need to be re-bundled when there are also any changes. Nodemon is a tool that is designed to restart a Node program when a change of the program codes is detected. Gulp is a task runner that can be utilized to watch any changes in the program codes and perform specific tasks. In this post, we will make a transpiler is run and the backend program is restarted when there are any changes. We will also compile our Sass-

Several Useful Linux Tools

The following tools may have been installed in your Linux because some are basic tools. But, if we installed any Linux distribution from the Docker registry which is shipped with only minimal programs, these following tools may be not available by default. net-tools This tool provides tools for network-related tasks such as ifconfig . software-properties-common If you want to enable add-apt-repository command, this tool is required. nano This text editor is usually already available. ca-certificates A deb package that contains certificates provided by the Certificate Authorities. It also contains an updater tool that can be used as a cronjob if needed. gnupg2 GNU Privacy Guard is GNU's tool that can be used to encrypt data and to create digital signatures. GnuPG is a complete replacement for PGP. It includes an advanced key management facility and is compliant with the proposed OpenPGP Internet standard. openssh-client Tools for generating authentication keys and

Persisting Data and Replication in Redis

As we know, Redis is an in-memory key-value store database. If our data is stored in our host memory (RAM), how can we restore all values from the last state of our system in case of system reboot or power outages? Redis provides two options for persisting our data. The first is by creating a snapshot and the second is by appending each write action into a file. The second is also called the append-only-file (AOF) method. Applying those options is as trivial as updating several lines of the Redis configuration file. Redis performs snapshotting with certain rules by default. Enabling the auto-snapshot method with different rules is done by configuring the following lines in the /etc/redis/redis.conf file. save 300 10 save 30 1000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /var/lib/redis The line save 300 10 means snapshot will be automatically updated in the background if at least 10 writes have occurred within 300 seconds. The li

Utilizing HTTP/2 Push for Faster Page Load in Node.js

HTTP/2 has several advantages over HTTP/1 that I've mention in my earlier post . In this post, I want to show how push-request can be performed using Node.js to create an HTTP/2 server. Push request is used to push static files such as scripts and styles so that the client can consume those static files as soon as possible without the need to request them first. In this example, several built-in Node modules are required and an external module for ease of content-type setting named mime . Let's install it first. npm init npm i --save mime HTTP/2 encodes all headers of a request and it presents several new headers for identifying a request such as :method and :path . For more clarity, I call some constants related to the HTTP/2 header from the http2.constants property. Let's create the server.js file. const http2 = require('http2'); const { HTTP2_HEADER_PATH, HTTP2_HEADER_METHOD, HTTP2_HEADER_CONTENT_TYPE, HTTP2_HEADER_CONTENT_LENGTH, HTTP2_

Utilizing Ref Attribute for Referring HTML or React Elements

For accessing the DOM node of an HTML element or referring a React element, we can utilize the ref attribute. In React, we can utilize React.crateRef() function or React.useRef() hook for generating an object that has access to the referred DOM node. Several cases may be faced while we are developing React application. 1) Referring HTML Element . class RefHtml extends React.Component { constructor(props){ super(props); this.textInput = React.createRef(); } render() { return ( <div> <input type="text" ref={this.textInput} /> <button onClick={()=>{this.textInput.current.focus();}}>Focus</button> </div> ); } } 2) Referring React Element (Class Component) . class ReferredElement extends React.Component { constructor(props){ super(props); this.state = {status: "inactive"}; } setActiveState() { this.setState({status: "active"}); } render() {

Serving Single-Page React App with Docker

If you build a single-page application using React while your app only gets backend data through API access, it will be convenient to utilize the Create-React-App toolchain. For serving your app to the public, you actually just need to serve the generated static files. Serving static files can be easier when you utilize a container for shipping a web server. Initiate react project then build the app. npx create-react-app my-app cd my-app npm run build Create Nginx server configuration for your app site.conf . server { listen 80; server_name my-app.localhost; index index.html; root /app; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; try_files $uri $uri/ index.html =404; } Create a Dockerfile for building containerized web server. # syntax=docker/dockerfile:1 FROM nginx:latest WORKDIR /app COPY ./build . COPY ["./site.conf", "/etc/nginx/conf.d/"] Build the image and run the container. docker build -t my-servic

Create HTTP/2 Client-Server on Node.js

Node.js has support for building HTTP2 communication. HTTP2 has better efficiency compared to HTTP1.1 as described in my earlier post . In this example, we will build a client-server program that utilizes the HTTP2 module. Create server application server.js . const http2 = require('http2'); const server = http2.createServer(); server.on('stream', (stream, headers) => { stream.respond({ status: 200, 'content-type': 'text/html' }); stream.end('<html><head><title>Hello</title></head><body><p>Hello World</p></body></html>') }); server.listen(6000); Create client application client.js . const http2 = require('http2'); const client = http2.connect('http://localhost:6000'); const request = client.request({ ':path': '/' }); let str = ''; request.on('data', (chunk)=>{ str+=chunk; }); request.on('end',

Film Thriller, Horror, dan Misteri Asia Terbaik

Dulu saya sudah pernah menulis beberapa film misteri yang menurut saya bagus di " Film Misteri Asia Terbaik ". Ini adalah beberapa film lainnya yang sangat cocok ditambahkan ke dalam daftar. 1. The Wailing (2016) Beberapa peristiwa kematian dan pembunuhan terjadi di sebuah desa. Seorang polisi setempat mulai menyelidiki kasus-kasus tersebut. Seorang kakek yang tinggal sendiri di gunung dicurigai sebagai penyebabnya. Namun, dugaan baru muncul bahwa ada makhluk lain yang menghantui yang mampu mencelakai dukun yang sedang dimintai bantuan untuk menyelesaikan masalah. Siapa yang benar dan siapa penyebab kasus-kasus di desa itu terjawab di akhir film. 2. Bedevilled (2010) Seorang perempuan berlibur ke tempat tinggal masa kecilnya di desa di sebuah pulau. Dia bertemu dengan teman perempuannya di masa kecil yang masih menetap di desa itu yang telah mengalami banyak diskriminasi oleh warga desa itu. Akibat berbagai kejadian dan besarnya rasa kecewa, sang teman berubah menjadi seorang

Create Multi-stage Dockerfile for Development and Production

By utilizing container technology like Docker, we can have an identical environment for development and production. But, working in a development environment requires us to be able to change the source codes directly. We can define multi-stage procedures in the Dockerfile and specific commands to run the container. For example, we have a Node.js program that will be shipped using a container. 1. Create a Dockerfile with stages for development and production. # syntax=docker/dockerfile:1 FROM node:14.17.1 as base WORKDIR /app COPY ["package.json", "package-lock.json", "./"] FROM base as development ENV NODE_ENV=development RUN npm ci COPY . . CMD ["nodemon", "-L", "app.js"] FROM base as production ENV NODE_ENV=production RUN npm ci --production COPY . . CMD ["node", "app.js"] In this example, the program for development is run using nodemon  therefore we need to install nodemon first by npm i -D node

Film Action dan Misteri Asia Terbaik

 Ini adalah beberapa film action dan misteri asia yang bagus menurut saya.  1. New World (2013) Sekelompok polisi ditugaskan menyamar sebagai bagian dari kelompok mafia untuk mengungkap skandal korupsi yang ada. Namun, hubungan yang kemudian terbangun kuat di dalamnya, membuat seorang polisi meragukan siapa yang benar, siapa temannya yang sesungguhnya, dan siapa yang harus dibela. 2. A Hard Day (2014) Film ini menceritakan seorang polisi yang mengalami sebuah kecelakaan saat mengendarai mobilnya yang kemudian malah menggiringnya ke sebuah kasus besar yang tidak terduga dan membuatnya harus berhadapan dengan musuh yang menyeramkan. Benar-benar hari yang berat. 3. Alive (2020) Ini adalah film dengan tema zombie. Ada seorang pemuda yang terperangkap di dalam komplek apartemennya ketika serangan zombie datang. Dia sempat frustasi karena tidak adanya pertolongan, namun ternyata ada survivor lain yang akhirnya dapat membantu dia untuk menyelamatkan diri dan tetap hidup. Salah satu aktris dal