<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arpit Khandelwal on Medium]]></title>
        <description><![CDATA[Stories by Arpit Khandelwal on Medium]]></description>
        <link>https://medium.com/@arpit.khandelwal.1984?source=rss-95896dd07c5b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qX1dlT02cTwmU2qScDuW4Q.png</url>
            <title>Stories by Arpit Khandelwal on Medium</title>
            <link>https://medium.com/@arpit.khandelwal.1984?source=rss-95896dd07c5b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 14 Nov 2026 16:08:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arpit.khandelwal.1984/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Hapi.js — Project Structure and Best Practices (Part 2)]]></title>
            <link>https://medium.com/the-resonant-web/production-ready-hapi-js-starter-kit-part-2-cba358373017?source=rss-95896dd07c5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/cba358373017</guid>
            <category><![CDATA[hapijs-best-practices]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[hapi]]></category>
            <category><![CDATA[hapijs]]></category>
            <category><![CDATA[hapijs-project-structure]]></category>
            <dc:creator><![CDATA[Arpit Khandelwal]]></dc:creator>
            <pubDate>Thu, 01 Aug 2019 04:38:39 GMT</pubDate>
            <atom:updated>2019-08-04T16:36:31.873Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hapi.js — Project Structure and Best Practices (Part 2)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*pRUy0F64RgdHtyB4YsZHqw.png" /></figure><p>This is the second part of the series on hapi.js, the <a href="https://medium.com/the-resonant-web/an-introduction-to-hapi-js-part-1-5f2edcb5eced">first part</a> explained what hapi is all about and basics of its plugins and request lifecycle along with a piece of code demonstrating how to create a “hello world” hapi server. This part explains about a starter-kit prepared by me using hapi v18.x which can help save a substantial amount of development effort towards doing the foundational work for a scalable web application.</p><h3>The Starter-Kit</h3><p>Most modern web applications need two front-ends for users — one that’s browser based (for desktops/laptops) and another which is usually a (native or hybrid) mobile application. Browser-based frontends are typically used for administrative purposes (admin portals) and have limited features compared to their mobile counterparts, which are generally designed for end-users.</p><p>The concept behind this starter-kit was to provide developers with a way to satisfy both of these needs by having the same hapi server cater to both kinds of consumers, while reducing the redundancies of controllers and routes at the same time.</p><h3>GitHub Repo</h3><p>The codebase for this writeup can be cloned from the following GitHub repo:</p><p><a href="https://github.com/khandelwal-arpit/hapijs-starterkit">khandelwal-arpit/hapijs-starterkit</a></p><h3>Starter-Kit Details</h3><p>This starter-kit serves as a great starting point for all the hapi developers who were looking for a hapi based platform as their production server. Once successfully deployed, this kit gives developers access to the following two modes of interaction with the server:</p><ol><li><strong>http://localhost:&lt;port&gt;</strong> — Serves web requests</li><li><strong>http://localhost:&lt;port&gt;/api </strong>— Serves rest api requests</li></ol><p>Web and REST APIs both have their independent authentication mechanisms. This kit assumes the availability of ‘<em>MongoDB</em>’ installation on the localhost where the server will run or the use of docker-compose to boot up a mongodb container and link the application with it within the realm of docker.</p><p>The starter-kit contains basic features such as user management with login, logout, password reset, profile view, dashboard view etc. Forgot password implementation is done using ‘<em>nodemailer</em>’ (sends an email with reset password link with a unique token that expires in an hour). All the environment variables are managed using ‘<em>dotenv</em>’ node package for development, the same will need to be configured at the host server in the production environment.</p><h3>Libraries Used</h3><p>The starter-kit uses the following primary libraries for satisfying various requirements:</p><ul><li><strong>Hapi</strong> — Server side framework</li><li><strong>Docker</strong> — Docker container support</li><li><strong>Handlebar</strong> — HTML templating engine</li><li><strong>Mongoose</strong> — Mongo database ORM</li><li><strong>SASS</strong> — CSS preprocessor</li><li><strong>WebPack</strong> — Asset pipeline</li><li><strong>Dotenv</strong> — Environment variable emulator</li><li><strong>Good</strong> — Logger mechanism</li><li><strong>JWT</strong> — Authentication mechanism for APIs</li><li><strong>Config</strong> — Configuration Handler</li><li><strong>Nodemailer</strong> — Module to send emails</li><li><strong>Mocha</strong> — Unit test framework</li></ul><h3>Opinionated Application Structure</h3><p>The kit is designed in such a way that all the relevant artefacts are organised in their separate folders. You will see app, assets and config being the topmost container directories along with the root directory. A detailed view of the application structure is specified below:</p><pre>├── app<br>│   ├── controllers<br>│   │   ├── api            // Controllers<br>│   │   └── web            // Controllers<br>│   ├── helpers            // Helpers<br>│   ├── models             // All mongoose models are defined here<br>│   ├── routes             // All app routes are defined here<br>│   │   ├── mobileApi      // RESTAPI routes for multiple versions<br>│   │   │   ├── v1         // Routes for version1<br>│   │   │   └── v2         // Routes for version2<br>│   │   └── webApi         // WEB api routes.<br>│   └── templates          // Server-rendered handlebar templates<br>├── assets                 // Contains all static resources <br>│   ├── fonts              // Fonts used in application<br>│   ├── images             // Images used in application<br>│   ├── scripts            // Scripts used in application<br>│   │   ├── js             // User defined scripts<br>│   │   └── vendor         // Vendor scripts<br>│   └── styles             // All SASS stylesheets<br>├── config                 // Contains all app configurations<br>│   ├── assets.js          // Assets configuration file<br>│   ├── config.js          // Application configuration <br>│   ├── default.json       // Configuration file<br>│   ├── manifest.js        // App manifest file<br>│   ├── meta.js            // App metadata file<br>│   └── ssl                // Contains ssl certificates<br>├── lib                    // Core application lib/plugins<br>├── logs                   // Contains app log file <br>├── .gitignore             // standard git ignore file<br>├── .babelrc               // Babel config<br>├── .eslintrc              // Define eslint rules<br>├── .eslintignore          // Ignores certain files for eslint rules<br>├── Dockerfile             // Standard docker file<br>├── docker-compose.yml     // Standard docker compose file <br>├── server.js              // Contains all app configurations<br>├── .env                   // dotenv configuration<br>└── test<br>    ├── testcases          // Test cases organised by module names<br>    └── test.js            // Test file</pre><p>The best thing about using opinionated structures is that there is basically one way (the <strong>right way</strong>) to do things and trying to do it differently will be difficult (and frustrating). Doing things the <strong>right way</strong> can make it very easy to develop with the software as the number of decisions that you have to make is reduced and the ability of the software designers to concentrate on making the software work is increased. This kit follows the same principle, I have used this kit in a variety of projects with a great amount of success due to the homogeneity of the code foundation and only variable being the business logic.</p><p>If you consider development in a big team environment then opinionated frameworks like these actually help you a lot as a technical lead. One thing you are always sure is about the quality of the code being written and the way different pieces join hands to combine into a big application. Without such frameworks, code review can be a nightmare considering there are global teams with developers having different thought process and ability to adapt certain coding styles.</p><h3>Application Flow</h3><p>Let’s walk ourselves through the signup flow in the codebase to understand how the different directories, plugins and scripts come into picture for a POST api request for signup from a mobile device.</p><p>The first point of contact in the application is the <strong>routes</strong> directory. It contains the routing information for the various HTTP requests. All the routers are configured as independent plugins, here is how the source code of the <em>routes/mobileApi/v1/signup.js</em> plugin looks like:</p><p><strong>signup.js (router)</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70464a5d7e1f59dcdeef7c257f8cc6bc/href">https://medium.com/media/70464a5d7e1f59dcdeef7c257f8cc6bc/href</a></iframe><p>Whenever a POST request for signup is sent to the server the, ‘signup_routes_v1’ plugins catches it and contacts the /controllers/api/signup controller for handling the request.</p><p><strong>signup.js (controller)</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee669945553ccc459b57e3ec6a3e75bd/href">https://medium.com/media/ee669945553ccc459b57e3ec6a3e75bd/href</a></iframe><p>Validating data can be very helpful in making sure that our application is stable and secure. hapi allows this functionality by using the module <a href="https://github.com/hapijs/joi">Joi</a>, which allows us to create our validations with a simple and clear object syntax. In this case the validate routine inside the userSignUp function does the same by applying rules on the email field, password and confirm password fields and the name field. If for any reasons the applied validations fail, a HTTP 400 response is immediately returned from the server.</p><p>Once the validations pass, the async handler function comes into picture. It is dependent on a helper /helpers/signup.js which is defined in the helpers directory at the root.</p><p><strong>signup.js (helper)</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b8e504ef830bc6ffa793a3616323cc3/href">https://medium.com/media/3b8e504ef830bc6ffa793a3616323cc3/href</a></iframe><p>The helper takes help from the User.js mongoose model to search if the passed user details match an existing user of the application, if it does then a HTTP 409 response is sent back to the caller which signifies that this is an existing user and system cannot allow its creation. However, if the details don’t match any existing user in the mongodb, then the save method results in creating one and returns a HTTP 201 stating the signup was successful.</p><p>This entire flow could be tested using the swagger documentation as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/981/1*g-qDsISt8e5DSjpbj0LGFA.png" /><figcaption>POST request : SignUp</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*WWJHFRB5kmiWyQgu7le2kg.png" /><figcaption>SignUp Response</figcaption></figure><p>The other API flows are very similar to the signup flow, hopefully the illustrations above have given you fair bit of insight on understanding the code base better.</p><h3>Authentication</h3><p>Both of the servers spun up in this kit have independent authentication measures in place. The web application server uses cookie-based authentication, while the API server uses <a href="https://jwt.io/">JWT</a> authentication for rest API access. The deployment and application management is done via standard node scripts, webpack and forever.</p><p>Mobile api authentication request handler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0328027922d0c7d721c429a88f8444af/href">https://medium.com/media/0328027922d0c7d721c429a88f8444af/href</a></iframe><p>Web authentication request handler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3d9a98766e77d1d7d5ba40d761320ec/href">https://medium.com/media/c3d9a98766e77d1d7d5ba40d761320ec/href</a></iframe><h3>Glued Server</h3><p>A question you might be having right now is where and how the hapi server has been defined. The answer is, its not defined in the traditional manner which most tutorials suggest you to do:</p><pre>const Hapi = require(‘hapi’); <br>const server = Hapi.server({ <br>      port: 3000, <br>      host: ‘localhost’<br>});</pre><p>Rather, the server is defined inside the server.js file as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16a13957c70803e7984312ac67dfd331/href">https://medium.com/media/16a13957c70803e7984312ac67dfd331/href</a></iframe><p><a href="https://github.com/hapijs/glue/blob/master/API.md">Glue</a> is a plugin that works with hapi to configure a server relatively easy, it works by exporting a single function named <em>compose </em>to your hapi server. Inside of <em>compose</em> are two parameters that work to configure your server automatically, from server connections to server plugins.</p><p>The manifest file contains declarations of plugins created throughout the codebase along with the configurations passed to them declaratively inside JSON objects. We will discuss more about the configurations in the next section, for now all you need to know is we are using Glue to create the hapi server, glue needs a pre defined (and of course opinionated) config file (called as manifest.js in this case) and then you can simply invoke the server.start() method to boot the hapi server.</p><h3>Configurations</h3><p>There are two important configuration files present in the starter-kit:</p><ol><li>config/config.js</li><li>config/manifest.js</li></ol><h4>config.js</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f698b176ae30fe5daa45b23f4af29251/href">https://medium.com/media/f698b176ae30fe5daa45b23f4af29251/href</a></iframe><p>The config.js file contains the configurations needed by various plugins defined in the codebase. For ex. the mongoose json object contains the mongodb connection uri according to the environment (default, test, prod), the email object contains the various settings needed to send an email from our starter-kit etc</p><p>The manifest.js file on the other hand contains the plugin declarations and provides them with the configuration needed defined in the config.js.</p><h4>manifest.js</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5811c9ab0252b9fbafd62a4c5d158c72/href">https://medium.com/media/5811c9ab0252b9fbafd62a4c5d158c72/href</a></iframe><p>For registering any new route using the structure explained so far, the last thing you will need to do is register the plugin in this manifest so that Glue can compose the hapi server with the set of plugins at runtime. If you have defined the plugin properly but forgot to include it here, Glue has no way of knowing that your new plugin needs to be registered with hapi and hence it won’t show up.</p><h3><strong>Unit Testing</strong></h3><p>If you are using the Chrome browser for unit testing the application, you can use the <a href="https://github.com/node-inspector/node-inspector">‘node-inspector’</a> plugin pre-configured with this app for debugging purposes. This kit supports writing unit test cases using ‘Mocha’ and ‘Supertest’ node packages. To execute the test cases please run the following command:</p><pre># Test the server<br>$ npm test</pre><h3><strong>Logging</strong></h3><p>The logging aspect for this kit is covered with <a href="https://github.com/hapijs/good">‘Good’</a> which is a hapi plugin for logging. The configurations are organised using <a href="https://github.com/hapijs/glue">‘Glue’</a> along with <a href="https://github.com/hapijs/confidence">‘Confidence’</a> (as explained earlier) which makes it very easy to switch between different plugins for the various functionalities while keeping the hapi’s ‘configuration over convention’ theme intact.</p><p>We have used the following configuration for the logging aspects of the starter-kit using Good (defined in config.js):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca124d1ba29233f40f94c2aefe0603e4/href">https://medium.com/media/ca124d1ba29233f40f94c2aefe0603e4/href</a></iframe><p>“myConsoleReporter” configuration above instructs hapi to log the request details on the console (stdout), whereas the “myFileReporter” tells hapi to log the same set of details in the /logs/log file.</p><h3>Swagger Documentation</h3><p>The APIs are well documented for unit testing and sharing with UI team using Swagger. Please use the following URL for accessing the same:</p><p><a href="http://localhost:8000/documentation#/">http://localhost:8000/documentation#/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*xbcIRNxOvvfJZlBaDyeolQ.png" /></figure><h3>Standard hapi Packages Used</h3><p>The following packages/plugins were used from the standard hapi.js ecosystem in the development of this kit:</p><ol><li><a href="https://github.com/hapijs/crumb">Crumb</a></li><li><a href="https://github.com/hapijs/glue">Glue</a></li><li><a href="https://github.com/hapijs/good">Good</a></li><li><a href="https://github.com/hapijs/good-console">Good-Console</a></li><li><a href="https://github.com/hapijs/hoek">Hoek</a></li><li><a href="https://github.com/hapijs/hapi-auth-cookie">Hapi-auth-cookie</a></li><li><a href="https://github.com/hapijs/inert">Inert</a></li><li><a href="https://github.com/hapijs/yar">Yar</a></li><li><a href="https://github.com/hapijs/wreck">Wreck</a></li></ol><h3>Webpack v4</h3><p>As you might already know, <em>Webpack</em> is an open-source JavaScript module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset.</p><p>In case of this starter-kit too, we have used webpack to create following two important files:</p><ol><li>bundle.js</li><li>vendor.js</li></ol><p>Along with these files, webpack also bundles the entire css scripts present in the development folders into a single file “index.css” and dumps all of them in the .build directory which gets created once webpack runs as a prestart script for the server execution. We have used v4 for this kit and the webpack configuration looks as follow:</p><p>webpack.config.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c806e5b1bf13e9cae5aae416d12bbbce/href">https://medium.com/media/c806e5b1bf13e9cae5aae416d12bbbce/href</a></iframe><h3>Running the Server Locally</h3><p>To run the server in standalone mode use either of the following commands:</p><pre># Install dependencies<br>$ npm install<br><br># Run the node server in dev mode<br>$ npm start<br><br># Run the node server in production mode<br>$ npm run prod</pre><h3>Running the Server in Docker Container</h3><p>Please ensure that docker and docker-compose are installed on your system.</p><p>The contents of the Dockerfile are as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0d1afce43bcce446c5a336f3960ba34/href">https://medium.com/media/e0d1afce43bcce446c5a336f3960ba34/href</a></iframe><p>The docker-compose file is organised as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5468e5f1a6c238dfc867fef7544408f6/href">https://medium.com/media/5468e5f1a6c238dfc867fef7544408f6/href</a></iframe><p>Steps to run app in docker container:</p><pre># cd to project dir</pre><pre># Create build<br>$ docker-compose build</pre><pre># Start the server in daemon thread<br>$ docker-compose up -d</pre><pre># Stop the server<br>$ docker-compose down</pre><p>In case you are willing to use this project as is (i.e. without external mongodb installation), the docker-compose provided with this project should suffice. It brings along a <em>mongodb</em> service which stores the data in the <em>/db/data</em> directory. But in case you wish to use your existing MongoDB installation then please remove the <em>mongodb</em> service from the docker-compose.yml file and correct the database_url environment variable.</p><h3>Application Screenshots</h3><p>Here are some of the screenshots of the default features being provided along with the starter-kit:</p><h4>Login Page</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*oqIGVGRN56HMliD21kyfxA.png" /></figure><h4>Sign-up Page</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*P_ngNUgk_ebaH_nIROvbfA.png" /></figure><h4>Forgot Password Page</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*mpXuDGBQ-U1c_wC7zYa5_w.png" /></figure><h4>Dashboard View</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ish8k66JAgSlhw0cp4eTzA.png" /></figure><h4>Settings View</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zLad785xVTN9KICF6bCj9Q.png" /></figure><h3>Conclusion</h3><p>As you can see, the starter-kit is designed to save you hours of coding time by giving you a streamlined, efficient and clean-coded interface and architecture to base your own development on. Try it out and be sure to let me know your feedback.</p><p>Hapi coding everyone!</p><p><strong><em>If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.</em></strong></p><p><strong><em>Did you know you could give up to 50 claps?</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cba358373017" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-resonant-web/production-ready-hapi-js-starter-kit-part-2-cba358373017">Hapi.js — Project Structure and Best Practices (Part 2)</a> was originally published in <a href="https://medium.com/the-resonant-web">The Resonant Web</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An introduction to Hapi.js (Part 1)]]></title>
            <link>https://medium.com/the-resonant-web/an-introduction-to-hapi-js-part-1-5f2edcb5eced?source=rss-95896dd07c5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f2edcb5eced</guid>
            <category><![CDATA[hapijs]]></category>
            <category><![CDATA[hapi]]></category>
            <category><![CDATA[walmart]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[express-vs-hapi]]></category>
            <dc:creator><![CDATA[Arpit Khandelwal]]></dc:creator>
            <pubDate>Thu, 01 Aug 2019 04:36:05 GMT</pubDate>
            <atom:updated>2019-08-01T04:39:53.982Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction to Hapi.js (Part 1)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cnxWQ0-aROOCuLBGcqunZA.png" /></figure><h3>Preface</h3><p>Node.js has unarguably become the backbone of the software industry. The increased popularity of JavaScript over the last few years has propelled Node forward in terms of production usage. Perhaps for the first time, JavaScript and Node offer the opportunity to develop entire n-tier applications using a single language.</p><p>As it is with all web frameworks, Node provides the essentials for an application — request and response objects, methods to manipulate HTTP requests, ability to interact with a database using popular ORM tools, robust production deployment libraries and more. “Pure” Node apps are stunningly fast, but they lack in the supporting cast of middleware, routing, and plugins that reduce the amount of code needed for a modern web application.</p><p>This is where web frameworks shine. This blog post is about <strong>hapi.js</strong> which is a framework with one of the fastest growing user base, the primary reason being the stability and scalability that hapi provides. Let’s explore more about this wonderful framework in the coming paragraphs.</p><h3>What is hapi.js?</h3><p><strong>hapi.js</strong> (short for Http-API, pronounced <em>happy</em> and also known as <em>hapi</em>) is an open-source framework for developing scalable web applications. One of the most basic use case of hapi is to build REST APIs. You can build application programming interface (API) servers, websites, and HTTP proxy applications with hapi.js.</p><p>It was created by the mobile team at <strong>Walmart Labs</strong> — led by <em>Eran Hammer </em>to handle their traffic for events like Black Friday, one of the busiest days for online shopping in the U.S. calendar.</p><h3>Why is there so much of fuss about hapi?</h3><p>To give you an idea of just why the developer community believes hapi is so robust and dependable, it was able to successfully manage millions of shopping transactions, returns and more at Walmart during <a href="https://en.wikipedia.org/wiki/Black_Friday_(shopping)">Black Friday</a>, arguably the busiest retail shopping day of the year in the U.S.</p><blockquote>hapi was able to handle all of Walmart mobile Black Friday traffic with about 10 CPU cores and 28Gb RAM (of course we used more but they were sitting idle at 0.75% load most of the time). This is mind blowing traffic going through VERY little resources. — Eran Hammer</blockquote><p>The original versions of hapi used the Express framework. Walmart found that there were limitations with Express that made the framework unsuitable for their particular requirements. Express was missing some key features, so Walmart eventually had hapi evolve to its own stand-alone framework.</p><p>Here is a list of some of the great names using hapi in production today:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/965/1*UlE6fy4DE5L5s-bLskTU0g.png" /><figcaption>A hapi community</figcaption></figure><h3>Building Blocks</h3><p>Throughout Walmart’s journey of rebuilding their mobile services tier and working with other frameworks, they realised that certain values were vital to building applications in Node for keeping the code intuitive, maintainable, and scalable. As a result, hapi development team centred around the following concepts:</p><ol><li>Building value, not infrastructure</li><li>Configuration is better than code</li><li>Separation of business logic from the transport layer</li><li>Open source and community-centric</li><li>Ecosystem (Opinionated nature, plugins)</li><li>Small modules</li></ol><h3>Plugins</h3><p>hapi provides a robust plugin system that allows you to add new features and fix bugs at a fast pace. There are dozens of plugins for hapi, ranging from documentation to authentication, and much more. Some of the most important plugins are listed <a href="https://hapijs.com/plugins">here</a>.</p><p>The benefits of splitting code into plugins reaches beyond just your own code, but easily allows users to share code that is beneficial to everybody. For example, you may have written code specific to Facebook authentication (bell) or a plugin to do something as simple as displaying the routes on startup (blipp).</p><p>A benefit of the plugin model and having replaceable parts in hapi is that you’re not bound to a particular way of doing things. If there later comes along a better plugin than the one you’re currently using, then switching it out is far easier than if that part of the code is tightly coupled with the code you want to keep. This applies not just to third-party plugins but anything within your own system as well.</p><p>When large companies work on plugins across multiple teams, things like caching can be determined on a per-plugin basis. The teams don’t have to communicate with each other, and even though the plugins run as part of the same server, routes in the different servers can be configured via the plugin to use, for example, different caching mechanisms that are suitable for what that particular piece of code is doing.</p><h3>Express vs hapi</h3><p>Both Express and hapi aim to be highly flexible, simple, and extensible. This similarity means that both have easy-to-use APIs, they’re highly modular, and can support your application as it grows potentially very large. The learning curve for these frameworks, since they are quite straightforward, is low, unlike a more opinionated framework like Meteor. If you are coming from using Express you should be able to quickly pick up Hapi, and vice versa.</p><p>There are, however, some philosophical differences between the two frameworks.</p><ul><li>Hapi.js includes more “batteries” by default than Express does. For instance, when parsing payload from forms to via “POST” requests, with Express, you have to include the body-parser middleware. You then use that to parse the POST payload and use the data from the user. On the other hand, with Hapi, you need no middleware. The payload is parsed for you by the framework itself, and you can access the payload directly on the request object. Little conveniences like that are replete in Hapi.</li><li>“Middleware” is the name given to software modules that work on HTTP requests in succession before the final output is returned to the user in a response. Express lets you attach middleware to handle each request. Hapi, however, works with plugins that provide middleware functionality.</li><li>Serving static files is remarkably similar between Hapi and Express. Hapi uses the <a href="https://github.com/hapijs/inert">Inert</a> plugin, whereas Express uses the <a href="http://expressjs.com/en/starter/static-files.html">express.static</a> built-in middleware.</li></ul><p>Hapi makes sense if you have a well-defined app idea that fits within sensible defaults for the programming approach. Hapi takes an opinionated approach to things like app security, and its plugins system is not as extensive as the Express middleware ecosystem. If you are a large team, the conventions of Hapi can come in useful to keep your code maintainable. Express, on the other hand, works best if you are after flexibility and making most development decisions on your own.</p><h3>Request lifecycle</h3><p>hapi is specifically designed to work in complex enterprise environments where multiple teams work on individual projects or overlapping pieces that must connect into one seamless result. By enabling developers to work on modular, reusable code without being narrowly constrained, developers can concentrate on the core project without trying to erect meta-frameworks or refactor the code.</p><p>To show this process in action, consider the typical hapi request lifecycle:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*O_MNahgq5BDXSb4Qb2xpdA.jpeg" /><figcaption>hapi.js request lifecycle</figcaption></figure><p>Here you can see that the developer is provided with hooks, in the form of handlers and extensions, to implement the logic. hapi largely stays out of what goes into these hooks. As project complexity grows, the need to decouple functionality and distribute the load also grows. The Hapi server becomes the outward-facing interface (by way of an API or UI), while leveraging a wide array of other technologies to break down monoliths of business logic into smaller, bite-sized pieces (which can in turn be managed by Hapi).</p><h3>hapi 16 v 17</h3><p>A <em>quick </em>side note about which version you should use. There was a substantial shift in the framework from 16 to 17 (latest being v18.x), where it went from using callbacks to async/await. This blog post, and the next one (with the starter-kit), will use hapi <strong>v18</strong>.</p><h3>A Simple Server</h3><p>A simple hapi based api server can be coded in a few lines of code as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90dcf43172fbdc0cbb7a770201d48b46/href">https://medium.com/media/90dcf43172fbdc0cbb7a770201d48b46/href</a></iframe><p>Save the above as server.js and start the server with the command:</p><p>node server.js</p><p>Now you&#39;ll find that if you visit <a href="http://localhost:3000/">http://localhost:3000</a> in your browser, you&#39;ll see the text “<em>Hello, world!”</em>, and if you visit <a href="http://localhost:3000/node">http://localhost:3000/node</a> you&#39;ll see “<em>Hello, node!</em>”.</p><h3>Part 2</h3><p>Thanks for reading the first part from a series of two articles on hapi. If this is enough to raise your interest in using this adaptable framework, be sure to check out the <strong>second part</strong> <a href="https://medium.com/the-resonant-web/production-ready-hapi-js-starter-kit-part-2-cba358373017">here</a>.</p><p>It contains a detailed walkthrough of the hapi based starter-kit app built by me, which provides an excellent starting point for all hapi developers to use in their own production environments.</p><p><strong><em>If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.</em></strong></p><p><strong><em>Did you know you could give up to 50 claps?</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f2edcb5eced" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-resonant-web/an-introduction-to-hapi-js-part-1-5f2edcb5eced">An introduction to Hapi.js (Part 1)</a> was originally published in <a href="https://medium.com/the-resonant-web">The Resonant Web</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to deploy Microservices based application on Kubernetes?]]></title>
            <link>https://medium.com/the-resonant-web/kubernetes-in-practice-part-2-2d2a7290dd65?source=rss-95896dd07c5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d2a7290dd65</guid>
            <category><![CDATA[deploy-microservice]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[kubernetes-cluster]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[learn-kubernetes]]></category>
            <dc:creator><![CDATA[Arpit Khandelwal]]></dc:creator>
            <pubDate>Wed, 01 May 2019 10:38:19 GMT</pubDate>
            <atom:updated>2019-06-04T05:44:33.300Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to deploy Microservices based application on Kubernetes ?</h3><p>This is the second article in the series of two, the <a href="https://medium.com/the-resonant-web/kubernetes-an-introduction-part-1-7d9cf6333d46">first part</a> concentrated on the theoretical aspects of Kubernetes. The aim of this story is to allow the readers to spin up their own fully functional Kubernetes cluster with a sample micro services based application deployed on it. The idea is to cover most of the Kubernetes objects and best practices those should be considered while setting up a new cluster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*emSnJ9j0vDchkU15y5ozgg.png" /><figcaption>A Microservices based Calculator application</figcaption></figure><h3>Philosophy</h3><p>I started learning Kubernetes almost an year ago after wrapping up my head around Docker. According to Google, <strong>Kubernetes</strong> is a portable, extensible open-source platform for managing containerised workloads and services, that facilitates both declarative configuration and automation.</p><p>Honestly, the only simple thing I could get my hands on for first few weeks was the definition of it. No matter how hard I tried to glue things together to understand this exciting technology, I kept on failing and for a very simple reason that I was trying to too hard by looking at different articles and blog posts and was trying to accomplish all at once, however I wasn’t building something around it which is unarguably the best approach to learn a new tech, yes, do it!</p><p>Its only then, that I embarked on a quest to make sure not only that I learn the concepts of Kubernetes myself, I do it in a way that makes it easier for my colleagues to understand and learn it faster than I did. The current starter-kit takes the approach of ‘read-it’ first along with ‘do-it’ next and encapsulates another hot topic ‘micro services’ inside the kit that will help a lot of techies to deploy and scale their micro services based deployment using docker containers and kubernetes.</p><h3>Application</h3><p>The application built as a part of this quest is a simple browser based calculator whose front end is written using Vue.js and the operations (addition, multiplication etc) are served by following micro services written in various programming languages:</p><p>1. <em>Expressed</em> service (Express.js)<br>2. <em>Happy</em> service (Hapi.js)<br>3. <em>Bootstorage</em> service (Spring Boot)<br>4. <em>Vuecalc</em> service (Vue.js)</p><p>The Expressed and Happy services are rest api based implementations not doing much tangibly but only serve basic calculator functions as APIs. The Bootstorage micro service acts as a backend service that stores and retrieves data provided to it via the former two.</p><p>The Vuecalc service is a frontend application designed in Vue.js. It resembles a calculator, two of whose primary operations i.e. Add and Subtract are served by the expressed service and the other two operations, i.e. multiply and division are served by the happy service.</p><p>Along with the micro-services, following components are deployed in the cluster as well:<br>1. <a href="https://www.getambassador.io/">Ambassador API Gateway</a><br>2. <a href="https://redis.io/">Redis</a> (data store)</p><p>Ambassador(provided by datawire.io) is an open source Kubernetes-Native API Gateway built on the Envoy Proxy which is a native kubernetes api gateway. Redis acted as a data store for this implementation, storing the operations done on the calculator and serving them on on demand.</p><h3>GitHub Repo</h3><p>The codebase for this story can be found in the following repo:</p><p><a href="https://github.com/khandelwal-arpit/kubernetes-starterkit">khandelwal-arpit/kubernetes-starterkit</a></p><h3>Architecture</h3><p>The complete architecture of this implementation can be summarised as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*U4Yfb7wZGt9iQHUZ_rUegg.jpeg" /><figcaption>Distributed Calculator Application Architecture</figcaption></figure><p>As we can see, Ambassador is acting as a single point of entry in the Kubernetes cluster, channeling all the incoming requests to appropriate services. The various microservices are deployed at the following addresses inside the cluster:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*rpzGqMyM76auMvZgC1fS0A.png" /><figcaption>API Endpoints</figcaption></figure><p>Each one of the microservices has liveness and readiness probes inbuilt for the cluster to determine their health and update the users about any potential downtime. The probes are configured for each service at the following endpoints:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*RQXTzVFtB416q6fswF4eOw.png" /><figcaption>Probes</figcaption></figure><p>These probes are configured in the individual deployment configurations as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f3b82f4df97ba967ed6295ce061a5a8/href">https://medium.com/media/1f3b82f4df97ba967ed6295ce061a5a8/href</a></iframe><p>All the services are currently deployed under the <em>default</em> namespace to keep things simple. However we strongly suggest that users should opt for different namespaces specially if they are planning to deploy their DEV, QA and UAT environments all on the same cluster (of course assuming <em>PROD</em> is going to be an independent cluster).</p><h3>Services</h3><p>The different services created for this starter-kit are summarised in the following diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*a0vgwjiaFP3bE_nNiQkokQ.jpeg" /><figcaption>Micro-Services</figcaption></figure><h4>Vuecalc</h4><p>This is a vue-cli 3.0 based Vue.js application which renders the calculator UI as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*6g8lrFSF1XrsljbYO3tMew.png" /><figcaption>VueCalc Calculator</figcaption></figure><p>The service is composed of ‘home’, ‘calculator’ and ‘history’ views. It uses <a href="https://github.com/axios/axios">Axios</a> library to do all the API calls to the kubernetes cluster. It uses a sound architecture with separate components and services for different aspects of the interface. The ‘history’ view shows a list of recent operations done over the calculator application by fetching them from the ‘Bootstorage’ service located inside the cluster.</p><h4>Expressed</h4><p>It is an Express.js (v4.16.x) based web application serving basic APIs for addition and subtraction. It is a containerised service and deployed inside the cluster as a pod eligible to horizontally scale under extreme conditions. The <em>Vuecalc</em> service calls the apis from this service when it has to do an add or subtract operation, once the operation is complete an async call is placed to the <em>Bootstorage</em> service to store the last performed operation in the Redis data store.</p><h4>Happy</h4><p>It is a Hapi.js (v17.8.x) based application having two basic APIs for multiplication and division. It is also a containerised service and similar to the <em>Expressed</em> service, is deployed internally in the kubernetes cluster for serving the apis. It also places an async call to the ‘Bootstorage’ service to store the last executed operation on Redis.</p><h4>Bootstorage</h4><p>It is a Java (v1.8) and Spring Boot (v2.x) based microservice that has two primary purposes:</p><ol><li>Store executed operations inside Redis, and</li><li>Return the list of operations stored in Redis sorted in the order of execution (last operation at top).</li></ol><p>Redis has been used in the cluster as a statefulset which has persistence enabled and uses persistent volume claim templates in the cluster to store the Redis dump files which allows the cluster to make sure Redis always retains the values in between cluster outages too. The Redis connection parameters are dynamically passed to the pod from the ENV variables which are provided to it using the config maps.</p><h3>Kubernetes Components</h3><p>Following kubernetes components were used during the development of this starter kit:</p><ol><li>Services</li><li>Deployments</li><li>Pods</li><li>StatefulSets</li><li>Cronjob</li><li>Secrets</li><li>Config-Maps</li><li>Persistent Volumes</li><li>Persistent Volumes Claims</li><li>Storage Class</li><li>Kubernetes Dashboard</li></ol><p>We are assuming that there is an existing kubernetes cluster already running on one of the renowned cloud hosting service provider -</p><ol><li>Google cloud (Kubernetes Engine)</li><li>MS Azure (Kubernetes Cluster Service)</li><li>AWS (AKS)</li></ol><p>Or, if this is not the case, you can try Docker-for-desktop, it allows us to create a sample kubernetes cluster with a single node inside it. Even though it only has a single node out of the box, it suffices the complete development and testing effort needed to run an application on kubernetes cluster before we actually go to the cloud. We suggest you try your configurations first locally and once it works as expected then move to the cloud to save unnecessary hosting cost.</p><p>Some of the important components used for current project’s development are explained in sections below.</p><h4>Config Maps</h4><p><em>ConfigMaps</em> allow us to decouple configuration artifacts from image content to keep containerized applications portable. For the purpose of demonstration, the <em>expressed</em> and <em>happy</em> service both use a config map of their own to pass ENV var <em>log_level</em>to them. The yaml look as follows:</p><p><em>expressed-cmap.yaml</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83cd1e8fdef1aceed0552360622931e2/href">https://medium.com/media/83cd1e8fdef1aceed0552360622931e2/href</a></iframe><p><em>happy-cmap.yaml</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/186ca142282ed0c782f6b37a7c63a0cf/href">https://medium.com/media/186ca142282ed0c782f6b37a7c63a0cf/href</a></iframe><p>These configurations are referred inside the deployment specifications as follows:</p><p><em>expressed.yaml</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6c6585c5fa8c25f26e7b515ad742ee6/href">https://medium.com/media/b6c6585c5fa8c25f26e7b515ad742ee6/href</a></iframe><h3>StatefulSet</h3><p>Like a Deployment, a <em>StatefulSet</em> manages pods that are based on an identical container spec. Unlike a Deployment, a StatefulSet maintains a sticky identity for each of their Pods. These pods are created from the same spec, but are not interchangeable as each has a persistent identifier that it maintains across any rescheduling.</p><p>A StatefulSet operates under the same pattern as any other Controller. We define our desired state in a StatefulSet object, and the StatefulSet controller makes any necessary updates to get there from the current state.</p><p>StatefulSets are valuable for applications that require one or more of unique network identifiers or persistent storage or ordered, graceful deployment and scaling. In our case we had the need to store the data for various operations happening in the calculator application so that we can show them when user goes to the history section of the application. Here is how the configuration yaml for it looks like:</p><p><em>redis.yaml</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c95340938f47ce7e642ced9396bcbbe/href">https://medium.com/media/2c95340938f47ce7e642ced9396bcbbe/href</a></iframe><p>The Redis runs on default port <em>6379</em> however its access is password protected and needs the password <em>calculator</em> for any access within the cluster. The appendonly command instructs Redis to create a dump file and append to it every 900 seconds if single edit has been made or every 30 seconds if 2 edits have been made.</p><p>The volume that gets mounted points us to a volume claim which is being dynamically allocated to us using the persistent volume claim template (pvc template) definition. The pvc-template instructs the cluster to create a storage on disk and allocate the same to Redis each time it reboots. This way we do maintain the state of Redis deployment which is crucial since it is acting like the data store for our use case. You can easily replace it with MySql or any other database of your choice, but please bear in mind that you will need to do certain modifications to the <em>Bootstorage</em> service code in order to support the new storage mechanism.</p><h3>Cronjob</h3><p>A Cron Job creates Jobs on a time-based schedule. One CronJob object is like one line of a crontab (cron table) file. It runs a job periodically on a given schedule, written in Cron format.</p><p>In our case, we have written a cronjob that invokes the following API every 15 minutes:</p><pre><a href="http://bootstorage-svc:5000/api/bootstorage/deletelru">http://bootstorage-svc:5000/api/bootstorage/deletelru</a></pre><p>The purpose of this api (written in the Bootstorage service) is to delete the oldest operation that was performed on the calculator. This way we keep the footprint of our data store small and at the same time have a perfectly valid use case to show how a cronjob can be executed in the application to do a recurring operation. The yaml for the same looks as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bce256a4b5580de6eed0848b4bcfa0b/href">https://medium.com/media/0bce256a4b5580de6eed0848b4bcfa0b/href</a></iframe><p>The parameter <em>successfulJobsHistoryLimit</em> makes sure that we see exactly 3 entries of the last successful executions of the cronjob in our kubernetes history either viewed via kubectl or on the dashboard. This cronjob also presents a classic example where a task defined in one of your micro service needs to execute periodically and you were hesitant to create an entire pod just for the sake of running the task, using this approach you can easily instruct any running pod via the API call which is executed inside a <em>~35MB</em> alpine container spun up at runtime.</p><h3>API Gateway</h3><p>Ambassador is a production tested, open source API Gateway that exposes the power of Envoy Proxy in Kubernetes. Ambassador relies entirely on Kubernetes for reliability, availability, and scalability. For example, Ambassador persists all state in Kubernetes, instead of requiring a separate database. Ambassador has virtually no moving parts, and delegates all routing and resilience to Envoy Proxy and Kubernetes, respectively. It integrates the features teams need for microservices, including authentication, rate limiting, observability, routing, TLS termination, and more.</p><p>In our case, we decided to deploy Ambassador service as a <em>LoadBalancer</em> listening on port 80. The configuration that was responsible for the routing of incoming calls to independent micro services is as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/140f94268c3e61ad1d411c52d3324a4c/href">https://medium.com/media/140f94268c3e61ad1d411c52d3324a4c/href</a></iframe><p>For services such as <em>Expressed</em>, <em>Happy</em> and <em>Bootstorage</em> we are rewriting the incoming request path to <em>/api/*</em> and the service mappings are following the convention of <em>service-name:port</em> where the ports are those allocated to these services inside the cluster. There are different ways how developers use the routing configurations, some of us prefer to put this information as a kubernetes-ambassador annotation inside the respective deployment configurations but we personally like to keep them all at a single place inside the ambassador configuration file and that’s exactly what you will notice in the configs.</p><h3>Kubernetes Dashboard</h3><p>Dashboard is a web-based Kubernetes user interface. You can use Dashboard to deploy containerised applications to a Kubernetes cluster, troubleshoot your containerised application, and manage the cluster resources. You can use Dashboard to get an overview of applications running on your cluster, as well as for creating or modifying individual Kubernetes resources (such as Deployments, Jobs, DaemonSets, etc).</p><p>Please follow this <a href="https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/">link</a> to learn more about how to set up the dashboard for your cluster and make use of a decent web interface to administer the kubernetes cluster. Here are a few screen shots of how the calculator application components look like on our dashboard after successful deployment:</p><p><strong>Overview</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vlSps_Gr55C43eNNMWmsgA.png" /><figcaption>Overview tab</figcaption></figure><p><strong>Resources</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3hytjSkviPbh2e0K7D0_fg.png" /><figcaption>Resources tab</figcaption></figure><p><strong>Services</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KNGceER-4OX0IUg6OLVOiQ.png" /><figcaption>Services tab</figcaption></figure><p><strong>Deployments</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1jPbi5jIkoWXKkRhIOG-yQ.png" /><figcaption>Deployments tab</figcaption></figure><p><strong>Pods</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fe2fTi3A6eO1EhPNn5nK3A.png" /><figcaption>Pods tab</figcaption></figure><p><strong>StatefulSets</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aliYbtSxvSau6AGchsod-Q.png" /><figcaption>Stateful Sets tab</figcaption></figure><p><strong>Cron Jobs</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TZ3JNNjJKVRvxZX81uXkBw.png" /><figcaption>Cron Jobs tab</figcaption></figure><p><strong>Config And Storage</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_jx6okdxA7XUDNBP6UJSrA.png" /><figcaption>Config Maps and Persistent Volumes</figcaption></figure><h3>Build and Deploy</h3><p>There are some assumptions that we are making (before you can run the build and deploy command) like you have the following pre-installed on the node/environment where this cluster is being set up -</p><ol><li><a href="https://www.docker.com/">Docker</a></li><li><a href="https://maven.apache.org/">Maven</a></li><li><a href="https://nodejs.org/en/">NPM</a></li><li><a href="https://kubernetes.io/docs/reference/kubectl/overview/">Kubectl</a></li></ol><p>Once these are setup, please execute the following command from the root directory of this repo:</p><pre>sh build-deploy.sh</pre><p>This command is equipped to prepare all the docker containers needed for the setup of this application. It will create them all one by one and finally execute the configurations placed in the k8s directory which will result in the creation of various artifacts on the kubernetes cluster.</p><h3>Important Kubernetes Commands</h3><p>Here are some basic commands for using Kubernetes command line utility:</p><ol><li><strong>kubectl proxy</strong> (Run kubernetes dashboard)</li><li><strong>kubectl describe secret admin-user-token-6kchd -n kube-system</strong> (Generate token)</li><li><strong>kubectl create -f k8s</strong> (Deploy to kubernetes)</li><li><strong>kubectl get deployments</strong> (Check existing deployments)</li><li><strong>kubectl get services</strong> (Check existing services)</li><li><strong>kubectl set image deployments expressed expressed=expressed:latest</strong> (Update image for Deployment)</li><li><strong>kubectl rollout status deployments expressed</strong> (Check status of updated rollout)</li><li><strong>kubectl scale — replicas=0 deployment express-svc</strong> (Scale down any deployment)</li><li><strong>kubectl scale — replicas=1 deployment express-svc</strong> (Scale up a deployment)</li><li><strong>docker image prune</strong> (Remove all dangling images)</li></ol><h3>Conclusion</h3><p>With this we now come to conclusion of the second part and the series on Kubernetes. I hope both the articles made sense to the readers and that it helps them gain a better grip on this exciting new technology and at the same time allows them to have a kick start without investing a plethora of hours just to set Kubernetes up.</p><p>As always, looking for your feedback!</p><p><strong><em>If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.</em></strong></p><p><strong><em>Did you know you could give up to 50 claps?</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d2a7290dd65" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-resonant-web/kubernetes-in-practice-part-2-2d2a7290dd65">How to deploy Microservices based application on Kubernetes?</a> was originally published in <a href="https://medium.com/the-resonant-web">The Resonant Web</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is Kubernetes?]]></title>
            <link>https://medium.com/the-resonant-web/kubernetes-an-introduction-part-1-7d9cf6333d46?source=rss-95896dd07c5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d9cf6333d46</guid>
            <category><![CDATA[kubernetes-tutorial]]></category>
            <category><![CDATA[kubernetes-documentation]]></category>
            <category><![CDATA[docker-vs-kubernetes]]></category>
            <category><![CDATA[kubernetes-engine]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Arpit Khandelwal]]></dc:creator>
            <pubDate>Wed, 01 May 2019 10:26:33 GMT</pubDate>
            <atom:updated>2019-06-04T05:45:13.343Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Kubernetes ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzvEyeSLt0nRfv69DI_hIQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/Esq0ovRY-Zs?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Maximilian Weisbecker</a> on <a href="https://unsplash.com/search/photos/ship-steering-wheel?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>It might be easy to deploy a container, but operationalising containers at scale, especially in concert with microservices — is not for weekend enthusiasts. It requires planning, and most experts say an orchestration tool is a must.</p><p>That’s the point at which the conversation will likely turn to <a href="https://kubernetes.io/">Kubernetes</a>. The platform was first developed by a team at Google, and later donated to the <a href="https://www.cncf.io/">Cloud Native Computing Foundation</a> (CNCF). It’s not the only option for container management, but it has rapidly become one of the most popular one.</p><p>I have been using Kubernetes for last twelve months or so and I must say I am quite impressed with the way our microservices based applications are being managed now. Earlier, some of the biggest challenge we faced were:</p><ul><li>Remembering where our containers are deployed.</li><li>Scaling them manually or taking help of cloud provider’s configurations.</li><li>Being so tightly coupled with the cloud provider that migrating the environment to a more economical provider was a nightmare.</li><li>Dependence on cloud provider’s API gateway.</li><li>Not being able to utilise the VMs to their full potential due to manual orchestration and having to do the math around RAM and processor consumption on the VMs.</li><li>Inability to use some of the industry best-practices such as circuit breakers, rater limiters, uniform logging at each VM etc.</li><li>Issues with reusing the same VMs for different environments such as DEV, QA, UAT and PROD.</li><li>Canary releases of updated containers.</li></ul><p>With the introduction of Kubernetes clusters, most of the challenges listed above have now been overcome with least amount of efforts. Kubernetes is an opinionated solution to these issues but the best part is it really doesn’t matter if you are deploying the cluster over AWS or Azure once the configurations are established. Also, managing which container goes where is no longer our headache, Kubernetes control plane takes over such tasks and does them very efficiently.</p><p>Throughout this series I will try to emphasise on the above mentioned challenges and how Kubernetes helped me solve them, also I will introduce a starter-kit that I have prepared for beginners to learn Kubernetes from Zero. Hopefully by the end of second part you will have fair bit of information and a good starting point into the Kubernetes wonderland!</p><h3>What does Kubernetes mean?</h3><p>The name <strong>Kubernetes</strong> originates from Greek, meaning <em>helmsman</em> or <em>pilot</em>, and is the root of <em>governor</em> and cybernetic. <em>K8s</em> is an abbreviation derived by replacing the 8 letters “ubernete” with “8”.</p><p>Its an open source platform that automates Linux container operations eliminating many of the manual processes involved in deploying and scaling containerised applications. Kubernetes provides a platform to schedule and run containers on clusters of physical or virtual machines. More broadly, it helps you fully implement and rely on a container-based infrastructure in production environments. And to do so in a way that automates many operational tasks.</p><p>In other words, you can cluster together groups of hosts running Linux containers, and Kubernetes helps you easily and efficiently manage those clusters.</p><h3>What does Kubernetes do?</h3><p>As a Kubernetes user, you can define how your applications should run and the ways they should be able to interact with other applications or the outside world. You can scale your services up or down, perform graceful rolling updates, and switch traffic between different versions of your applications to test features or rollback problematic deployments. Kubernetes provides interfaces and composable platform primitives that allow you to define and manage your applications with high degrees of flexibility, power, and reliability.</p><p>Kubernetes eliminates all of the manual processes involved in the deployment and scaling of containerised applications. It’s capable of clustering together groups of servers hosting Linux Containers while also allowing administrators to manage those clusters easily and efficiently.</p><p>Kubernetes makes it possible to respond to consumer demands quickly by deploying your applications within a timely manner, scaling those same applications with ease and seamlessly rolling out new features, all while limiting hardware resource consumption. It’s extremely modular and can be hooked into by other applications or frameworks easily. It also provides additional self-healing services, including auto-placement, auto-replication and auto-restart of containers.</p><h3>Kubernetes vs Docker Swarm</h3><p>There also exists Docker’s own platform called Swarm. It accomplishes much of the same tasks and boasts a lot of the same features. The primary difference between the two is that Swarm is centralised around the use of Docker, while Kubernetes tends to adopt a more generalised container support model.</p><p>Docker and Kubernetes can actually work together. As far as management platforms go, you can use either Kubernetes or Docker Swarm for your Docker engines. Kubernetes currently holds the largest market share and is pretty much the standard platform. It works around the concept of pods, which are scheduling units (and can contain one or more containers) in the Kubernetes ecosystem and they are distributed among nodes to provide high availability.</p><p>It has the advantage of using years of research done by Google, hence leveraging the expertise. Being open source, it has a vibrant, growing community with plenty of helpful resources, and guidance available to anyone looking. It can run on either a public cloud service or on premises, and is easy to learn and implement.</p><p>Docker Swarm has the advantage of tightly integrated into the Docker ecosystem, and uses its own API. Its filtering and scheduling system enables the selection of optimal nodes in a cluster to deploy containers. As it is developed by Docker itself, Docker Swarm removes many compatibility and other differences and integrates smoothly. Kubernetes usually is not a complete solution and requires custom plug-ins to set up.</p><p>Sometimes production applications will span across multiple containers, and those containers may be deployed across multiple physical server machines. Both Kubernetes and Swarm give you the orchestration and management capabilities required to deploy and scale those containers to accommodate the always changing workload requirements.</p><h3>Architecture</h3><p>Kubernetes follows the master-slave architecture. The components of Kubernetes can be divided into those that manage an individual node and those that are part of the control plane.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*2n7a_9paxCGgdTzaKqqJtA.png" /><figcaption>Kubernetes Architecture (Courtesy wikipedia)</figcaption></figure><h4>Kubernetes control plane (master)</h4><p>The Kubernetes Master is the main controlling unit of the cluster, managing its workload and directing communication across the system. The Kubernetes control plane consists of various components:</p><ul><li><strong><em>etcd</em></strong>: Consistent and highly-available key value store used as Kubernetes’ backing store for all cluster data. Practically speaking I was never required to bother how etcd works, its well encapsulated within the control plane boundaries and is efficiently managed by Kubernetes.</li><li><strong><em>API-Server</em></strong>: Component on the master that exposes the Kubernetes API. It is the front-end for the Kubernetes control plane. Every time we execute a kubectl command, it goes directly to the api server and is executed by it after taking a good look at the configuration and available hardware.</li><li><strong><em>Kube-Scheduler</em></strong>: Component on the master that watches newly created pods that have no node assigned, and selects a node for them to run on.</li><li><strong><em>Kube-Controller-Manager</em></strong>: Component on the master that runs controllers. Logically, each controller is a separate process, but to reduce complexity, they are all compiled into a single binary and run in a single process. These controllers include:</li><li><em>Node Controller</em>: Responsible for noticing and responding when nodes go down.</li><li><em>Replication Controller</em>: Responsible for maintaining the correct number of pods for every replication controller object in the system.</li><li><em>Endpoints Controller</em>: Populates the Endpoints object (that is, joins Services &amp; Pods).</li><li><em>Service Account &amp; Token Controllers</em>: Create default accounts and API access tokens for new namespaces.</li></ul><h4>Kubernetes node (slave)</h4><p>The Node, also known as Worker or Minion, is a machine where containers are deployed. If you take example of AWS, you can say these nodes are nothing but simple EC2 instances with a given RAM and CPU configuration.</p><p>Every node in the cluster must run a container runtime such as Docker, as well as the below-mentioned components, for communication with master for network configuration of these containers.</p><ul><li><strong><em>Kubelet</em></strong>: An agent that runs on each node in the cluster. It makes sure that containers are running in a pod. The kubelet takes a set of PodSpecs that are provided through various mechanisms and ensures that the containers described in those PodSpecs are running and healthy.</li><li><strong><em>Kube-proxy</em></strong>: Enables the Kubernetes service abstraction by maintaining network rules on the host and performing connection forwarding.</li><li><strong><em>Container runtime</em></strong>: The container runtime is the software that is responsible for running containers. Kubernetes supports several runtimes: <a href="http://www.docker.com/">Docker</a>, <a href="https://containerd.io/">containerd</a>, <a href="https://cri-o.io/">cri-o</a>, <a href="https://github.com/kubernetes-incubator/rktlet">rktlet</a> and any implementation of the <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/container-runtime-interface.md">Kubernetes CRI (Container Runtime Interface)</a>.</li></ul><h3>Kubernetes Objects</h3><p>Kubernetes defines a set of building blocks (“primitives”), which collectively provide mechanisms that deploy, maintain, and scale applications based on CPU, memory or custom metrics.</p><p>This extensibility is provided in large part by the Kubernetes API, which is used by internal components as well as extensions and containers that run on Kubernetes. The platform exerts its control over compute and storage resources by defining resources as Objects, which can then be managed as such. The key objects are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*ySGpvUseuuJiMf9_o5jnTA.png" /></figure><ul><li><strong>Pods</strong></li></ul><p>A Pod is a Kubernetes abstraction that represents a group of one or more application containers (such as Docker or rkt), and some shared resources for those containers such as volumes, DNS etc.</p><p>A Pod models an application-specific “logical host” and can contain different application containers which are relatively tightly coupled. For example, a Pod might include both the container with your Node.js app as well as a different container that feeds the data to be published by the Node.js webserver. The containers in a Pod share an IP Address and port space, are always co-located and co-scheduled, and run in a shared context on the same Node.</p><p>Example of a Pod definition:</p><pre><strong>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: myapp-pod<br>  labels:<br>    app: myapp<br>spec:<br>  containers:<br>  - name: myapp-container<br>    image: busybox<br>    command: [&#39;sh&#39;, &#39;-c&#39;, &#39;echo Hello Kubernetes! &amp;&amp; sleep 3600&#39;]</strong></pre><ul><li><strong>Services</strong></li></ul><p>A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service. The set of Pods targeted by a Service is (usually) determined by a Label Selector.</p><p>Kubernetes Pods are mortal. They are born and when they die, they are not resurrected. ReplicaSets in particular create and destroy Pods dynamically. While each Pod gets its own IP address, even those IP addresses cannot be relied upon to be stable over time. This leads to a problem: if some set of Pods (let’s call them backends) provides functionality to other Pods (let’s call them frontends) inside the Kubernetes cluster, how do those frontends find out and keep track of which backends are in that set? This is where the concept of a service kicks in.</p><p>Example of a Service definition:</p><pre><strong>kind: Service<br>apiVersion: v1<br>metadata:<br>  name: my-service<br>spec:<br>  selector:<br>    app: MyApp<br>  ports:<br>  - protocol: TCP<br>    port: 80<br>    targetPort: 9376</strong></pre><ul><li><strong>Volumes</strong></li></ul><p>In Docker, a volume is simply a directory on disk or in another Container. Lifetimes are not managed and until very recently there were only local-disk-backed volumes. Docker now provides volume drivers, but the functionality is very limited for now (e.g. as of Docker 1.7 only one volume driver is allowed per Container and there is no way to pass parameters to volumes).</p><p>A Kubernetes volume, on the other hand, has an explicit lifetime — the same as the Pod that encloses it. Consequently, a volume outlives any Containers that run within the Pod, and data is preserved across Container restarts. Of course, when a Pod ceases to exist, the volume will cease to exist, too. Perhaps more importantly than this, Kubernetes supports many types of volumes, and a Pod can use any number of them simultaneously.</p><p>At its core, a volume is just a directory, possibly with some data in it, which is accessible to the Containers in a Pod. How that directory comes to be, the medium that backs it, and the contents of it are determined by the particular volume type used.</p><p>AWS EBS Example configuration:</p><pre><strong>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: test-ebs<br>spec:<br>  containers:<br>  - image: k8s.gcr.io/test-webserver<br>    name: test-container<br>    volumeMounts:<br>    - mountPath: /test-ebs<br>      name: test-volume<br>  volumes:<br>  - name: test-volume<br>    </strong><strong><em># This AWS EBS volume must already exist.</em></strong><strong><br>    awsElasticBlockStore:<br>      volumeID: &lt;volume-id</strong><strong><em>&gt;<br>      fsType: ext4</em></strong></pre><ul><li><strong>Namespaces</strong></li></ul><p>Kubernetes namespaces can be seen as a logical entity used to represent cluster resources for usage of a particular set of users. This logical entity can also be termed as a virtual cluster. One physical cluster can be represented as a set of multiple such virtual clusters (namespaces). The namespace provides the scope for names. Names of resources within one namespace need to be unique.</p><p>By default, Kubernetes starts with the following three namespaces:</p><ul><li><strong>Default</strong>: Catch-all namespace for all objects not belonging to either of the kube-public or kube-system namespaces. The default namespace is used to hold the default set of pods, services, and deployments used by the cluster.</li><li><strong>Kube-public</strong>: Namespace for resources that are publicly available/readable by all</li><li><strong>Kube-system</strong>: Namespace for objects/resources created by Kubernetes systems</li></ul><h3>kubectl</h3><p><strong>Kubectl</strong> controls the Kubernetes Cluster. It is one of the key components of Kubernetes which runs on the workstation on any machine when the setup is done. It has the capability to manage the nodes in the cluster. Kubectl commands are used to interact and manage Kubernetes objects and the cluster. Some of the important commands are as follows:</p><ul><li><strong>kubectl apply</strong> − It has the capability to configure a resource by file or stdin.</li></ul><pre>$ kubectl apply –f &lt;filename&gt;</pre><ul><li><strong>kubectl attach</strong> − This attaches things to the running container.</li></ul><pre>$ kubectl attach &lt;pod&gt; –c &lt;container&gt;<br>$ kubectl attach 123456-7890 -c tomcat-conatiner</pre><ul><li><strong>kubectl autoscale</strong> − This is used to auto scale pods which are defined such as Deployment, replica set, Replication Controller.</li></ul><pre>$ kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min = MINPODS] --max = MAXPODS [--cpu-percent = CPU] [flags]<br>$ kubectl autoscale deployment foo --min = 2 --max = 10</pre><ul><li><strong>kubectl cluster-info</strong> − It displays the cluster Info.</li></ul><pre>$ kubectl cluster-info</pre><h3>Web UI (Dashboard)</h3><p>Dashboard is a web-based Kubernetes user interface. You can use Dashboard to deploy containerised applications to a Kubernetes cluster, troubleshoot your containerised application, and manage the cluster resources. You can use Dashboard to get an overview of applications running on your cluster, as well as for creating or modifying individual Kubernetes resources (such as Deployments, Jobs, DaemonSets, etc). For example, you can scale a Deployment, initiate a rolling update, restart a pod or deploy new applications using a deploy wizard.</p><p>Dashboard also provides information on the state of Kubernetes resources in your cluster and on any errors that may have occurred. Some sample images from the starter-kit we are going to discuss in part-2 of this series are as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vlSps_Gr55C43eNNMWmsgA.png" /><figcaption>Overview tab</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3hytjSkviPbh2e0K7D0_fg.png" /><figcaption>Resources tab</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KNGceER-4OX0IUg6OLVOiQ.png" /><figcaption>Services tab</figcaption></figure><h3>Conclusion</h3><p>This concludes the first part of the series on Kubernetes. I hope via this illustration, the readers would have received some insight into what this new technology entails.</p><p>The <a href="https://medium.com/the-resonant-web/kubernetes-in-practice-part-2-2d2a7290dd65">second part</a> of the series is a little more hands-on kind which will allow you to setup a Kubernetes cluster of your own in a matter of minutes with all the prominent Kubernetes objects covered in-depth along with practical examples of deploying a microservices based product on it.</p><p><strong><em>If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.</em></strong></p><p><strong><em>Did you know you could give up to 50 claps?</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d9cf6333d46" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-resonant-web/kubernetes-an-introduction-part-1-7d9cf6333d46">What is Kubernetes?</a> was originally published in <a href="https://medium.com/the-resonant-web">The Resonant Web</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot 2.0 — Project Structure and Best Practices (Part 2)]]></title>
            <link>https://medium.com/the-resonant-web/spring-boot-2-0-project-structure-and-best-practices-part-2-7137bdcba7d3?source=rss-95896dd07c5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7137bdcba7d3</guid>
            <category><![CDATA[spring-boot-2]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[springboot-best-practices]]></category>
            <category><![CDATA[learn-spring-boot-2]]></category>
            <category><![CDATA[spring-boot-project]]></category>
            <dc:creator><![CDATA[Arpit Khandelwal]]></dc:creator>
            <pubDate>Sun, 31 Mar 2019 09:37:51 GMT</pubDate>
            <atom:updated>2021-04-28T17:12:02.587Z</atom:updated>
            <content:encoded><![CDATA[<h3>Spring Boot 2.0 — Project Structure and Best Practices (Part 2)</h3><p>In continuation to the <a href="https://medium.com/the-resonant-web/spring-boot-2-0-starter-kit-part-1-23ddff0c7da2">first part</a> of the series on Spring Boot v2.0, this is the second and concluding part where we will go through the application project structure along with some important snippets and best practices which should be considered while doing Spring Boot based development.</p><p>Just a small disclaimer, after setting up this starter-kit and doing a thorough review, you might feel that this is not actually a micro service implementation using Spring Boot, I must say I completely agree with you it is not, rather this is a monolith developed in Spring Boot. This article is more about learning to code with the best practices in Spring Boot and ideally a reservation system should have more than just one service or code base, I promise I will soon be publishing a new series on Spring Boot’s usage in creating a distributed application with the help of Netflix OSS stack. Till then please treat this as a starting point and get the project structure and related tools aligned.</p><h3>Source Code</h3><p>The source code for this starter kit with MongoDB as the database can be cloned from the following GitHub repository :</p><p><a href="https://github.com/khandelwal-arpit/springboot-starterkit">khandelwal-arpit/springboot-starterkit</a></p><p>The MySQL version of the same is available here:</p><p><a href="https://github.com/khandelwal-arpit/springboot-starterkit-mysql">khandelwal-arpit/springboot-starterkit-mysql</a></p><h3>Application Structure</h3><p>Spring Boot is an opinionated framework that makes our life very easy since we don’t have to choose the versions of different dependencies based on the version of Spring framework, its all taken care of by Spring Boot.</p><p>I have tried to follow the same ideology while creating the project structure, at first it might seem like overwhelming, but do believe me once you start writing your pieces the structure will help you immensely by saving your time and thinking about questions which are already answered. The structure look as follows :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*qZojlcTl55yJROnjiVyzfA.png" /></figure><h4>Models &amp; DTOs</h4><p>The various models of the application are organised under the <em>model</em> package, their DTOs(data transfer objects) are present under the <em>dto</em> package. There are different opinions about whether we should use DTOs or not, I belong to the set of minds who think we definitely should and not using DTOs makes your model layer very tightly coupled with the UI layer and that is something that no enterprise project should ever get into.</p><p>DTOs let us transfer only the data that we need to share with the user interface and not the entire model object that we may have aggregated using several sub-objects and persisted in the database. The mapping of models to the DTOs can be handled using <a href="http://modelmapper.org/">ModelMapper</a> utility, however its only useful when your DTO is almost similar (literally) to the concerned models which is not always the case and hence I prefer using custom mapper classes. You can find some examples under the “dto/mapper” package.</p><p>For ex, let’s take a look at how our Trip.java model is organised :</p><p><strong>Trip.java</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ae9962b18c0ad20c25370c878959fa1/href">https://medium.com/media/0ae9962b18c0ad20c25370c878959fa1/href</a></iframe><p>As we can see the model has references to the Stop, Bus and Agency collections. This is required to maintain the relationship between these various collections with a Trip. While this is not the same as a foreign key concept in MySQL and there is no default cascading applied here, it does give us a way to emulate the same in MongoDB. The corresponding data transfer object looks as follows :</p><p><strong>TripDto.java</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba38d004fa69f9c15cc130b311e9186c/href">https://medium.com/media/ba38d004fa69f9c15cc130b311e9186c/href</a></iframe><p>As I described earlier, DTOs are not meant to be the mirror image of their model counterparts, rather they should be a reflection of what the user interface or the api response demands. In this case there was no sense in making a composition relationship between a Trip and its Bus or Agency or the Stops, rather their primary keys actually can do the trick. This not only decouples these DTOs, it also reduces the overall size of the response packet that will travel over HTTP from server to the client.</p><p>The very next question can be how to convert the model POJO to this DTO, well there are more than one ways to do it but my preference is to be explicit and DIY as follows :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4eea2fbeca882851d738550c5f0db23/href">https://medium.com/media/d4eea2fbeca882851d738550c5f0db23/href</a></iframe><h4>Services and DAOs</h4><p>The data access objects (DAOs) are present in the <em>repository</em> package. They are all extensions of the MongoRepository interface helping the service layer to persist and retrieve the data from MongoDB.</p><p>The service layer is defined under the <em>service</em> package, considering the current case study it made sense to create two basic services :</p><ol><li>UserService and</li><li>BusReservationService</li></ol><p>Explaining each method in these services is out of the scope of this blog post, however I will list out the interfaces showing the operations supported by these services.</p><p><strong>UserService.java</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2be7d3927e794bb6f1196c916109d845/href">https://medium.com/media/2be7d3927e794bb6f1196c916109d845/href</a></iframe><p><strong>BusReservationService.java</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1f747ab8681675b129d5805da70632c/href">https://medium.com/media/f1f747ab8681675b129d5805da70632c/href</a></iframe><p>Apart from noticing the method naming conventions, I am sure you have noticed that the service layer never accepts a model as input and never ever returns one either. This is another best practice that Spring developers should follow in a layered architecture. The controller layer interacts with the service layer to get a job done whenever it receives a request from the view or api layer, when it does it should not have access to the model objects and should always converse in terms of neutral DTOs.</p><p>When the service receives a DTO object, it should try to make sense out of it by querying for the corresponding model object from database and then do the necessary operation and create a response DTO to be sent back to the calling service. This approach lets you change the view and models independently without having to worry one might break the other.</p><p>To illustrate the aforementioned approach, let’s take an example of a service operation “updateProfile” that is used to update user’s information. The method definition look as follows :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b9e47c43bbd3b24a079eae9b39bdf4e/href">https://medium.com/media/5b9e47c43bbd3b24a079eae9b39bdf4e/href</a></iframe><h4>Security</h4><p>The security setting are present under the <em>config</em> package and the actual configurations are done under the class present in the <em>security</em> package. The application has different security concepts for the admin portal and the REST APIs, for the portal I have applied the default spring session mechanism that is based on the concept of sessionID and cookies. For the REST APIs I have used JWT token based authentication mechanism.</p><p>The security for web and apis are configured both in the same class <em>MultiHttpSecurityConfig</em>.java. It has two static classes extending from <em>WebSecurityConfigurerAdapter</em> which let’s us configure the http security for incoming requests.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cec04ddbb2a4c998cf1a770323178281/href">https://medium.com/media/cec04ddbb2a4c998cf1a770323178281/href</a></iframe><p>The @<strong>Order</strong> annotation allows the requests to be scanned through the different configurations in the specified order. So an API request goes through the ApiWebSecurityConfigurationAdapter and gets absorbed there, however an Admin request first goes through it but since it doesn’t match the criteria Spring Security tries to make it go through the next configuration with an immediate higher order which in this case is FormLoginWebSecurityConfigurerAdapter.</p><p>The API requests have to go through the ApiJWTAuthenticationFilter and ApiJWTAuthorizationFilter which are responsible for the creation and validation of the JWT token issued at the time of login. If you are wondering which URL should be used for API authentication (login) then here it is :</p><p><a href="http://localhost:8080/api/auth">http://localhost:8080/api/auth</a></p><p>And if you are wondering how this was configured, then the answer lies in the class ApiJWTAuthenticationFilter, its constructor has the following information coded :</p><pre><strong>this</strong>.setRequiresAuthenticationRequestMatcher(<strong>new </strong>AntPathRequestMatcher(<strong>&quot;/api/auth&quot;</strong>, <strong>&quot;POST&quot;</strong>));</pre><p>This tells <em>AbstractAuthenticationProcessingFilter</em> to hook the <em>AuthenticationRequestMatcher</em> with “/api/auth” path for the API requests.</p><p>The admin application is only allowed for those users whose role is “ADMIN”. All the passwords are encrypted using BCryptPasswordEncoder and we can never see their actual value once they are stored in the database.</p><h4>Controllers</h4><p>Last, but the most important part is the controller layer. It binds everything together right from the moment a request is intercepted till the response is prepared and sent back. The controller layer is present in the <em>controller</em> package, the best practices suggest that we keep this layer versioned to support multiple versions of the application and the same practice is applied here.</p><p>For now code is only present under v1 but over the time I expect to have different versions having different features. The Admin portal related controllers are present in the <em>ui</em> package and its concerning form command objects are located under the <em>command</em> package. The REST API controllers are located under the <em>api</em> package and the corresponding request classes are located under the <em>request</em> package.</p><p>The admin panel controllers work on the Spring WebMVC concept. They respond to incoming web requests with Spring’s ModelAndView objects containing the data to be shown on the concerned views/forms and the name of the view to be rendered, an example from DashboardController class is as follows :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6e410a76466336f1c35ddef5dff852d/href">https://medium.com/media/a6e410a76466336f1c35ddef5dff852d/href</a></iframe><h4>Request and Form Commands</h4><p>Again, there are different opinions amongst the developer community regarding the usage of separate classes for mapping the incoming request vs using the DTOs vs using the models directly, I am personally a fan of segregating them as far as possible to promote loose coupling amongst UI and controller layer.</p><p>The request objects and the form commands do give us a way to apply an additional level of validations on the incoming requests before they get converted to the DTOs which transfer valid information to the service layer for persistence and data retrieval. We could use DTOs here and some developers prefer that approach as it reduces some additional classes, however I usually prefer to keep the validation logic separate from the transfer objects and hence am inclined to use the request/command objects ahead of them.</p><p>An example command pattern based class BusFormCommand looks as follows :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89de42d49357d239b0cd0257215b39f1/href">https://medium.com/media/89de42d49357d239b0cd0257215b39f1/href</a></iframe><p>And an example of a request sent via API, BookTicketRequest is as follows :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4dc296000fbeb0af07f23c64643e5179/href">https://medium.com/media/4dc296000fbeb0af07f23c64643e5179/href</a></iframe><p>The static resources are grouped under the <em>resources</em> directory. All the UI objects and their styling aspects can be located here.</p><h3>Lombok</h3><p>One of the biggest complaints against Java is how much noise can be found in a single class. Project <a href="https://projectlombok.org/">Lombok</a> saw this as a problem and aims to reduce the noise of some of the worst offenders by replacing them with a simple set of annotations. You will find Lombok employed everywhere in this starter kit, it has actually helped in reducing the lines of code, save a lot of development time and effort and make the code a lot more readable. Some of the most important annotations that I prefer using are :</p><p>@Getter /@Setter</p><p>Never write public int getFoo() {return foo;} again.</p><p>@ToString</p><p>No need to start a debugger to see your fields: Just let lombok generate a toString for you!</p><p>@EqualsAndHashCode</p><p>Equality made easy: Generates hashCode and equals implementations from the fields of your object..</p><p>@Data</p><p>All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and</p><p>@RequiredArgsConstructor!</p><p>In essence, something as verbose as :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae3454fa4b90b884f0fa575192cc5fea/href">https://medium.com/media/ae3454fa4b90b884f0fa575192cc5fea/href</a></iframe><p>Could be written simply as :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6e99b400f0da43594dd2100b3354322/href">https://medium.com/media/a6e99b400f0da43594dd2100b3354322/href</a></iframe><p>You can very well see the difference, not only the later looks much cleaner, we have come down from 59 lines of boring POJO to a 8 lines based java class file.</p><h3>API Response and Exception Handling</h3><p>I have tried to experiment a bit with the RuntimeExceptions and come up with a mini framework for handling the entire application’s exceptions using a few classes and the properties file. If you carefully observe the <em>exception</em> package, it consists of two enums :</p><ol><li>EntityType and</li><li>ExceptionType</li></ol><p>The EntityType enum contains all the entity names that we are using in the system for persistence and those which can result in a run time exception. The ExceptionType enum consists of the different entity level exceptions such as the EntityNotFound and DuplicateEntity exceptions.</p><p>The BRSException class has two static classes <em>EntityNotFoundException</em> and <em>DuplicateEntityException</em> which are the two most widely thrown exceptions from the service layer. It also contains a set of overloaded methods <em>throwException</em> which take the EntityType, ExceptionType and arguments to come up with a formatted message whose template is present under the <em>custom.properties</em> file.</p><p>Using this class I was able to empower the entire services layer to throw a variety of entity exceptions in a uniform manner without cluttering the code base with all sorts of NOT_FOUND and DUPLICATE entity exceptions.</p><p>For example, while login if you try to use a email address which is not registered, an exception is raised and thrown using the following single line of code -</p><pre>throw exception(USER, ENTITY_NOT_FOUND, userDto.getEmail());</pre><p>This results in clubbing the names of these two enum elements USER(“user”) and ENTITY_NOT_FOUND(“not.found”) and coming up with a key <strong>user.not.found</strong> which is present in the custom.properties files as follows :</p><pre>user.not.found=Requested user with email - {0} does not exist.</pre><p>By simply replacing the {0} param with the email address included in the thrown exception you can get a well formatted message to be shown to the user or to be sent back as the response of the REST API call. The definition of the BRSException class is as follows :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/decd450dada146ef9623d4404a7cc23c/href">https://medium.com/media/decd450dada146ef9623d4404a7cc23c/href</a></iframe><p>Another important part of this mini framework is the <em>CustomizedResponseEntityExceptionHandler</em> class which looks as follows :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95300645a286179ca6b83fc3556f936a/href">https://medium.com/media/95300645a286179ca6b83fc3556f936a/href</a></iframe><p>This class takes care of these RuntimeExceptions before sending a response to the API requests. Its a controller advice that checks if a service layer invocation resulted in a EntityNotFoundException or a DuplicateEntityException and sends an appropriate response to the caller.</p><p>Last, the API response are all being sent in a uniform manner using the <em>Response</em> class present in the dto/response package. This class allows us to create uniform objects which result in a response as shown below (this one is a response for the “api/v1/reservation/stops” api) :</p><pre><br>{<br> “status”: “OK”,<br> “payload”: [<br>   {<br>    “code”: “STPA”,<br>    “name”: “Stop A”,<br>    “detail”: “Near hills”<br>   },<br>   {<br>    “code”: “STPB”,<br>    “name”: “Stop B”,<br>    “detail”: “Near river”<br>   }<br> ]<br>}</pre><p>And when there is an exception (for example searching for a trip between two stops which are not linked by any bus) the following responses are sent back (result of “api/v1/reservation/tripsbystops” GET request) :</p><pre><br>{<br>  “status”: “NOT_FOUND”,<br>  “errors”: “No trips between source stop — ‘STPD’ and destination  stop — ‘STPC’ are available at this time.”<br>}</pre><pre><br>{<br>  “status”: “NOT_FOUND”,<br>  “errors”: <br>  {<br>    “timestamp”: “2019–03–13T07:47:10.990+0000”,<br>    “message”: “Requested stop with code — STPF does not exist.”<br>  }<br>}</pre><p>As you can observe, both type of responses, one with a HTTP-200 and another with HTTP-404 the response payload doesn’t change its structure and the calling framework can blindly accept the response knowing that there is a status and a error or payload field in the JSON object.</p><h3>API Documentation</h3><p>It is as important to document your work (as is its development) and make sure your Spring Boot APIs are available in a readable manner for frontend teams(internal) or external consumers. The tool for API documentation used in this starter kit is <a href="https://swagger.io/">Swagger2</a>, you can open the same inside a browser at the following url -</p><p><a href="http://localhost:8080/swagger-ui.html">http://localhost:8080/swagger-ui.html</a></p><p>It will present you with a well structured UI which has two specs :</p><p>1. User<br>2. BRS</p><p>You can use the User spec to execute the login api for generating the Bearer token. The token then should be applied in the “Authorize” popup which will by default apply it to all secured apis (get and post both). Please bear in mind that you should append the word “Bearer” followed by a space, in front of the token before applying it in the authorize dialog box.</p><p>The configuration of Swagger is being taken care of by class <em>BrsConfiguration</em>. I have defined two specs there with the help of “swaggerBRSApi” and “swaggerUserApi” methods. Since the login part is by default taken care of by Spring Security we don’t get to expose its apis implicitly as the rest of the apis defined in the system and for the same reason I have defined a controller in the config package with the name “FakeController” :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/daffd39df4c8fdf3b6ba38839bca1d13/href">https://medium.com/media/daffd39df4c8fdf3b6ba38839bca1d13/href</a></iframe><p>Its purpose is to facilitate the generation of swagger documentation for login and logout apis, it will never come into existence during the application life cycle as the “/api/auth” api is being handled by the security filters defined in the code base. Here are some sample screenshots to help you visualise things a little better :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*R-VGVO5iBj7n8RTWmUgzxA.png" /><figcaption>Swagger UI</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*dyTeRrf_bAqQQBR8lpyIvA.png" /><figcaption>/api/auth login, courtesy FakeController</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*QZ96PHvrQ24jTXVy1a-ueQ.png" /><figcaption>Authorize dialog for registering Bearer tokens</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*3aZdqVOk0-OfAX2jjbt_zg.png" /><figcaption>BRS APIs</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*WZE0_eNVpxKZh19E4yG9mQ.png" /><figcaption>BRS APIs listed out</figcaption></figure><p>To use the Swagger UI and execute the secured APIs, you will need to first operate the “/api/auth” from the User spec and generate a Bearer token. Once the token is issued, you can register it in the Authorize popup and then move on to the BRS spec to execute the secured APIs. If you don’t register the token then you will keep on receiving the HTTP 401 error.</p><p>You may get into issues while trying out the Swagger-Ui for those HTTP-GET requests which have a body with parameters, don’t worry it’s not the fault of the code it is Swagger’s decision to stop supporting body params for GET and DELETE requests. As a workaround, you may copy the curl request from swagger and execute it inside a terminal window and that should work fine or you may opt to go for Postman or a similar tool as they don’t (yet) enforce such a tight restriction. In my views, as long as the Open API specifications don’t restrict the body params in GET requests, tools like Swagger shouldn’t too, however its a call that they need to make and not us as developers.</p><h3>UI Architecture</h3><p>The user interface for the admin portal is designed using material design with the help of Bootstrap and responsive web app concept. The UI is server side rendered using Thymeleaf templates (preferred templating engine in Spring).</p><p>The standard way of working with Thymeleaf is to use includes. This quite often leads to repetitive code, especially, when a website has many pages and each page has several reusable components (e.g. header, navigation, sidebar, and footer). It is repetitive as each content page has to include the same fragments at the same locations. This also has a negative effect when the page layout changes, e.g. when putting the sidebar from the left to the right side.</p><p>The decorator pattern used by the Thymeleaf Layout dialect solves these issues. In the context of template engines, the decorator pattern doesn’t work with includes on content pages anymore, but it refers to a common template file. Each page basically only provides the main content and by describing which basic template to use the template engine can build the final page. The content is being decorated with the template file.This approach has advantages compared to the standard way of including fragments:</p><ul><li>The page itself only has to provide the content.</li><li>As a template file is being used to build the final page global changes can be applied easily.</li><li>The code becomes shorter and cleaner.</li><li>As each content page references which template file to use, it is easy to use different templates for different areas of the application (e.g. public area and admin area).</li></ul><p>The layout for admin portal is designed as follows :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*w7pdtGbGXYGSbELX3CGUDw.png" /><figcaption>Blog Like Layout</figcaption></figure><p>The individual areas in this layout serve the following purpose :</p><ul><li><strong>Header</strong> : this fragment is used for the static imports (CSS and JavaScript), the title and meta tags.</li><li><strong>Navigation</strong> : the navigation bar with user profile at top right.</li><li><strong>Content</strong> : the content placeholder that will be replaced by the requested page.</li><li><strong>Sidebar</strong> : a sidebar for additional information.</li><li><strong>Footer</strong> : the footer area that provides the copyright info.</li></ul><p>These components can be located in the resources/templates directory at the root as well as under the sub-directories fragments and layout. The content area in this layout will host the following pages :</p><ul><li>Dashboard</li><li>Agency</li><li>Bus</li><li>Trip</li><li>Profile</li></ul><p>Also, an error page for any unhandled exception is designed with the name “error.html”. The login and signup pages are designed separately from the portal accessible to a logged-in user.</p><h3>Running the Server Locally</h3><p>To be able to run this Spring Boot app you will need to first build it. To build and package the Spring Boot app into a single executable Jar file with a Maven, use the below command. You will need to run it from the project folder which contains the pom.xml file.</p><pre>maven package</pre><p>or you can also use</p><pre>mvn clean install</pre><p>To run the app from a terminal window you can you the java -jar command. This is provided your Spring Boot app was packaged as an executable jar file.</p><pre>java -jar target/springboot-starterkit-0.0.1-SNAPSHOT.jar</pre><p>You can also use Maven plugin to run the app. Use the below example to run your Spring Boot app with Maven plugin :</p><pre>mvn spring-boot:run</pre><p>You can follow any/all of the above commands, or simply use the run configuration provided by your favourite IDE and run/debug the app from there for development purposes. Once the server is setup you should be able to access the admin interface at the following URL :</p><p><a href="http://localhost:8080/">http://localhost:8080</a></p><p>And the REST APIs can be accessed over the following base-path :</p><p><a href="http://localhost:8080/api/">http://localhost:8080/api/</a></p><p>Some of the important api endpoints are as follows :</p><ul><li><a href="http://localhost:8080/api/v1/user/signup">http://localhost:8080/api/v1/user/signup</a> (HTTP:POST)</li><li><a href="http://localhost:8080/api/auth">http://localhost:8080/api/auth</a> (HTTP:POST)</li><li><a href="http://localhost:8080/api/v1/reservation/stops">http://localhost:8080/api/v1/reservation/stops</a> (HTTP:GET)</li><li><a href="http://localhost:8080/api/v1/reservation/tripsbystops">http://localhost:8080/api/v1/reservation/tripsbystops</a> (HTTP:GET)</li><li><a href="http://localhost:8080/api/v1/reservation/tripschedules">http://localhost:8080/api/v1/reservation/tripschedules</a> (HTTP:GET)</li><li><a href="http://localhost:8080/api/v1/reservation/bookticket">http://localhost:8080/api/v1/reservation/bookticket</a> (HTTP:POST)</li></ul><h3>Docker Container</h3><p>Please use the following command to build the container image :</p><pre>docker build -t spring/starterkit .</pre><p>And the following command to run the container :</p><pre>docker run -p 8080:8080 spring/starterkit</pre><p>Please <strong>note</strong> when you build the container image and if mongodb is running locally on your system, you will need to provide your system’s IP address (or cloud hosted database’s IP) in the application.properties file (or env vars) to be able to connect to the database from within the container.</p><h3>Conclusion</h3><p>As you can see, the starter-kit is designed to save you hours of coding time by giving you a streamlined, efficient and clean-coded interface and architecture to base your own development on. Try it out and be sure to let me know your feedback.</p><p>I hope this series of two articles has helped you in improving your Spring Boot skillset and that it has given you a solid platform to begin writing your next Spring Boot app.</p><p>Happy coding everyone!</p><p><strong><em>Did you know you could give up to 50 claps?</em></strong></p><p><strong><em>If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.</em></strong></p><p><strong><em>If you find this article and associated GitHub project useful, I’d appreciate if you’d buy me a cofee.</em></strong></p><figure><a href="https://www.buymeacoffee.com/arpitk"><img alt="" src="https://cdn-images-1.medium.com/max/170/1*9vg3-OY14aZN1UpKwIxxZg.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7137bdcba7d3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-resonant-web/spring-boot-2-0-project-structure-and-best-practices-part-2-7137bdcba7d3">Spring Boot 2.0 — Project Structure and Best Practices (Part 2)</a> was originally published in <a href="https://medium.com/the-resonant-web">The Resonant Web</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot 2.0 — Project Structure and Best Practices (Part 1)]]></title>
            <link>https://medium.com/the-resonant-web/spring-boot-2-0-starter-kit-part-1-23ddff0c7da2?source=rss-95896dd07c5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/23ddff0c7da2</guid>
            <category><![CDATA[springboot-best-practices]]></category>
            <category><![CDATA[learn-spring-boot]]></category>
            <category><![CDATA[spring-boot-project]]></category>
            <category><![CDATA[spring-boot-2]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Arpit Khandelwal]]></dc:creator>
            <pubDate>Sun, 31 Mar 2019 09:36:01 GMT</pubDate>
            <atom:updated>2021-04-28T17:10:07.415Z</atom:updated>
            <content:encoded><![CDATA[<h3>Spring Boot 2.0 — Project Structure and Best Practices (Part 1)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FYW8jxpzlZXRIfqS6oV47A.jpeg" /></figure><blockquote>The Spring Framework has been around for over a decade and has found a place as the de facto standard framework for developing Java applications. With such a long and storied history, some might think that Spring has settled, resting on its laurels, and is not doing anything new or exciting. Some might even say that Spring is legacy and that it’s time to look elsewhere for innovation.</blockquote><blockquote>Some would be wrong.</blockquote><blockquote>There are many exciting new things taking place in the Spring ecosystem, including work in the areas of cloud computing, big data, schema-less data persistence, reactive programming, and client-side application development. Perhaps the most exciting, most head-turning, most game-changing new thing to come to Spring in the past year or so is Spring Boot. Spring Boot offers a new paradigm for developing Spring applications with minimal friction.</blockquote><blockquote>— Craig Walls</blockquote><h3>Preface</h3><p>I got the inspiration to write this article (first in the series of two) when I was trying to goole my way through the internet to find an ideal project structure (boilerplate) for Spring Boot v2 based projects. Even though <a href="https://github.com/spring-io/initializr/">Spring Initializr</a> does a decent job to let us create a basic pom.xml (similar to package.json for js developers) using a handy graphical user interface, it doesn’t quite create a complete project structure for us that we can leverage and build our application upon.</p><p>Also the fact that there are tons of undocumented github contributions made by spring developers and plethora of misleading articles online, I finally decided to study what’s new in Spring Boot v2 and come up with a project structure that is not just a starting point, but an almost complete bus reservation system of its own written completely in Spring Boot and the user interface designed using <a href="https://www.thymeleaf.org/">Thymeleaf</a>.</p><p>In these two articles I would try to cover the basics of the application that I have built along with details on the code structure, various artefacts used for its creation along with some important mini frameworks that I built on the fly to support some of the cross cutting concerns.</p><h3>Spring Boot</h3><p>I suppose we all are well aware of the strengths and weaknesses of Spring Framework, specially the complex XML configurations which proved to be a nightmare for enterprise grade applications with hundreds of beans and services to support a requirement.</p><p>A lot of work has gone into Spring Boot to reduce complexity and dependencies, which largely alleviates our previous reservations. If you live in a Spring ecosystem and are moving to microservices, Spring Boot is now the obvious choice. Spring Boot allows easy set up of standalone Spring-based applications. It’s ideal for pulling up new microservices and easy to deploy. It also makes data access less of a pain due to the hibernate mappings with much less boilerplate code.</p><p>Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. It takes an opinionated view of the Spring platform and third-party libraries so that we can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration, it has following distinguishing features:</p><ul><li>Easy dependency Management</li><li>Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)</li><li>Provide opinionated ‘starter’ dependencies to simplify your build configuration</li><li>Automatically configure Spring and 3rd party libraries whenever possible</li><li>Provide production-ready features such as metrics, health checks and externalised configuration</li><li>Absolutely no code generation and no requirement for XML configuration</li><li>DevTools to auto restart server on code/config updates</li><li>Easy management of profile specific properties</li></ul><h3>What’s New in Spring Boot v2.0?</h3><p>Spring Boot 2.0 is the first major revision of Spring Boot since 1.0 was released years ago and it’s also the first GA version of Spring Boot that provides support for <a href="https://spring.io/blog/2017/09/28/spring-framework-5-0-goes-ga">Spring Framework 5.0</a>.</p><ul><li>Minimum version of Java required is <strong>Java 8</strong>.</li><li>It supports <strong>Java 9</strong> too.</li><li>Out of the box support for “Reactive Web Programming” with <strong>Spring WebFlux</strong>.</li><li>Auto-configuration and starter POMs for reactive Spring Data Cassandra, MongoDB, Couchbase, and Redis.</li><li>Finally support for <strong>HTTP/2</strong>.</li><li>A freshly written Gradle plugin with minimum requirement of <strong>Gradle 4.x</strong></li><li>Support for <strong>Kotlin 1.2.x</strong></li><li>Out of the box support for <strong>InfluxDB</strong>.</li></ul><p>A complete list of changes along with basic upgrade guide is located <a href="https://therealdanvega.com/blog/2018/03/01/what-is-new-spring-boot-2">here</a>.</p><h3>Bus Reservation System</h3><p>This starter kit focuses on how to use Spring Boot, following all the best practices that are recommended by Spring Framework 5.0 and Spring Boot v2.0, ensure the code is loosely coupled and that all the layers in the application are completely independent of each other and talk using neutral objects. While writing this kit, I have done sufficient research around the code structure and usage of appropriate design patterns to make this as an excellent starting point to begin coding your own web application.</p><p>The kit would have been incomplete if it did not have a concrete use case to implement, here I have taken a case study of a very simple yet worthy <strong><em>Bus Reservation System</em></strong> and tried to implement an Admin portal which can be operated via browser and a series of REST APIs to interact with the system using mobile applications or frontend applications. The complete systems has two prominent actors :</p><p>1. Admin user<br>2. End user</p><p>The <em>Admin</em> user can access this application on browser (laptop or mobile/tablet, doesn’t really matter as this is built using bootstrap, material design and is completely responsive) and can perform the following actions :</p><p>1. Signup<br>2. Login (Spring sessions)<br>3. Update their profile<br>4. Create an agency<br>5. Add buses to the agency<br>6. Add trips consisting of predefined stops and buses<br> <br>The <em>End</em> user can use their mobile application (yet to be built, however the REST APIs are ready and could be used via Postman or Swagger) to perform the following actions :</p><p>1. Signup<br>2. Login (and get a JWT token) <br>3. List all available stops<br>4. Search a trip between any two stops<br>5. Filter search results with a date option<br>6. Book a ticket for a given trip schedule</p><p>Admin interface and REST APIs both have their independent authentication mechanisms, the web application uses the cookie based authentication (provided by default by Spring security) and the REST API uses the JWT authentication for access. This application assumes the availability of ‘MongoDB’ installation on the localhost where the server will run or the use of docker-compose to boot up a mongodb container and link the application with it within the realm of docker.</p><p>Any changes that the admin users will do on the web portal will impact the search results of the end users, there will be certain use cases which you may find missing here, I hope you will appreciate that the overall idea was to present a way to create such an application completely inside the realm of Spring Boot and not to actually building a fully operational reservation system.</p><p>The admin user interface is completely written in material design using Bootstrap v4 and is responsive to suite a variety of devices. The template engine used to render the admin views is Thymeleaf since the library is extremely extensible and its natural templating capability ensures templates can be prototyped without a back-end — which makes development very fast when compared with other popular template engines such as JSP.</p><h3>Database Schema</h3><p>The current schema looks as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XgcVrBtvbIZmqvaIOCC1FA.png" /><figcaption>BRS Database Schema (Mongodb)</figcaption></figure><ul><li>The authentication and authorization is governed by <em>User</em> and <em>Role</em> collection.</li><li>The <em>Agency</em> collection keeps the details of agency along with who owns it.</li><li>The <em>Stop</em> collection keeps the data about all the stops in the system.</li><li>The <em>Bus</em> collection has the data of all the buses for various agencies along with their capacity and make years.</li><li>The <em>Trip</em> and <em>TripSchedule</em> collections keep the information about all the trips that agency owners create within the system. Information like source and destination stops, journey time, data of travel, tickets sold so far and the available seats is collected in them.</li><li>Last, the <em>Ticket</em> collection keeps information about all the tickets issued in the application for various trips.</li></ul><p>Some of you might wonder the reason behind choosing String data type for the dates instead of Date data type, well, to be honest MongoDB has its own way to deal with dates and tends to use the UTC format which led to some avoidable complications within the codebase and hence the preferred approach was to go with a String instead and do some calculations in house within the java code if needed.</p><h3>Libraries/Tools Used</h3><p>Following libraries were used during the development of this starter kit :</p><ul><li><strong>Spring Boot</strong> : Server side framework</li><li><strong>Docker</strong> : Containerising framework</li><li><strong>MongoDB</strong> : NoSQL database</li><li><strong>Swagger</strong> : API documentation</li><li><strong>Thymeleaf</strong> : Templating engine</li><li><strong>Material</strong> : UI themeing/design</li><li><strong>Bootstrap</strong> : CSS framework</li><li><strong>JWT</strong> : Authentication mechanism for REST APIs</li></ul><h3>User Interface</h3><p>Here are the various screens of the Admin portal that you should be able to use once the application is setup properly :</p><p><strong>Login</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ki0ge--qbKAELD-OiP2KJg.png" /></figure><p><strong>Signup</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Byq_dFymROQQeMBDfarebg.png" /></figure><p><strong>Dashboard</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nm0fVwanwgobYiPpYYY1JA.png" /></figure><p><strong>Agency</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UkNcHAjiE3f0Q_8RK8aGzQ.png" /></figure><p><strong>Buses</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7nvf1TF9bxkNim_KJMrmGw.png" /></figure><p><strong>Trips</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wwKtKSf2efy5nwvRnKK09Q.png" /></figure><p><strong>Profile</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*79v4a-BQ40N4Obenn9IpMQ.png" /></figure><h3>Part 2</h3><p>Thanks for reading the first part from a series of two articles on Spring Boot. If this is enough to raise your interest in using this impressive web framework, be sure to check out the <strong>second part</strong> <a href="https://medium.com/the-resonant-web/spring-boot-2-0-project-structure-and-best-practices-part-2-7137bdcba7d3">here</a>.</p><p>It contains a detailed walkthrough of the starter-kit app built by me, which provides an excellent starting point for all Spring Boot developers to use in their own production environments. It will cover topics such as the project structure, dockerisation, exception handling, web security, decorated Thymeleaf view layout etc.</p><p><strong><em>Did you know you could give up to 50 claps?</em></strong></p><p><strong><em>If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.</em></strong></p><p><strong><em>If you find this article and associated GitHub project useful, I’d appreciate if you’d buy me a cofee.</em></strong></p><figure><a href="https://www.buymeacoffee.com/arpitk"><img alt="https://www.buymeacoffee.com/arpitk" src="https://cdn-images-1.medium.com/max/170/1*9vg3-OY14aZN1UpKwIxxZg.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23ddff0c7da2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-resonant-web/spring-boot-2-0-starter-kit-part-1-23ddff0c7da2">Spring Boot 2.0 — Project Structure and Best Practices (Part 1)</a> was originally published in <a href="https://medium.com/the-resonant-web">The Resonant Web</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to choose the right technologies for web applications?]]></title>
            <link>https://medium.com/the-resonant-web/best-tech-stacks-for-web-development-e35229e0b473?source=rss-95896dd07c5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/e35229e0b473</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[best-stacks-for-web]]></category>
            <category><![CDATA[what-is-frontend]]></category>
            <category><![CDATA[what-is-backend]]></category>
            <category><![CDATA[frontend-vs-backend]]></category>
            <dc:creator><![CDATA[Arpit Khandelwal]]></dc:creator>
            <pubDate>Wed, 13 Feb 2019 11:20:52 GMT</pubDate>
            <atom:updated>2019-06-04T05:46:28.846Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever since ‘Internet’ was invented in the year 1990, followed by the birth of a sophisticated client and server side programming language ‘Java’ in 1995, a whole new dimension of sharing knowledge emerged in the space of Information Technology via websites (both static and dynamic ones). With this, a new term ‘Web-Development’ coined itself which has become the top most role that software engineers choose today (according to stack overflow’s 2017 developer survey) as their career in the field of Information Technology.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*5uQB_ZBEkmIfmZ5DM18ZNg.png" /></figure><p>I recently came across a question from a peer in the organisation — “<em>what are the top web development stacks and how do we decide the right stack for the problem in hand?</em>” I wanted to redirect the question towards internet, so I started looking for a decent guide that could help in answering this question. Unfortunately, I couldn’t come across a single writeup that answered the different facets related to this question. This was the motivation behind coming up with a definitive guide here that lists some of the key concepts of web development and the tools and technologies available today to achieve the goal of a scalable web application.</p><p>Before you start reading any further, please bear in mind this article is not about a list of ‘hottest languages or tech stacks’ for web application development throwing a cluster of buzzwords at you, rather this is a practical review of the trends in the industry backed up by statistics from various online sources and in-depth research mixed with years of experience working in the field of web applications.</p><pre><strong>According to wikipedia,</strong> “<em>Web development is a broad term for the work involved in developing a website for the Internet (World Wide Web) or an intranet (a private network). Web development can range from developing the simplest static single page of plain text to the most complex web-based internet applications (or just ‘web apps’) electronic businesses, and social network services. A more comprehensive list of tasks to which web development commonly refers, may include web engineering, web design, web content development, client liaison, client-side/server-side scripting, web server and network security configuration, and e-commerce development. Among web professionals, “web development” usually refers to the main non-design aspects of building web sites: writing markup and coding.</em>”</pre><p>While I agree with the entire description provided by wikipedia, the most important part to me was the closing line in the above para. As a web professional the first thing that we need to understand is the composition of a typical web application –</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZqpUDXWhPfQMUY-yCEUSEw.png" /><figcaption>Web Application Components</figcaption></figure><h3>Front-end</h3><p>This is the interface that a developer creates for interaction with the main application. This can be as simple as a static web page designed in HTML or a complex implementation done using a technology based on virtual dom (such as React.js) for browsers or a mobile application developed natively using android and iOS technologies or a hybrid platform such as React Native.</p><h3>Back-end</h3><p>This refers to the server side master program running on single or distributed topology and responsible for catering to the stateful and stateless requests sent from the front-end. There is a huge spectrum of technologies available currently to write the back-end implementation of a web application, we will be reviewing some of those in the sections to come.</p><h3>Datastore</h3><p>This is the organised collection of information that the front-end usually demands on from the back-end. The datastore (a.k.a. database) contains every persistent information that is useful in the present date as well in future to come up with a custom metrics based on the data gathered over a given period of time. There are two primary divisions in terms of the ways to store the data and those are-</p><ul><li>SQL database and</li><li>NoSQL database</li></ul><h3>Cloud Hosting Service</h3><p>This is the information technology (IT) paradigm that enables ubiquitous access to shared pools of configurable system resources and higher-level services that can be rapidly provisioned with minimal management effort, often over the Internet. Cloud computing relies on sharing of resources to achieve coherence and economies of scale, similar to a public utility. The idea in general is to take the complexities of setting decent hardware for deploying the web applications to another level by abstracting the details from routine back-end dev and letting domain experts like AWS and Azure take care of these. This not just reduces the overall cost, it also makes the application secure in a lot of sense by applying default security configurations while deploying the applications. The time it takes to spin a new server on cloud hosting services is close to minutes as compared to hours, if you do the same task in house.</p><h3>Front-end Technologies</h3><pre><em>“A front-end dev is responsible for the interior design of a house that’s been built by a back-end dev.</em>”<br>                                                            -twitter</pre><p>As mentioned earlier, the front-end of a web application is the part that end users interact with to gain information of the system or to share their thoughts.</p><p>There are two promising technologies that I would like to present to the reader in the context of front-end development in current scenario –</p><h3>React.js</h3><p><a href="https://reactjs.org/">React.js</a> is a front-end library based on JavaScript and built and maintained by Facebook. It abstracts away the DOM from you, giving a simpler programming model and better performance. React can also render on the server using Node.js, and it can power native mobile apps using React Native. The core objective of this library is providing the best possible rendering performance and it draws its strength from individual components instead of focusing on the entire web app at once.</p><p>Even though Angular has been in existence for a substantial time, it never came across as something that is easy to use and maintain and the fact that it has gone through major revisions in last two years has rendered the initial knowledge on the technology pretty much useless. Also, Google (founders of Angular) didn’t do a lot of justice with Angular by using <a href="https://github.com/google/closure-library/">closure-library</a>instead of promoting Angular in their top notch public facing applications (including but not limited to Gmail). React on the other hand is not only maintained by Facebook, also it is used heavily by them. PayPal, Uber, Instagram, and Airbnb are just some names in the list of React admirers along with close to 95,000 live websites based on React.</p><h3>Vue.js</h3><pre><a href="https://vuejs.org/"><strong><em>Vue</em></strong></a><em> (pronounced /vjuː/, like view) is a progressive framework for building user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable</em>.</pre><p>The core library is focused on the view layer only, and is easy to pick up and integrate with other libraries or existing projects. On the other hand, Vue is also perfectly capable of powering sophisticated Single-Page Applications when used in combination with modern tooling and supporting libraries.</p><p>Vue.js is one of those new software technologies that is being widely used across the world for web development. Vue.js is actually a JavaScript framework with various optional tools for building user interfaces. There are numerous advantages of using Vue in web frontend development –</p><ul><li><strong>It’s small size</strong><br>The size of this framework is 18–21KB and it takes no time for the user to download and use it. This does not mean that it has low speed because of small size. Instead, it beats all the bulky frameworks like React.js, Angular.js, and Ember.js.</li><li><strong>It’s simply easy</strong><br>Personally I am a big fan of Vue, even without a proper front-end development experience I was able to learn and adapt Vue in daily development tasks in less than two weeks.</li><li><strong>Vue’s documentation is admirable</strong><br>Even though it is not backed up by multi billion dollar organisations such as Google or FB, it still has managed to evolve in a very short span of time and has ensured that the updates are not as problematic to absorb as has been the case with some of its competitors.</li><li><strong>Offers a great deal of flexibility</strong><br>It allows the user to write their template in HTML file, JavaScript file, and pure JavaScript file using virtual nodes. This flexibility also makes it easy to understand for React, Angular or any other JS developers.</li></ul><h3>Vue or React?</h3><p>Here is a result of a recent survey that was conducted online to assess the percentage of front-end developers who would like to reuse their past frameworks/libraries for UI development.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*tcG-xAALVxpWPYQsLMo-gg.png" /><figcaption>Framework Retention</figcaption></figure><p>No surprises, the top two contenders are React and Vue. This itself speaks a lot about the comfort and faith that these two technologies have now gained over their competitors. Another important consideration is the speed of DOM rendering. If we were to consider vanilla JavaScript as the fastest mode of component rendering, Vue 2.0 is only 1.37 times slower as compared to 1.82 times slow React 15.3 and 1.85 times slower Angular 2.0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*WY-To4kvymDlV57ovm25og.png" /></figure><p>If you are still looking for some more guidelines on choosing one over the other, here are a few important points to consider –</p><p><strong>Vue has the following advantages over React:</strong></p><ul><li>it is smaller and faster</li><li>convenient templates simplify the process of developing</li><li>it is simpler in terms of syntax</li><li><strong>92,500+</strong> Github Stars and <strong>189</strong> contributors</li></ul><p><strong>React has the following advantages over Vue:</strong></p><ul><li>gives more flexibility in large apps developing</li><li>easier to test</li><li>suitable for mobile apps creating</li><li>more information and solutions available</li><li><strong>94,500+</strong> Github Stars and <strong>1177</strong> contributors</li></ul><p><strong>Back-end Technologies</strong><br>The back-end stack, comprises of three important layers –</p><ul><li>Server-side software (application server/web server)</li><li>A set of APIs to collect and serve data</li><li>Database to store and return data as requested by APIs</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Iciv3NkTpWkMGa-2khczVQ.png" /><figcaption>Typical Backend Stack</figcaption></figure><p>The server is a powerful hardware (physical or virtual) that runs in the back-end software and database. It’s primary job is to serve or collect data via Application Programming Interfaces (a.k.a. API). The front-end applications, be it browser based thick clients or native/hybrid mobile applications, connects with the server by invoking one of the many live APIs on the server and GET or POST the data which is eventually stored in the database. APIs play an integral role in how most server-side software architectures are built, oftentimes replacing more complicated programming to allow software to communicate and data to be transferred.</p><p>The intricacies of the back-end stack are beyond the scope of this article and the next section focuses on some of the most promising server side technologies/frameworks which are available as options to the back-end developer today –</p><h3>Node.js</h3><pre><strong>As per Wiki :</strong> <em>Node.js is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code server-side. Historically, JavaScript was used primarily for client-side scripting, in which scripts written in JavaScript are embedded in a webpage’s HTML, to be run client-side by a JavaScript engine in the user’s web browser.</em></pre><p>Node.js was first conceived in 2009 by Ryan Dahl and he continued to develop and maintain it till it got sponsored and supported by Joyent. Dahl was not happy with the way Apache Http server used to handle the lot of concurrent connections and the way code was being created which either blocked the entire process or implied multiple execution stacks in the case of simultaneous connections. This lead him to create a Node.js project, he used Google’s V8 JavaScript engine, an event loop, and a low-level I/O API in his project which won him a standing ovation.</p><p>Node JS Platform does not follow Request/Response Multi-Threaded Stateless Model. It follows Single Threaded with Event Loop Model. Node JS Processing model is mainly based on Javascript Event based model with Javascript callback mechanism. Node.js comprises of two main component core &amp; its modules. Core: built in C &amp; C++. It combines Google V8 JS engine with Node’s Libuv library and protocol bindings including sockets &amp; Http.</p><p>Node.js is especially good for developing real-time apps intended for simultaneous connection of multiple users. However, it is not recommended for apps intended for complex computations. Some of the big names you know — Netflix, Trello, PayPal, LinkedIn, Walmart, Uber, Groupon, eBay, NASA — all use Node.js for their apps.</p><p><strong>Some of the typical use cases of Node.js can be listed as follows –</strong></p><ul><li>I/O bound Applications</li><li>Data Streaming Applications</li><li>Data Intensive Real-time Applications (DIRT)</li><li>JSON APIs based Applications</li><li>Single Page Applications</li></ul><p><strong>Some of the most promising frameworks built on top of Node.js are –</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*k-H6sSE9cXR6MAuH3Tz5ZQ.png" /></figure><h3>Python</h3><pre><strong>As per wiki: </strong><em>Python is an interpreted high-level programming language for general-purpose programming. It was created by Guido van Rossum and first released in 1991. Python features a dynamic type system and automatic memory management. It supports multiple programming paradigms, including object-oriented, imperative, functional and procedural, and has a large and comprehensive standard library.</em></pre><p>Python is one of those rare languages which can claim to be both simple and powerful. You will find yourself pleasantly surprised to see how easy it is to concentrate on the solution rather than the syntax and structure of the language. Websites built in Python include the likes of Google, Yahoo Maps, Dropbox, Venmo, YouTube, Instagram, Spotify, Reddit, and BitTorrent. Blender 3D, Unity 3D, and Mercurial are some of the well known softwares written in Python. Some of the important features of Python language are –</p><ul><li><em>Its is a simple and minimalistic language</em><br>Reading a good Python program feels almost like reading English, although very strict English! This pseudo-code nature of Python is one of its greatest strengths. It allows you to concentrate on the solution to the problem rather than the language itself.</li><li><em>Python is an example of a FLOSS</em> (Free/Libré and Open Source Software)<br>In simple terms, you can freely distribute copies of this software, read its source code, make changes to it, and use pieces of it in new free programs.</li><li><em>It’s a high level language</em><br>When you write programs in Python, you never need to bother about the low-level details such as managing the memory used by your program, etc.</li><li>Python supports procedure-oriented programming as well as object-oriented programming.</li></ul><p><strong>Typical use cases for using Python language –</strong></p><ul><li>APIs based development</li><li>Data-heavy sites, servers with high-traffic volume</li><li>Academic and scientific programming. Python has a slew of libraries for data work.</li><li>Machine learning</li><li>Data scientists use Python for prototypes and data analysis</li></ul><p><strong>Server side Frameworks built on Python –</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*8dQpT9ubEe7SMJEEDhyXMg.png" /></figure><h3>Java</h3><p>Java is a general-purpose computer-programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. There were five primary goals while creating Java:</p><ol><li>It must be “simple, object-oriented, and familiar”.</li><li>It must be “robust and secure”.</li><li>It must be “architecture-neutral and portable”.</li><li>It must execute with “high performance”.</li><li>It must be “interpreted, threaded, and dynamic”.</li></ol><p>The key factor contributing to Java’s success lies in the output of a Java compiler, which is not a executable code. Rather, it is bytecode. Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). While Java serves as a great client side programming language, it also is one of the most popular server side choice of language and does power some of the biggest corporate banks and industries such as aviation and communication.</p><p><strong>Typical use cases for Java language usage is as follows-</strong></p><ul><li>Multi threaded systems</li><li>Heavy computation</li><li>Machine learning</li><li>Transaction based systems</li><li>Real time communication</li></ul><p>Even though J2EE has been in existence for roughly the same amount of time as Java, the Servlet and JSP concepts have remained fairly stable over these years. There have been numerous server side frameworks (MVC and non MVC) written on top of the Servlet specifications given by J2EE, most dominant and prevalent amongst them being <a href="https://spring.io/">Spring</a>.</p><p><strong>Most widely used server side frameworks based on Java –</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*MbC7DYL-GZuBQ7di0vx2uA.png" /></figure><h3>Ruby</h3><p>Ruby is a language of careful balance. Its creator, Yukihiro “Matz” Matsumoto, blended parts of his favorite languages (Perl, Smalltalk, Eiffel, Ada, and Lisp) to form a new language that balanced functional programming with imperative programming. Since its public release in 1995, Ruby has drawn devoted coders worldwide. In 2006, Ruby achieved mass acceptance with active user groups in the world’s major cities and Ruby-related conferences filled to capacity.</p><p>As a dynamically typed language, Ruby does not have hard rules on how to build features, and it is very close to spoken languages. There’s more flexibility in solving problems using different methods. Furthermore, Ruby is also more forgiving of errors, so you’ll still be able to compile and run your program until you hit the problematic part. The emergence of Node.js in recent times has led to a seriously narrower Ruby (and RoR) developer community however it still is used widely for websites such as Hulu, Twitter, ZenDesk, Basecamp, Shopify, Urban Dictionary and GitHub.</p><p>It’s almost impossible to talk about Ruby without talking about Rails. In some ways, Rails has become Ruby. Part of this is because of the sheer popularity of the Rails framework. It’s a very popular framework that’s gotten a lot of use and Ruby has come to exist primarily as a service to the Rails framework. It is strongly opinionated, favoring convention over configuration which means unlike Ruby which believes there are many ways to do things, Rails thinks very strongly as far as web development is concerned that there is a best Rails way to do things. It allows you to fast prototyping and is popular with startups wanting to iterate quickly.</p><p><strong>Typical use cases for Ruby –</strong></p><ul><li>Web development</li><li>Functional programming</li><li>Getting complex, high-traffic sites and apps up and running quickly</li></ul><p><strong>Most widely used server side frameworks written in Ruby –</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*eC3aSgmLner1SsiVsWydLQ.png" /></figure><h3>Elixir</h3><p><a href="https://elixir-lang.org/">Elixir</a> is a dynamic, functional language designed for building scalable and maintainable applications. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.</p><p>Erlang was made at Ericsson in the 1980s to allow for better development of telephony applications. If you use 4G data on your phone it is more likely than not that the data is going through equipment from Ericsson. And it runs Erlang. <strong><em>Whatsapp</em></strong> has hundreds of millions of users. On most other platforms, handling more than ten thousand concurrent connections on one machine is seen as challenging, but Whatsapp has individual servers with more than 2 million concurrent connections, handled by Erlang. Erlang is also popular for game servers. Millions of users play multiplayer games with game server infrastructure handled by Erlang for titles such as Call of Duty and Game of War.</p><p>With all these pros, Erlang suffers from some major drawbacks as well. It is not as easy to get up and running a new website, the Erlang syntax looks a bit strange if you are used to looking at languages such as C, Java, JavaScript, PHP, Ruby, Perl etc. And things like package management, build tools, meta-programming, unicode handling and web frameworks are not as straightforward as in languages such as Ruby.</p><p>These areas are all addressed in Elixir. Elixir creator José Valim said: “I liked everything I saw in Erlang, but I hated the things that I didn’t see”. Elixir has its own package management system, macros, an easy to use build tool and unicode handling. The difference between Erlang and Elixir is not just the syntax. But syntax does matter, and especially for Ruby developers the Elixir syntax is very familiar.</p><p><strong>Typical use cases for using Elixir –</strong></p><ul><li>Writing highly concurrent web applications</li><li>A text chat server that can handle 100–1000000 simultaneous connections per computer</li><li>A telecommunications gateway able to support 1000’s of phones</li><li>Software licensing and inventory system</li><li>Wireshark export parsers</li><li>Connecting IoT devices</li></ul><p><strong>Most widely used frameworks written in Elixir –</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*6-6VgXhRJqxfzZ_dTAHjGw.png" /></figure><p>Hopefully the introductory explanation of these server side technologies will give you a head start towards thinking in terms of back-end development. The choice of either of these technologies for web application development purely depends on the kind of experience you as a programmer have and if you are an entrepreneur, then it depends to an extent on the capabilities of your existing team and their comfort with object oriented vs procedure oriented languages.</p><p>At times, it’s a little difficult to join the league of functional programming specially if you started your career as an object oriented language fanatic, however while evaluating certain technology or programming methodology we should always take the factors such as ease of writing code, being opinionated, time to take application live on internet etc in mind. Even if it takes a few extra days or weeks to learn a new language, to me it makes all the sense as it saves a lot more effort in the future when the application goes online.</p><p>Along with the choice of server side technology, another very important part of web application development these days is choice of cloud for deployment of the application. There are many available options in the tech industry today (such as but not limited to)-</p><ul><li>AWS</li><li>Azure</li><li>Google Cloud</li><li>Orale Cloud</li><li>Digital Ocean</li></ul><p>The choice amongst these boils down to the following factors –</p><ul><li>Cost of service</li><li>Certifications &amp; Standards</li><li>Technologies &amp; Service Roadmap</li><li>Data Security, Data Governance and Business policies</li><li>Service Dependencies &amp; Partnerships</li><li>Contracts, Commercials &amp; SLAs</li><li>Reliability &amp; Performance</li><li>Migration Support, Vendor Lock in &amp; Exit Planning</li><li>Business health &amp; Company profile</li></ul><p>While cost should never be the single or most important factor, there’s no denying that cost will play a big role in deciding which cloud service provider you choose. In my personal experience AWS has been the best choice in terms of all the factors mentioned above as well as the cost of individual services. It has reliable data centers across the globe and it keeps on adding new and relevant cloud hosting solutions every year, to me AWS has been an industry leader and a trend setter and others are mostly following everything that AWS comes up with.</p><p>With these thoughts, I would like to conclude this article hoping that it was a value addition to new web application developers as well as to the experienced campaigners. This is just the first article in the series of articles to come detailing different aspects of technologies mentioned here. Thanks for reading it through and looking forward to your views.</p><p><strong><em>If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too.</em></strong></p><p><strong><em>Did you know you could give up to 50 claps?</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e35229e0b473" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-resonant-web/best-tech-stacks-for-web-development-e35229e0b473">How to choose the right technologies for web applications?</a> was originally published in <a href="https://medium.com/the-resonant-web">The Resonant Web</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>