public SitesModule() : base("/sites/") { Get["/create/"] = p => { ModelIncluders.IncludeCertificates(ref Model); var site = new Site(); Model.Site = site; Model.ApplicationPools = SiteManager.GetApplicationPools(); return(View["Create", Model]); }; Post["/create/"] = p => { ModelIncluders.IncludeCertificates(ref Model); ModelIncluders.IncludeApplicationPools(ref Model); var site = this.Bind <Site>(); Model.Site = site; ValidateSite(ref site); if (!HasErrors) { var result = SiteManager.CreateSite(site); switch (result.Result) { case SiteResult.NameAlreadyInUse: AddPropertyError("name", "There's already a site with that name."); break; case SiteResult.BindingAlreadyInUse: AddPropertyError("httpbindings", "The binding is already in use."); break; case SiteResult.Failed: AddGlobalError("Something went completely wrong :-/"); break; case SiteResult.Success: System.Threading.Thread.Sleep(1000); AddMessage("Site has successfully been created.", MessageType.Success); return(new RedirectResponse("/sites/" + result.IisSiteId + "/settings/")); } } return(View["Create", Model]); }; Get[@"/(?<Id>[\d]{1,4})/settings/"] = p => { ModelIncluders.IncludeCertificates(ref Model); ModelIncluders.IncludeApplicationPools(ref Model); Site site = SiteManager.GetSiteById(p.Id); Model.Site = site; Model.ApplicationPools = SiteManager.GetApplicationPools(); return(View["Settings", Model]); }; Post[@"/(?<Id>[\d]{1,4})/settings/"] = p => { ModelIncluders.IncludeCertificates(ref Model); ModelIncluders.IncludeApplicationPools(ref Model); Site site = SiteManager.GetSiteById(p.Id); site.Name = Request.Form.Name; site.SitePath = Request.Form.SitePath; site.ApplicationPool = Request.Form.ApplicationPool; ValidateSite(ref site); Model.Site = site; if (!HasErrors) { try { SiteManager.UpdateSite(site); AddMessage("Settings have been saved.", MessageType.Success); } catch (System.ArgumentException ex) { AddMessage("IIS error: " + ex.Message, MessageType.Error); } } return(View["Settings", Model]); }; Post[@"/(?<Id>[\d]{1,4})/stop/"] = p => { Site site = SiteManager.GetSiteById(p.Id); SiteManager.StopSite(site); AddMessage("Site has been stopped."); return(new RedirectResponse("/sites/" + site.IisId + "/settings/")); }; Post[@"/(?<Id>[\d]{1,4})/start/"] = p => { Site site = SiteManager.GetSiteById(p.Id); var result = SiteManager.StartSite(site); switch (result) { case SiteStartResult.BindingIsAlreadyInUse: AddMessage("Could not start the site because a binding is already in use.", MessageType.Error); break; case SiteStartResult.CannotAccessSitePath: AddMessage("Could not start the site because IIS could not obtain access to the site path. Maybe another process is using the files?", MessageType.Error); break; case SiteStartResult.Started: AddMessage("Site has been started."); break; } return(new RedirectResponse("/sites/" + site.IisId + "/settings/")); }; Post[@"/(?<Id>[\d]{1,4})/restart/"] = p => { Site site = SiteManager.GetSiteById(p.Id); SiteManager.RestartSite(site.IisId); AddMessage("Site has been restarted."); return(new RedirectResponse("/sites/" + site.IisId + "/settings/")); }; Post[@"/(?<Id>[\d]{1,4})/recycle/"] = p => { Site site = SiteManager.GetSiteById(p.Id); SiteManager.RecycleApplicationPool(site.ApplicationPool); AddMessage("Application pool has been recycled."); return(new RedirectResponse("/sites/" + site.IisId + "/settings/")); }; Post[@"/(?<Id>[\d]{1,4})/delete/"] = p => { Site site = SiteManager.GetSiteById(p.Id); SiteManager.DeleteSite(site.IisId); AddMessage("The site {0} was deleted.", site.Name); return(new RedirectResponse("/")); }; Get[@"/(?<Id>[\d]{1,4})/applications/"] = p => { ModelIncluders.IncludeApplicationPools(ref Model); Site site = SiteManager.GetSiteById(p.Id); Model.Site = site; Model.ApplicationPools = SiteManager.GetApplicationPools(); return(View["Applications", Model]); }; Post[@"/(?<Id>[\d]{1,4})/applications/"] = p => { ModelIncluders.IncludeApplicationPools(ref Model); Site site = SiteManager.GetSiteById(p.Id); string[] paths = Request.Form.Path != null?Request.Form.Path.ToString().Split(',') : null; string[] applicationPools = Request.Form.ApplicationPool.ToString().Split(','); string[] diskPaths = Request.Form.DiskPath.ToString().Split(','); site.Applications.Clear(); if (paths != null) { for (int i = 0; i < paths.Length; i++) { site.Applications.Add(new SiteApplication { ApplicationPool = applicationPools[i], DiskPath = diskPaths[i], Path = paths[i] }); } ValidateSiteApplications(site); } if (!HasErrors) { SiteManager.UpdateSite(site); AddMessage("Applications have been saved.", MessageType.Success); } Model.Site = site; Model.ApplicationPools = SiteManager.GetApplicationPools(); return(View["Applications", Model]); }; Get[@"/(?<Id>[\d]{1,4})/errors/"] = p => { StatsRange range; var rValue = Request.Query["r"]; if (rValue == null) { range = StatsRange.Last24Hours; } else { Enum.TryParse(rValue, true, out range); // Defaults "Last24hours" by position } Model.Range = range; Site site = SiteManager.GetSiteById(p.Id); var hasAnyErrors = true; var sw = new Stopwatch(); sw.Start(); var errors = EventLogHelper.GetBySite(site.IisId, range).ToList(); sw.Stop(); Model.QueryTime = sw.ElapsedMilliseconds; Model.HasAnyErrors = hasAnyErrors; Model.Site = site; Model.Exceptions = errors.ToList(); return(View["Errors", Model]); }; Get[@"/(?<Id>[\d]{1,4})/errors/(?<EventLogId>[\d]{1,7})/"] = p => { Site site = SiteManager.GetSiteById(p.Id); ApplicationError exception = EventLogHelper.GetById(p.EventLogId); Model.Site = site; Model.Exception = exception; return(View["Error", Model]); }; }