Intro to Angular, Web API and Azure
Intro to Angular, Web API and Azure
So I thought it would be beneficial to discuss Angular, Web API and Azure in some form of depth as well as provide an entire set of functioning code. I will start by addressing a few things, What is Angular, What is Web API, and what is Azure? Followed by the code and explanation of the code. The code itself provides a simple website, which has restful routing, requests for processing and lists out data from a database acquired from said processing.
This is a pull from my website: www.indiedevspot.com Check out my other articles!
What is Angular?
What is Web API?
Web API is an ASP.Net (or .net) framework for providing restful services to clients. These services are easily data mined and therefor very conducive to search engine optimization. Not only that, but they are easily repackaged and sold. Beyond THAT they are also cross platform and follow a standard that most client developers understand. Finally it provides all of the security and goodliness that classic MVC provides. So in short, Web API is just pure awesomeness for a server side developer who wishes to ship product to the world in a timely fashion.
What is Azure?
Azure is the Microsoft Cloud Platform. I have tons of slide decks on my site regarding intro to Azure, Azure Architecture, How to – Azure etc. Go to the Events section and just download nearly any events slide deck and you will find something regarding Azure. In the instance of what we are discussing today, Azure provides the hosting platform for the website as well as the compute space for our web job which scrapes git hub, synchronizing the scale of both together. Azure supports a plethora of technologies and languages on its “Web Apps” platform, which is what we primarily intend to take advantage of. Beyond the Web Apps platform, this solution takes advantage of SQL Azure and Azure Storage. You will notice that all three technologies are within the PaaS world. This is purposeful as PaaS enables me as a developer to push out quality product faster. If there is enough interest, I will post an article about PaaS vs SaaS vs IaaS.
ONTO THE CODE!
The code is located here: https://github.com/drcrook1/Nerds4Hire As of the time of this writing, it is 8:33pm EST on 4/27/2015. Look through the check-in history for that specifically if you see significant changes, as this is a living demo/project.
You should notice in the Solution, three different projects.
- NerdsForHire.Services: This is the C# Web API, AngularJS, HTML etc. This comprises the primary components of the Client and Client-Server interaction of the App. This essentially IS the app from a user’s perspective.
- NerdsForHire.SQL: This is the SQL code that you need to deploy to your SQL Azure database for use in the Services Project.
- NerdsForHire-GitHubScraper: This is the Web Job we deploy along side our app which scrapes GitHub for users data. It uses one of my favorite languages, F# :).
To Start, We should Deploy this solution to your own Azure Account.
Always deploy your database first. This is as simple as going to www.azure.com, signing into your account (or creating a new one) and finding the Databases tab on the left and choosing to create a new one. After which, you can right click the NerdsForHire.SQL project and publish. There will be a series of boxes that you must fill out for this. I’m going to assume that you can figure this out. If you struggle here, you should tweet me @DavidCrook1988 for assistance and I will not only assist you, but also update this blog with more specific steps.
You will also need to create a storage account with a queue. The name of the queue will need to be all lowercase. You will need to change the Web API code and the Scraper code to reflect the name change of this queue.
After the Database, let Deploy the App itself. This is SUPER simple! Right click the .Services project, click publish, and select “Azure Websites”. It will prompt for you to login, do so, and then elect to create a new website with whatever name you feel like. Note, you will need to modify the web.config within the project to point to your database specifically. Change the connection string to point to YOUR SQL Database (mine might not still be around).
Finally, Deploy the Web Job. Again, super simple, right click publish, select the previously created web site. You are golden! Note, on this, you may need to install several F# extensions or plugins. Tweet me @DavidCrook1988 if you run into trouble, or see the following article: http://indiedevspot.azurewebsites.net/2015/02/18/deploying-f-web-job-to-azure/
Explaining the Code
We will only focus on NerdsForHire.Services as the other two are pure Web Job and SQL Script, which is not the core focus of this article.
Within the NerdsForHire.Services Project, you will see a folder called “Scripts”. This is where all of our Angular magic happens. Vendor contains all of the logic provided by third parties. These are dependencies that our client takes on to provide the experience that it does. App.module.js contains all of the core setup and initialization logic, including our routing. You will notice as you click on links you get mywebsite.com/#/routeOne. This is set up by the routing. Controllers contain the logic which is injected into the views and Views contains the actual views for our code.
There is only one view, which is not a .html page, and this is a result of the current ASP.Net templates defaulting to requiring a HomeController and needing an index to route to. This page is basically an html page, which provides the initial landing of our app and you can begin to experience how databinding occurs in Angular.
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33||
Above follows John Papa’s Angular Style guide. This particular HTML is considered the View in the MVC model. To see the C or, controller, take a look at the code below from the controllers folder.
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27||
This page shows hooking up a function to a button press. In this instance, on the push of the button with tag ng-click we fire off the scrapeGitHub function, which posts out this data to our Web API, which then in turn pushes an item on Azure Queue storage, which is picked up by our webjob, processed and pushed into SQL Azure. Bad Ass!
Lets take a quick look at the code that does this…
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23||
Pretty straightforward, we receive a moderate sized packet, and we really only need the githubId portion of that and push it onto the queue. The web job actually then does something with that. Lets go ahead and peek at that then. Note that this code lives in the project: NerdsForHire-GitHubScraper. I am only pasting the code regarding receiving the message and not the actual HTML parsing. You can look at the rest of the program.fs file for that information if you wish, which is located here: https://github.com/drcrook1/Nerds4Hire/blob/master/NerdsForHire/NerdsForHire-GitHubScraper/Program.fs
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20||
So as you can see here, we make a little bit further use of our Azure Storage account for monitoring and storage of queued messages. This is a requirement of queue storage. Also notice the function with the annotation: [<QueueTrigger("githubScrapeInput")>]. This indicates we are listening to incoming messages on the queue “githubscrapeinput”. It is camel cased here, but F# takes care of reducing this to lowercase for us. Typically this should be all lower case.
Wow, we are making some serious progress here. Lets talk a little further about displaying this data. routeTwo is where we go to make the call for all github repositories we have stored in our database. So once the webjob finishes, it puts the data into the SQL Database for us, and routeTwo makes a web api call out to go get the data from the database. It has two way databinding to display that data on the routeTwo injected HTML.
Lets look at the HTML first.
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16||
You can see we define our controller as RouteTwoController. This controller must be defined within the context of our app prior to this html being loaded. Also we rename the controller to RouteTwo, so we can refer to objects within it. We then have a “foreach” in which each repository is named repo and we list out the title, how many stars and the description of each repository.
Lets now look at the Controller powering this.
|1 2 3 4 5 6 7 8 9 10 11 12||
Wow, this is a nice short one. We have a self executing function (per John Papa Styling), in which we define an empty repositories variable, and make a get call out to get ALL repositories from our API. We then assign the resulting data from that api call to our repositories. Due to the Angular two way data binding, we do not have to raise any events. Its all taken care of for us. On loading the page, give it about 10 seconds and it will load (due to being on free tier websites and free tier sql).
So what does the Web API for this look like?
|1 2 3 4 5 6 7 8 9||
My goodness do I like Entity Framework right about now. Because that kicks so much ass! That’s it folks, that gets all the repositories from our database. Of course we can use OData options to restrict this back to a page by page result set, but that is for you to look up :).
We covered AngularJS basics, Web API basics and some Azure. I know this is a ton of info to take in as we covered the whole stack plus web jobs. Tweet me: @DavidCrook1988 for any assistance and I will gladly help you get this rolled out.