예제 #1
0
        public SimpleApiPage(Color elementColor, string title, string handle)
        {
            if (WebApiHandleStore.Handles.Any(h => h.HandleId.Equals(handle)))
            {
                ILogManager.Log($"API Handler with the same name [{handle}] already exists. The page will not be registered.", this.ToString(), LogType.Error);
                return;
            }

            if (HttpHandleStore.Handles.Any(h => h.Value.Path.Equals(handle)))
            {
                ILogManager.Log($"Http handle with the same name [{handle}] already exists. The page will not be registered.", this.ToString(), LogType.Error);
                return;
            }


            this.Title        = title;
            this.Handle       = handle;
            this.ElementColor = elementColor;
            this.Handler      = new ApiHandleHolder(handle);
            WebApiHandleStore.Add(this.Handler);
            // compile page
            this.Html = SimpleApiPageSplicerConstant.Page.Replace(SimpleApiPageSplicerConstant.ReplaceInTitle, title);
            this.Html = this.Html.Replace(SimpleApiPageSplicerConstant.ReplaceInColor, GetColor());
            this.Html = this.Html.Replace(SimpleApiPageSplicerConstant.ReplaceInPort, IConfigurationStore.GetByType <RequiemConfig>().ApiPort.ToString());
            this.Html = this.Html.Replace(SimpleApiPageSplicerConstant.ReplaceInHandle, handle);
            using (var ms = new MemoryStream())
            {
                using var headers = new HttpResponseHeaders(this.Html.Length, ResponseStatusCode.Ok200, new IResponseField[] {
                    new FieldAcceptRanges(HttpConstant.AcceptRanges),
                    new FieldServer(HttpConstant.ServerName),
                    new FieldContentType("text/html")
                }, "HTTP/1.1");
                var h = headers.Compile();
                ms.Write(h.Item1, 0, h.Item2);
                ms.Write(Encoding.UTF8.GetBytes(this.Html));
                this.HtmlBytes = ms.ToArray();
            }
            var webHandle = new SpecialHandler(handle, async(client) =>
            {
                await client.SafeWriteAsync(this.HtmlBytes);
            });

            HttpHandleStore.AddHandler(webHandle);
        }
예제 #2
0
        public ApiGraphPage(string handle, string title, ApiGraph[] graphs, byte width)
        {
            this.ApiHandle = handle;
            this.Title     = title;
            this.Graphs    = graphs;

            var graphCodeStr             = new List <string>();
            var graphCodeHandles         = new List <string>();
            var declareVariablesInScript = new List <string>();

            foreach (var graph in graphs)
            {
                graphCodeStr.Add(graph.Code);
                graphCodeHandles.Add(graph.GraphCtx);
                declareVariablesInScript.Add(graph.Variable);
            }

            var script = GraphPageSplicer.SpliceMainScript(
                IConfigurationStore.GetByType <RequiemConfig>().ApiPort,
                handle,
                declareVariablesInScript.ToArray(),
                graphCodeStr.ToArray(),
                graphCodeHandles.ToArray());

            this.Code    = GraphPageSplicer.SpliceFinalHtml(script, title, graphCodeHandles.ToArray(), width);
            using var ms = new MemoryStream();

            using var headers = new HttpResponseHeaders(Code.Length, ResponseStatusCode.Ok200, new IResponseField[]
            {
                new FieldAcceptRanges("bytes"),
                new FieldContentType("text/html"),
                new FieldServer(HttpConstant.ServerName)
            }, "HTTP/1.1");
            this.Header = headers.Compile();
            ms.Write(Header.Item1, 0, Header.Item2);
            ms.Write(Encoding.UTF8.GetBytes(this.Code));
            this.Stream = ms.ToArray();
            var httpHandle = new SpecialHandler(handle, async(ctx) => await ctx.SafeWriteAsync(Stream));

            HttpHandleStore.AddHandler(httpHandle);
            this.Api = new ApiHandleHolder(handle);
            WebApiHandleStore.Add(this.Api);
        }
 public SimplePageHandler(string name, TableSite site)
 {
     this.Site   = site;
     this.Handle = new SpecialHandler(name, async(holder) =>
     {
         var document      = Encoding.UTF8.GetBytes(Site.GetLatest());
         using var headers = new HttpResponseHeaders(document.Length, ResponseStatusCode.Ok200, new IResponseField[]
         {
             new FieldServer(HttpConstant.ServerName),
             new FieldAcceptRanges(HttpConstant.AcceptRanges),
             new FieldContentType("text/html")
         }, "HTTP/1.1");
         var h = headers.Compile();
         if (!await holder.SafeWriteAsync(h.Item1, h.Item2))
         {
             return;
         }
         await holder.SafeWriteAsync(document);
     });
     HttpHandleStore.AddHandler(this.Handle);
 }
예제 #4
0
        public ApiConsolePage(Color boxColor, string title, string handle, string placeholder)
        {
            this.Handle      = handle;
            this.Title       = title;
            this.Placeholder = placeholder;
            this.BoxColor    = boxColor;

            this.Html = ApiConsolePageSplicer.Html.Replace(ApiConsolePageSplicer.ReplaceInTitle, title);
            this.Html = this.Html.Replace(ApiConsolePageSplicer.ReplaceInColor, GetColor());
            this.Html = this.Html.Replace(ApiConsolePageSplicer.ReplaceInPort, IConfigurationStore.GetByType <RequiemConfig>().ApiPort.ToString());
            this.Html = this.Html.Replace(ApiConsolePageSplicer.ReplaceInHandle, handle);
            this.Html = this.Html.Replace(ApiConsolePageSplicer.ReplaceInMainPlaceholder, placeholder);

            this.Handler = new ApiHandleHolder(handle);
            WebApiHandleStore.Add(this.Handler);

            using (var ms = new MemoryStream())
            {
                using var headers = new HttpResponseHeaders(this.Html.Length, ResponseStatusCode.Ok200, new IResponseField[]
                {
                    new FieldAcceptRanges(HttpConstant.AcceptRanges),
                    new FieldServer(HttpConstant.ServerName),
                    new FieldContentType("text/html")
                }, "HTTP/1.1");
                var h = headers.Compile();
                ms.Write(h.Item1, 0, h.Item2);
                ms.Write(Encoding.UTF8.GetBytes(this.Html));
                this.HtmlBytes = ms.ToArray();
            }
            var webHandle = new SpecialHandler(handle, async(client) =>
            {
                await client.SafeWriteAsync(this.HtmlBytes);
            });

            HttpHandleStore.AddHandler(webHandle);
        }