예제 #1
0
        public override void InitializeCommandHandler(Dictionary <string, Handler> _handler)
        {
            _handler.Add("getroutes",
                         (ctx) =>
            {
                List <object> o = new List <object>();
                foreach (var rr in _routing)
                {
                    o.Add(new { URL = rr.Value.URL, Description = rr.Value.ToString() });
                }
                OutputJsonData(ctx, o);
            });

            _handler.Add("getviews",
                         (ctx) =>
            {
                List <object> o = new List <object>();
                foreach (var v in _rdb.GetViews())
                {
                    o.Add(new { Name = v.Name, Description = v.Description, BackgroundIndexing = v.BackgroundIndexing, Version = v.Version, isPrimaryList = v.isPrimaryList });
                }
                OutputJsonData(ctx, o);
            });

            _handler.Add("getschema",
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                if (qry == "")
                {
                    WriteResponse(ctx, 404, "GetSchema requires a viewname to be defined e.g. ?view=customerview");
                }
                else
                {
                    List <object> o = new List <object>();
                    string view     = qry.Split('=')[1];
                    var sc          = _rdb.GetSchema(view);
                    foreach (var i in sc.Columns)
                    {
                        o.Add(new { ColumnName = i.Key, Type = i.Value.Name });
                    }
                    OutputJsonData(ctx, o);
                }
            });

            _handler.Add("systeminfo",
                         (ctx) =>
            {
                var oo = GetInfo();
                var s  = fastJSON.JSON.ToJSON(oo, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, EnableAnonymousTypes = true
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("action",
                         (ctx) =>
            {
                string action = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                switch (action)
                {
                case "backup":
                    _rdb.Backup();
                    WriteResponse(ctx, 200, "\"Done\"");
                    break;

                case "compact":
                    _rdb.GetKVHF().CompactStorageHF();
                    WriteResponse(ctx, 200, "\"Done\"");
                    break;

                case "getconfigs":
                    WriteResponse(ctx, 200, File.ReadAllText(_path + "raptordb.config"));
                    break;
                }
            });

            _handler.Add("docget", // takes : guid
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                var g      = Guid.Parse(qry);
                _log.Debug("docid = " + qry);
                var s = fastJSON.JSON.ToNiceJSON(_rdb.Fetch(g), new fastJSON.JSONParameters {
                    UseExtensions = true, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("dochistory", // takes : guid
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                var g      = Guid.Parse(qry);
                var h      = _rdb.FetchHistoryInfo(g);
                _log.Debug("docid = " + qry);
                var s = fastJSON.JSON.ToJSON(h, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("docversion", // takes : version
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                var v      = int.Parse(qry);
                var oo     = _rdb.FetchVersion(v);
                var s      = fastJSON.JSON.ToNiceJSON(oo, new fastJSON.JSONParameters {
                    UseExtensions = true, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("fileget", // takes : guid
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                var g      = Guid.Parse(qry);
                _log.Debug("fileid = " + qry);
                var s = fastJSON.JSON.ToNiceJSON(_rdb.FetchBytes(g), new fastJSON.JSONParameters {
                    UseExtensions = true, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("filehistory", // takes : guid
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                var g      = Guid.Parse(qry);
                var h      = _rdb.FetchBytesHistoryInfo(g);
                _log.Debug("fileid = " + qry);
                var s = fastJSON.JSON.ToJSON(h, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("fileversion", // takes : version
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                var v      = int.Parse(qry);
                var oo     = _rdb.FetchBytesVersion(v);
                var s      = fastJSON.JSON.ToNiceJSON(oo, new fastJSON.JSONParameters {
                    UseExtensions = true, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("docsearch", // takes : string & count =x &start=y
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                int start  = 0;
                int count  = -1;

                var m = _start_regex.Match(qry);
                if (m.Success)
                {
                    start = int.Parse(m.Groups["start"].Value);
                    qry   = qry.Replace(m.Value, "");
                }
                m = _count_regex.Match(qry);
                if (m.Success)
                {
                    count = int.Parse(m.Groups["count"].Value);
                    qry   = qry.Replace(m.Value, "");
                }
                var h           = _rdb.FullTextSearch(qry);
                List <int> list = new List <int>();
                _log.Debug("search = " + qry);
                if (count > -1 && h.Length > 0)
                {
                    int c = h.Length;
                    for (int i = start; i < start + count && i < c; i++)
                    {
                        list.Add(h[i]);
                    }
                }
                var obj = new
                {
                    Items      = list,
                    Count      = count,
                    TotalCount = h.Length
                };
                var s = fastJSON.JSON.ToJSON(obj, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false, EnableAnonymousTypes = true
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("hfkeys", // takes : count =x &start=y
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                int start  = 0;
                int count  = -1;

                var m = _start_regex.Match(qry);
                if (m.Success)
                {
                    start = int.Parse(m.Groups["start"].Value);
                    qry   = qry.Replace(m.Value, "");
                }
                m = _count_regex.Match(qry);
                if (m.Success)
                {
                    count = int.Parse(m.Groups["count"].Value);
                    qry   = qry.Replace(m.Value, "");
                }
                var h = _rdb.GetKVHF().GetKeysHF();
                List <string> list = new List <string>();
                if (count > -1 && h.Length > 0)
                {
                    int c = h.Length;
                    for (int i = start; i < start + count && i < c; i++)
                    {
                        list.Add(h[i]);
                    }
                }
                var obj = new
                {
                    Items      = list,
                    Count      = count,
                    TotalCount = h.Length
                };
                var s = fastJSON.JSON.ToJSON(obj, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false, EnableAnonymousTypes = true
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("hfget", // takes : string
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                var h      = _rdb.GetKVHF().GetObjectHF(qry);
                var s      = fastJSON.JSON.ToNiceJSON(h, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false, EnableAnonymousTypes = true
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("viewinfo", // takes : viewname
                         (ctx) =>
            {
                string qry = ctx.Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
                if (qry == "")
                {
                    WriteResponse(ctx, 404, "ViewInfo requires a viewname to be defined e.g. ?customerview");
                }
                else
                {
                    var vi = GetViewInfo(qry);
                    if (vi == "")
                    {
                        WriteResponse(ctx, 500, "View not found.");
                    }
                    else
                    {
                        ctx.Response.ContentType = "application/json";
                        WriteResponse(ctx, 200, vi);
                    }
                }
            });

            _handler.Add("excelexport",
                         (ctx) =>
            {
                string path = ctx.Request.Url.GetComponents(UriComponents.Path, UriFormat.Unescaped).ToLower();

                var data = DoQuery(_rdb, ctx, path.Replace("raptordb/excelexport/", ""), null);
                ctx.Response.AddHeader("content-disposition", "attachment;filename='" + data.Title + ".csv'");
                ctx.Response.AddHeader("Content-Type", "application/vnd.ms-excel");
                _log.Debug("exporting to excel rows : " + data.Rows.Count);
                WriteResponse(ctx, 200, WriteCsv(data.Rows), true);
            });

            _handler.Add("views",
                         (ctx) =>
            {
                string path = ctx.Request.Url.GetComponents(UriComponents.Path, UriFormat.Unescaped).ToLower();
                ProcessGET(_rdb, ctx, path.Replace("raptordb/views/", ""), null);
            });
        }
예제 #2
0
        private void InitializeCommandHandler()
        {
            _handler.Add("getroutes",
                         (ctx, args, o) =>
            {
                foreach (var rr in _routing)
                {
                    o.Add(new { URL = rr.Value.URL, Description = rr.Value.ToString() });
                }
            });

            _handler.Add("getviews",
                         (ctx, args, o) =>
            {
                foreach (var v in _rdb.GetViews())
                {
                    o.Add(new { Name = v.Name, Description = v.Description, BackgroundIndexing = v.BackgroundIndexing, Version = v.Version, isPrimaryList = v.isPrimaryList });
                }
            });

            _handler.Add("getschema",
                         (ctx, qry, o) =>
            {
                if (qry == "")
                {
                    WriteResponse(ctx, 404, "GetSchema requires a viewname to be defined e.g. ?view=customerview");
                }
                else
                {
                    string view = qry.Split('=')[1];
                    var sc      = _rdb.GetSchema(view);
                    foreach (var i in sc.Columns)
                    {
                        o.Add(new { ColumnName = i.Key, Type = i.Value.Name });
                    }
                }
            });

            _handler.Add("systeminfo",
                         (ctx, args, o) =>
            {
                var oo = GetInfo();
                var s  = fastJSON.JSON.ToJSON(oo, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, EnableAnonymousTypes = true
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("action",
                         (ctx, action, o) =>
            {
                switch (action)
                {
                case "backup":
                    _rdb.Backup();
                    WriteResponse(ctx, 200, "\"Done\"");
                    break;

                case "compact":
                    _rdb.GetKVHF().CompactStorageHF();
                    WriteResponse(ctx, 200, "\"Done\"");
                    break;

                case "getconfigs":
                    WriteResponse(ctx, 200, File.ReadAllText(_path + "raptordb.config"));
                    break;
                }
            });

            _handler.Add("docget", // takes : guid
                         (ctx, qry, o) =>
            {
                var g = Guid.Parse(qry);
                _log.Debug("docid = " + qry);
                var s = fastJSON.JSON.ToNiceJSON(_rdb.Fetch(g), new fastJSON.JSONParameters {
                    UseExtensions = true, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("dochistory", // takes : guid
                         (ctx, qry, o) =>
            {
                var g = Guid.Parse(qry);
                var h = _rdb.FetchHistoryInfo(g);
                _log.Debug("docid = " + qry);
                var s = fastJSON.JSON.ToJSON(h, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("docversion", // takes : version
                         (ctx, qry, o) =>
            {
                var v  = int.Parse(qry);
                var oo = _rdb.FetchVersion(v);
                var s  = fastJSON.JSON.ToNiceJSON(oo, new fastJSON.JSONParameters {
                    UseExtensions = true, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("fileget", // takes : guid
                         (ctx, qry, o) =>
            {
                var g = Guid.Parse(qry);
                _log.Debug("fileid = " + qry);
                var s = fastJSON.JSON.ToNiceJSON(_rdb.FetchBytes(g), new fastJSON.JSONParameters {
                    UseExtensions = true, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("filehistory", // takes : guid
                         (ctx, qry, o) =>
            {
                var g = Guid.Parse(qry);
                var h = _rdb.FetchBytesHistoryInfo(g);
                _log.Debug("fileid = " + qry);
                var s = fastJSON.JSON.ToJSON(h, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("fileversion", // takes : version
                         (ctx, qry, o) =>
            {
                var v  = int.Parse(qry);
                var oo = _rdb.FetchBytesVersion(v);
                var s  = fastJSON.JSON.ToNiceJSON(oo, new fastJSON.JSONParameters {
                    UseExtensions = true, UseFastGuid = false, UseEscapedUnicode = false
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("docsearch", // takes : string & count =x &start=y
                         (ctx, qry, o) =>
            {
                int start = 0;
                int count = -1;

                var m = _start_regex.Match(qry);
                if (m.Success)
                {
                    start = int.Parse(m.Groups["start"].Value);
                    qry   = qry.Replace(m.Value, "");
                }
                m = _count_regex.Match(qry);
                if (m.Success)
                {
                    count = int.Parse(m.Groups["count"].Value);
                    qry   = qry.Replace(m.Value, "");
                }
                var h           = _rdb.FullTextSearch(qry);
                List <int> list = new List <int>();
                _log.Debug("search = " + qry);
                if (count > -1 && h.Length > 0)
                {
                    int c = list.Count;
                    for (int i = start; i < start + count; i++)
                    {
                        list.Add(h[i]);
                    }
                }
                var obj = new
                {
                    Items      = list,
                    Count      = count,
                    TotalCount = h.Length
                };
                var s = fastJSON.JSON.ToJSON(obj, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false, EnableAnonymousTypes = true
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("hfkeys", // takes : count =x &start=y
                         (ctx, qry, o) =>
            {
                int start = 0;
                int count = -1;

                var m = _start_regex.Match(qry);
                if (m.Success)
                {
                    start = int.Parse(m.Groups["start"].Value);
                    qry   = qry.Replace(m.Value, "");
                }
                m = _count_regex.Match(qry);
                if (m.Success)
                {
                    count = int.Parse(m.Groups["count"].Value);
                    qry   = qry.Replace(m.Value, "");
                }
                var h = _rdb.GetKVHF().GetKeysHF();
                List <string> list = new List <string>();
                if (count > -1 && h.Length > 0)
                {
                    for (int i = start; i < start + count; i++)
                    {
                        list.Add(h[i]);
                    }
                }
                var obj = new
                {
                    Items      = list,
                    Count      = count,
                    TotalCount = h.Length
                };
                var s = fastJSON.JSON.ToJSON(obj, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false, EnableAnonymousTypes = true
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("hfget", // takes : string
                         (ctx, qry, o) =>
            {
                var h = _rdb.GetKVHF().GetObjectHF(qry);
                var s = fastJSON.JSON.ToNiceJSON(h, new fastJSON.JSONParameters {
                    UseExtensions = false, UseFastGuid = false, UseEscapedUnicode = false, EnableAnonymousTypes = true
                });
                ctx.Response.ContentType = "application/json";
                WriteResponse(ctx, 200, s);
            });

            _handler.Add("viewinfo", // takes : viewname
                         (ctx, qry, o) =>
            {
                if (qry == "")
                {
                    WriteResponse(ctx, 404, "ViewInfo requires a viewname to be defined e.g. ?customerview");
                }
                else
                {
                    var vi = GetViewInfo(qry);
                    if (vi == "")
                    {
                        WriteResponse(ctx, 500, "View not found.");
                    }
                    else
                    {
                        ctx.Response.ContentType = "application/json";
                        WriteResponse(ctx, 200, vi);
                    }
                }
            });
        }