示例#1
0
        private async Task PerformSave(CargoEngine cargoEngine, IDictionary <string, object> environment, CancellationToken cancellationToken)
        {
            int itemsWritten = 0;

            var request = ReadJsonFromRequest(environment) as JObject;

            if (request != null)
            {
                var items = request.Properties()
                            .Select(x => new { id = x.Name, val = ((x.Value as JObject)?.Property("content")?.Value as JValue)?.Value as string })
                            .Where(x => x.id != null && x.val != null)
                            .ToDictionary(x => x.id, x => x.val);

                if (items.Count > 0)
                {
                    using (var ds = cargoEngine.CreateDataSource())
                    {
                        ds.SetById(items);
                        itemsWritten = items.Count;
                    }
                }
            }

            await WriteObject(environment, new { message = $"saved {itemsWritten} items" }, cancellationToken);
        }
示例#2
0
        public Task Invoke(IDictionary <string, object> environment)
        {
            var path = Get <string>(environment, "owin.RequestPath");

            //get the cargo engine safely
            CargoEngine cargoEngine = null;

            try { cargoEngine = _getCargoEngine(); } catch { }

            if (cargoEngine != null)
            {
                string             cargoRoutePrefix = null;
                CargoConfiguration config           = null;

                //get configuration safely
                try
                {
                    config           = cargoEngine.Configuration;
                    cargoRoutePrefix = config?.CargoRoutePrefix;
                }
                catch { }

                //if the route prefix matches handle the request
                if (!string.IsNullOrEmpty(cargoRoutePrefix) &&
                    path.StartsWith(cargoRoutePrefix))
                {
                    bool   authorized   = cargoEngine.AuthenticateRequest(environment);
                    string strippedPath = path.Substring(cargoRoutePrefix.Length);

                    var method            = Get <string>(environment, "owin.RequestMethod");
                    var scheme            = Get <string>(environment, "owin.RequestScheme");
                    var headers           = Get <IDictionary <string, string[]> >(environment, "owin.RequestHeaders");
                    var pathBase          = Get <string>(environment, "owin.RequestPathBase");
                    var queryString       = Get <string>(environment, "owin.RequestQueryString");
                    var body              = Get <Stream>(environment, "owin.RequestBody");
                    var protocol          = Get <string>(environment, "owin.RequestProtocol");
                    var cancellationToken = Get <CancellationToken>(environment, "owin.CallCancelled");

                    switch (method)
                    {
                    case "HEAD":
                    case "GET":
                        return(HandleGetAsync(cargoEngine, environment, method == "HEAD", strippedPath, cancellationToken));

                    case "POST":
                        return(HandlePostAsync(cargoEngine, environment, strippedPath, cancellationToken));

                    default:
                        return(Return405Async(environment));
                    }
                }
            }

            //if we get here the request remained unhandled
            return(_next(environment));
        }
示例#3
0
        private async Task PerformImport(CargoEngine cargoEngine, IDictionary <string, object> environment, CancellationToken cancellationToken)
        {
            var request = ReadObjectFromRequest <List <ContentItem> >(environment);

            using (var ds = cargoEngine.CreateDataSource())
            {
                ds.Set(request);
                await WriteObject(environment, new { message = "ok" }, cancellationToken);
            }
        }
示例#4
0
 private async Task HandlePostAsync(CargoEngine cargoEngine, IDictionary <string, object> environment, string strippedPath, CancellationToken cancellationToken)
 {
     if (strippedPath == "/save")
     {
         await PerformSave(cargoEngine, environment, cancellationToken);
     }
     else if (strippedPath == "/import")
     {
         await PerformImport(cargoEngine, environment, cancellationToken);
     }
     else
     {
         await Return404Async(environment);
     }
 }
示例#5
0
 private async Task HandleGetAsync(CargoEngine cargoEngine, IDictionary <string, object> environment, bool onlyHead, string strippedPath, CancellationToken cancellationToken)
 {
     if (strippedPath == "/js")
     {
         await WriteFromResource(environment, "cargo.js", "application/json", cancellationToken, TimeSpan.FromDays(10));
     }
     else if (strippedPath == "/css")
     {
         await WriteFromResource(environment, "cargo.css", "text/css", cancellationToken, TimeSpan.FromDays(10));
     }
     else if (strippedPath == "/export")
     {
         using (var ds = cargoEngine.CreateDataSource())
         {
             await WriteObject(environment, ds.GetAllContent(), cancellationToken);
         }
     }
     else
     {
         await Return404Async(environment);
     }
 }
示例#6
0
 /// <summary>
 /// Register the cargo API.
 /// </summary>
 /// <param name="app">The <see cref="IAppBuilder"/> provided by OWIN.</param>
 /// <param name="cargoEngine">The <see cref="CargoEngine"/> to register the API for.</param>
 public static IAppBuilder UseCargo(this IAppBuilder app, CargoEngine cargoEngine)
 {
     return app.Use(typeof(CargoPipeline), cargoEngine);
 }
示例#7
0
 public void ConfigureCargo(IAppBuilder app)
 {
     _cargoEngine = new MyCargoEngine();
     app.UseCargo(_cargoEngine);
 }
示例#8
0
 public CargoPipeline(Func <IDictionary <string, object>, Task> next, CargoEngine cargoEngine)
     : this(next, () => cargoEngine)
 {
 }
示例#9
0
 /// <summary>
 /// Register the cargo API.
 /// </summary>
 /// <param name="app">The <see cref="IAppBuilder"/> provided by OWIN.</param>
 /// <param name="cargoEngine">The <see cref="CargoEngine"/> to register the API for.</param>
 public static IAppBuilder UseCargo(this IAppBuilder app, CargoEngine cargoEngine)
 {
     return(app.Use(typeof(CargoPipeline), cargoEngine));
 }