Website up!

These days it is a 5 minutes step to have a professional website up and running. I decided I would configure everything from the ground up to gain more practice with the tools and technologies that are normally automatically configured for us.

Step #1 – My domain and Route53

I had already bought a domain jfalco.com in Route53 but it was not being routed anywhere yet. I followed this guide to forward the jfalco.com traffic to the EC2 instance.

  • Created an A record http://www.jfalco.com that pointed to the EC2 instance IP. Wait! Isn’t http://www.jfalco.com different than jfalco.com? I tried to access my EC2 instance through my newly configured domain but my browser kept returning “This site can’t be reached – ERR_NAME_RESOLUTION_FAILED”. What?! But I just configured the route! Maybe it has not propagated yet, it being the cloud and all… Waited a good 20minutes and still would not budge.
  • After a lot of reading and googling, turns out my jfalco.com name servers were different than my hosted zones name servers. Doh! I made the assumption Route53 would automatically use the same name servers as my domain given that I literally created the hosted zone by clicking on my domain through the Route53 console. I copy pasted my hosted zones name servers to my domain name servers and it took roughly 30 seconds to start working. I also added a tiny CNAME record to link jfalco.com to http://www.jfalco.com. Woot! My domain is finally pointing to my website!

Step #2 – EC2, Tomcat8 and the actual content of the Website

  • Previously I had very hastily installed tomcat8 just to make sure my EC2 instance could be accessed. Now I had to make the real configuration and deploy my war file. First, I moved the manager app outside of the root and very quickly found the configuration I required to make my war be the root as well as change the port to be 80 instead of :8080; it would not look very good if people had to type http://www.jfalco.com:8080/mywebsite.
  • To make the war be root, the war file has to be named ROOT.war and the folder with the content has to also be named ROOT.
  • To change the port to be :80, the file ‘server.xml’ in the tomcat8/conf folder needs to have the changes for port :80.
<Connector port="80" protocol="HTTP/1.1"
 connectionTimeout="20000"
 redirectPort="8443" />
  • Also had to change users to secure access.

The Website

  • Using Java EE I went ahead to setup my project in Eclipse. First hurdle, how do I configure the web.xml to serve the content of my website? At first I tried
<servlet>
    <servlet-name>home</servlet-name>
    <jsp-file>/index.html</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>home</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

And that did work with my <html><body>Hello, World!</body></html>, but failed once I tried to serve any CSS, JS or image file.

  • One more thing to note in here is that the html files need to be at the root of the WebContent folder, right above WEB-INF.
  • Turns out I did not need any servlet mappings yet.
<welcome-file-list><welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>

Was all I needed.

  • Great, now to the actual development of the content. Since I am doing everything from scratch I want to leverage on some frameworks, that will also help me learn. ReactJS + Typescript + bootstrap + LESS or SASS and a package manager like npm? Maybe I could use AngularJS? What about the testing frameworks? Wait… I need to take a step back. Do I really need all this complexity? I am going to have a very simple home page with some of my information and the blog is currently hosted in wordpress, so… Maybe I just need bootstrap  to make my CSS life easier and npm to make packaging slightly easier.
  • My initial setup includes jquery and bootstrap. I created a very simple introductory home page that needs more styling but its not terrible. I think its a good start and I finally have my landing page that gives me an online presence. I have a ton of plans for that website, but one step at a time.

A lot of good lessons learnt in here: how to configure name servers in Route53; a good tomcat8 installation in EC2 that displays the WAR file at the root level; and refreshing those web development skills. I am not happy with the theme yet but its something.

Advertisements

Two Sum

Problem:

Given an array of integers and an integer target, find any 2 numbers in the list that add up to the target.

My first naive approach was to brute force it: for every number, iterate it against every other number and check to see if the addition matches the target. That is great, we will find the solution but its a very slow algorithm. It consumes n^2 processing cycles in the worse case scenario.

Trying to do better – initial thoughts:

  • If the target is greater than zero, we do not need to consider any number that is larger than the target.
  • If the target is small than zero, we do not need to consider any number that is smaller than the target.
  • Is the data organized in the data structure that we need? Maybe the algorithm is slow because it is constrained by the list/array of integers.
  • Is there any data structure that gives me O(1) access to the data? A map or a set could do the job but then how would the algorithm look like? Plus I would need to process the data to store it in the new data structure, that would take n cycles.

This did not come right away to my thoughts but after reading more about it I did understand it: the difference of any index in the array and the target can be found in the newly built set fairly quickly. If we do not find the data, then there is no answer. I also ignore any number larger than target.

Java solution

Not terribly bad – 2 for loops over n, so n+n(2n). Definitely better than n^2. The solution also traded processing for memory because now an extra Set is required.

Update and reminder to myself: at first I did not take into consideration negative numbers. My initial logic was discarding all negative numbers because of the logic: “we do not need to consider any number that is smaller than the target”. The previous logical statement works only when target is greater than zero. Funny enough by trying to optimize, I ended up making it more complicated and breaking the whole thing with any target less than zero.

Update #2: The optimization was actually breaking a case. What happens when the target is 10 and the array has numbers like 12 and -2?

jfalco

The intention of this initial post is to hopefully serve as a guide for myself to remind me of the goal for this blog.

I often find myself writing small notes or documents so that I remember a tutorial, a problem I was working on, how to configure a service or some other technical piece of information that I want to keep for a later date. I am far from a good writer, but I thought to give it a try at writing things in a blog to keep it organized. At the same time I might also begin establishing a professional footprint on the web.

Lets see how it goes!