Пример #1
0
        static async Task StartWatcher(IApplicationHost host, Boolean bAdmin)
        {
            if (_redirectLoaded)
            {
                return;
            }
            String redFilePath = host.MakeFullPath(bAdmin, String.Empty, "redirect.json");

            if (System.IO.File.Exists(redFilePath))
            {
                String json = await host.ReadTextFile(bAdmin, String.Empty, "redirect.json");

                _redirect = JsonConvert.DeserializeObject <Dictionary <String, String> >(json);
            }
            if (host.IsDebugConfiguration && _redirectWatcher == null)
            {
                _redirectWatcher = new FileSystemWatcher(Path.GetDirectoryName(redFilePath), "*.json")
                {
                    NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.Attributes | NotifyFilters.LastAccess
                };
                _redirectWatcher.Changed += (sender, e) =>
                {
                    _redirectLoaded = false;
                };
                _redirectWatcher.EnableRaisingEvents = true;
            }
            _redirectLoaded = true;
        }
Пример #2
0
        public async Task Default(String pathInfo)
        {
            var page = pathInfo.ToLowerInvariant();

            try
            {
                if (pathInfo.ToLowerInvariant() == "appstyles")
                {
                    AppStyles();
                    return;
                }

                String path = _host.MakeFullPath(false, "_pages", $"{page}.{CurrentLang}.html");
                if (!System.IO.File.Exists(path))
                {
                    throw new IOException($"Application page not found ({page}.{CurrentLang}).");
                }
                String pageContent = System.IO.File.ReadAllText(path);
                await SendPage(_localizer.Localize(null, pageContent));
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
Пример #3
0
 public void Default(String pathInfo)
 {
     try
     {
         pathInfo = pathInfo.ToLowerInvariant();
         if (pathInfo.IndexOf('.') == -1)
         {
             pathInfo = Path.ChangeExtension(pathInfo, "html");                     // no extension -> .html
         }
         var path = _host.MakeFullPath(false, "_static/", pathInfo);
         if (!System.IO.File.Exists(path))
         {
             throw new FileNotFoundException($"File not found '{pathInfo}'");
         }
         Response.ContentType = MimeMapping.GetMimeMapping(path);
         using (var stream = System.IO.File.OpenRead(path))
         {
             stream.CopyTo(Response.OutputStream);
         }
     }
     catch (Exception ex)
     {
         Response.ContentType = "text/plain";
         if (ex.InnerException != null)
         {
             ex = ex.InnerException;
         }
         Response.Write(ex.Message);
     }
 }
Пример #4
0
        public static String AppLinks(this IApplicationHost host)
        {
            var appPath = host.MakeFullPath(false, "", "links.json");

            if (System.IO.File.Exists(appPath))
            {
                String appLinks = System.IO.File.ReadAllText(appPath);
                Object links    = JsonConvert.DeserializeObject <List <Object> >(appLinks);
                return(JsonConvert.SerializeObject(links));
            }
            return("[]");
        }
Пример #5
0
        String GetSiteMetaTags()
        {
            var host = Request.Headers["Host"];

            if (host == null)
            {
                return(String.Empty);
            }
            Int32 dotPos = host.IndexOfAny(":".ToCharArray());

            if (dotPos != -1)
            {
                host = host.Substring(0, dotPos);
            }
            host = host.Replace('.', '_').ToLowerInvariant();
            String path = _host.MakeFullPath(false, "_meta/", $"{host}.head");

            if (System.IO.File.Exists(path))
            {
                return(System.IO.File.ReadAllText(path));
            }
            return(String.Empty);
        }
Пример #6
0
        public static String AppStyleSheetsLink(this IApplicationHost host, String controllerName)
        {
            controllerName = controllerName ?? throw new ArgumentNullException(nameof(controllerName));
            // TODO _host AssestsDistionary
            var fp = host.MakeFullPath(false, "_assets", "");

            if (!Directory.Exists(fp))
            {
                return(String.Empty);
            }
            foreach (var f in Directory.EnumerateFiles(fp, "*.css"))
            {
                // at least one file
                return($"<link  href=\"/{controllerName.ToLowerInvariant()}/appstyles\" rel=\"stylesheet\" />");
            }
            return(String.Empty);
        }
Пример #7
0
        protected async Task Render(RequestView rw, TextWriter writer, ExpandoObject loadPrms)
        {
            String     loadProc = rw.LoadProcedure;
            IDataModel model    = null;

            if (rw.parameters != null && loadPrms == null)
            {
                loadPrms = rw.parameters;
            }
            if (loadPrms != null)
            {
                loadPrms.Set("Id", rw.Id);
                loadPrms.AppendAndReplace(rw.parameters);
            }
            if (loadProc != null)
            {
                ExpandoObject prms2 = loadPrms;
                if (rw.indirect)
                {
                    // for indirect - @TenantId, @UserId and @Id only
                    prms2 = new ExpandoObject();
                    prms2.Set("Id", rw.Id);
                    if (loadPrms != null)
                    {
                        prms2.Set("UserId", loadPrms.Get <Int64>("UserId"));
                        prms2.Set("TenantId", loadPrms.Get <Int32>("TenantId"));
                    }
                }
                model = await _dbContext.LoadModelAsync(rw.CurrentSource, loadProc, prms2);

                if (!String.IsNullOrEmpty(rw.Id))
                {
                    var modelId = model.FirstElementId ?? String.Empty;
                    if (rw.Id != modelId.ToString())
                    {
                        throw new RequestModelException($"Element not found. Id={rw.Id}");
                    }
                }
            }
            if (rw.indirect)
            {
                rw = await LoadIndirect(rw, model, loadPrms);
            }

            if (_userStateManager != null && model != null)
            {
                Int64 userId = loadPrms.Get <Int64>("UserId");
                if (_userStateManager.IsReadOnly(userId))
                {
                    model.SetReadOnly();
                }
            }

            String rootId      = "el" + Guid.NewGuid().ToString();
            String modelScript = null;

            String viewName = rw.GetView();

            if (viewName == NO_VIEW)
            {
                modelScript = await WriteModelScriptModel(rw, model, rootId);

                writer.Write(modelScript);
                return;
            }

            if (_renderer == null)
            {
                throw new InvalidOperationException("Service 'IRenderer' not registered");
            }

            modelScript = await WriteModelScript(rw, model, rootId);

            // TODO: use view engines
            // try xaml
            String  fileName  = rw.GetView() + ".xaml";
            String  filePath  = _host.MakeFullPath(Admin, rw.Path, fileName);
            Boolean bRendered = false;

            if (System.IO.File.Exists(filePath))
            {
                // render XAML
                if (System.IO.File.Exists(filePath))
                {
                    using (var strWriter = new StringWriter())
                    {
                        var ri = new RenderInfo()
                        {
                            RootId               = rootId,
                            FileName             = filePath,
                            FileTitle            = fileName,
                            Writer               = strWriter,
                            DataModel            = model,
                            Localizer            = _localizer,
                            CurrentLocale        = null,
                            IsDebugConfiguration = _host.IsDebugConfiguration
                        };
                        _renderer.Render(ri);
                        // write markup
                        writer.Write(strWriter.ToString());
                        bRendered = true;
                    }
                }
            }
            else
            {
                // try html
                fileName = rw.GetView() + ".html";
                filePath = _host.MakeFullPath(Admin, rw.Path, fileName);
                if (System.IO.File.Exists(filePath))
                {
                    using (_host.Profiler.CurrentRequest.Start(ProfileAction.Render, $"render: {fileName}"))
                    {
                        using (var tr = new StreamReader(filePath))
                        {
                            String htmlText = await tr.ReadToEndAsync();

                            htmlText = htmlText.Replace("$(RootId)", rootId);
                            writer.Write(htmlText);
                            bRendered = true;
                        }
                    }
                }
            }
            if (!bRendered)
            {
                throw new RequestModelException($"The view '{rw.GetView()}' was not found. The following locations were searched:\n{rw.GetRelativePath(".xaml")}\n{rw.GetRelativePath(".html")}");
            }
            writer.Write(modelScript);
        }