Posts

How to Deploy Microservices on Google Cloud

Today, I'll show you how you can deploy a multi module project on GAE using Maven. Because the EAR file is not allowed, we basically wrap our microservices inside a parent module (pom). Parent Module The parent module has its packaging tag set to  pom . All shared configuration will be placed inside the parent pom.xml via dependencyManagement or pluginManagement . As you probably know Maven provides the notion of aggregation by declaring submodules explicitly in the parent pom.xml. In the above configuration, I've declared 3 submodules (war). You can create the parent project with the following cmd: mvn archetype:generate -DgroupId=com.vedrax -DartifactId=parent-project PS: I'll soon register an archetype to generate the configuration as presented above with the required submodules. Submodules You can in the parent's directory run the following cmd: mvn archetype:generate -DgroupId=com.vedrax -DartifactId=company-service mvn archety...

Exception Handling with Angular (6.0.7)

Our target : globally catch errors. The angular way to catch error globally is to implement a custom  ErrorHandler . Basically, I would like to act differently when we have server or client errors. When the server notifies an error, Angular throws an  HttpErrorResponse . For client-side error we will have an Error instance instead. When the user is not authorized (401), he may be redirected to the login page. When the user is not allowed to see a resource (403), he may be redirected to the forbidden page. 1. The errors service Basically, this service takes all errors information and send them to an endpoint (e.g. Help Desk). 2. The errors handler The router navigation should only be triggered inside Angular zone. 3. The core routing module Please take a look at my demo . Don't hesitate to share and comment...

Deploy Angular 2+ to GAE using Maven

Image
For a big project we have in my company, we needed to deploy an angular application (version 6) to the google cloud infrastructure (GAE). I'll show you step by step how you can deploy an angular application to the GAE by using Maven. My configuration: Java 8 and Maven 3.5 1. Create a GCP project Create a Google Cloud Project (GCP) through your Google Cloud Console . You will need the app ID for the following steps. You will need also to install the latest version of the App Engine SDK and add the directory to your PATH 2. Create a new project using maven command Go to the directory where you want to build the project and open a terminal at this location. On your terminal invoque this maven command to create an empty App Engine project: When prompted, you should accept default values (archetype, version...) and enter the groupId (your namespace), the artifactId (project name), version (accept default), package (accept default). When your project success...

Authentication and Authorization with Angular 2+

In this tutorial, I'll show you how you can secure your Angular 2+ application. Please, take a look at my previous post ( Authorization and Authentication with AngularJS + Jersey) for the backend implementation.   1. Authentication mechanism We use JWT (JSON Web Token) authentication mechanism in our application. We have decided to store our JWT in a cookie with HttpOnly (prevent XSS attacks) and Secure  (sent over HTTPS only). In order to prevent CSRF attack we'll use the double submit cookie .   This pattern is defined as sending a random value in both a cookie and as a request header. When a user authenticates to our application via the login page, the API responds with 2 cookies: A cookie (XSRF-TOKEN) set with a strong random value with Secure flag only.  We will instruct Angular HttpClient  to read this value and set it as an HTTP header (X-XSRF-TOKEN) for each subsequent request. Since only JavaScript that runs on your domain can read the cooki...

Angular Testing: routerLink

Today, I'll show you how you can test such a code: The routerLink directive on the anchor tag give the router control over this element. Find more info here . A. Provide the routerTestingModule with dummy routes as: B. Declare  Router and Location for use in your test: C. Routing is an asynchronous activity so we use one of the asynchronous testing methods at our disposal, in this case the fakeAsync method. Happy coding!!!

Angular Testing: ActivatedRoute

The route information and parameters are available through the ActivatedRoute service. While testing your components, it may be good to mock this service. 1. Mock ActivatedRoute If the user select an item within a list, the snapshot strategy can be used in your detail component. The route.snapshot provides the initial value of the route parameter map. The parameters can be accessed this way: The mocking service: 2. How to use? First we need to make available our ActivatedRouteStub in our test and pass testing parameters. So, in your test: 3. Mocking ActivatedRoute using a factory Using a factory is somewhat easier. In our component we have this code which we'd like to test: The test below uses a fake ActivatedRoute registered with the useFactory : Thanks

Rounded Percentages up to 100% using Insertion-Sort Algorithm

When you need to represent percentages as whole numbers using   Math.round() ,  you end up with a total of 101%.  Value Percentage Rounded A 650 49.88% 50 B 230 17.65% 18 C 150 11.51% 12 D 273 20.95% 21 1303 100.00% 101 As you can see above the total in the rounded column is 101. Let's solve this problem using a well known algorithm. Step 1: We begin to sum the given values for calculating the percentages. While iterating, we copy all the properties to a destination object ( NVPPercentage ) which will be pushed to an array ( temp ) . Step 2: The insertion sort, is an efficient algorithm for sorting a small number of elements. We start for each element to calculate the percentage ( P ) and the rounded percentage ( RP ). We sum also the rounded percentages (e.g. 101). We also compare in an inner loop the current element ( CE ) with the left ones for sorting purpo...