0

获取此代码(模块化 Sinatra 应用程序)

#application_controller.rb

require 'sinatra/base'
require "sinatra/activerecord"
require 'bcrypt'
require "sysrandom/securerandom"
include BCrypt
enable :sessions

    class Application < Sinatra::Base
    
        post '/identifier' do 
            user = User.find_by_email(params['email'])
            if user && user.authenticate(params[:password]) 
                session[:babyami] = user.prenom 
                puts "after identification: #{session[:babyami]}"
                redirect to '/'
            else
                erb :'principal/identifier' 
            end
        end


        get '/' do
            puts "at root: #{session[:babyami]}"
        end

在控制台中,使用 shotgun 时:

== Shotgun/Thin on http://127.0.0.1:9393/
D, [2020-12-02T08:48:30.672731 #8992] DEBUG -- :   User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "moshe.man@gmail.com"], ["LIMIT", 1]]
after identification : Moshe
127.0.0.1 - - [02/Dec/2020:08:48:30 +0100] "POST /identifier HTTP/1.1" 303 - 0.2239
at root: 
127.0.0.1 - - [02/Dec/2020:08:48:31 +0100] "GET / HTTP/1.1" 200 - 0.0045

但是当我使用机架时有效!

$ rackup
2020-12-02 08:51:46 +0100 Thin web server (v1.8.0 codename Possessed Pickle)

D, [2020-12-02T08:52:11.639346 #9165] DEBUG -- :   User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "moshe.man@gmail.com"], ["LIMIT", 1]]
after identification : Moshe
::1 - - [02/Dec/2020:08:52:11 +0100] "POST /identifier HTTP/1.1" 303 - 0.2519
at root: Moshe
::1 - - [02/Dec/2020:08:52:11 +0100] "GET / HTTP/1.1" 200 - 0.0009

启动机架时会话正常运行但使用shot弹枪时不能正常运行的任何原因?如何使用霰弹枪正确启用会话?

4

1 回答 1

0

愿这有用。

shutgun 在每次请求时都会重新启动服务器,因此会话会丢失。解决方法是设置 en ENV 变量,如下所述:sinatra docs(不要忘记重新启动)

并在应用程序中要求该秘密,如下所示:

enable :sessions
set :session_secret, ENV.fetch('SESSION_SECRET') { SecureRandom.hex(64) }

也见这里:谷歌讨论

于 2020-12-02T08:27:38.293 回答