Skip to main content

Posts

Showing posts with the label Programming

Deliver SaaS According Twelve-Factor App

If you haven't heard of  the twelve-factor app , it gives us a recommendation or a methodology for developing SaaS or web apps structured into twelve items. The recommendation has some connections with microservice architecture and cloud-native environments which become more popular today. We can learn the details on its website . In this post, we will do a quick review of the twelve points. One Codebase Multiple Deployment We should maintain only one codebase for our application even though the application may be deployed into multiple environments like development, staging, and production. Having multiple codebases will lead to any kinds of complicated issues. Explicitly State Dependencies All the dependencies for running our application should be stated in the project itself. Many programming languages have a kind of file that maintains a list of the dependencies like package.json in Node.js. We should also be aware of the dependencies related to the pla...

How To Measure Modularity

A module is a set of parts that can be used to build a more complex system. How parts can be set or grouped together is based on some considerations. How optimised our module or how good the modularity level of our system is, are our questions. Several aspects are very common when we want to measure the modularity of our system or software: cohesion , coupling , and connascence . Cohesion It is the indicator of whether we efficiently group some parts together. A cohesive module means all parts in the module are well coupled. If we break a cohesive module in our code into pieces or smaller modules, that will lead to an increase in coupling across modules and a decrease in the readability of the code. There are a few types of cohesion based on the cause of cohesiveness such as functional, sequential (input-output relation), procedural (execution order), logical, or temporal. One that is not strongly related to the functional aspect is logical cohesion. For example, we may...

Tools To Help You Create API Documentation

Nowadays, many paid and free tools can help us make beautiful API documentation for our software projects. These are a few of them with an explanation of each advantage. RapiDoc This open-source tool can generate API documentation based on OpenAPI specifications. So, if you already use the Swagger tool to generate your documentation, you can use the configuration and generate a new documentation page instantly. To use RapiDoc, we can create an HTML file that includes the Javascript library provided by RapiDoc or we can include it in a Javascript framework like React and Vue. It is very customisable, we can add custom HTML or markdown in the generated documentation, apply a dark theme or custom style, create custom methods, and many more. It also supports in page console to try an API request. ReadMe It is a service that can transform static API documentation into interactive developer hubs. Developer hubs mean it can mon...

Why DevSecOps Is Important

DevSecOps stands for development, security, and operations. By its name, we can guess it is more like DevOps with the integration of security tools. The more formal definition, it is an approach to design and automate the integration of security at every phase of the software development lifecycle. This term became more popular when many cloud providers and code management tools started to use the name in many places on their platforms. As it integrates security tools in every phase of SDLC and automates the process, this approach can help developers catch vulnerabilities early. Besides it can help us to ensure that our project aligns with regulatory compliance from the beginning. This state can lead to cost-effective software delivery by reducing time to market and can help organizations build a security-aware culture. Security become a concern of more companies nowadays as the increasing of cyber incidents. Traditional DevOps may lack in a few aspects. First, in traditio...

Create Effective Documentation for Software Project

As your software project grows, it may involve more contributors. If you build a platform that publishes APIs that can be consumed by the public, you may expect more users to use your platform. If you work on an internal project that involves many parties from several vendors, you may expect everyone can understand your project and collaborate well. In any scenario, effective documentation can help you achieve what you want. We should consider a  user-oriented design for our documentation which considers who will use our product and what goal our users pursue by reading the documentation. Sometimes, it can help us in developing the project itself by trying to see the project from a user perspective. These are types of common audiences and the information needed. Evaluators who examine whether the service or tool is useful. They need a high-level overview, a list of features, or expected benefits. New users who just learn the usage...

Invisible Closure Scope in Javascript

When we are maintaining variables in our Javascript code, we must already know about the scope that determines the visibility of variables. There are three types of scope which are block, function, and global scope. A variable defined inside a function is not visible (accessible) from outside the function. But, the variable is visible to any blocks or functions inside the function. When a function is created, it has access to variables in the parent scope and its own scope, and it is known as closure scope. For example, if we create a function (child) inside another function (parent), in the creation time the child function will also have access to variables declared in its parent. Another way to think of closure is that every function in JavaScript has a hidden property called "Scope", which contains a reference to the environment where the function was created. The environment consists of the local variables, parameters, and arguments that were available to the...

Utilise GraphQL and Apollo Client for Maintaining React State

One library that is quite popular to allow our application to interact with the GraphQL server is Apollo Client ( @apollo/client ). It has support for several popular client libraries including React. It also provides cache management functionality to improve the performance of the application when interacting with GraphQL. Rather than integrating another library to manage our application state, we can leverage what Apollo Client already has to maintain the state. The solution is achieved by creating a customised query to load the result from a local variable or storage. Then, the query is executed like other queries using useQuery() . The steps are as follows. Create a local query that will read data only from the cache. Create a cache that defines a procedure for the query to read data from local values. Call the query anytime we want to read the state value. For instance, we want to read the information of the current user that has successfully logged in...

Communicate Through RabbitMQ Using NodeJS and Fastify

If we have two or more services that need to talk to each other but it is allowed to be asynchronous, we can implement a queue system in our system using RabbitMQ. RabbitMQ server will maintain all queues and connections to all services connected to it. This post will utilize Fastify as a NodeJS framework to build our program. This framework is similar to Express but implements some unique features like a plugin concept and an improved request-respond handler. First, we need to create two plugins, one is for sending a message, another one is for consuming the sent message. At first, we will make it using a normal queue. There is one mechanism for how a queue works, it is like a queue in the real world. When there are five persons in a queue and three staff to handle the queue, one person is served only by one staff, there is no need for other staff to handle any person that has been served, and there is no need for a person to be handled repeatedly by other staffs. For example...

Detecting Main Module in ESM Package

ESM package works in a different fashion from CJS. ESM package imports required modules asynchronously and have no support for several functions related to system files and directories. ESM follows Javascript cores that are implemented in the browser. There is no such entity like __dirname , __filename , require() , and so on. If we want to detect whether a module is a main module that is being run in CJS, we can compare the value of require.module and module . If it is the same, the module is being run as the main module. Meanwhile, for a module in the ESM package, we can use the following approach. Get the value of process.argv[1] that contains information of the main file that is being called by Node. Get the value of import.meta.url that contains information of the module's location that is being accessed. Transform the information to have a similar format, then compare it. For example, we have a module in an ESM package, named module1.js . import { r...

Prototypal Inheritance in Javascript

Some programming languages support object-oriented approaches natively by providing a feature to create an object instance based on a class definition. Meanwhile, in Javascript, we know there is  class syntax, but it is just a kind of syntactic sugar that allows us to instantiate an object that can inherit some traits from another object or class definition in a similar fashion to other programming languages. In Javascript, a class is just a function with a  prototype property that maintain traits that can be passed down to other object instance. There are several ways to allow property inheritance in Javascript. Functional In this way, we utilize the Object.create() method built in Javascript. The steps are: Create an object as the prototype provider. Pass the prototype provider to the Object.create() method along with additional properties declaration if needed. For example, const provider = { sum: (a, b) => a + b }; const obj = Object.create( ...

Measuring Correlation

If we want to know a correlation between a boxer winning a match and using red pants, we can mathematically determine it based on past records of those conditions. There are four possible combinations of the conditions. The boxer loses and he doesn’t use red pants (00) The boxer loses and he uses red pants (01) The boxer wins and he doesn’t use red pants (10) The boxer wins and he uses red pants (11) For example, we have the following past records. Condition “00” is 23 times Condition “01” is 10 times Condition “10” is 45 times Condition “11” is 9 times Then, we can use the phi coefficient formula. The result of this formula is between -1 and 1. If the result is close to 0, it means the conditions have no correlation. If it is close to 1, it means the conditions are strongly correlated. Meanwhile, if it is negative, the correlation is strong but in the opposite way. The formula is as follows. phi = (n11 x n00) – (n01 x n10) / sqrt(n1X x n0X x nX1 x nX0) n1X represents the condition wh...

Double Invoking In React Application with Strict Mode Enabled

When we use create-react-app to generate our application source code, we will be provided with a base code that enables  React.StrictMode by default. This wrapper component doesn't render an HTML element but it performs useful functionality to verify that our application is safe for production. It checks for legacy or deprecated React functions or APIs, unexpected side-effects, and unsafe lifecycle. We can read the details about those functionalities on its documentation page . To help us spot side-effects, React will double-invoking several functions in our application such as render , constructor , functions passed in useMemo or useReducer , and so on. By double-invoking such functions, we can detect if unexpected results will show up. But, this process will be initiated only in development mode. When we ship our application in a production environment with Strict Mode enabled, double-invoking will never happen. This is why when we render a component that contains useEffect ...

Compress and Decompress File/Text Content using Zlib Module in Node.js

Zlib module provides compression functionality implemented using Gzip, Deflate/Inflate, and Brotli. Compression/decompression can be used by utilizing stream directly or read the text content first. Compressing Text Content const zlib = require('zlib'); let rawStr = '--hello--world--'; let compressedStr = ''; zlib.gzip(rawStr, (err, buffer)=>{ if (!err) { compressedStr = buffer.toString('base64'); } }); Decompressing Text Content const zlib = require('zlib'); let rawStr = ''; let compressedStr = 'H4sIAAAAAAAACtPVzUjNycnX1S3PL8pJ0dUFABFjTmQQAAAA'; let inputBuffer = Buffer.from(compressedStr, 'base64'); zlib.unzip(inputBuffer, (err, buffer)=>{ if (!err) { rawStr = buffer.toString(); } }); Compression Using Stream Pipeline const stream = require('stream'); const fs = require('fs'); const zlib = require('zlib'); stream.pipeline(fs.createReadStream(...

Run Python Scripts Using PHP in a Linux Environment

How to run python or other scripts using PHP or other popular web programming languages in a Linux environment? Problem We want to access the PHP program through a web server application (Apache/Nginx). The PHP program needs to run other scripts stored in the same machine. Sample Problem PHP program is located in /var/www/html/index.php Python program is located in /var/www/html/capture.py Python program will access Pi Camera, capture an image, and store it in /var/www/html/assets/photo.jpg Solution index.php should access and run capture.py script. The script is as follows: $output = shell_exec("python /var/www/html/capture.py"); Execution of external program using shell_exec() or exec() will block the program. The next line of PHP codes will be run after the external program returns an output. So, we shouldn't run an excessive amount of external programs. Clients will wait too long on their browser. If our application actually needs ma...

Zurmo CRM wtih Nginx and PHP 7

Zurmo is one of open source CRM application based on PHP. Current application isn't supported for PHP 7 environment because some application scripts still implement unsupported (obsolete) PHP function such as mysql_connect , mysql_fetch_array , etc. First, we must change some scripts which still implement unsupported PHP 7 functions. The scripts are: app\protected\commands\tests\unit\DatabaseCommandTest.php app\protected\core\tests\unit\DatabaseCompatibilityUtilTest.php app\protected\core\utils\DatabaseCompatibilityUtil.php You can download the scripts from HERE . Second, if we run Nginx server, we can use this following configuration. server {         listen 443 ssl;         root /path/to/zurmo ;         index index.php index.html;         server_name yourdomain.com ;         ssl_certificate /pat/to/yourdomain.crt ;         ssl_certifi...

Generate .SQLITE File from .SQL File in Windows

You can generate .SQLite file from .SQL file by using SQLite-CLI. For that purpose, you need to prepare some gears as the following. 1. Sqlite 32-bit DLL 2. Sqlite Tool for Windows (Command Line Tools) You can download all needs from here . After you download the items, you need to extract them into a directory for example "C:\\sqlite". Now, in you directory, you will have sqldiff.exe, sqlite3.def, sqlite3.dll, sqlite3.exe, and sqlite3_analyzer.eze. Next, you should add "C:\\sqlite" or your own location into PATH data in Windows environment variable so you can access "sqlite3.exe" from command prompt without full path declaration. In command prompt you can type sqlite3 then the following messages will appear. SQLite Messages Now, you are in SQLite command terminal. First, you need to open the SQL file. Your opened file will be assigned into an in-memory database. Then you can save that in-memory database into a SQLite file. Before you open...

Upgrading PHP 5 to PHP 7 in Ubuntu

PHP 7 comes with a new version of the Zend Engine, numerous improvements and new features such as: Improved performance: PHP 7 is up to twice as fast as PHP 5.6 Significantly reduced memory usage Abstract Syntax Tree Consistent 64-bit support Improved Exception hierarchy Many fatal errors converted to Exceptions Secure random number generator Removed old and unsupported SAPIs and extensions The null coalescing operator Return and Scalar Type Declarations Anonymous Classes Zero cost asserts Today (12 April 2016), latest Ubuntu release doesn't include PHP 7. You can install PHP 7 from third-party repository such as PPA. PPAs are not bound by the release schedules or policies of Ubuntu so they are free to change versions more frequently, among other things. Ondrey PPA is a popular way of staying more up to date with PHP. Ondrey is the official owner of the PHP tree in Debian, which is upstream from Ubuntu. To install PHP 7 in Ubuntu you can do the following: ...

Proxy Configuration for Git, NPM, and Bower

If you are in a suck place with proxy, you need to configure proxy setting for your repository and package manager tools such as git, npm, or bower. Git You can use this command to set configuration globally git config --global http.proxy http://username:password@proxy.server.com:8080 git config --global https.proxy https://username:password@proxy.server.com:8080 Command to unset git config --global --unset http.proxy git config --global --unset https.proxy Command to check the value git config --global --get http.proxy git config --global --get https.proxy NPM Command format npm config set <key> <value> npm config get <key> npm config delete <key> npm config list npm config edit Command to set (don't forget to include quote and trailing slash) npm config set proxy "http : //username:password@proxy.server.com:8080/" npm config set https - proxy "http : //username:password@proxy.server.com:8080/" Bo...

Sends Email in The Background in PHP

If you send email in PHP with any library through SMTP server, the process will take a while (2 seconds or more). The process isn't necessary to be sequential. Clients don't need to wait and know the sending process had finished or not. So, the sending process is better to be run as background process. If you use Linux, you can use exec method. If you're in Windows environment, you can use popen method. Then you send the result to the background. In Linux, you can use wget command to be executed to access email sending application script. The message contents can be setup and maintain in database before this action is run. exec("wget -qO- http://domain.com/send.php &> /dev/null &"); Or you can pass the contents directly as parameters for PHP arguments which is executed. exec("php /path/to/your/mailer/script \"arg1\" \"arg2\" > /dev/null 2> /dev/null &"); If you are in Windows pclose(popen(...