Skip to main content

Posts

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',