Skip to content

Instantly share code, notes, and snippets.

@jimoneil
Created August 30, 2012 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimoneil/3522269 to your computer and use it in GitHub Desktop.
Save jimoneil/3522269 to your computer and use it in GitHub Desktop.
/*
Copyright (c) Microsoft
All rights reserved.
Licensed under the Microsoft Limited Public License (the “License”); you may not
use this file except in compliance with the License.
You may obtain a copy of the License at http://msdn.microsoft.com/en-us/cc300389.
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS
OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
win
See the Microsoft Limited Public License (Ms-LPL) for specific language governing
permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace TileServiceRole.Controllers
{
public class TilesController : ApiController
{
// /api/tiles/<tilename>
public HttpResponseMessage Get(String id)
{
// set cloud storage credentials
var client = new Microsoft.WindowsAzure.StorageClient.CloudBlobClient(
"http://AZURE_ACCOUNT_NAME.blob.core.windows.net",
new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(
"AZURE_ACCOUNT_NAME",
"AZURE_ACCOUNT_KEY"
)
);
// create HTTP response
var response = new HttpResponseMessage();
try
{
// get XML template from storage
var xml = client.GetBlobReference(id.Replace("_", "/")).DownloadText();
// format response
response.StatusCode = System.Net.HttpStatusCode.OK;
response.Content = new StringContent(xml);
response.Content.Headers.ContentType =
new System.Net.Http.Headers.MediaTypeHeaderValue("text/xml");
// set expires header to invalidate tile when content is obsolete
response.Content.Headers.Add("X-WNS-Expires", GetExpiryTime().ToString("R"));
}
catch (Exception e)
{
// send a 400 if there's a problem
response.StatusCode = System.Net.HttpStatusCode.BadRequest;
response.Content = new StringContent(e.Message + "\r\n" + e.StackTrace);
response.Content.Headers.ContentType =
new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");
}
// return response
return response;
}
private DateTime GetExpiryTime()
{
Int32 TimeZoneOffset = -4; // EDT offset from UTC
// get representation of local time for restaurant
var requestLocalTime = DateTime.UtcNow.AddHours(TimeZoneOffset);
// if request is hitting before 9 a.m., information is stale
if (requestLocalTime.Hour <= 8)
return DateTime.UtcNow.AddDays(-1);
// else, set tile to expire at midnight local time
else
{
var minutesUntilExpiry = (24 - requestLocalTime.Hour) * 60 - requestLocalTime.Minute;
return DateTime.UtcNow.AddMinutes(minutesUntilExpiry);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment