server.pl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/env perl
  2. # Copyright (C) 2015 Alex Schroeder <alex@gnu.org>
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. my $wiki = $ARGV[0] || 'wiki.pl';
  15. my $port = $ARGV[1] || 8080;
  16. my $dir = $ARGV[2];
  17. $ENV{WikiDataDir} = $dir if $dir;
  18. {
  19. package Oddmuse::Server;
  20. use HTTP::Server::Simple::CGI;
  21. use base qw(HTTP::Server::Simple::CGI);
  22. $OddMuse::RunCGI = 0;
  23. do $wiki; # load just once
  24. sub handle_request {
  25. my $self = shift;
  26. package OddMuse;
  27. $q = shift;
  28. # NPH, or "no-parsed-header", scripts bypass the server completely by
  29. # sending the complete HTTP header directly to the browser.
  30. $q->nph(1);
  31. DoWikiRequest();
  32. }
  33. }
  34. die <<'EOT' unless -f $wiki;
  35. Usage: perl server.pl [WIKI [PORT [DIR]]]
  36. Example: perl server.pl wiki.pl 8080 ~/src/oddmuse/test-data
  37. You may provide the Oddmuse wiki script on the command line. If you do not
  38. provide it, WIKI will default to 'wiki.pl'.
  39. You may provide a port number on the command line. If you do not provide it,
  40. PORT will default to 8080.
  41. You may provide a data directory on the command line. It will be used to set the
  42. environment variable 'WikiDataDir'. If it is not not set, Oddmuse will default
  43. to '/tmp/oddmuse'.
  44. A simple test setup would be the following shell script:
  45. #/bin/sh
  46. if test -z "$WikiDataDir"; then
  47. export WikiDataDir=/tmp/oddmuse
  48. fi
  49. mkdir -p "$WikiDataDir"
  50. echo <<EOF > "$WikiDataDir/config"
  51. $AdminPass = 'foo';
  52. $ScriptName = 'http://localhost/';
  53. EOF
  54. perl stuff/server.pl wiki.pl &
  55. SERVER=$!
  56. sleep 1
  57. w3m http://localhost:8080/
  58. kill $!
  59. This will run the server exactly once, allow you to browse the site using w3m,
  60. and when you're done, it'll kill the server for you.
  61. EOT
  62. my $server = Oddmuse::Server->new($port);
  63. # $server->background();
  64. $server->run();