<?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 Lakshyajit Laxmikant on Medium]]></title>
        <description><![CDATA[Stories by Lakshyajit Laxmikant on Medium]]></description>
        <link>https://medium.com/@lakshyajit165?source=rss-792ccd21180e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*rRZmcvE-92v-I9EwuLKnfg.jpeg</url>
            <title>Stories by Lakshyajit Laxmikant on Medium</title>
            <link>https://medium.com/@lakshyajit165?source=rss-792ccd21180e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 23:48:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lakshyajit165/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[SpringBoot Dockerfile Diagnostics: Essential Dockerfile Tweaks for optimizing an image]]></title>
            <link>https://lakshyajit165.medium.com/springboot-dockerfile-diagnostics-essential-dockerfile-tweaks-for-optimizing-an-image-1009eb2543fd?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1009eb2543fd</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[dockerfile-best-practice]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Sat, 24 Feb 2024 10:49:35 GMT</pubDate>
            <atom:updated>2024-02-24T10:58:43.356Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rqA_aLS8RV5Dnzt_YiDTfg.png" /></figure><p>In the world of software development, efficiency and optimization are not just buzzwords; they are essential practices that can significantly enhance performance and reduce resource consumption. This is especially true when deploying Spring Boot applications using Docker, where every byte and every second counts towards achieving a lean, fast, and reliable service. Let’s dive into how you can optimize your Dockerfiles for Spring Boot applications, transforming a standard Dockerfile into an optimized version for better performance and smaller image sizes. For those who are unaware or have little knowledge about what docker is,</p><blockquote>Docker is a containerization platform that allows developers to package their applications and all of their dependencies into a standardized unit called a container. These containers can then be easily distributed and deployed across different environments, from development to production, without any changes.</blockquote><p>For this example we’ll use the good old spring initializer website: <a href="https://start.spring.io/">https://start.spring.io/</a> to create a simple spring web project. Below is the screenshot of the dependencies and versions used:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qzoj6KESSCWDFwzLg6U1Pg.png" /></figure><p>Next you can import the project into your preferred IDE. For simplicity purposes I defined only one REST endpoint inside the app which will return a message when that endpoint is hit. I will let you know why I chose not to include any other dependencies or functionalities into this application in a bit. Here is the code for my endpoint:</p><pre>@RestController<br>public class HelloWorldController {<br><br>    @GetMapping(&quot;/hello&quot;)<br>    public ResponseEntity&lt;?&gt; getResponse() {<br>        HashMap&lt;String, String&gt; response = new HashMap&lt;&gt;();<br>        response.put(&quot;message&quot;, &quot;Hello World!&quot;);<br>        return ResponseEntity.status(200).body(response);<br>    }<br>}</pre><p>Once this is done, we can build it using any IDE or manually by running the following command in the project root:</p><pre>./gradlew build -x test</pre><p>Now let’s verify that our simple endpoint is working as intended by running the application. Run the application using the following command and hit the “/hello” endpoint in a browser(or through a terminal).</p><pre>./gradlew bootRun</pre><p>Here is a screenshot of the response my endpoint returns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SYxl2u_bkmJ3YsOF-gHr5g.png" /></figure><p>Alright, we are all set with setting up our application. Next let’s build and run it as Docker container. Create a Dockerfile in the project root. First let’s keep things simple and create a very basic Dockerfile. For this example we’ll be using Java version 17 from the <strong>amazoncorretto:17-alpine3.17</strong> base image.</p><blockquote>Note: You can use any other base image version of your preferrence. I used amazoncorretto because I have used it previously in several other projects and it works just fine.</blockquote><p>Here is the code for the 1st version of our Dockerfile:</p><pre># Step 1: Use Amazon Corretto 17 with Alpine Linux as the base image for building the application<br>FROM amazoncorretto:17-alpine3.17 as build<br><br># Set the working directory in the container<br>WORKDIR /workspace/app<br><br># Copy the Gradle wrapper and build files to the container<br>COPY gradlew .<br>COPY gradle gradle<br>COPY build.gradle .<br>COPY settings.gradle .<br>COPY src src<br><br># Grant execution permissions to the Gradle wrapper and build the application<br>RUN chmod +x ./gradlew &amp;&amp; ./gradlew build -x test<br><br># Step 2: Use Amazon Corretto 17 with Alpine Linux as the runtime base image<br>FROM amazoncorretto:17-alpine3.17<br><br># Argument for passing the JAR file name<br>ARG JAR_FILE=/workspace/app/build/libs/*.jar<br><br># Copy the JAR file from the build stage to the /app directory in the image<br>COPY --from=build ${JAR_FILE} app.jar<br><br># Command to run the Spring Boot application<br>ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;/app.jar&quot;]</pre><p>As you can see the file is pretty self explanatory — We define our base image, then our image directory. Finally we copy the respective files into the image, build it and then define the ENTRYPOINT or RUN command. Now let’s build this docker image with the help of the above Dockerfile. In the project root, run the following command —</p><pre># Build your Docker image<br>docker build -t springboot-docker .</pre><p>This will build our docker image out of the Dockerfile that we created previously. Once this is done, let’s verify the image size by running the command <strong>docker images </strong>in our terminal. Here is a screenshot of the result of this command for me:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dVnR5WcinK0UWn5cST7hBw.png" /></figure><p>Woah! Are you seeing this?! we just wrote a simple endpoint and did not include any other dependency in our project, yet our image size is a whopping <strong>306MB</strong>!. Now some of the Java devs might be thinking well everyone knows unlike other frameworks/languages like Golang, Node+Express.js, Java is a bit memory intensive — that’s true actually, but as the age old Computer Science saying goes — “<strong><em>we can definitely do better!</em></strong>” Now let’s optimize our Dockerfile. Here is how the optimized version looks like —</p><pre>FROM --platform=linux/amd64 amazoncorretto:17-alpine3.17 as corretto-jdk<br><br># required for strip-debug to work<br>RUN apk add --no-cache binutils<br><br># Build small JRE image<br>RUN $JAVA_HOME/bin/jlink \<br>    --verbose \<br>    --add-modules ALL-MODULE-PATH \<br>    --strip-debug \<br>    --no-man-pages \<br>    --no-header-files \<br>    --compress=2 \<br>    --output /customjre<br><br># main app image<br>FROM alpine:latest<br>ENV JAVA_HOME=/jre<br>ENV PATH=&quot;${JAVA_HOME}/bin:${PATH}&quot;<br><br># copy JRE from the base image<br>COPY --from=corretto-jdk /customjre $JAVA_HOME<br><br># Add app user<br>ARG APPLICATION_USER=appuser<br>RUN adduser --no-create-home -u 1000 -D $APPLICATION_USER<br><br># Configure working directory<br>RUN mkdir /app &amp;&amp; \<br>    chown -R $APPLICATION_USER /app<br><br>USER 1000<br><br>COPY --chown=1000:1000 build/libs/springboot-docker-0.0.1-SNAPSHOT.jar /app/app.jar<br>WORKDIR /app<br><br>EXPOSE 8080<br>ENTRYPOINT [ &quot;/jre/bin/java&quot;, &quot;-Dspring.profiles.active=prod&quot;, &quot;-jar&quot;, &quot;/app/app.jar&quot; ]</pre><p>Before going into the details of the optimizations, let’s run the docker build command again and see how it goes. Here is another screenshot for the update image size:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I-kw6d4c3a08DczUD4ECMg.png" /></figure><p>That’s <strong>118MB </strong>now! More than 60% reduction in size which is definitely a good start. Now let’s discuss about the optimizations in detail:</p><p><strong>Custom JRE Creation</strong>: Using jlink to create a minimal JRE tailored for the application reduces the image size significantly. By stripping debug information, removing man pages, and compressing the JRE, we ensure that only the necessary runtime components are included.</p><p><strong>Security Enhancements</strong>: Switching to a non-root user (appuser) improves security by limiting the permissions of the application process. This is a best practice for running applications in Docker containers.</p><p><strong>Base Image Optimization</strong>: Starting with the alpine image minimizes the size of the runtime image. Alpine Linux is renowned for its minimal footprint and security profile, making it an ideal choice for production environments.</p><p><strong>Efficient Layering</strong>: By carefully organizing copy commands and minimizing the number of layers, the Dockerfilereduces build time and potential attack surfaces.</p><blockquote>Note: Since I am running docker on a mac, I had to include the --platform flag while importing the base image.</blockquote><p>There are quite a few other optimizations possible as well, but those are out of the scope of this article. I leave it to the readers of this article to explore further and make the necessary changes as per their own business or technical requirements. That’s it for now.<br>Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1009eb2543fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rate Limiting in REST APIs (Using SpringBoot and Redis)]]></title>
            <link>https://lakshyajit165.medium.com/rate-limiting-in-rest-apis-using-springboot-and-redis-135cd5cde754?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/135cd5cde754</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[rate-limiting]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Sat, 04 Feb 2023 12:02:44 GMT</pubDate>
            <atom:updated>2023-02-04T12:02:44.796Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BPaJkTpUnhUTOqcOXiw0XQ.png" /></figure><p>Rate limiting is a technique used to control the amount of incoming requests to a server or a system within a specified time frame. It helps to prevent overloading and ensure stability, reliability and fairness for all users. This technique is particularly important for REST APIs, which are designed to handle a large number of requests from various clients.</p><p>In this article, we will discuss the implementation of rate limiting in REST APIs using Spring Boot and Redis. Spring Boot is a popular Java-based framework for building microservices, while Redis is an in-memory data structure store that can be used as a database, cache, and message broker. By combining these two technologies, we can create a scalable and efficient rate limiting system for REST APIs.</p><p>Applications of rate limiting in REST APIs include controlling access to limited resources, preventing abuse and exploitation, and managing traffic spikes. By implementing rate limiting, API owners can ensure that their API remains available and responsive for all users, even during high traffic conditions. Talking about an example, one may apply rate limiting to the “password reset” endpoint in an application in order to prevent malicious users from bombarding the API with requests while carrying out a brute-force attack to hack a particular user’s password.</p><p>Here in this article I will demonstrate how we can implement rate-limiting on a simple REST end point using SpringBoot and Redis(which we will run as a container on docker). So without further ado, let’s get started.</p><blockquote><strong>To download a Spring project with the specified libraries, follow these steps:</strong></blockquote><ol><li>Go to “start.spring.io” website.</li><li>Select the desired Spring Boot version.</li><li>Choose the project type as “Maven Project” or “Gradle Project” based on your preference.</li><li>Select the packaging type as “Jar”.</li><li>Choose the Java version you want to use.</li><li>Enter the project name and group name.</li><li>In the “Dependencies” section, search for “spring-boot-starter-web”.</li><li>Check the box for “spring-boot-starter-web”, “lombok” and click on “Add”.</li><li>We would also need a few other libraries like: “redisson-spring-boot-starter”, “bucket4j-spring-boot-starter” which can be found in the mvrepository website. Make sure to include the gradle version of these packages. In case of any doubts, you can refer the repository link shared at the end of this article.</li><li>Click on the “Generate” button to download the project.</li></ol><blockquote><strong>The utilities of the above libraries/packages are as follows:</strong></blockquote><ol><li>“<strong>spring-boot-starter-web</strong>”: This is the Spring Boot starter for building web applications, including RESTful services.</li><li>“<strong>redisson-spring-boot-starter</strong>”: This is a Redis client library that provides support for Redis-based data structures and can be used as a distributed lock, cache, or queue.</li><li>“<strong>bucket4j-spring-boot-starter</strong>”: This is a Java library for rate limiting that implements the token bucket algorithm. It is used to control the rate of incoming requests to a system.</li><li>“<strong>lombok</strong>”: This is a library that provides annotations for code generation, reducing boilerplate code.</li><li>“<strong>junit</strong>”: This is a popular testing framework for Java. It is used to write and run tests for the Spring project.</li></ol><blockquote>Note: In this article we’ll be using Java version 11 and along with that we’ll implment the rate-limiting using “<strong>token-bucket</strong>” algorithm. So what is this token-bucket algorithm all about? Let’s discuss a bit about it.</blockquote><p>Token Bucket is a simple and widely used algorithm for rate limiting. It works by maintaining a bucket with a certain number of tokens. Each incoming request to the system removes one token from the bucket. If there are no tokens left in the bucket, the request is rejected. The tokens are replenished at a fixed rate, so that the number of tokens in the bucket increases over time.</p><blockquote><strong>Advantages of Token Bucket algorithm:</strong></blockquote><ul><li><strong>Simple to implement</strong>: The Token Bucket algorithm is relatively straightforward to implement, making it a popular choice for rate limiting.</li><li><strong>Flexible</strong>: Token Bucket can be used to limit the rate of requests over a variety of time frames, making it highly customizable.</li><li><strong>Scalable</strong>: Token Bucket can be used for rate limiting across multiple servers, making it a scalable solution for large systems.</li></ul><blockquote><strong>Disadvantages of Token Bucket algorithm:</strong></blockquote><ul><li><strong>Bursty traffic</strong>: Token Bucket may not be effective for rate limiting when there is bursty traffic, as the tokens may be used up quickly, leading to an excessive number of rejected requests.</li><li><strong>Lack of fine-grained control</strong>: Token Bucket does not provide fine-grained control over the rate of incoming requests, as the rate is determined by the rate at which tokens are replenished.</li></ul><blockquote><strong>Other rate limiting algorithms include:</strong></blockquote><ul><li><strong>Leaky Bucket</strong>: This is similar to Token Bucket, but instead of adding tokens to the bucket at a fixed rate, incoming requests are accumulated in the bucket and released at a fixed rate.</li><li><strong>Fixed Window</strong>: In this algorithm, the number of requests is limited over a fixed time window. Requests are rejected once the limit is reached.</li><li><strong>Sliding Window</strong>: In this algorithm, the rate of incoming requests is limited over a sliding time window. Requests are rejected if the rate exceeds the specified limit over any part of the sliding window.</li><li><strong>Smooth Bursting</strong>: This algorithm combines the benefits of Fixed Window and Token Bucket algorithms, allowing for a certain number of bursts of incoming requests, beyond which requests are rejected.</li></ul><p>Each of these algorithms has its own advantages and disadvantages, and the choice of algorithm will depend on the specific requirements of the system being rate limited. For the sake of simplicity, I have considered the token-bucket implementation over here. You can go through the details of these algorithms if you wish. I found this article quite useful — <a href="https://betterprogramming.pub/4-rate-limit-algorithms-every-developer-should-know-7472cb482f48">https://betterprogramming.pub/4-rate-limit-algorithms-every-developer-should-know-7472cb482f48</a></p><p>Enough talk. Let’s get start with our project setup. Here is the build.gradle file for your reference:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe50c39c1ca03fe20432010f4c3ee31b/href">https://medium.com/media/fe50c39c1ca03fe20432010f4c3ee31b/href</a></iframe><p>So as I mentioned before we’ll be using Redis as our DB for implmenting rate-limiting functionality. We can easily spin-up a redis docker container with the following command.</p><pre>docker run -d -p 6379:6379 --name rate-limit redis</pre><p>Now let’s have a look at the folder structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lo3Txi_2dCEEx7RbFIyCeQ.png" /></figure><p>If you’re a java developer, you’ll find most of the files are pretty much self-explanatory. Like the controller here for example. We are taking in a “path param” which we have assumed will be the user id and based on the user id we are implmenting rate limiting using the token-bucket algorithm. Please note that this is done as such, just to keep things simple. In a typical production environment, we would most likely collect the user data(if we are rate-limiting based on limits assigned to different users) from an authentication token from the Spring security context. Then using that token extract the user details from the DB - like what are the API limits assigned to this user and subsequently use that value as our limit. There are 3 important files here in this screenshot which we’ll be discussing. The RedisConfig.java, RateLimitConfig.java and the RedisspringratelimitingApplicationTests.java file in which we’ll be writing some simple test cases to verify our implmentation. So let’s look at these files one by one.</p><p>Here is the RedisConfig.java file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bef2cf88d63af8a430c15b9d284e1b19/href">https://medium.com/media/bef2cf88d63af8a430c15b9d284e1b19/href</a></iframe><p>Since this is a configuration file, it’s annotated with @Configuration. Inside this file we have defined certain beans.</p><p><strong>First</strong> one is the Config bean. This defines the redis configuration. As discussed earlier our redis docker container is running on the local machine with port mapping of 6379 which is the default port for redis.</p><p>The <strong>second </strong>one is the CacheManager bean. It takes in the reference to redis configuration defined before and creates a cache by the name of “cache”. In simple words this is the step where we actually create a cache inside our redis container.</p><p>The <strong>third</strong> bean is the proxyManager bean which returns an instance of JCacheProxyManger which is the default implementation in the bucket-4j library. But if this is the default cache implementation — how do we instruct Spring to use redis itself as our cache and not the default one provided by this library?</p><p>This is where the <strong>last </strong>bean comes into picture. That’s why it’s annotated as Primary. This is the SyncCacheResolver bean which resolves this conflict between the 2 implementations. Please note that this last bean too takes in a reference of the CacheManager bean(because as mentioned earlier, this is where we direct springboot to use redis as our cache) and resolves it using that. So that’s pretty much all about the Redis Configuration. Now let’s move on to the next file i.e. RateLimitConfig.java</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfa6a8012ef3e4c1f9f5472e8f67aa81/href">https://medium.com/media/dfa6a8012ef3e4c1f9f5472e8f67aa81/href</a></iframe><p>The class has an autowired field buckets of type ProxyManager. This field is used to access the shared instance of Bucket which will be used for rate limiting across multiple instances of the application.</p><p>The resolveBucket method takes in a key parameter, which can be an authentication token. In a production environment, the relevant user details could be extracted from the token and used to fetch the corresponding rate limit details for that particular user from the database as discussed earlier. The method then returns a Bucket instance for the specified key using the buckets ProxyManager field.</p><p>The getConfigSupplierForUser method returns a supplier of BucketConfiguration. It creates a Refill object with a refill rate of 20 tokens every 1 minute. It also creates a Bandwidth object with a capacity of 20 tokens and the specified Refill. Finally, it returns a BucketConfiguration built from the Bandwidth object. Now let’s have a look at the endpoint(or the controller layer) that we have created.</p><p>Here is the RateLimitController:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0596183bf5253bbe453a2fa0fde6079a/href">https://medium.com/media/0596183bf5253bbe453a2fa0fde6079a/href</a></iframe><p>The class uses the @RestController and @RequestMapping(&quot;/&quot;) annotations to define the endpoint and map it to the class. The class also makes use of Autowired annotation to automatically wire in a bean of type RateLimitConfig into the class instance. The getInfo method maps to the GET HTTP request and takes a path variable id as an input, which represents the user ID.</p><p>In the method body, the RateLimitConfig instance is used to obtain a Bucket object that corresponds to the given user ID. The Bucket instance is then used to check if there are enough tokens (1) available to allow access to the API by calling the tryConsume method. If there are sufficient tokens, a ResponseEntity with a 200 HTTP status code and a ApiResponse body containing the message Success for user {id} is returned. If there are not enough tokens, a ResponseEntity with a 429 HTTP status code and a ApiResponse body containing the message Rate limit exceeded for user {id} is returned. The RateLimitConfig class provides the implementation for rate limiting and the RateLimitController class enforces the rate limit by consuming tokens from the Bucket for a given user ID.</p><p>Now let’s test our API. Below is the test file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1022260df48a54fbb376e1940c16e973/href">https://medium.com/media/1022260df48a54fbb376e1940c16e973/href</a></iframe><p>The class is annotated with @SpringBootTest indicating that it&#39;s a test for a Spring Boot application. It declares two mock beans: rateLimitConfig and redisConfig. These beans are necessary for initializing the application context for the tests. There are two test methods: testAPIWithoutRateLimit() and testAPIWithRateLimitExceeding().</p><p>The testAPIWithoutRateLimit() method tests an API endpoint without rate limiting. It creates an instance of the RestTemplate class and uses it to make a GET request to http://localhost:8085/user/1. Then it uses JUnit&#39;s assertions to verify the response status code and the response body. The expected status code is 200, and the expected response body is &quot;{\&quot;message\&quot;:\&quot;Success for user 1\&quot;}&quot;.</p><p>The testAPIWithRateLimitExceeding() method tests the same API endpoint but with rate limiting enabled. It makes 20 GET requests to http://localhost:8085/user/2 using a RestTemplate instance. After that, it tries to make the 21st request. Since the rate limit is exceeded, the server returns a 429 status code and a response body of &quot;{\&quot;message\&quot;:\&quot;Rate limit exceeded for user 2\&quot;}&quot;. The test uses JUnit&#39;s assertions to verify the status code and response body as expected.</p><blockquote><strong>Note</strong>: The test cases are written in a manner such that they will execute successfully only when the application is already running. In simple terms these are NOT unit test cases for our methods as we have not done any mocking for Redis DB. Instead they are more like integration tests. So the correct way to verify our implementation is to run the program and then run the tests while the program is already running.</blockquote><blockquote>We could have implemented unit testing as well, but I just found this way a bit simple. Anyways as long as we are able to verify the functionality of the API to check if it’s working as intended — it doesn’t matter how we test it.</blockquote><p>Here is a screenshot of the test cases passing:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PFCzGhgfRPOR_u62HQNeDw.png" /></figure><p>So that’s all for this article. You can find the repository link below. I have included relevant links like links to github issues and even stackoverflow links as part of the comments inside the code. Do give this article some claps if you found it useful.</p><p><a href="https://github.com/lakshyajit165/redis-spring-ratelimiting">GitHub - lakshyajit165/redis-spring-ratelimiting</a></p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=135cd5cde754" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[API Testing in Node.js — using Chai, Mocha and Github Actions]]></title>
            <link>https://lakshyajit165.medium.com/api-testing-in-node-js-using-chai-mocha-and-github-actions-9dde037d735b?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/9dde037d735b</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[github-actions]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Fri, 06 May 2022 04:43:13 GMT</pubDate>
            <atom:updated>2022-05-06T04:45:19.470Z</atom:updated>
            <content:encoded><![CDATA[<h3>API Testing in Node.js — using Chai, Mocha and Github Actions</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*p90qDgBf-HGNOmehoXXfQQ.jpeg" /></figure><p>In this article I will be discussing, to setup a basic <strong><em>API testing architecture</em></strong> for a Node.js application with Postgres.</p><p>Now there are different kinds of testing which are typically done in a large scale production application namely — <strong><em>unit-testing, end-to-end testing, integration testing</em></strong> etc. Here in this example to keep things simple, we’ll be looking at an example of integration testing as it will involve connecting to our <strong><em>postgreSQL</em></strong> DB as well as other methods involved at the respository/service and the middleware layer.</p><p>According to Google:</p><blockquote><strong>Unit testing</strong> involves testing individual pieces of code while <strong>integration testing</strong> involves testing modules of code to understand how they perform alone and how they interact with each other.</blockquote><p>So as the above definition suggests, integration testing will be helpful for us in order to get to know how all the methods written in our application work together and interact with each other on various flows.</p><p>Now before we start discussing any further, let me break down some points about what our Node.js application consists of.</p><ol><li>The application consists of a simple CRUD API for a “Task” schema and uses postgres running on <strong><em>docker</em></strong> as a database.</li><li>We’ll be using “<strong><em>sequelize</em></strong>” as the <strong><em>ORM</em></strong> to connect to postgres from our application.(Doc reference: <a href="https://sequelize.org/api/v6/identifiers.html">https://sequelize.org/api/v6/identifiers.html</a>)</li><li>We’ll be using <strong><em>Chai </em></strong>and <strong><em>Mocha</em></strong> to test our endpoints. Both are standard libraries using for API testing in Node.js</li><li>We will also setup a <strong><em>Github Actions</em></strong> pipeline to trigger our tests if our code is pushed to certain branches. This helps in ensuring code quality througout the development process as we are notified immediately if the pipeline fails.</li></ol><blockquote>Note: To keep this article short I will only be discussing few of the many test cases. The entire code for the application can be found in the github repo here: <a href="https://github.com/lakshyajit165/node_postgres_cicd">https://github.com/lakshyajit165/node_postgres_cicd</a>. I suggest cloning this repo in order to get a full picture of how the code is written. It’s pretty simple if you’re even a little bit familiar with Node.js actually. I have made sure that the code is well documented and formatted so that readers don’t have too much trouble in browsing it.</blockquote><p>I suggest the readers to spin up a postgres docker container by running the following command on the terminal:</p><pre>docker run -d -p 5432:5432 --name node-postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=node_postgres_cicd -e POSTGRES_USER=postgres postgres</pre><p>I have included the .env file format and details in the Readme file of the project in the github repo. Now, before moving ahead with discussing the testing functionalities/features, here’s what our schema looks like as defined in Node.js using sequelize. Good thing is we don’t need to ssh into our running Postgres docker container ourselves and create the tables. The ORM will do that for us according to the schema that we’ve defined in our application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b075d64b129a922f2350d3fffc8565e6/href">https://medium.com/media/b075d64b129a922f2350d3fffc8565e6/href</a></iframe><p>We’ve imported this schema along with all the db config variables in the index.js file inside the “models” folder as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95d4cbd1b84fe41d7db500e6cc58f70b/href">https://medium.com/media/95d4cbd1b84fe41d7db500e6cc58f70b/href</a></iframe><p>Now let’s discuss the “Create Task”(POST) endpoint.</p><p>This is what the code for this endpoint looks like at the controller/service and repository layers.</p><p>At the Controller Layer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d216d12062354af5a9e6cd5554707765/href">https://medium.com/media/d216d12062354af5a9e6cd5554707765/href</a></iframe><p>At the service layer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77431930381269e7f401106107db9cd5/href">https://medium.com/media/77431930381269e7f401106107db9cd5/href</a></iframe><p>At the repository layer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc850322b62e46b8a62c453696af28aa/href">https://medium.com/media/dc850322b62e46b8a62c453696af28aa/href</a></iframe><p>As you can see, it’s a simple API with no complex setups/schemas. Now in order to do the integration testing for this “create-task” functionality, we’ll create a “test” folder in the project root and inside it we’ll create a file called “app.test.js”. The folder structure looks like this -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LfviEdQg9KaBuiWIBrSPEQ.png" /><figcaption>Folder structure for writing tests</figcaption></figure><p>So to get started with the testing we’ll make the necessary imports and declare some variables as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b69c81f045253b1deb518b662f4c0dfd/href">https://medium.com/media/b69c81f045253b1deb518b662f4c0dfd/href</a></iframe><p>As you can see the imports include setting the env. which in our case set to “test” because we are doing integration testing. We’ve also imported the app.js file along with our model schema and ORM which will be necessary for making some db calls. So for the “create-task” functionality I have considered 3 cases for testing here — 2 failure cases and 1 success case. As mentioned previously I’ve included a simple middleware to do some sanity checks for the request payload as the request hits our application. Based on that we’ll be able to test out some failure cases like — what should the response be like if some attribute(s) is/are missing from the request payload. The success case includes creating a task succesfully and testing the corresponding response received. For the create task functionality the request payload consists of 3 things — “title”, “description” and “created_by”. Here is the code consisting of the test cases related to this flow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e12a9bf26c6cf1ef1b76fefad1ef138/href">https://medium.com/media/4e12a9bf26c6cf1ef1b76fefad1ef138/href</a></iframe><p>We are using the “describe” function to encapsulate a set of tests for a particular flow. In real production environments where an entire microservice might have 100s of test-cases we can and infact we should give a more meaningful description under this function. Here I’ve only written “POST task” to keep things simple and clear. Under this we’ve 3 cases as described above. The first case is missing the “title” field from the request payload. Notice how the testing resembles meaningful english sentences/phrases as we’ve written <strong><em>res.body.should.be.a(“object”) </em></strong>and <strong><em>res.body.should.have.property(“errorMessage”)</em></strong>. Similarly in the second test, we’ve the entire request payload as an empty object and the 3rd case is the success case where we create a task in the DB. Notice after the 3 cases I have included an async delete function. This is a part of the clean up process. Now consider this — even if we’re testing on our testing environment where our DB is different from the one used in production, we don’t want our DB to get bloated as the test functions increase over time. So it’s a good practice to delete a resource once we’ve tested all the required functionality. Here I have kept this function blank because I’ve included a separate test for the “delete” functionality in the same file below.</p><p>So this was the test for the “create-task” functionality. Similary I have included tests (both failure and success cases) for other functionalities like <strong><em>GET, PUT, DELETE </em></strong>as well. I request my readers to check those out to get an idea about the overall working of the entire test suite. Now once all our tests are done, we are going to modify the test script in our package.json file which will help us in running all the tests together using a simple “npm test” command on our terminal.</p><p>The script in the package.json file is modified as follows:</p><pre>mocha --timeout 10000 --exit</pre><p>Now when we run “npm test” in our terminal we should get a result of something like this -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b4Nkht7-FM6f1NOojysQZw.png" /></figure><p>We see that all our tests are passing. Now the only thing left is to define a .yml file and then push our code to a github repository to leverage the power of github actions. But before we push our .yml file to the repo, we have to define some “secrets” in our repository as follows(these are basically the variables declared in the .env file):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QHi_hzehI0O6uVPjY6ZsMg.png" /></figure><p>Once this is done, we can go ahead and configure our .yml file. It’s present inside the .github/workflows folder and for our case, I have configured it as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9317db8cfa73d169b6f0f58511d9ee47/href">https://medium.com/media/9317db8cfa73d169b6f0f58511d9ee47/href</a></iframe><p>The only thing to note here is the “on” clause. You can define how you want to set up this clause like on which branch, on a pull request etc. We can also include branch protection rules along with this so that for e.g. a PR from another branch must be merged to master only when the testing job succeeds and the changes are approved by a reviewer. Having this file defined and pushing it github, will trigger the job according to the rules defined in this file. For my case it looked something like this -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pJLGJ04hvEaTaBJqAb7sgQ.png" /></figure><p>And that’s pretty much it! We’ve setup a testing architecture within a short span of time — no doubt github actions is one of the best friends a developer can get! 😎</p><p>Feel free to reach out in case of any issues or comment down on this article and I will revert back.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9dde037d735b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS Cognito — Auth setup has never been more simple!]]></title>
            <link>https://medium.com/swlh/aws-cognito-auth-setup-has-never-been-more-simple-f53fb170eeac?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f53fb170eeac</guid>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[aws-cognito]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Tue, 03 Aug 2021 06:47:51 GMT</pubDate>
            <atom:updated>2021-08-03T06:47:51.905Z</atom:updated>
            <content:encoded><![CDATA[<h3>AWS Cognito — Auth setup has never been more simple!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*gW7GKiiyqHXtTM0Qbs1Ozw.png" /><figcaption>Setting up auth with node.js along with Cognito is pretty sweet!</figcaption></figure><p>Yup! you heard it right. I don’t know how many of my readers have heard of AWS Cognito before so I will start with a brief intro about it. According to the official website —</p><blockquote><strong>Amazon Cognito </strong>lets you add user sign-up, sign-in, and access control to your web and mobile apps quickly and easily. Amazon Cognito scales to millions of users and supports sign-in with social identity providers, such as Apple, Facebook, Google, and Amazon, and enterprise identity providers via SAML 2.0 and OpenID Connect. (<a href="https://aws.amazon.com/cognito/">More about AWS Cognito</a>)</blockquote><p>So basically it’s a service provided by Amazon Web Services(AWS) to help developers setup authentication in their web/mobile apps with ease. We are living in an agile age where even most of the medium scale webapps use microservices architecture(atleast many of which I am aware of), because not only does that make the overall codebase clean and segregated, it also helps make it easier to maintain for the developers actually coding it. In this article I will be discussing on how to setup an authentication/authorization server using AWS Cognito in Node.js, following some simple steps. I must say though — AWS does make things simple but setting the entire thing up from scratch might be a little intimidating for a beginner considering the fact that the official documentation, although very well written is practically an ocean of information due to the plethora of services/functionalities provided by AWS!</p><p>Now let me give a brief idea about what we’ll be doing here — We’ll setup an authentication server which will handle the basic authentication stuff that pretty much every web/mobile app out there needs to have. We’ll also be using <strong><em>AWS SES(Simple Email Service</em></strong><em>) </em>to send OTPs to users.</p><blockquote><strong>Note: </strong>The services used in the setup process here are NOT free! But it won’t cost you anything to make the basic setup as I have described here. That will depend on how you use this setup(or any similar setup) in your own webapp. Anyways I would suggest it is always a good practice to go through the pricing of the services once and also monitor your account billing once you start setting up(in case you’re setting this up over a span of couple of days or more for some reason)</blockquote><p>Let me break down the setup process into a number of steps so that I can discuss the steps one after the other.</p><p><strong><em>Basic steps to setup authentication with AWS Cognito in Node.js -</em></strong></p><ol><li>We need to have a website of our own. Could be a static site on github pages — this is needed to setup AWS SES as we would have to request for production access since the default sandbox environment won’t allow us to send OTPs to any user email we want.</li><li>Setup AWS SES by making a request to move our environment out of the sandbox.</li><li>Setup a user pool in Cognito with the relevant settings — In this setup we are planning the following flow:<em> </em><strong><em>(I)</em></strong><em> User tries to signup with an email, (</em><strong><em>II) </em></strong><em>He gets a verification code on his email, (</em><strong><em>III) </em></strong><em>He enters the verification code along with a valid password and signs up, (</em><strong><em>IV) </em></strong><em>He can then use those credentials to login. </em>(<strong><em>V) </em></strong><em>He can change his password using the forgot password option. </em><strong><em>(VI) </em></strong><em>He can access a protected resource from the server using valid credentials. </em>So for this flow we will need a rather simple setup on Cognito’s end — Don’t worry, I will walk you through the entire setup process along with relevant screenshots and documentation links as well.</li><li>Setup an Express.js server and include the appropriate SDKs.</li><li>Write routes/utilities/functions/middlewares for the functionalities discussed in step 3 above.</li></ol><p>We now have an idea of what we’re gonna setup, so without further ado, let’s buckle up and get started!</p><p><strong><em>Steps 1 and 2:</em></strong></p><p>I assume at this point you already have an account on AWS and know the basics of the cloud console. Head over to SES and make sure the “region” selected on the top right corner is N.Virginia(or US East, Virginia). We need this because when setting up an emailing functionality with Cognito with the help of SES, AWS allows the email address to be from 3 regions (at the time this article was written) namely — US East(Virginia), US West(Oregon) and EU(Ireland). Now we need to verify an email address. Go to verified identities section and on this screen,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cPyMpQ2S5JjGTk5DV4RRLw.png" /></figure><p>click Create Identity. Then on the next screen select “email” and type in your email like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BqdNNX4jxS57LRhTSY2RSQ.png" /></figure><p>Then click on create identity. This send a verification code to the mentioned email address. Click on the link mentioned in the email to verify it. Once that’s done, it’s time to request a production access for SES. For this we would need a static website and AWS demands all these details. Please follow the steps mentioned here — <a href="https://docs.aws.amazon.com/ses/latest/DeveloperGuide/request-production-access.html">https://docs.aws.amazon.com/ses/latest/DeveloperGuide/request-production-access.html</a></p><p>You can keep the email type field as “Transactional”, I think the other option is “Promotional” which is not quite our use case here. And if you face any issue in setting this up, don’t hesitate to contact me personally. I will provide links to my profiles at the end of this article. Once your SES account is moved out of sandbox to production environment, you’ll receive a confirmation email stating the same.</p><p><strong><em>Step 3:</em></strong></p><p>Now that we have setup SES, let’s proceed further and setup a user pool in AWS Cognito. But before that we need an access key and a secret key from our AWS account which are needed for the AWS SDKs that we’ll be using. It’s actually pretty easy to setup so I won’t be mentioning details. Just head over to the IAM section and click on “Create Access Key” as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C0C_53RVAS0Eec0EucSxig.png" /></figure><p>As mentioned before, we are using pretty basic setup here(like for example using only email addresses for signup, not enforcing too many constraints on the password field etc. as will be evident from the snapshots below). So go ahead and select the Cognito service from the list of services and follow along as shown(<em>snapshots first, explanation below</em>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PWy5npHJioTfV8bnC4VKkg.png" /></figure><p>Click on create user pool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N1Vn3DIMkr9jlpLm2JGlTA.png" /></figure><p>Give a name to your user pool and then click on “Step through settings”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FAPKFCece3deEkPAnfQkLw.png" /></figure><p>Check/Select the fields as shown above and then click on next. Just for your information, this is the place where you can configure what all details you want from users during signup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vvZmq9aeaqKvZSXVtMwRqQ.png" /></figure><p>Then in the policies tab, we have to set the password restrictions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PlClNuYqi6NyK4pg5N2nqA.png" /></figure><p>This is an important step where you have to select the email only option as this is the only field on which we want to authenticate our users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*62clj9s4aNkdqALXTzNsPw.png" /></figure><p>And then this is the step where we have to setup our email functionality. As is evident from the screenshot only 3 SES regions are allowed here. So your verified “from” email address must be verified in any of those 3 regions. Select the verified entity from the dropdown and then we can also do some customizations on the email format which is really upto you. You might set that up as you like or leave the defaults. And we’re done! Finally click on save and our user pool is ready.</p><p><strong><em>Step 4:</em></strong></p><p>Now let’s setup a basic express server. Here is what my final package.json file looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ef7e08ca9173a425876dafe4255f22f/href">https://medium.com/media/9ef7e08ca9173a425876dafe4255f22f/href</a></iframe><p>As you can see, there are some basic/standard npm packages like dotenv, cors, express etc — besides that we’ve some packages to handle authentication with Cognito. And the following is my final version of app.js:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b6b0ae11d4e34b2a4caf5a52cceffd4/href">https://medium.com/media/8b6b0ae11d4e34b2a4caf5a52cceffd4/href</a></iframe><p><strong><em>Step 5:</em></strong></p><p>Now let me tell you from a technical point of view what all we’ll be doing. Once the user is authenticated, cognito issues 3 tokens — <strong><em>An IdToken, An AccessToken and a RefreshToken</em></strong>. In case of default settings,<strong><em> the id and access tokens are valid for an hour </em></strong>and <strong><em>the refresh token is valid for 30 days</em></strong>. So the idea is to get those tokens and access a protected resource on the server — In this flow we’ll be checking if the ID/Access token is expired we’ll issue a new token and pass them in the response headers. It’s the responsibility of the client to check if tokens are present in the response headers and if so, to update them on the client side(mostly through cookies).</p><blockquote><strong>Note: </strong>In a production scenario, even if we know that the token themselves are encrypted it’s a good practice to hash them using some algorithms and decode them as and when required(applicable for both client and server side). Now there are several ways to hash any data so I will leave it upto the readers to decide what package/method suits them the most.</blockquote><p>I have used certain middlewares and utility functions in my code to make it more structured. If you go through the code you will also find a lot of implementations related to the AWS SDK we installed. Now going over the working of the every line of code will make this article insanely verbose and boring. So what I will do is, I will post the code here as gist files and provide the appropriate links to AWS docs containing the implementation details of the specific SDK utilities which were needed to write that function. So let’s start according to the flow which I described above. You can find the exact imports and other details from my repo link at the end of this article. Here is -</p><p><strong><em>(I) The</em></strong> <strong><em>function for Signup:</em></strong></p><p>Useful Links:</p><ul><li><a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminCreateUser.html">https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminCreateUser.html</a></li><li><a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDeleteUser.html">https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminDeleteUser.html</a></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7ccd808e1ac4f0841af894b3dcf5efd/href">https://medium.com/media/e7ccd808e1ac4f0841af894b3dcf5efd/href</a></iframe><p><strong><em>(II) The function for Confirm Password(new password along with the verification code received after signup):</em></strong></p><p>Useful Links:</p><ul><li><a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminRespondToAuthChallenge.html">https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminRespondToAuthChallenge.html</a></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5bab073397890a5d77aa546317329313/href">https://medium.com/media/5bab073397890a5d77aa546317329313/href</a></iframe><p><strong><em>(III) The function for SignIn:</em></strong></p><p>Useful Links:</p><ul><li><a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html">https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html</a></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0508199f19ba748abb2a8f58cb7e43e6/href">https://medium.com/media/0508199f19ba748abb2a8f58cb7e43e6/href</a></iframe><p><strong><em>(IV) The function for forgot password(User get a verification code as soon as he opts for forgot password option):</em></strong></p><p>Useful Links:</p><ul><li><a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ForgotPassword.html">https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ForgotPassword.html</a></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82f717fa1c3739f3f9d4575845c32236/href">https://medium.com/media/82f717fa1c3739f3f9d4575845c32236/href</a></iframe><p><strong><em>(V) The function for confirmForgotPassword(User sets a new password along with the verification code received in the above step)</em></strong></p><p>Useful Links:</p><ul><li><a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ConfirmForgotPassword.html">https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ConfirmForgotPassword.html</a></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/202e358c63fa5755a7b28d004e4afce6/href">https://medium.com/media/202e358c63fa5755a7b28d004e4afce6/href</a></iframe><p><strong><em>(VI) The function for logout:</em></strong></p><p>Useful Links:</p><ul><li><a href="https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminUserGlobalSignOut.html">https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminUserGlobalSignOut.html</a></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64dbed9866afb687c2ed740a36c4e15c/href">https://medium.com/media/64dbed9866afb687c2ed740a36c4e15c/href</a></iframe><p>Now as far as the functionality is concerned we’re all set. Now I will be showing you the screenshots of the postman requests which I made so that you can get an overview of the request and response payloads.</p><p><strong><em>(I) The signup request:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z-clu0bYTqQePfmNASXSHg.png" /><figcaption>The request is made only with email as the payload.</figcaption></figure><p><strong><em>(II) We got the verification code:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TUsstll3ATbxQCiD_mrWZw.png" /><figcaption>This is the template I set for emails while setting up our cognito user pool</figcaption></figure><p><strong><em>(III) Set up the password using the verification code:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C8viS_XxaSEHJ-AiOBbDNw.png" /><figcaption>On successful completion we get the signup complete response.</figcaption></figure><p><strong><em>(IV) Sign in:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P1fHyJ-Su6XDqG8LBwAN6A.png" /><figcaption>On successful signup we pass the id_token, access_token and refresh_tokens in the response headers. Just a reminder that these values should typically be hashed in a production environment.</figcaption></figure><p><strong><em>(V) Forgot Password:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nWYtw8a2oaTttJNvRGyw7A.png" /><figcaption>A verification code is sent to the registered email id.</figcaption></figure><blockquote><strong>Note: </strong>For simplicity purposes I passed the email in the request body. It’s actually better to pass the id_token in the headers and extract the email from there.</blockquote><p><strong><em>(VI) Received the verification code for password reset:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jwOPAisHe11gw193LL3WuA.png" /><figcaption>This verification code will be used in the next request — to set up a new password.</figcaption></figure><p><strong><em>(VII) Password reset using the verification code:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VJKAfKqifvjG9zIH_Dau8Q.png" /><figcaption>I used a wrong email address to demonstrate one of the error messages!</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mX3L_PDUA_VmF53r4d73tw.png" /><figcaption>Password reset is successful</figcaption></figure><p><strong><em>(VIII) Accessing a protected resource:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JbocaDJr-JZwGfFxW-PWeA.png" /><figcaption>Pass the id_token, access_token and refresh_token in the headers and we’re set!</figcaption></figure><p>And that’s how it’s done! Pretty sweet huh?</p><p>Now that our auth server is setup and running quite good, let’s discuss some areas of concern/improvements that you should definitely consider on a production environment:</p><ol><li><em>As mentioned earlier when handling tokens, we should hash them using some algorithm and then pass them in headers/cookies.</em></li><li><em>Passwords should be hashed too while sending the request from the client. It can be decrypted and validated on the server side.</em></li><li><em>We should implement rate limiting for our server side APIs. This is important because malicious users/bots might try and make a DDoS(or similar kind of) attack on our APIs(especially the password reset ones) if they come to know of the request payloads somehow. Some of the popular rate limiting packages for Node.js and SpringBoot are </em><a href="https://www.npmjs.com/package/express-rate-limit"><strong><em>express-rate-limit</em></strong></a><em> and </em><a href="https://www.baeldung.com/spring-bucket4j"><strong><em>Bucket4j</em></strong></a><strong><em> </em></strong><em>respectively</em><strong><em>. </em></strong><em>Feel free to explore these and implement in your server-side frameworks.</em></li><li><em>Typically in a production environment(most likely containers on k8s or docker or any other serverless solution even) we should make sure to handle the Access Keys and IDs carefully! These values should be encrypted and should definitely NOT be pushed along with the code to any version control. Some popular solutions are K8s configMap and Secrets, Github/Gitlab environment secrets(in case you’re using Github/Gitlab) for CI/CD etc.</em></li></ol><p>Now as said earlier, this is the github repo containing all the code for the above implementation.</p><p><a href="https://github.com/lakshyajit165/node_cognito">GitHub - lakshyajit165/node_cognito: Authentication/Authorization in Node.js using AWS Cognito</a></p><p>If you go through the code you can see that I have implemented utilities and some middleware functions even. The way of implementation totally depends on the developers and overall app/business requirements. I had worked on Cognito before so thought of sharing my knowledge with others who might need it. Now Cognito also allows to setup authentication with social authentication providers like Google and Facebook. May be I will do it in another article.</p><p><a href="https://www.linkedin.com/in/lakshyajit/">Lakshyajit Laxmikant - Software Developer - Programming Pathshala | LinkedIn</a></p><p>This is the link to my linkedin profile. Feel free to connect(or comment here) in case of any issues with the setup and if you liked this article, do give it some claps share it with others too!</p><p>Happy Learning!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f53fb170eeac" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/aws-cognito-auth-setup-has-never-been-more-simple-f53fb170eeac">AWS Cognito — Auth setup has never been more simple!</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use Nginx as a Reverse Proxy for Containerized Node.js Microservices]]></title>
            <link>https://javascript.plainenglish.io/using-nginx-as-a-reverse-proxy-for-containerized-node-js-microservices-e7c4800f05a1?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7c4800f05a1</guid>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Tue, 11 May 2021 17:05:42 GMT</pubDate>
            <atom:updated>2023-07-11T04:03:05.448Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using Nginx as a Reverse Proxy for Containerized Node.js Microservices</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*-jqdUMi76msZG3dK21iyhw.jpeg" /><figcaption>Architechture for this mockup</figcaption></figure><p>Today I will be discussing how to quickly setup a microservices architecture using Node.js, Nginx, Docker and Angular (well you could use any client side framework/library like React.js, Vue.js — but I chose Angular for simplicity purposes).</p><p>So here is a brief summary of what we’ll be doing today:</p><ul><li>Setup 2 basic express.js servers exposing pretty basic enpoints</li><li>Setup the nginx configuration for these servers(each server here refers to a microservice)</li><li>Dockerize these servers including setting up the nginx web server as a docker container and running the entire workload through docker-compose</li><li>Test our endpoints in postman</li><li>Setup an Angular client and dockerize it</li><li>Modify the Nginx configuration to contain the client’s configuration</li></ul><p>But before getting started, one might wonder, what is reverse proxy and why would we even need it?</p><p><em>A </em><strong><em>reverse proxy</em></strong><em> server is a type of </em><strong><em>proxy</em></strong><em> server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server.</em></p><p>So in plain simple terms, when we setup a microservice architecture, we would want our client requests to be forwarded to the concerned microservice according the route patterns. For e.g. the requests of the form <strong>/auth/ </strong>should be routed to the <strong>authentication microservice</strong>. Similarly the requests of the form /<strong>payments/ </strong>should be forwarded to the <strong>payment microservice</strong> and so on and so forth. So typically you have many options to set this up based on the tech stack you’re using. For example -</p><ol><li>If you’re using SpringBoot, you can use <strong>Netflix Eureka</strong> as a <strong>service discovery</strong> along with <strong>Spring Cloud Gateway</strong> to forward your requests(and later deploy those microservices as docker containers or kubernetes pods based on your requirements).</li><li>If you’re implementing a serverless architecture(let’s say on AWS), you could use <strong>AWS Service Mesh</strong> and <strong>Service Discovery</strong>.</li><li>If you’re using <strong>Node.js</strong>, you could setup a load balancer and revevrse proxy using <strong>Nginx </strong>— which is what we’ll be doing in this article. Please note:</li></ol><blockquote>Above mentioned methods are some of the ways which are robust and I have known from my experience to be used in several production systems. Ofcourse they’re NOT the only way to setup a Gateway/Loadbalancer/Reverse Proxy. Feel free to explore or find services suitable to your use case on the web.</blockquote><p>So without further ado, let’s get to the first step: setting up two basic express.js servers.</p><p>For that, just fire up your terminal and type:</p><pre>npm init -y</pre><p>Next install express.js and cors package (cors is needed because we’ll be making requests from an Angular app eventually) with the command:</p><pre>npm i --save express cors</pre><p>You can look up the exact folder structure in the GitHub repo shared at the end of this article. I would suggest going through the repository as you read through this article to make more sense as I won’t be diving deep into the coding part here.</p><p>Note that we will be running the above commands twice inside the folders server1 (represents our 1st microservice) and server2 (represents our 2nd microservice). We’ll also setup some basic ‘<em>get’</em> routes in these microservices.</p><p>These can be any distinctive message that you might want to try out. Feel free to setup those up according to your own requirements. After setting up these 2 microservices, the app.js file for the 1st microservice looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aeef63a2cab748b84cf7f1e9ada2842c/href">https://medium.com/media/aeef63a2cab748b84cf7f1e9ada2842c/href</a></iframe><p>Similarly the app.js for the 2nd microservice looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ee60fa667bfeb9aaee2c9a42ab9f19d/href">https://medium.com/media/1ee60fa667bfeb9aaee2c9a42ab9f19d/href</a></iframe><p>As you can see, nothing fancy going on here. Just 2 simple servers with slightly different response messages.</p><p>Next we’ll create a new folder named <strong>nginx</strong> in the project root and create a file called <strong>default.conf </strong>inside it.</p><p>This file is supposed to contain all the load balancing and reverse proxy configuration for our microservices. The configuration file looks as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcafb8a8a6c8f7f1d8d2fb418f27001e/href">https://medium.com/media/bcafb8a8a6c8f7f1d8d2fb418f27001e/href</a></iframe><p>All the above configurations are pretty much basic and you can find the details about specific statements(or terms) on the official website<a href="https://docs.nginx.com/nginx/admin-guide/web-server/web-server/"> <strong><em>here</em></strong></a>. Please note that here we are just configuring the “reverse proxy” part and have NOT explicitly setup any load balancing. I have intentionally left it due to -</p><ul><li>The setup is simple with minor changes to the above configuration.</li><li>There are several other ways to setup load balancers when it comes to deploying our containers(especially docker/kubernetes containers) such as cloud load balancers.</li></ul><p>Now we’re done with the configuration of the backend except for the docker files.</p><p>Let’s set them up real quick. You have to put the Dockerfiles inside the respective folders for the servers(or microservices) and the docker-compose.yml file in the project root.</p><p>The final folder structure looks like this(ignore the client folder for now):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ROWVFIfJFCMnsbZzHik09Q.png" /><figcaption>Project folder structure</figcaption></figure><p>Here are the Dockerfiles for server1 and server2 and the docker-compose file.</p><p>Dockerfile for server1:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd19813eb8abea9bf3bfcf92cd0f4970/href">https://medium.com/media/cd19813eb8abea9bf3bfcf92cd0f4970/href</a></iframe><p>Dockerfile for server2:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1162e0a1bd4ef1d0f783fd607059ca3d/href">https://medium.com/media/1162e0a1bd4ef1d0f783fd607059ca3d/href</a></iframe><p>Docker-compose file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6085e0c46b47b051c68463b96b27aa71/href">https://medium.com/media/6085e0c46b47b051c68463b96b27aa71/href</a></iframe><p>As you can see, nothing fancy/complicated going on around here. Pretty basic stuff — if you know the basics of docker and containers you should be able to understand these files at the first glance. Now that we have our files setup, we can run our servers as containers by running the following command in the project root:</p><pre>docker-compose up --build -d</pre><p>This command pulls the images and fires our containers. It might take a little while depending on your network bandwidth. But once done, you’ll be able to see your containers running by typing docker container ls OR docker ps</p><p>Having said that, let’s open Postman and test our endpoints.</p><p>Now according to our reverse proxy configuration, we should be able to hit <strong>http://localhost:8080/server1/app</strong> and <strong>http://localhost:8080/server2/app </strong>(note how we’re only changing the urls as per the server names while the base url remains same) to get responses from both our servers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xy9V2yZ9KefHiFNRiqG__Q.png" /><figcaption>Response from server1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FZcfjwqPWPdJqmvwPQmyRA.png" /><figcaption>Response from server2</figcaption></figure><p>Yay! we are getting the responses from our servers and you know what? The heavy lifting is done!</p><p>Now we have to configure a client(well it could be any client actually). My personal preference was Angular, but you could have any other Library/Framework as per your choice and requirements.</p><p>The concept remains the same. But in case you choose Angular, feel free to browse the repository to see what all I’ve done(there is nothing much going there actually…I am just fetching the response from the servers and displaying them in different components…that’s it). I am intentionally skipping that part here in this article so that it doesn’t become too verbose.</p><p>I will share the updated default.conf file, the docker-compose file and the Dockerfile for the client though.</p><p>Here is the Dockerfile for the client:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fae195cccd964ba6954a4bc5eb3a2273/href">https://medium.com/media/fae195cccd964ba6954a4bc5eb3a2273/href</a></iframe><p>The only tricky part here is the last line i.e ng serve --prod --host 0.0.0.0 which is specific to Angular in order to make the client container accessible from any network (in our case it helps access the client from a browser). You can explore ways to access the client if you’re using a different framework.</p><p>The updated default.conf file is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/32341dc521c70a9bdffa23d6a8c0f1b8/href">https://medium.com/media/32341dc521c70a9bdffa23d6a8c0f1b8/href</a></iframe><p>And the final docker-compose.yml file is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0747c58ce7efce8ae1db58734199090f/href">https://medium.com/media/0747c58ce7efce8ae1db58734199090f/href</a></iframe><p>So that’s pretty much for our setup. Once all these things are included, you can run the docker-compose up command again to start all the containers. In my case the response from client(and the running client) looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*StkdAX2tmXa007Xfv5zHiw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ywn_hxRHFXQusNgsfSib7Q.png" /></figure><h4><strong>Update - 26/06/2022:</strong></h4><p>I have updated the github repo to contain the deployment for kubernetes as well for all the microservices along with ingress controller. You can find the files in the “<strong>k8s</strong>” branch. I have only setup to run the microservices on minikube. The implementations however to run on different cloud service providers like AWS/GCP/Azure will be different. I leave it to the readers to explore that.</p><h4><strong>Update - 26/12/2022:</strong></h4><p>Have modified the default.conf files for nginx. Just removed some unnecessary headers. Hope that’s helpful.</p><h4>Conclusion</h4><p>So that’s all folks.</p><p>You see how nginx and docker make a developer’s life so easy! You can checkout the GitHub repo here:</p><p><a href="https://github.com/lakshyajit165/nginx_reverse_proxy">lakshyajit165/nginx_reverse_proxy</a></p><p>Cheers!</p><h4>Further Reading</h4><p><a href="https://bit.cloud/blog/component-driven-microservices-with-nodejs-and-bit-l64shurc">Component-Driven Microservices with NodeJS and Bit</a></p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a>, <a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>, </em><a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><strong><em>YouTube</em></strong></a><em>, and </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Discord</em></strong></a><em>. Interested in Growth Hacking? Check out </em><a href="https://circuit.ooo/"><strong><em>Circuit</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7c4800f05a1" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/using-nginx-as-a-reverse-proxy-for-containerized-node-js-microservices-e7c4800f05a1">Use Nginx as a Reverse Proxy for Containerized Node.js Microservices</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Microservice Architecture with Spring Boot]]></title>
            <link>https://lakshyajit165.medium.com/microservice-architecture-with-spring-boot-732d58b1c695?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/732d58b1c695</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[microservice-architecture]]></category>
            <category><![CDATA[spring-cloud]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Thu, 06 Aug 2020 17:20:00 GMT</pubDate>
            <atom:updated>2021-03-12T12:56:00.581Z</atom:updated>
            <content:encoded><![CDATA[<p>When it comes to scaling your applications to a larger level(may be expecting a considerable number of users viewing/using it), microservice architecture comes to our rescue. There are a plethora of configurations which can be done in order to load balance, route, secure our applications(among many-many other options ofcourse!).</p><p>Well, as a developer myself I have learnt that when it comes to web scaling, “Java” is the king!. So today I will be demonstrating a POC(Proof Of Concept) of microservice architecture using SpringBoot with the help of a small project which I built myself.</p><blockquote>It has been an year when I was trained to work using this architecture and I must say it’s so much fun to explore the vivid array of tasks/end-goals which can be achieved using this</blockquote><p>Let’s first peek into the following diagram which depicts the architecture of our application which we will be building.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*CUorNS1rDmjVBBpd7OSp1g.jpeg" /><figcaption>Architecture for Sample Application</figcaption></figure><p>In order to simplify things, let me jot down the tasks here in terms of user-stories and we’ll look into each one of those step-by-step.</p><ol><li><strong><em>Understanding the architecture.</em></strong></li><li><strong><em>Generate a multimodule parent project in Spring Boot.</em></strong></li><li><strong><em>Add the required dependencies in the parent pom file.</em></strong></li><li><strong><em>Generate each individual module except the UI(or in our case, each microservice)as a spring starter project and adding the required dependencies for it.</em></strong></li><li><strong><em>Preparing the UI as an angular project.</em></strong></li><li><strong><em>Creating a separate project for the UI in spring boot (to package the angular application into Spring)</em></strong></li><li><strong><em>Modifying the routes in the gateway to run all the applications.</em></strong></li><li><strong><em>Voila! we’re done!</em></strong></li></ol><p>So without further ado, let’s dive into the steps and check them out in greater detail. My entire code is present on github(the link of which will be present a few lines below), but I will putting code snippets in between the steps wherever I feel, necessary.</p><blockquote><strong>Understanding the architecture</strong></blockquote><p>Now, to understand the entire architecture I have tried my best to simplify the namings. The names mentioned in the boxes are the names of our microservices and the numbers in the brackets represent the ports on which they will be running in the end. Now if you look at the architecture, I think it’s pretty clear that, the Dashboard Service, Pricing service and the UI are the parts which will contain all of(if not all, most of) the business logic of our application. So what are the other two, namely, API Gateway and Discovery Service? Well, in simple words, the API gateway is the entry point to our application. Any request for any resource/endpoint will have to pass through the gateway in order to get routed to the respective service(microservice). See, this is the way to separate/break our application into different parts. Now if someday, let’s suppose we have to modify something on our dashboard-service, we need not touch/modify any other microservices at all! Separation on concerns after all! For the gateway we will be using Spring Cloud Gateway which is non-blocking as compared to its old counter part Zuul. To learn more about the differences between the two, click <a href="https://www.bytesville.com/zuul-spring-cloud-gateway-comparison-benchmarks-loadtesting/"><em>here</em></a>.</p><p>Coming to the second service, i.e Discovery Service a.k.a Eureka Discovery Service. According to google, <strong><em>Eureka</em></strong><em> Server is an application that holds the information about all client-service applications. Every Micro service will register into the </em><strong><em>Eureka</em></strong><em> server and </em><strong><em>Eureka</em></strong><em> server knows all the client applications running on each port and IP address</em>. We definitely need it because we need a “monitor” which will basically be able to tell us, which microservices are up and running and which are down(if any). So let’s get to the next step.</p><blockquote><strong>Generate a multimodule parent project in Spring Boot</strong></blockquote><p>So first of all we need to have a parent project and subsequently, the different microservices (or modules) are going to be inside it. This comes in handy when we have to include certain dependencies and if we are containerizing our entire application(we can keep the docker-compose file here). The command for generating the parent is :</p><pre>mvn archetype:generate -DartifactId=&lt;artifactID&gt; -DgroupId=&lt;group_id&gt; -Dversion=1.0.0-SNAPSHOT</pre><p>(Replace the content withing the angle brackets with your own group id and artifact id).</p><blockquote><strong>Add the required dependencies in the parent pom file.</strong></blockquote><p>Now above command will provide you a whole bunch of options, make sure you select the <strong>pom root</strong> option(you could search your terminal for this). Finally after it has been generated and open your project in intellij. You can find my final code here(but I will be providing code snippets along the way too).</p><p><a href="https://github.com/lakshyajit165/microservices_demo">lakshyajit165/microservices_demo</a></p><p>So finally my parent pom looked like this(along with other dependencies included, make sure to include them as per your requirements)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f9b68043b9ce92b607e08ffd0068444/href">https://medium.com/media/6f9b68043b9ce92b607e08ffd0068444/href</a></iframe><p>Now the “modules” section in the above code snippet represents all the microservices in our project. Moving on further,</p><blockquote><strong>Generate each individual module except the UI(or in our case, each microservice)as a spring starter project and adding the required dependencies for it</strong></blockquote><p>Now proceed to <a href="https://start.spring.io/">https://start.spring.io</a> to create individual spring boot projects(except the UI). Here is an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xpo_PK3AvGNzkDjpXOE7dQ.png" /></figure><p>I will be sharing three important files for each project here namely, the pom file, the application.properties and the controller(APIs). Here’s an important note though: for gateway and eureka server, we need the respective dependencies, but for dashboard and pricing microservices, we only need, web and eureka client dependencies and don’t forget to add the <strong>module </strong>in the parent pom after including each project into our parent folder. Let’s peek into the afore-mentioned files one-by-one.</p><p><strong>The pom.xml for eureka server:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b03ffde4917957ce7310dbdb3019d6c7/href">https://medium.com/media/b03ffde4917957ce7310dbdb3019d6c7/href</a></iframe><p><strong>The application.properties for eureka-server</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92fb22284bced6c1ff5663c0b68d1c98/href">https://medium.com/media/92fb22284bced6c1ff5663c0b68d1c98/href</a></iframe><p><strong>The main file for eureka-server:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fab0dfb3747532fc715b500b2f27c836/href">https://medium.com/media/fab0dfb3747532fc715b500b2f27c836/href</a></iframe><p>As you can see from the code snippets, we are doing the bare minimum setups for a microservice architecture. Now let’s create our gateway application. Head over to spring starter website and add the dependencies. Download and unzip the folder and copy it into the parent folder.</p><p><strong>The pom.xml for gateway:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c30f037e005123daee369581dc7c6d3/href">https://medium.com/media/2c30f037e005123daee369581dc7c6d3/href</a></iframe><p><strong>The application.properties file for gateway:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab6c16fe4da4e20ce383a48e5b2406f9/href">https://medium.com/media/ab6c16fe4da4e20ce383a48e5b2406f9/href</a></iframe><p><strong><em>Note</em></strong>: Don’t worry about the routes. They will be clear once we have finished setting up all other microservices. We don’t have to run anything yet. I did create a SimpleFilter.java file in case we need some custom configuration for our routes but not to worry, we won’t be implementing anything as such in this project.</p><p><strong>The main file for gateway:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40afedce3863b4467add3d9d8f51e505/href">https://medium.com/media/40afedce3863b4467add3d9d8f51e505/href</a></iframe><p>Now if you noticed before the example screenshot for the spring initializer website contained the dependencies needed(web and eureka client) for the <strong><em>dashboard</em></strong> application. Both these microservices (<strong><em>dashboard and pricing</em></strong>) are identical in terms for the dependencies needed. The only thing which differs is — the two different routes we are exposing(which is no rocket science if you’re already familiar with spring). So in order to prevent this article from getting too verbose, I will only be sharing the controller files from both these applications.</p><p><strong>Controller for Dashboard Service:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19aa462a2fc4f75e140dff3291ea4d62/href">https://medium.com/media/19aa462a2fc4f75e140dff3291ea4d62/href</a></iframe><p><strong>Controller for the Pricing Service:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d946a4e8e617b004f493e490a070f87b/href">https://medium.com/media/d946a4e8e617b004f493e490a070f87b/href</a></iframe><p>Now our backend is ready! 😄 Since we have not containerized our application, we will have to start it in a specific order. (before that make sure to remove the micro-ui module from the parent pom and the default route in gateway’s application.properties as we haven’t prepared our frontend yet)</p><p><strong>Discovery -&gt; Gateway -&gt; Dashboard -&gt; Pricing</strong></p><p>Now if you use postman to send get requests to the created routes, we will get the following responses.</p><p>Response from: <a href="http://localhost:8080/dashboard-service/api/v1/details">http://localhost:8080/dashboard-service/api/v1/details</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cMR4alStL4N1AA3S26TZUQ.png" /></figure><p>Response from: <a href="http://localhost:8080/pricing-service/api/v1/pricing">http://localhost:8080/pricing-service/api/v1/pricing</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i_BzkmjH3XiDHsfun0Gfsw.png" /></figure><p>Now let’s go ahead and create the frontend part.</p><blockquote><strong>Preparing the UI as an angular project</strong></blockquote><p>What we need in the frontend is two components which will display the above responses. Honestly, if you managed to setup until the last step and got everything running, that’s a very good step, because setting up the frontend and getting it running is going to be way more easy. So just prepare a basic angular project with angular-cli and make sure you create two components along with default app component. If you’re not a frontend loving guy like me, feel free to browse the explanation and in the end, it will all make sense 😎</p><blockquote><strong>Creating a separate project for the UI in spring boot (to package the angular application into Spring)</strong></blockquote><p>We need to understand a few things here. In order to make it simple I will jot them down as different points.</p><ul><li>We’ll be packaging the angular application inside a spring-boot application.</li><li>We’ll be doing this in order to keep our UI microservice “<strong><em>behind</em></strong>” the API gateway, in order to avoid having CORS errors(because as mentioned earlier and depicted in the first architecture diagram, our gateway will be the single entry point for our application).</li><li>First of all make sure to use <strong>hash-routing </strong>by providing setting the “useHash” property to true in angular.json file(useHash: true, refer code). This is done so that our gateway service can resolve the routes, when they’re changed from the frontend without throwing a “Whitelabel Error”.</li><li>So create a spring boot project and only provide the web and eureka client dependencies. Now copy the angular project into this spring boot project(after importing it in the project-root directory, and make sure to remove node_modules folder before you copy).</li><li>Now we have to serve the frontend from within this spring application. We will do that by adding a maven goal. First build your frontend with the help of “ng build” command. (<strong>Note: </strong><em>Angular 8 onwards, whenever we build an angular project, instead of rendering the files inside the “dist” folder directly, it renders into the “dist/&lt;project-name&gt;” folder, it’s upto you how you like it, I prefer everything to be inside the dist folder directly, so I have modified my code accordingly, don’t worry, you can google to find out how to do it or just refer my code</em>).</li><li>The following maven goal (it’s a plugin to be more specific, which is added inside the &lt;build&gt; goal)needs to be added in the pom file then:</li></ul><pre>&lt;plugin&gt;<br>   &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;<br>   &lt;version&gt;3.1.0&lt;/version&gt;<br>   &lt;executions&gt;<br>      &lt;execution&gt;<br>         &lt;id&gt;copy-resources&lt;/id&gt;<br>         &lt;!-- here the phase you need --&gt;<br>         &lt;phase&gt;validate&lt;/phase&gt;<br>         &lt;goals&gt;<br>            &lt;goal&gt;copy-resources&lt;/goal&gt;<br>         &lt;/goals&gt;<br>         &lt;configuration&gt;<br>            &lt;outputDirectory&gt;${basedir}/src/main/resources/static&lt;/outputDirectory&gt;<br>            &lt;resources&gt;<br>               &lt;resource&gt;<br>                  &lt;directory&gt;${basedir}/frontend/dist&lt;/directory&gt;<br>               &lt;/resource&gt;<br>            &lt;/resources&gt;<br>         &lt;/configuration&gt;<br>      &lt;/execution&gt;<br>   &lt;/executions&gt;<br>&lt;/plugin&gt;</pre><ul><li>As you can see, after implementing the above plugin when we build our project, it will copy all the files in the dist folder to the static folder of our spring boot project(refer my code side-by-side to get more clarity). So now our UI is packaged inside the spring microservice!</li></ul><blockquote><strong>Modifying the routes in the gateway to run all the applications</strong></blockquote><p>Now keep the routes in the gateway as mentioned previously. The below mentioned lines,</p><pre>spring.cloud.gateway.routes[2].id=micro-ui<br>spring.cloud.gateway.routes[2].uri=http://localhost:8085<br>spring.cloud.gateway.routes[2].predicates[0]=Path=/**</pre><p>help us to route the default “/” route to render our frontend(in our case, I named it, micro-ui). And now after running all the applications in the previously mentioned order, run the UI application too. If you head over to eureka’s home page(localhost:8761), you can see that all the microservices are register to eureka and are up and running now. Like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6RLhphlnj9dS-Ijnwd0AeA.png" /><figcaption>Eureka Discovery service</figcaption></figure><p>Now let’s head over and check out the frontend part:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pT9boo0HU6J9T3fEsOP0FQ.png" /><figcaption>Home component</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ryIzsgtIP1LiFIVYZC6ZEQ.png" /><figcaption>Pricing component</figcaption></figure><p>Yay! 😃 All our microservices are now running and if you notice the browser’s console while changing routes, there are no nasty CORS errors. It’s because of our architecture. All the microservices are behind the API-Gateway.</p><p>I hope this article was helpful in providing an insight into setting up a basic microservice architecture. Please note that, though many projects in production use this architecture today, but when it comes to a business critical apps/websites, there are a lot more configurations and setup that need to be done. I learnt this during my training last year and decided to document this simple application after lots of experimenting and reading of course. Also from my experience I can say that, springboot can be a pain for new learners because sometimes the errors are difficult to comprehend. I suggest to take help from some seniors/mentors/connections whom you might know. As the saying goes “<strong><em>the best way to learn something is, to learnt from the experts themselves</em></strong>”. I was lucky enough to have amazing mentors during my training. Hope you liked this article and if you did, show some appreciation by giving it a few claps and share it with your peers who you think might need it.</p><p><strong><em>Update</em></strong>: I have updated this article to contain shell scripts. You can find 3 files in the root of the repository named, start_services.sh, stop_services.sh and restart_services.sh. They help us to start/stop/restart any of the above microservices as and when needed. This is especially helpful if the no. of microservices increases — as in that case we would need more time just to start/stop a particular microservice(or multiple microservices) on which we are working at any time.</p><p>Happy learning! 😎 😎</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=732d58b1c695" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PageFile in Windows 10 — A life saver (P.S. I still hate windows for programming)]]></title>
            <link>https://lakshyajit165.medium.com/pagefile-in-windows-10-a-life-saver-p-s-i-still-hate-windows-for-programming-2b583d50fb26?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b583d50fb26</guid>
            <category><![CDATA[windows-10]]></category>
            <category><![CDATA[operating-systems]]></category>
            <category><![CDATA[ubuntu]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Fri, 17 Jul 2020 14:38:54 GMT</pubDate>
            <atom:updated>2020-07-17T14:38:54.372Z</atom:updated>
            <content:encoded><![CDATA[<h3>PageFile in Windows 10 — A life saver (P.S. I still hate windows for programming)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*7YDchDq7OJmm0O9ZvNrE5Q.jpeg" /></figure><p>So the other day, I had to reset my laptop because I had some bootsector issues and after that I had to install ubuntu 20.04 alongside windows(as dual boot). Must say, I had tried installing ubuntu in other drives(non-SSD ones) but the performance was quite slow. So this time I decided to install it on the SSD drive.</p><p>Currently I am using the Dell Inspiron 7560 laptop whose features include — 8 GB RAM, 1 TB hard-disk along with 128 GB SSD. Also it has an i7 processor. But when I went to shrink the drive, I showed me only 8 GB of shrinkable space. I was surprised, since I already had 36 GB taken up by the default OS, i.e. windows 10 and the rest of the drive was empty. I did some digging and found out about something called as the “PageFile”.</p><p>Now according to Google —</p><blockquote>Pagefile in Windows 10 is a hidden system file with the .SYS extension that is stored on your computer’s system drive (usually C:\). The Pagefile allows the computer to perform smoothly by reducing the workload of the physical memory, or RAM.</blockquote><p>Some more points about the pagefile are as follows(courtesy - Google):</p><ul><li>Simply put, every time you open more applications than the RAM on your PC can accommodate, the programs already present in the RAM are automatically transferred to the Pagefile. This process is technically called Paging. Because the Pagefile works as a secondary RAM, many times it is also referred to as Virtual Memory.</li><li>The minimum and maximum size of the Pagefile can be up to 1.5 times and 4 times of the physical memory that your computer has, respectively. For example, if your computer has 1GB of RAM, the minimum Pagefile size can be 1.5GB, and the maximum size of the file can be 4GB.</li><li>By default, Windows 10 automatically manages the Pagefile according to your computer’s configuration and the RAM present in it. However, if you ever face lagging while working on Windows 10, or you start getting the PAGE_FAULT_IN_NONPAGED_AREA or KERNEL_DATA_INPAGE_ERROR Blue Screen of Death (BSOD), managing the size of the Pagefile manually is the first thing you should look for.</li></ul><p>Now, I came across another article which said that I had to <strong><em>uncheck the Automatically manage paging file size for all drives checkbox </em></strong>in order to allow me to have more shrinkable size for my ubuntu installation. So I did that under system properties and after that I was able to get 32 GB of shrinkable space, out of which I allocated 30 GB for ubuntu. I am still curious though, why does the system not allow me to shrink more than that! Well honestly, 30 GB is less I know, but I can handle that much of space along with all other apps and personal projects. May be it’s something related to the configuration of my laptop. I know it could be changed, and I will try my best to figure it out eventually. To be frank, <strong><em>when it comes to programming</em></strong>, <strong><em>I hate the windows OS</em></strong>, too many setups, and stuff like that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eDoDEvEv8GfZ0nSPjJz69A.png" /><figcaption>Ubuntu 20.04</figcaption></figure><p>Well Ubuntu has always been my favourite OS since college and now that I have installed the latest version i.e. 20.04, I must say it is ❤️ 😍!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b583d50fb26" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Riveting Tale of Docker Network Bridge & Docker Network Host]]></title>
            <link>https://medium.com/swlh/the-riveting-tale-of-docker-network-bridge-docker-network-host-3a543403a357?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a543403a357</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[docker-networking]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Tue, 02 Jun 2020 18:08:39 GMT</pubDate>
            <atom:updated>2020-06-03T10:14:27.783Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ovRuAuqPf4r2xpiWh71rUg.png" /></figure><p>Well I suppose there’s no developer out there, who isn’t familiar with docker. If you have ever built a near-production level app, or worked in a team that does, you’re bound to have used it to deploy your apps/services in containers. I said “near-production” level because, in my opinion, that’s the stage when you start thinking about deploying your product somewhere in containers. We as developers, may have built numerous small apps, which mostly happen to be POCs for some bigger issue that we might be working on, but we don’t usually think about containers, unless it’s a project or app, that’s suppose to scale.</p><p>Enough said, today in this article I aim to put my thoughts regarding a very serious, yet quite interesting topic, i.e <strong><em>docker-networking</em></strong>. We all know that multiple containers communicate with each other through a network. Well there are different kinds of networking that we may require according to our app’s requirements/architecture(feel free to check them out on the official docs <a href="https://docs.docker.com/network/"><strong><em>here</em></strong></a>, but in this article I will specifically discuss about two network modes, namely, <strong><em>bridge</em></strong><em> </em>and <strong><em>host</em>.</strong></p><p>I would like to mention that, I created a spring-boot app and connected it to a mysql database for the purpose of my example. So for those of you, who would like to refer to the code while reading this article, you can checkout my repository below(I would suggest, please go through the article entire if you want to make complete sense of what’s in the repository):</p><p><a href="https://github.com/lakshyajit165/springbootmysqlcrud">lakshyajit165/springbootmysqlcrud</a></p><p>So just to brief you about my project, it’s a simple spring-boot app, aimed to simulate CRUD operations. Here I have considered the idea of an online library, where, say an admin can Add, Remove, Edit and Lookup the book details. No fancy stuff, and the code is pretty straightforward, if you’re familiar with spring. Here is a snapshot of my controller class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A8IfwsZruNoQnhCocba_xw.png" /><figcaption>BookController Class(along with the spring folder structure)</figcaption></figure><p>And my data model looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hyr7hzm-9ODUTWuV6QOOHQ.png" /><figcaption>Book Data Model (and my springboot folder structure)</figcaption></figure><p>Now, the reason I wanted to discuss these network-modes in specific, is because sometime back I was working on a project, where I faced a lot of trouble in starting my containers, where one of the microservices(spring) was linked to a mysql database (also running on a container). I found a work-around at that time, but was curious to find out, what might I be doing wrong. It turns out that, it was a misunderstanding of the working of these network modes. Now let’s understand the points that I learnt from the entire process(I organized my learning into points so that it will easier to comprehend and remember).</p><ol><li>The <strong>bridge</strong> network mode is the default mode that containers use when no network mode is specified.</li><li>In case of <strong>bridge </strong>network mode, we need to do a port mapping in order to map the exposed ports of the containers to a specific port of the docker host.</li></ol><blockquote>For those you who are not clear what a docker host is, a <strong>Docker host</strong> is a physical computer system or virtual machine running Linux. This can be your laptop, server or virtual machine in your data center, or computing resource provided by a cloud provider. The component on the <strong>host</strong> that does the work of building and running containers is the <strong>Docker</strong> Daemon.</blockquote><p>3. So in case of a <strong>bridge </strong>network mode, if the mapped ports are say 3308:3306, 3308 refers to our host machine’s port, while 3306 refers to the port exposed by the container.</p><p>4. Now since we have done the port mapping we need to specify the URL in our spring boot project(in our case, the URL on which mysql container is running) as something like this:</p><pre>spring.datasource.url = jdbc:mysql://172.17.0.1:3308/library?allowPublicKeyRetrieval=true&amp;useSSL=false</pre><blockquote>Please note that, 172.17.0.1 is default IP Address of a docker host. The other details in the snippet are pretty much self explanatory.</blockquote><p>5. So in case of <strong>bridge </strong>networking, my docker-compose file looks like this:</p><pre>version: &#39;3&#39;<br>services:<br>  springsqlcrud:<br>    build:<br>      context: ./<br>      dockerfile: Dockerfile<br>    depends_on:<br>      - mysql<br>    restart: always<br><em>#    network_mode: host<br>#    hostname: localhost<br>    </em>ports:<br>      - 8081:8081<br><br>  mysql:<br>    image: mysql:8<br>    restart: always<br>    ports:<br>      - 3308:3306<br>    environment:<br>      - MYSQL_ROOT_PASSWORD=root<br>      - MYSQL_DATABASE=library<br>    container_name: mysqldb2<br><em>#    network_mode: host<br>#    hostname: localhost</em></pre><p>And the dockerfile for my spring project looks like this:</p><pre><em>#stage -1  for volume mounting maven dependencies<br></em>FROM maven:3.6-jdk-8 AS build<br>COPY pom.xml .<br>RUN mvn -Dmaven.repo.local=$MVN_LOCAL_REPO dependency:go-offline<br>COPY src src<br>RUN mvn -Dmaven.repo.local=$MVN_LOCAL_REPO -f pom.xml clean package -DskipTests<br><br><em>#stage -2 for creating the image of the project<br></em>FROM openjdk:8<br>EXPOSE 8081<br>COPY --from=build /target/springsqlcrud-0.0.1-SNAPSHOT.jar springsqlcrud.jar<br>ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;, &quot;/springsqlcrud.jar&quot;]</pre><p>I think the commands/steps/multi-stage builds are simple to understand. So now all we need to do, is go to the root directory of our project in the terminal and type:</p><pre>docker-compose up --build</pre><p>This will build the spring and mysql containers after downloading the images(if not already present).</p><blockquote><strong>Note: </strong>The mysql container takes some time to start, but since our springboot project “depends” on it, it may so happen that, you’ll notice the spring project restarting a few times after failing. Actually, it’s waiting for the mysql container to start.</blockquote><p>6. After both the containers start, you should see a screen similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hk2LAVNQaPMIw1lmR_QDyQ.png" /><figcaption>Network mode — bridge</figcaption></figure><p>7. Yohoo! Now you can go ahead and test your application by using a software like postman to make API calls and perform CRUD operations on the database. If you wish to go checkout the database, I would suggest the following steps:</p><p>In your terminal, checkout the name of your mysql container, and then type the following one after the other:</p><pre>docker exec -it &lt;container-name&gt; bash</pre><pre>mysql -uroot -proot</pre><p>Select the books table and checkout the data inside it.</p><p>8. Now let’s discuss about <strong>host </strong>networking mode. In case of <strong>host</strong>-networking, the containers instead of running on their own ports, map their ports to the host environment/machine’s ports. So here there is no need of a port mapping.</p><p>9. Here is what the docker-compose file looks like in case of <strong>host</strong>-networking mode.</p><pre>version: &#39;3&#39;<br>services:<br>  springsqlcrud:<br>    build:<br>      context: ./<br>      dockerfile: Dockerfile<br>    depends_on:<br>      - mysql<br>    restart: always<br>    network_mode: host<br>    hostname: localhost<br><em>#    ports:<br>#      - 8081:8081<br><br>  </em>mysql:<br>    image: mysql:8<br>    restart: always<br><em>#    ports:<br>#      - 3308:3306<br>    </em>environment:<br>      - MYSQL_ROOT_PASSWORD=root<br>      - MYSQL_DATABASE=library<br>    container_name: mysqldb1<br>    network_mode: host<br>    hostname: localhost</pre><p>10. Please note that the I have commented the port mappings we did previously. Now we have to modify our spring datasource url too. Since we are using host machine’s network(in this case it will be our laptop/desktop/workstation, while deploying to cloud, it may be a VM), we can mention localhost</p><pre>spring.datasource.url = jdbc:mysql://localhost:3306/library?allowPublicKeyRetrieval=true&amp;useSSL=false</pre><p>11. The other steps of building and creating the containers remains absolutely same. After it runs successfully, you should see a screen similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KRg5V_NBrwaVXHTU6gcOSw.png" /><figcaption>Network mode host</figcaption></figure><p><strong>NOTE: (Few important take aways):</strong></p><ul><li><strong><em>Docker network host works well on a linux platform but not on a windows platform(I have no idea about MacOS, please feel free to check that out).</em></strong></li><li><strong><em>On a windows platform, the docker host’s IP is chosen randomly at runtime.</em></strong></li><li><strong><em>We can skip mentioning the “hostname: localhost” when using host networking mode.</em></strong></li><li><strong><em>On a windows platform the users, instead of using hostname, can use host.docker.internal(please check the official docs for windows platform configurations)</em></strong></li></ul><p>So that was all about these to networking modes in docker. I have been using docker since quite sometime now and I find it to be extremely useful. I hope that I will be able to clear the confusion which might have crept-in in some people’s minds(especially beginners) about these networking modes. <strong><em>FYI, the repository link whick I provided, contains the host-networking example in the master branch and the bridge-networking example in the network-bridge branch. Do check out both of them.</em></strong></p><p>I like to work on different projects as a part of my learning process, even if I am already a full time working employee at IBM India Pvt. Ltd. I believe that there’s no subsitute to <strong><em>“learning by self”</em></strong> and when it comes to acquiring knowledge, well, <strong><em>“Sky is the limit”</em></strong></p><p>Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a543403a357" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/the-riveting-tale-of-docker-network-bridge-docker-network-host-3a543403a357">The Riveting Tale of Docker Network Bridge &amp; Docker Network Host</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying an Angular App to Netlify using Netlify CLI and Gitlab CI]]></title>
            <link>https://lakshyajit165.medium.com/deploying-an-angular-app-to-netlify-using-netlify-cli-and-gitlab-ci-313d9ff9180a?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/313d9ff9180a</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[netlify]]></category>
            <category><![CDATA[gitlab-ci]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Thu, 14 May 2020 10:26:47 GMT</pubDate>
            <atom:updated>2020-05-15T03:38:53.745Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vjjNi1M4w3xY95suju3utw.png" /></figure><p>Hola folks! I planned on writing this article quite sometime ago, but eventually got caught up with some work. Finally here it is.</p><p>So basically, my focus in this article is the “CI” part. We will build a simple angular app, using a frontend framework(because that will help us focus more on the task at hand) and we’ll also take a peek on writing unit test cases. For those of you, who are totally new the concept of CI/CD, you can go through this amazing article at hackernoon,<a href="https://hackernoon.com/understanding-the-basic-concepts-of-cicd-fw4k32s1"><strong> here</strong></a>.</p><p>Following are the steps we’ll follow in order to deploy an Angular App to netlify, using Gitlab CI.</p><ol><li>Create an Angular App using Bulma CSS framework.</li><li>Write a simple unit test case for our app.</li><li>Download and install netlify-cli.</li><li>Use netlify-cli to create a new app and generate the Auth Token and Site ID for our app.</li><li>Configuring the netlify.toml file.</li><li>Setup a Gitlab project for our angular app, and add the environment variables.</li><li>Configuring the .gitlab-ci.yml file to trigger the pipeline in Gitlab, once we push our code.</li><li>Voila! We’re done. Our site is deployed to netlify and is now live!</li></ol><h3>Now let’s have a look at these steps in details:</h3><h4>1. Create an Angular App using the Bulma CSS framework.</h4><p>I assume you are familiar with angular. It’s a frontend framework developed and maintained by Google. I won’t go into every bit of installing and configuring the angular-cli and everything, because those steps are pretty much straight forward and including them here will make this article unnecessarily verbose. Same applies for the <a href="https://bulma.io/"><strong><em>Bulma CSS</em></strong></a> framework as well. I just chose some components to resemble a Blog for an imaginary company called Acme solutions. Feel free to go through the <a href="https://angular.io/"><strong><em>official docs</em></strong></a> and design an app using any framework/concept you like. My folder structure and the app design looked something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pEnm95-I0JVontUJACetYA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PIS5WicmYSvX3JqGbHcM3Q.png" /></figure><h4>2. Write a simple unit test case for our app.</h4><p>Unit tests help us in maintain a track of the overall functionality of a single component. By component I don’t necessarily mean an Angular Component. We could write unit test for a service or even a function. In my app, I just wrote a unit test to verify whether a specific title is present in the component or not. My entire test file looked something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nes3pvCUVxV013ovSPE7Og.png" /></figure><p>Now that we have written a simple unit test, it’s time to test everything out. Run ng test in the terminal and we can check that all the test cases are passing, which is a good job!</p><blockquote>FYI, Angular uses Jasmine framework to carry out all the unit and end-to-end tests. The unit tests are run using Karma, and the end-to-end tests are run using Protractor. Feel free to checkout the official documentation at <a href="https://angular.io/"><strong>angular.io</strong></a>.</blockquote><h4>3. Download and install the netlify-cli.</h4><p>Go to your terminal and install netlify-cli using npm i -g netlify-cli</p><h4>4. Use netlify-cli to create a new app and generate the Auth Token and Site ID for our app.</h4><p>Before using the netlify-cli make sure you have an account on netlify.com and create a team(steps are pretty much straight-forward, similar to sites of heroku or digital ocean). Now run the following commands in the terminal:</p><p>Run netlify init --manual and follow these wizard steps:</p><ol><li>Select:<em> Create &amp; configure new site</em></li><li><em>Give it a name (optional)</em> (I’ll call it acmesolutions-dev<em>)</em></li><li><em>Pick your team, if any and press Enter to finish.</em></li><li>This generates a SITE_ID</li></ol><p>Now go the netlify site and under the user section, inside applications tab, generate a new access token. This token will be used as one of the environment variables in our CI environment.</p><h4>5. Configuring the netlify.toml file.</h4><p>Create a new file in your project root name <strong>netlify.toml</strong><em>. (By definition: </em><strong><em>TOML</em></strong><em> stands for Tom’s Obvious, Minimal Language. It is a data serialisation language designed to be a minimal configuration </em><strong><em>file</em></strong><em> format that’s easy to read due to obvious semantics. </em><strong><em>TOML</em></strong><em> is designed to map unambiguously to a hash table. </em><strong><em>TOML</em></strong><em> should be easy to parse into data structures in a wide variety of languages)</em></p><p>Add the following lines to the file.</p><pre>[build]<br>  publish = &quot;dist&quot;</pre><p>NOTE: Make sure the spacing and the double quotes are correctly placed, otherwise this would result in pipeline failures. Please note that, I refer the build folder here to be “dist”. But from Angular 8 onwards, when you run ng build in the root of your project, it builds your project inside the build/&lt;projectname&gt; folder. To change it to build directly into the dist folder, make sure you modify the angular.json file appropriately.</p><h4>6. Setup a Gitlab project for our angular app, and add the environment variables.</h4><p>Create a new gitlab project and connect to your project as a remote. Now inside this project under the settings tab, go to CI/CD and add the NETLIFY_AUTH_TOKEN and NETLIFY_SITE_ID with the values generated before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*paPnMpC3csTXC5hfxr_DTw.png" /></figure><h4>7. Configuring the .gitlab-ci.yml file to trigger the pipeline in Gitlab, once we push our code.</h4><p>Now we setup the .gitlab-ci.yml file in our project root. This is the file which is responsible for triggering the multi-stage builds when we push our code and subsequently deploy our site to netlify(if there are no build failures). The file looks something like this <em>(If you want to explore more about gitlab-ci file, I would suggest you to go through the documentation </em><strong><em>here)</em></strong>.</p><pre>image: trion/ng-cli-karma<br><br>cache:<br>  paths:<br>    - node_modules/<br><br>stages:<br>  - setup<br>  - test<br>  - build<br>  - deploy<br><br>setup:<br>  stage: setup<br>  script:<br>    - npm ci<br>  only:<br>    - master<br><br>test:<br>  stage: test<br>  script:<br>    - ./node_modules/@angular/cli/bin/ng test --progress false --watch=false<br>  only:<br>    - master<br><br>build:<br>  stage: build<br>  script:<br>    - node_modules/@angular/cli/bin/ng build<br>  artifacts:<br>    paths:<br>      - dist<br>  only:<br>    - master<br><br>deploy:<br>  stage: deploy<br>  script:<br>    - npm i -g netlify-cli<br>    - netlify deploy --site $NETLIFY_SITE_ID --auth $NETLIFY_AUTH_TOKEN --prod<br>  dependencies:<br>    - build<br>  only:<br>    - master</pre><p>Phew! We completed a lot of stuff. Now we are ready to push our code to gitlab. Commit your changes and push them to your remote repository. If everything is setup well, it will trigger the pipeline immediately. Since we have declared 4 stages in our gitlab-ci file, the pipeline should look something like this(while processing).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TL73NNEE9BrxfVsC88eQJg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KWcsr8YkBseJDYKO27j0CQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5QnjWPpH3TkjPbbO15KuMw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EloauCpAbv-zdKADL_YfDQ.png" /></figure><p>Now go to your site (&lt;yoursitename&gt;.netlify.app) to find it live. Mine’s shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tBPNmQ_4ek-_rtfUzylWRw.png" /></figure><p>Wohoo! We did it. Thanks to Gitlab-CI, our site is build, tested and deployed to netlify, in minutes! I hope you found this article helpful. Please give it some claps if you liked it.</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=313d9ff9180a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Final Project — Java Full Stack]]></title>
            <link>https://lakshyajit165.medium.com/final-project-java-full-stack-a95e00197aec?source=rss-792ccd21180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a95e00197aec</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-framework]]></category>
            <category><![CDATA[full-stack-development]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Lakshyajit Laxmikant]]></dc:creator>
            <pubDate>Tue, 12 Nov 2019 13:06:58 GMT</pubDate>
            <atom:updated>2019-11-22T06:15:08.145Z</atom:updated>
            <content:encoded><![CDATA[<h3>Final Project — Java Full Stack</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XhLu0b-kQaXNWpWuHvD-hg.jpeg" /></figure><p>So, I joined IBM in the month of June, this year and was pretty much excited, but I never ever, ever ever imagined this was going to be the toughest training I have undergone till date!😂😢.</p><p>So there I was in Stackroute, Kormangala, for my training in Java Full Stack. Our entire batch was divided into 3 teams. Each team had to develop and deploy a product in the end. For my team, the product was — an <em>easy to use</em> and <em>pluggable</em> <strong><em>Helpdesk System</em>.</strong> One of the teams in our batch had to develop an app call <strong><em>Umove</em></strong>(basically a bike renting system)<strong>.</strong> So our team, had the task of integrating our helpdesk system with the Umove team.</p><p>We decided to name our app as “<strong><em>Nikolaj</em></strong>”(which means victory to the people). So we developed the app using Spring boot as backend and Angular in the frontend. Below is a concise description of our app.</p><ul><li>First of all we have a bot named <strong>Optimus</strong>,(we used <strong><em>IBM watson</em></strong>, and along with it, <strong><em>Stanford NLP</em></strong>), which is supposed to resolve a particular query by a user.</li><li>The bot is supposed to resolve a user’s query based on some <strong><em>intents</em></strong> and after that <strong><em>execute some commands</em></strong>. The intent is identified by the bot itself based on the user’s query.</li><li>If the bot has a particular intent, and it is confident about the command(each command stored with the bot has a<strong><em> confidence level </em></strong>associated with it, which when greater than 90%, is executed by the bot), it executes the command and resolves the query and then, asks the user for a rating.</li><li>If the bot is unable to resolve a particular query, there are 2 cases. Either the intent and command both are unavailable with the bot or the bot has the command with it, but is not confident enough to execute it.</li><li>In such a case, the bot creates a ticket for that particular query and sends it to the queue(We used<strong><em> RabbitMQ in our project for such queueing and messaging services</em></strong>).</li><li>Here comes the role of the<strong><em> CSR</em></strong>(Customer Support Representative). As stated earlier, whenever the bot is unable to resolve a query, a ticket is raised and sent to the queue. On the CSR’s side, he/she is supposed to resolve a particular query by assigning himself/herself a ticket. This can be done with the click of a button.</li><li>Whenever a CSR assigns himself a ticket, he is redirected to the ticket details page, where he is able to see the ticket details namely, the user who raised the ticket, user’s query, ticketID and also the previous chat which the user had with the bot.</li><li>The CSR then goes on to resolve the user’s query by chatting with him, and based on the query, he can also execute a command, which will give him a response, which can be shared with the user as a response to their query.</li><li>Now, if the bot does not have a command or intent with it, for a particular query, the CSR is provided with the option to provide a suggestion(which is optional) for the names of the command and intent.</li><li>If the bot is not confident enough for a particular command, it will suggest that particular command to the CSR. If the CSR executes that command and the results obtained after executing it resolve the user’s query, the CSR is supposed to give a success rating to that command which will in turn update the confidence of the bot for that particular command.</li><li>For this helpdesk system, we also have 2 other aspects — the command <strong><em>framework and reports</em></strong>.</li><li>The command framework basically is a storehouse of all the commands that are supposed to be executed by the bot/CSR or referred to by the CSR. It also contains a <strong><em>sunburst chart</em></strong>, depicting the queries which have no command/no intent.</li><li>The other part is the reports — there are different kinds of reports, namely ticket status reports(which show the number of tickets in the various states — open, closed, engaged), Service reports, which are calculated based on the service which may have an issue. These reports help provide an insight to the service which integrates our helpdesk system, about the different areas where they might want to improve their app.</li></ul><p><strong><em>The main motive of our app was to help the services elevate their profits over a period of time by eliminating the need to maintain customer support representatives. This can achieved gradually as the bot gets trained to resolve the user’s queries by executing commands.</em></strong></p><p>Following is the<strong><em> architecture diagram </em></strong>of our app:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a3zF4N7Fp_fc3CT1mDj5oA.jpeg" /><figcaption>Architecture diagram for Nikolaj</figcaption></figure><p>The technologies used in this project are:</p><ul><li><strong><em>Angular 8: for frontend.</em></strong></li><li><strong><em>SpringBoot: for backend.</em></strong></li><li><strong><em>Docker: for containerizing our microservices.</em></strong></li><li><strong><em>RabbitMQ: for queueing and messaging services.</em></strong></li><li><strong><em>Databases: MongoDB, Neo4j and Redis</em></strong></li></ul><p>P.S. <em>The final product was deployed on AWS VM, which at present, is not active (as the VMs were assigned to the respective teams only for deployment and one time demonstration purposes)</em>.</p><p>Following is the code for this project. Do give it a star, if you like it!</p><p><a href="https://github.com/lakshyajit165/Nikolaj">lakshyajit165/Nikolaj</a></p><p>Following are some of the glimpses from the day of our presentation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ih4JYbu5ewAFXTUgGRdMOA.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FspQOLfsFSeqbBxRnawTGw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YwjBCIpqEudsKvND_Tijug.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*-2hfYdbYGvW4shWQxYsQ5A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uCd67sn1NjLoUTb7qcArwg.jpeg" /><figcaption>The Graduation Day!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a95e00197aec" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>