Most Recent Thoughts
Sites.Local – for developers with too many local sites
Posted December 5th, 2009 at 5:28 pm by Matt Chepeleff
As an ASP/C# developer it’s key I work locally and save time wasted uploading files after every change to a page. Then, when I’ve got a bunch of changes to share with a client and I want to update a staging/production environment, I can do it all at once. Well, I resumed working on a project this afternoon that I haven’t worked on for a couple days – and I just couldn’t recall the local site address!
My laptop runs Vista Business and I use Internet Information Services (IIS) to run local apps. To map the various apps and keep everything running on port 80 I take advantage of the Windows Hosts file to create custom urls for local apps. For example, I can create an entry for clientname.localdev which enables me to setup and view a site at http://clientname.localdev/. You can learn more about Hosts files here if they’re new to you.
Back to this morning: I couldn’t recall if I setup localdev.project, project.localdev, project.local, or local.project. Only way to check is to open up the Hosts file and see what I had originally created. That’s when I thought: let’s write a quick page to read all the entries in my Hosts file. So I did, and I setup sites.local as my own, local directory of entries read directly from my Hosts file. Check it out:
I figured I’d create some categories to group the multiple entries I’ve got in my Hosts file – and add a quick flag for showing a category collapsed by default (more on this later). Here’s a modified (sample) Hosts file:
First, let’s look at a few changes to the Hosts file itself to make all this work:
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 34 35 36 37 38 39 40 41 | # Copyright (c) 1993-2006 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host 127.0.0.1 localhost #ExcludeFromSites.Local ::1 localhost #ExcludeFromSites.Local #Sites.Local.Category: New Projects Category# 127.0.0.1 project1.local 127.0.0.1 project2.dev 127.0.0.1 anotherclient.devel 127.0.0.1 project3.local #Sites.Local.Category: Other Category Here# 127.0.0.1 project4.local 127.0.0.1 project5.dev 127.0.0.1 project6.local #Sites.Local.Category: Archived/Past Por {start:collapsed}# 127.0.0.1 project7.local 127.0.0.1 project8.dev 127.0.0.1 client23.devel 127.0.0.1 project9.local 127.0.0.1 project10.local 127.0.0.1 project11.dev 127.0.0.1 clientname.devel 127.0.0.1 project12.local |
This obviously is not my Hosts file – I created a sample file for demo purposes. But either way, there are a couple things to note here – most of which will make sense as you look at the upcoming .aspx and code behind:
Now onto the code – I made comments throughout the code behind to make things easy to follow. The code could certainly be simplified, but for the sake of clarity I expanded some of it.
First, the .aspx page:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="siteslocal_default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Local Site Addresses</title> <style type="text/css"> * { font-family:Arial; font-size:13px; } body { background-color:#dfd; } div#centercontent { width: 800px; height: 500px; text-align: center; position: absolute; left: 50%; top: 50%; margin-left: -400px; margin-top: -250px; } div#header { text-align:left; font-size:23px; font-weight:bold; height:30px; } div#content { border: 1px solid #000; padding: 30px; text-align:left; height:360px; background-color:#fff; overflow-y:scroll; } div#footer { text-align:left; font-size:12px; } div.category { margin-bottom:15px; margin-left:10px; } </style> </head> <body> <form id="form1" runat="server"> <div id="centercontent"> <div id="header">Local Site Addresses</div> <div id="content"> <asp:Literal id="litHosts" runat="server"></asp:Literal> </div> <div id="footer">The contents of this page are being read directly from the hosts file in C:WindowsSystem32driversetc</div> </div> </form> </body> </html> |
And for the meat of the page, here’s the code behind written in c#:
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | using System.Web; using System.Web.UI; using System.IO; public partial class siteslocal_default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { StreamReader reader = File.OpenText(@"C:WindowsSystem32driversetchosts"); string line = string.Empty; while ((line = reader.ReadLine()) != null) { if (!line.StartsWith("# ") && !line.StartsWith(" ") && line != "#" && line != "" && !line.EndsWith("#ExcludeFromSites.Local")) { bool show_collapsed = false; string showhidecode = string.Empty; if (line.StartsWith("#")) { /******* Append the HTML for the start of a new category *******/ if (litHosts.Text != "") litHosts.Text += ""; //only append if we're not on the first category if (line.Contains("{start:collapsed}")) show_collapsed = true; string catText = line; catText = catText.Replace(" ", "").Replace("t", ""); //remove spaces and tabs catText = catText.Replace("{start:collapsed}", ""); //remove from display catText = catText.Replace("Sites.Local.Category:", ""); //remove category prefix catText = catText.Replace("#", ""); //remove any stray #'s //prep the javascript onclick code and div id string id = catText.ToLower(); string js = string.Format("document.getElementById('{0}').style.display",id); showhidecode = _ @"onclick=""if({0}=='block'){{0}='none';}else{{0}='block';}""".Replace("{0}", js); //append the <div> for this category's name litHosts.Text += string.Format(@"<div style=""cursor:pointer;"" {0}> _ <b>{1}</b></div>", showhidecode, catText); //append the <div> opening tag for this category litHosts.Text += string.Format(@"<div id=""{0}"" class=""category"" _ style=""display:{1};"">", id, show_collapsed ? "none" : "block"); } else { /******* Append the HTML for each item in the current category *******/ //first let's isolate the host name (assumes we don't need to display the IPs) //so...let's the find the index of the first space or tab string url = string.Empty; line = line.Trim(); int spacer_location = 0; spacer_location = line.IndexOf(' '); if (spacer_location == -1) spacer_location = line.IndexOf("t"); //display from the spacer to the end url = line.Substring(spacer_location, line.Length - spacer_location); url = url.Replace(" ", "").Replace("t", ""); //remove any spaces or tabs //write out this line litHosts.Text += string.Format(@"<a href=""http://{0}/"">{0}</a><br />", url); } } } litHosts.Text += ""; //close out the last div reader.Dispose(); } } |
The only other things to note are:
- I setup this page to run at http://sites.local and site this as my home page in Chrome – in IIS I changed the anonymous user to my own username (to ensure the code has permission to access the file)
- Be sure to not to start an entry in your Hosts file with a Tab – the code allows preceeding spaced, but would need to be changed to allow preceeding Tabs
That’s it, hope you find this useful and a bit of a timesaver too. Only took about 30 minutes to pull together, and I think you ought to be able to set it up in less time than that.
Happy coding!
Tags: Code
11/8/09: I’m happy, kind of.
Posted November 8th, 2009 at 2:12 am by Matt Chepeleff
I’m pretty happy right now. The House just passed the epic healthcare reform bill and kept public funding of abortions out of it. I’m having a stream of thoughts as I watch this unfold on TV and the web though:
People who complain about the government take over of health care and offer instead that the private sector is a better solution should keep in mind the free market approach got us into this in the first place.
I think the Republican complaint that government shouldn’t get involved is just silly. They make points including government will be wasteful, inefficient, incompetent etc. I understand the concern – but isn’t that also saying that Republicans themselves are probably about half of that problem (other half being the Democrats as we have a lovely 2 party system)? The reality here is the government is already responsible for a lot. A ton. Trillions actually. If we already must trust them with things as important as our national security then perhaps the real issue is holding our government more responsible instead of putting less on their plate. Why can’t our government be afraid of what we think like some other countries – I think that’d be great. A “truer” democracy IMHO.
Related to the last item (if you want to talk about the private sector being more efficient with health care than the public sector): We spend hundreds of billions on wars and if the private sector is so efficient then why don’t we privatize the military too? We allocate hundreds of billions here – we ought to be able to save a ton! Now, this would be a horrible idea – I think most would agree with that – because our security and our health shouldn’t make people rich – it should not be profitable. They’re both (healthcare and security) rights every American is due and should all contribute towards. (Couple notes: I know there are private contracts in Iraq etc so my insinuation that the military is entirely publicly run isn’t entirely true. Also, on the topic of wars – I find it interesting that the GOP was fine charging up the wars on our nation’s charge card – why isn’t our own health worthy of this?)
I hate the lobbies. Some of what the big lobbies get away (on both sides!) should be criminalized. Clink go the handcuffs.
I hate earmarks – it’s going to be a shame when a bunch get tagged along to this bill in the Senate. It’s baloney.
I don’t fully understand how a private system would work better anyway. Any private system will inherently put shareholders over policyholders. How is that an ideal system? Profit cannot serve as the reward, goal, or incentive. Private insurance is a simple operation: less paid in claims/for care = more profit. The system needs to put patients first. I think profits need to be taken out of the equation. Bottom line. But it should be noted that’s not what this bill is going to do. Ugh
for me.
The government doesn’t innovate! They’re going to kill us all with crappy care! We’ll let’s keep in mind the government isn’t taking over the system with this new bill. They’re offering a public option with the goal of stimulating competition in an industry enjoying massive profits. Now some say that “It is the U.S. government — via the Energy and Defense departments, the National Institutes of Health, and the Small Business Innovation Research and Small Business Technology Transfer programs [who] provide money to support research and development in government-funded national labs, at universities and in industry that are largely the driving forces for our nation’s innovation.” I believe, like with defense, innovation will continue if the government takes this over.
Is everyone aware that these private insurance companies are printing millions (tens of millions for some) in pure profit every single day! Since the companies are public – look up their quarterly tax filings. The SEC provides us with Edgar online – access to all public filings. Pick an insurance company and pull up their most recent 10-Q (quarterly filing). The drill into the Income Statement – you’re looking for Net Income (or Net Earnings, same thing). Look for the column showing the most recent quarter (3 months) – and keep in mind this figure will be in millions – so 1,000 is really a billion dollars. Now Net Income or Net Earnings are what the company has left over after they paid their bills, interest on debt, employees etc. This is profit – pure and simple. 1 billion per quarter is 11 million a day – in profit, not revenue! Are you really okay with that kind of money going to top investors and to top management (through performance based bonuses) because of your health? I’d bet these profits go more towards the top 2% of earners than the lower class – so who is really cashing in on care here? Um…not me so that’s a problem
This bill isn’t anti-Freedom. You know what’s anti-Freedom? Being a diabetic who can’t get insurance because I was born with a pre-existing condition. Or how about a family who is left without insurance because the mother/father/both lost jobs. Fox News and CSPAN callers fail here.
People love Medicare and Medicaid – whenever you talk about touching those people freak out. I especially hate the commercials with old people complaining that we need to protect the greatest generation’s right to healthcare during retirement. I don’t think we’re kicking you to the curb here – so first off calm down. Secondly, I am paying for your retirement already – keep in mind I am paying Social Security and might not ever see that money out the other end. Also – your generation has charged enough to my generation’s charge card – so zip it and let us in on what you’re fighting so hard to protect. If I had a grandparent alive I might put this more gently – but I don’t and am assuming they understand what I’m saying (plus they have to forgive me in heaven right?)
I like the approach businesses take when making big decisions – you first look for best practices in the market – it’s always a great reference or starting point. It would seem that countries like Italy, the Netherlands, France, the UK etc. would be great places to start. These countries rank towards the top of all the lists – and more importantly they always beat out the US system. It should be noted the most common system in these top countries is a form of nationalized coverage. But again this bill doesn’t even take us that far – so chillax folks.
Cost, cost, cost! We’re spending too much! I understand the concern. But rather than harp on the 1 trillion dollar price tag – let’s keep in mind 2 other really important points. The CBO says (a) health care costs are rising fast and consuming a larger and larger portion of the federal budget and (b) this bill “would yield a net reduction in federal budget deficits of $109 billion over the 2010-2019 period.”
I hate US media. My stance is – you’re always going to remove other’s bias if you go right to the CBO, independent polls, (some) non-US news sources etc. to get your info. It honestly concerns me that people who get their news solely from Bill O’Reilly or Keith Olbermann have the same weight in their vote as some of the rest of us.
That’s all – I’m tired and going to bed. Apologies for typos or grammar errors – it is past 1am right now.
Tags: Healthcare, Politics
