Re: Web service without a server
by thomas895 (Deacon) on Mar 17, 2014 at 04:18 UTC
|
I think you need to step back and get the fundamentals down before attempting to make a web service right away. Moreover, your question is contradictory, without having a web server installed or available, it follows that it is illogical to attempt to do anything for or with it.
A web server is not a "file"(as you are likely imagining it) that can be saved like a text document or image. Surely you must have learned the fundamentals of the web server within the introductory part of whichever course you follow or book you are reading. If not, you may want to reconsider your investment.
The most I can recommend is to follow the tutorials presented for popular solutions such as Dancer or Catalyst.
Also, see How do I post a question effectively?.
~Thomas~
"Excuse me for butting in, but I'm interrupt-driven..."
| [reply] |
|
|
I think this is a typo:
"web service without need of a web service"
And what they mean is that they have to make a script send a response to a browser, without having a traditional webserver (apache and so on) installed.
| [reply] |
|
|
thank you for correcting the typo, yes I'm looking for some answers related to 'Web Service' without a need of installing ' a Traditional Web Server'
| [reply] |
|
|
Thomas, appreciate your time ! but you should know how to respond, mistakes do happen !
| [reply] |
|
|
I just wish I did! Now that you've updated your question to be more specific, I can once again recommend Dancer, Catalyst, Mojolicious(::Lite), or even HTTP::Server::Simple.
Here is an example of your desire(borrowed code from the latter):
#!/usr/bin/perl
{
package MyWebServer;
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
my %dispatch = (
'/bla' => \&resp_bla,
);
sub handle_request {
my $self = shift;
my $cgi = shift;
my $path = $cgi->path_info();
my $handler = $dispatch{$path};
if (ref($handler) eq "CODE") {
print "HTTP/1.0 200 OK\r\n";
$handler->($cgi);
} else {
print "HTTP/1.0 404 Not found\r\n";
print $cgi->header,
$cgi->start_html('Not found'),
$cgi->h1('Not found'),
$cgi->end_html;
}
}
sub resp_bla {
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
print $cgi->header("text/plain"),
(rand(10) < 5) # pick a random number and see if it is less
+ than 5...
? 1 : 0; #...and print 1 if it is, or 0 if it isn't.
}
}
# start the server on port 8080
# access me at http://localhost:8080/bla
my $pid = MyWebServer->new(8080)->background();
print "Use 'kill $pid' to stop server.\n";
Of course, you will want to do a more interesting check than a random number -- say, whether a file exists, a row is present in a table, or if it is a certain time of day.
~Thomas~
"Excuse me for butting in, but I'm interrupt-driven..."
| [reply] [d/l] |
Re: Web service without a server
by zentara (Cardinal) on Mar 17, 2014 at 09:43 UTC
|
| [reply] |
Re: Web service without a server
by Your Mother (Archbishop) on Mar 17, 2014 at 18:54 UTC
|
Pretty bare bones. Install Plack, probably save yourself time in the end if you do it via Task::Plack. Then a file named app.psgi–
#!/usr/bin/env perl
use strict;
use warnings;
use Plack::Request;
sub {
my $request = Plack::Request->new(+shift);
# If the URI starts with /success, it is one.
my $success = $request->path =~ m{\A/success\b};
[ 200,
[ "Content-Type" => "text/plain" ],
[ $success ? 1 : 0 ]
];
}
Then run it with the built in server (there are MANY available, including uWSGI)–
~>plackup
HTTP::Server::PSGI: Accepting connections at http://0:5000/
Then visit the port in your browser, probably http://localhost:5000/. Once you see that, try http://localhost:5000/success.
Google for “plack tutorial” (or psgi) to pick up more.
Your questions about configuration and deployment are BIG questions and have thousands of pages of possible answers. You should probably just try to get that example running and in the process you will likely learn enough to start answering some of the follow-up questions for yourself; or at least make your questions specific enough for someone to answer succinctly. | [reply] [d/l] [select] |
Re: Web service without a server
by locked_user sundialsvc4 (Abbot) on Mar 17, 2014 at 11:46 UTC
|
I personally feel that this response is quite harsh ... and inaccurate. “A web service” is simply a program that takes (usually) an XML-encoded input and produces an XML-encoded response, both by means of a TCP/IP socket. There are many CPAN modules that implement such web services, and the only thing which you have to do to run it “without a web service installed” is to, well, run it. You cannot tell it to bind to an HTTP port below 1024, and its URI-name will probably be localhost (unless you define a host-name of your own, e.g. with a hosts file or an equivalent Windows registry-entry, but otherwise, “voilá, a fully-functional web service, albeit not on-the-web, ready for developer testing.” To the client, it makes no difference.
It is extremely inefficient for a developer to futz around with starting and restarting actual service-processes in order to test his/her stuff. So, this is the way it’s done, instead. Instead of www.myclientsite.com:80 (for HTTP), it might be dev.myclientsite.com:8080 (defined on the developer’s own machine). After changing the code, hit control-C on the command line, up-arrow to reselect the previous command entered, and Enter to restart it. The client can’t tell, and doesn’t particularly care, where the host is.
The developer is now able to write, test, and debug exactly the same body of code that will, eventually, provide the same set of services “in production.” No changes will be required to the source-code when the software “goes live,” and software that is already “live” does not need to be disturbed by, or upon, the developer’s own machine. Offhand, I can’t think of any environment, whether for web services or for web pages, that does not provide this capability to software developers.
| |
|
|
thank you for your response And sorry for the typo
I was actually looking for some info about Web Service without a need of installing a traditional web server such as apache...etc,
| [reply] |