示例#1
0
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            // Enable Compression with Settings
            var settings = new GzipCompressionSettings();

            settings.MinimumBytes = 1024;
            pipelines.EnableGzipCompression(settings);

            base.ApplicationStartup(container, pipelines);
        }
示例#2
0
        /// <summary>
        /// This function override the RequestStartup which is used each time a request came to Nancy
        /// </summary>
        protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines,
                                               NancyContext context)
        {
            StaticConfiguration.EnableRequestTracing = true;
            var configuration =
                new StatelessAuthenticationConfiguration(nancyContext =>
            {
                //try to take "apikey" from header
                string apiKey = nancyContext.Request.Headers["apikey"].FirstOrDefault();
                if (string.IsNullOrEmpty(apiKey))
                {
                    //take out value of "apikey" from query that was pass in request and check for User
                    apiKey = (string)nancyContext.Request.Query.apikey.Value;
                }
                if (apiKey != null)
                {
                    return(UserDatabase.GetUserFromApiKey(apiKey));
                }
                else
                {
                    return(null);
                }
            });

            StaticConfiguration.DisableErrorTraces = false;
            StatelessAuthentication.Enable(pipelines, configuration);

            pipelines.OnError += (ctx, ex) => onError(ctx, ex);

            pipelines.BeforeRequest += BeforeProcessing;
            pipelines.AfterRequest  += AfterProcessing;

            #region CORS Enable

            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>
            {
                ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
                .WithHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS")
                .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type, apikey");
            });

            #endregion

            #region Gzip compression

            GzipCompressionSettings gzipsettings = new GzipCompressionSettings
            {
                MinimumBytes = 16384 //16k
            };
            gzipsettings.MimeTypes.Add("application/xml");
            gzipsettings.MimeTypes.Add("application/json");
            pipelines.EnableGzipCompression(gzipsettings);

            #endregion
        }
        /// <inheritdoc />
        /// <summary>
        /// This function override the RequestStartup which is used each time a request came to Nancy
        /// </summary>
        protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines,
                                               NancyContext context)
        {
            StaticConfiguration.EnableRequestTracing = true;
            var configuration =
                new StatelessAuthenticationConfiguration(nancyContext =>
            {
                // If the server isn't up yet, we can't access the db for users
                if (!(ServerState.Instance?.ServerOnline ?? false))
                {
                    return(null);
                }
                // get apikey from header
                string apiKey = nancyContext.Request.Headers["apikey"].FirstOrDefault()?.Trim();
                // if not in header
                if (string.IsNullOrEmpty(apiKey))
                {
                    // try from query string instead
                    try
                    {
                        apiKey = (string)nancyContext.Request.Query.apikey.Value;
                    }
                    catch
                    {
                        // ignore
                    }
                }
                AuthTokens auth = RepoFactory.AuthTokens.GetByToken(apiKey);
                return(auth != null
                        ? RepoFactory.JMMUser.GetByID(auth.UserID)
                        : null);
            });

            StaticConfiguration.DisableErrorTraces = false;
            StatelessAuthentication.Enable(pipelines, configuration);

            pipelines.OnError += (ctx, ex) => onError(ctx, ex);

            pipelines.BeforeRequest += BeforeProcessing;
            pipelines.AfterRequest  += AfterProcessing;

            #region CORS Enable

            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>
            {
                ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
                .WithHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS")
                .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type, apikey");
            });

            #endregion

            #region Gzip compression

            GzipCompressionSettings gzipsettings = new GzipCompressionSettings
            {
                MinimumBytes = 16384 //16k
            };
            gzipsettings.MimeTypes.Add("application/xml");
            gzipsettings.MimeTypes.Add("application/json");
            pipelines.EnableGzipCompression(gzipsettings);

            #endregion
        }
        /// <summary>
        /// This function override the RequestStartup which is used each time a request came to Nancy
        /// </summary>
        protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context)
        {
            var configuration =
                new StatelessAuthenticationConfiguration(nancyContext =>
                {
                    //try to take "apikey" from header
                    string apiKey = nancyContext.Request.Headers["apikey"].FirstOrDefault();
                    if (string.IsNullOrEmpty(apiKey))
                    {
                        //take out value of "apikey" from query that was pass in request and check for User
                        apiKey = (string)nancyContext.Request.Query.apikey.Value;
                    }
                    if (apiKey != null)
                    {
                        return UserDatabase.GetUserFromApiKey(apiKey);
                    }
                    else
                    {
                        return null;
                    }
                });
            StaticConfiguration.DisableErrorTraces = false;
            StatelessAuthentication.Enable(pipelines, configuration);

            pipelines.OnError += (NancyContext ctx, Exception ex) => onError(ctx, ex);

            pipelines.BeforeRequest += (NancyContext ctx) => BeforeProcessing(ctx);
            pipelines.AfterRequest += (NancyContext ctx) => AfterProcessing(ctx);

            #region CORS Enable
            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>
            {
                ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
                                .WithHeader("Access-Control-Allow-Methods", "POST,GET")
                                .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type");

            });
            #endregion

            #region Gzip compression
            GzipCompressionSettings gzipsettings = new GzipCompressionSettings();
            gzipsettings.MinimumBytes = 16384; //16k
            gzipsettings.MimeTypes.Add("application/xml");
            gzipsettings.MimeTypes.Add("application/json");
            pipelines.EnableGzipCompression(gzipsettings);
            #endregion
        }