Chapter 7. Consuming Services Using a Microservice Web App

Now, after developing the microservices, it would be interesting to see how the services offered by the Online Table Reservation System (OTRS) could be consumed by web or mobile applications. We will develop the web application (UI) using AngularJS/bootstrap to build the prototype of the web application. This sample application will display the data and flow of this sample project – a small utility project. This web application will also be a sample project and will run independently. Earlier, web applications were being developed in single web archives (files with .war extensions) that contain both UI and server-side code. The reason for doing so was pretty simple as UI was also developed using Java with JSPs, servlets, JSF, and so on. Nowadays, UIs are being developed independently using JavaScript. Therefore, these UI apps also deploy as a single microservice. In this chapter, we'll explore how these independent UI applications are being developed. We will develop and implement the OTRS sample app without login and authorization flow. We'll deploy a very limited functionality implementation and cover the high level AngularJS concepts. For more information on AngularJS, you can refer to AngularJS by Example, Chandermani, Packt publishing.

In this chapter, we will cover the following topics:

Now since we are ready with our HTML5 web app setup, we can go through the basics of AngularJS. This will help us to understand the AngularJS code. This section depicts the high level of understanding that you can utilize to understand the sample app and explore further using AngularJS documentation or by referring to other Packt publications.

AngularJS is a client side JavaScript framework. It is flexible enough to be used as a MVC (Model View Controller) or MVVM (Model-View-ViewModel). It also provides built-in services like $http or $log using a dependency injection pattern.

A module is the first thing we define for any AngularJS application. A module is a container that contains the different parts of the app such as controllers, services, filters, and so on. An AngularJS app can be written in a single module or multiple modules. An AngularJS module can contain other modules also.

Many other JavaScript frameworks use the main method for instantiating and wiring the different parts of the app. AngularJS does not have the main method. It uses the module as an entry point due to following reasons:

Each AngularJS app needs to have a single module for bootstrapping the AngularJS app. Bootstrapping our app requires the following three parts:

An AngularJS module has two important parts, config() and run(), apart from other components like controllers, services, filters, and so on.

Let's have a look at the following code:

$log is an inbuilt AngularJS service that provides the logging API. Here, we are using another inbuilt service, $injector, that allows us to use the $log service. $injector is an argument in the controller. AngularJS uses function definitions and regex to provide the $injector service to a caller, also known as the controller. These are examples of how AngularJS effectively uses the dependency injection pattern.

AngularJS heavily uses the dependency injection pattern. AngularJS uses the injector service ($injector) to instantiate and wire most of the objects we use in our AngularJS app. This injector creates two types of objects – services and specialized objects.

For simplification, you can say that we (developers) define services. On the contrary, specialized objects are AngularJS stuff like controllers, filters, directives, and so on.

AngularJS provides five recipe types that tell the injector how to create service objects – provider, value, factory, service, and constant.

As we have now some understanding of services, we can say that there are two common uses of services – organizing code and sharing code across apps. Services are singleton objects, which are lazily instantiated by the AngularJS service factory. By now, we have already seen a few of the in-built AngularJS services like $injector, $log, and so on. AngularJS services are prefixed with the $ symbol.