public Site(IShortener shortener) { StaticConfiguration.DisableCaches = true; StaticConfiguration.DisableErrorTraces = false; Get["/"] = _ => View["index"]; Get["/api/expand-url"] = _ => { if (!Request.Query.Code.HasValue || String.IsNullOrEmpty(Request.Query.Code)) { return(HttpStatusCode.BadRequest); } String code = Request.Query.Code; if (code.Contains("/")) { code = code.Split('/').Last(); } Console.WriteLine(code); var info = shortener.Expand(code); var res = new DetailsResponse(); if (String.IsNullOrEmpty(info.Item1)) { res.Error = "No url found"; } else { res.Url = info.Item1; res.Clicks = info.Item2; } return(Response.AsJson(res)); }; Get["/{shortCode}"] = (arg) => { String realUrl = shortener.Expand(arg.shortCode).Item1; if (String.IsNullOrEmpty(realUrl)) { return(HttpStatusCode.NotFound); } return(Response.AsRedirect(realUrl)); }; Post["/api/shorten-url"] = _ => { if (!Request.Form.Url.HasValue || String.IsNullOrEmpty(Request.Form.Url)) { return(HttpStatusCode.BadRequest); } var url = HttpUtility.HtmlEncode(Request.Form.Url); return(String.Concat(SiteUrl, shortener.Shorten(url))); }; }
public Site (IShortener shortener) { StaticConfiguration.DisableCaches = true; StaticConfiguration.DisableErrorTraces = false; Get["/"] = _ => View["index"]; Get["/api/expand-url"] = _ => { if(!Request.Query.Code.HasValue || String.IsNullOrEmpty(Request.Query.Code)) return HttpStatusCode.BadRequest; String code = Request.Query.Code; if(code.Contains("/")) { code = code.Split('/').Last(); } Console.WriteLine(code); var info = shortener.Expand(code); var res = new DetailsResponse(); if(String.IsNullOrEmpty(info.Item1)) { res.Error = "No url found"; } else { res.Url = info.Item1; res.Clicks = info.Item2; } return Response.AsJson(res); }; Get["/{shortCode}"] = (arg) => { String realUrl = shortener.Expand(arg.shortCode).Item1; if(String.IsNullOrEmpty(realUrl)) return HttpStatusCode.NotFound; return Response.AsRedirect(realUrl); }; Post["/api/shorten-url"] = _ => { if(!Request.Form.Url.HasValue || String.IsNullOrEmpty(Request.Form.Url)) return HttpStatusCode.BadRequest; var url = HttpUtility.HtmlEncode(Request.Form.Url); return String.Concat(SiteUrl, shortener.Shorten(url)); }; }