Torquebox: unable to work with SLIM - sinatra

How do I launch a simple Sinatra app with Torquebox and slim as a html rendering engine?
Plain html works fine... whenever I use slim the server fails:
HTTP Status 500 - org.jruby.exceptions.RaiseException: (NoMethodError) undefined method join' for #<String:0xf94f510>
I am dying to get torquebox up and running!
config.ru:
require 'rubygems'
require 'sinatra'
require File.expand_path '../hello.rb', __FILE__
run Sinatra::Application
hello.rb:
get '/' do
File.read(File.join('public', 'index.html'))
end
get '/kalli' do
slim :some_file
end

Related

How to integrate Dancer2::Core::HTTP helpers for rendering HTTP status codes?

Dancer2 is a PSGI web application framework, we can use the "plackup" tool (provided by Plack) for launching the application:
plackup -p 5000 bin/app.psgi
The web application created using Dancer2 script can be viewed on localhost and port 5000.
http://localhost:5000
We can then write a get request as follows:
get '/hello/:name' => sub {
return "Hi there " . route_parameters->get('name');
};
For GET requests to "/hello/...", the code block provided above gets executed.
If the application is down, for GET requests to "/hello/...", the following error is shown:
curl: (7) Failed connect to localhost:5000; Connection refused
How do we integrate Dancer2::Core::HTTP helpers for rendering HTTP status codes for Dancer2 so that the following status code is shown when the app is down? Similarly, there are other error handlers too, how to integrate them as well?
status_500 status_error, status_internal_server_error

Magento 2 - Custom rest api endpoint for PDF download - net::ERR_HTTP2_PROTOCOL_ERROR 200

I have created custom rest api endpoint in magento for downloading pdf file.
When testing it locally: From separate react JS project I am sending request to that endpoint and file is downloaded successfuly. http/1.1 protocol is used (checked in google chrome dev tools, network tab).
After deployment to staging servers, when I try to make request between staging servers from reactJS project to magento2 project, then upon sending request it takes 30-40 seconds without getting any response and then error is shown in console. There is not anything in the error logs. http2 protocol is used (not sure if that can be a reason for issue).
Failed to fetch - net::ERR_HTTP2_PROTOCOL_ERROR 200
Here is the piece of php code for downloading pdf file:
...
$filename = $outputFileName . time() . '.' . $extension;
$directoryTmpWrite = $this->filesystem->getDirectoryWrite(DirectoryList::TMP);
$directoryTmpWrite->writeFile($filename, $fileContent);
return $this->fileFactory->create(
$outputFileName . '.' . $extension,
[
'type' => 'filename',
'value' => $filename,
'rm' => true,
],
DirectoryList::TMP, //basedir
'application/octet-stream',
''
);
How I resolved my issue?
I had to turn on output buffering in php in magento, to ensure that no output has been sent from a script and instead of that output has been store to a buffer and then sent all together from a script when script execution is finished.
So ob_start() fixed my issue. Right before return statement.

Lua open socket error

I am using lua as module for nginx (openresty) to get files from remote host. My function:
function readfile(url)
local http = require ("socket.http")
if not http then
error("Couldn't open socket.http")
end
http.TIMEOUT = 5
local body, code = http.request(url)
if not body then
error("Couldn't read the remote file: " .. code)
end
return body
end
I have tested this code by using Siege. When I set the users more then 100 (for example), I catch this error:
2018/03/27 09:36:38 [info] 10#10: *91018 shutdown() failed (107: Socket not connected), client: 172.18.0.7, server: localhost
I have more errors when i set more users. What does it mean? Thank you for the help.
Don't use luasocket library with OpenResty. The resulting code would block on http.request().
I suppose that all nginx worker just blocked and it is the reason of these errors.
For you purpose you may use one of the libraries below:
lua-resty-http
lua-resty-http-simple
First is more fexible, allow to use secure transport.
Second has simpler API.
And both use internally nginx Lua cosocket API and are 100% nonblocking out of the box.
The lua-resty-http or lua-resty-http-simple do not work in the init_by_lua in http context.
it is fine to use it in the init context where blocking is not considered harmful.

deploying cgi to psgi converted application in apache

#!C:/perl/bin/perl.exe
use CGI;
my $q = CGI->new;
print $q->header('text/plain'),
"Hello ", $q->param('name');
#CONVERTED PSGI PAGE
#!C:/perl/bin/perl.exe
use CGI::PSGI;
my $app = sub {
my $env = shift;
my $q = CGI::PSGI->new($env);
return [
$q->psgi_header('text/plain'),
[ "Hello ", $q->param('name') ],
];
};
I run this cgi.pl in apache server as
http://localhost/cgi-bin/cgi.pl
but I cant able to run the converted psgi.pl in apache server
its displaying
please help
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at admin#example.com to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log.
CGI and PSGI are two different specifications of how a web server and an external program communicate.
Under CGI, the web server expects to receive text output from the program, consisting of the HTTP Response headers, a blank line, and the HTML rendered by the program.
The CGI module implements this logic for the apache server, and if the output from the program does not comply, apache reports the 500 error.
Under PSGI, the web server expects the program to return a three element list consisting of the HTTP response code, the HTTP Response headers, and the HTML rendered by the program.
So you can see that a program conforming to the PSGI spec would confuse the mod_cgi.
So you need to install an apache module that implements PSGI, or employ a Perl module (the CGI::PSGI docs suggest CGI::Emulate::PSGI ) that will accept your PSGI list and convert to CGI for you.

Why do perl dancer pages render slowly (while I'm not connected to internet)?

I'm running Dancer and found it slow -- pages took a long time to render.
This is the example code from Dancer::Introduction:
#!/usr/bin/perl
# make this script a webapp
use Dancer;
# declare routes/actions
get '/' => sub {
"Hello World";
};
get '/hello/:name' => sub {
"Hello ".param('name');
};
# run the webserver
Dancer->dance;
It takes my browser 10 seconds to get&render the response( using firebug in firefox ).
And Dancer message:
[20734] core #0.000228> request: GET / from 192.168.1.101 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Handler.pm l. 57
[20734] core #0.000809> [hit #44]trying to match `/' against /^\/$/ in /usr/lib/perl5/site_perl/5.8.8/Dancer/Route.pm l. 84
[20734] core #0.000953> [hit #44] --> got 1 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Route.pm l. 101
[20734] core #0.001645> [hit #44]response: 200 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Handler.pm l. 175
[20734] core #0.000135> request: GET /favicon.ico from 192.168.1.101 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Handler.pm l. 57
[20734] core #0.000873> [hit #45]response: 200 in /usr/lib/perl5/site_perl/5.8.8/Dancer/Handler.pm l. 175
Why is Dancer so slow? Did I miss something?
Is the computer connected to internet? I got the same problem when testing from a computer not connected to internet; fixed it by deleting
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>;
from main.tt
As you can see from the debugging log, it took Dancer 0.6ms to serve the request. The problem is somewhere else in the stack. A frequent culprit is reverse DNS — the webserver tries to reverse-lookup the remote IP address for access logging purposes, and if your DNS is misconfigured, that can take quite a while (sometimes 30 or 60 seconds) before it fails.
Use Dancer::Plugin::NYTProf to profile your application. From the documentation:
By simply loading this plugin, you'll have the detailed, helpful
profiling provided by Devel::NYTProf.
Each individual request to your app is profiled. Going to the URL
/nytprof in your app will present a list of profiles.
Or, if it's a browser-side issue, you can use an extension like Firebug to see what part of a page load is slow.