Esempio n. 1
0
        async Task LoadNew()
        {
            textEditor.MimeType = MimeType;
            string text = null;
            var    res  = await TextFileUtility.GetTextAsync(FileModel.GetContent());

            text = textEditor.Text = res.Text;
            textEditor.Encoding = res.Encoding;
            await RunFirstTimeFoldUpdate(text);

            textEditorImpl.InformLoadComplete();
        }
Esempio n. 2
0
        public async Task RenderCache(string projectName)
        {
            var entryPointName = projectName.ToLowerInvariant();
            var indexHtml      = new FileModel(_hostingEnvironment, entryPointName, "index.html");

            if (!indexHtml.IsExist)
            {
                return;
            }
            var     html           = indexHtml.GetContent();
            dynamic serverJsResult = new JObject();;

            serverJsResult.Env       = new JObject();
            serverJsResult.GlobalEnv = new JObject();
            var cacheKey = entryPointName + "_" + indexHtml.LastModifyTime.ToString("yyyMMddHHmmss");

            try
            {
                var cacheResult = _engine.Handler.Cache.RetrieveTemplate(cacheKey);
                if (cacheResult.Success)
                {
                    var itemple = cacheResult.Template.TemplatePageFactory();
                    itemple.DisableEncoding = true;
                    await _engine.RenderTemplateAsync(itemple, serverJsResult);

                    return;
                }

                string result = await _engine.CompileRenderStringAsync(cacheKey, html, serverJsResult);

                if (!cacheList.TryGetValue(entryPointName, out var oldCache))
                {
                    cacheList.TryAdd(entryPointName, cacheKey);
                }
                else
                {
                    //之前有缓存了 就清掉
                    _engine.Handler.Cache.Remove(oldCache);
                    cacheList[entryPointName] = cacheKey;
                }
            }
            catch (Exception e)
            {
                logger.Error(e.ToString());
            }
        }
Esempio n. 3
0
        public async Task <RenderResult> RenderAsync(string path, object model, dynamic viewBag, RouteValueDictionary routevalues, string area,
                                                     ViewType viewType)
        {
            var re = new RenderResult
            {
                Html   = "",
                Status = 200
            };

            if (string.IsNullOrEmpty(path) || path.Equals("/"))
            {
                return(re);
            }
            var nomarlPath = path;

            try
            {
                if (routevalues != null && routevalues.ContainsKey("url"))
                {
                    path = routevalues["url"].ToString();
                }

                //只拿第一层路径
                var entryPointName = path.Split('/').FirstOrDefault();
                if (string.IsNullOrEmpty(entryPointName))
                {
                    return(re);
                }

                entryPointName = entryPointName.ToLowerInvariant();
                var indexHtml = new FileModel(_hostingEnvironment, entryPointName, "index.html");
                if (!indexHtml.IsExist)
                {
                    return(re);
                }

                var jsonFile = new FileInfo(Path.Combine(_hostingEnvironment.WebRootPath, "appsettings.json"));
                if (jsonFile.Exists && (_appJsonLastWriteTime == null || _appJsonLastWriteTime != jsonFile.LastWriteTime))
                {
                    _appJsonLastWriteTime = jsonFile.LastWriteTime;
                    try
                    {
                        this._appsettingsJson = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, string> >(CopyHelper.ReadAllText(jsonFile.FullName));
                    }
                    catch (Exception e)
                    {
                        logger.Info(e.ToString());
                    }
                }

                var html = indexHtml.GetContent();
                re.Html = html;

                var cacheKey = entryPointName + "_" + indexHtml.LastModifyTime.ToString("yyyMMddHHmmss");

                var     jsFileContent  = new FileModel(_hostingEnvironment, entryPointName, "server.js");
                dynamic serverJsResult = null;
                if (jsFileContent.IsExist)
                {
                    var exports = engine
                                  .CommonJS()
                                  .RegisterInternalModule("server", typeof(Server))
                                  .RunMain(jsFileContent.FilePath);

                    try
                    {
                        var jsValue = exports.AsObject().Get("main").Invoke(new JsValue(nomarlPath)).ToString();
                        if (!string.IsNullOrEmpty(jsValue) && jsValue != "null" && jsValue != "undefined")
                        {
                            serverJsResult = JObject.Parse(jsValue);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error(e.ToString());
                    }
                }

                if (serverJsResult == null)
                {
                    serverJsResult = new JObject();
                }

                serverJsResult.Env = new JObject();
                if (_appsettingsJson != null)
                {
                    foreach (var jsonItem in _appsettingsJson)
                    {
                        serverJsResult.Env[jsonItem.Key] = jsonItem.Value;
                    }
                }

                try
                {
                    var cacheResult = _engine.Handler.Cache.RetrieveTemplate(cacheKey);
                    if (cacheResult.Success)
                    {
                        var itemple = cacheResult.Template.TemplatePageFactory();
                        itemple.DisableEncoding = true;
                        string result2 = await _engine.RenderTemplateAsync(itemple, serverJsResult);

                        re.Html = result2;
                        return(re);
                    }

                    string result = await _engine.CompileRenderStringAsync(cacheKey, html, serverJsResult);

                    if (!cacheList.TryGetValue(entryPointName, out var oldCache))
                    {
                        cacheList.TryAdd(entryPointName, cacheKey);
                    }
                    else
                    {
                        //之前有缓存了 就清掉
                        _engine.Handler.Cache.Remove(oldCache);
                        cacheList[entryPointName] = cacheKey;
                    }

                    re.Html = result;
                }
                catch (Exception e)
                {
                    logger.Error(e.ToString());
                }
            }
            catch (Exception e1)
            {
                //ignore
                logger.Error(e1.ToString());
            }

            return(re);
        }
Esempio n. 4
0
        public async Task <string> RenderAsync(HttpContext context)
        {
            var re        = "";
            var pathValue = context.Request.Path.Value !.ToString();

            if (string.IsNullOrEmpty(pathValue) || pathValue.Equals("/"))
            {
                return(re);
            }
            var path       = pathValue.Substring(1, pathValue.Length - 1);
            var nomarlPath = context.Request.GetDisplayUrl();

            try
            {
                //只拿第一层路径
                var entryPointName = path.Split('/').FirstOrDefault();
                if (string.IsNullOrEmpty(entryPointName))
                {
                    return(re);
                }

                entryPointName = entryPointName.ToLowerInvariant();
                var indexHtml = new FileModel(_hostingEnvironment, entryPointName, "index.html");
                if (!indexHtml.IsExist)
                {
                    return(re);
                }

                // 有 但是是文件的话
                if (_contentTypeProvider.TryGetContentType(pathValue.ToLower(), out _))
                {
                    return(re);
                }

                CheckConfigRefresh();
                CheckConfigRefresh(entryPointName);

                var html = indexHtml.GetContent();
                re = html;

                var cacheKey = entryPointName + "_" + indexHtml.LastModifyTime.ToString("yyyMMddHHmmss");

                var     jsFileContent  = new FileModel(_hostingEnvironment, entryPointName, "_server_.js");
                dynamic serverJsResult = null;
                if (jsFileContent.IsExist)
                {
                    var exports = engine
                                  .CommonJS()
                                  .RegisterInternalModule("server", typeof(PluginFactory))
                                  .RunMain(jsFileContent.FilePath);

                    try
                    {
                        var jsValue = exports.AsObject().Get("main").Invoke(new JsValue(nomarlPath)).ToString();
                        if (!string.IsNullOrEmpty(jsValue) && jsValue != "null" && jsValue != "undefined")
                        {
                            serverJsResult = JObject.Parse(jsValue);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error($"excute {ConfigHelper.ServerJsFile} fail:" + e.Message);
                    }
                }

                if (serverJsResult == null)
                {
                    serverJsResult = new JObject();
                }

                serverJsResult.GlobalEnv = new JObject();
                if (_appsettingsJson != null)
                {
                    foreach (var jsonItem in _appsettingsJson)
                    {
                        serverJsResult.GlobalEnv[jsonItem.Key] = jsonItem.Value;
                    }
                }

                serverJsResult.Env = new JObject();
                if (_currentAppsettingsJson != null)
                {
                    foreach (var jsonItem in _currentAppsettingsJson)
                    {
                        serverJsResult.Env[jsonItem.Key] = jsonItem.Value;
                    }
                }

                try
                {
                    var cacheResult = _engine.Handler.Cache.RetrieveTemplate(cacheKey);
                    if (cacheResult.Success)
                    {
                        var itemple = cacheResult.Template.TemplatePageFactory();
                        itemple.DisableEncoding = true;
                        string result2 = await _engine.RenderTemplateAsync(itemple, serverJsResult);

                        re = result2;
                        return(re);
                    }

                    string result = await _engine.CompileRenderStringAsync(cacheKey, html, serverJsResult);

                    if (!cacheList.TryGetValue(entryPointName, out var oldCache))
                    {
                        cacheList.TryAdd(entryPointName, cacheKey);
                    }
                    else
                    {
                        //之前有缓存了 就清掉
                        _engine.Handler.Cache.Remove(oldCache);
                        cacheList[entryPointName] = cacheKey;
                    }
                    re = result;
                    context.Response.Cookies.Append("spa_project", entryPointName);
                }
                catch (Exception e)
                {
                    logger.Error(e.ToString());
                }
            }
            catch (Exception e1)
            {
                //ignore
                logger.Error(e1.ToString());
            }

            return(re);
        }