server.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/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. # What is this?
  15. # =============
  16. #
  17. # This is a script that will server a wiki using a web server written in Perl,
  18. # without a fancy framework like Mojolicious. Instead, it uses
  19. # HTTP::Server::Simple::CGI.
  20. #
  21. # A simple usecase would be that you have had a wiki running years ago but then
  22. # you forgot all about it and your Apache config no longer works and who knows
  23. # how the system Perl is doing. So check out the data dir and notice that the
  24. # files belong to a user called _www... And so you run the following:
  25. #
  26. # sudo -u _www perl stuff/server.pl ./wiki.pl 3000 \
  27. # /Users/alex/WebServer/Oddmuse
  28. #
  29. # Your old wiki is served on localhost:3000 for you to examine.
  30. my $wiki = $ARGV[0] || './wiki.pl';
  31. my $port = $ARGV[1] || 8080;
  32. my $dir = $ARGV[2];
  33. $ENV{WikiDataDir} = $dir if $dir;
  34. {
  35. package Oddmuse::Server;
  36. use HTTP::Server::Simple::CGI;
  37. use base qw(HTTP::Server::Simple::CGI);
  38. $OddMuse::RunCGI = 0;
  39. do $wiki; # load just once
  40. sub handle_request {
  41. my $self = shift;
  42. package OddMuse;
  43. $q = shift;
  44. # The equivalent of use CGI qw(-utf8) because it didn't work as part of
  45. # cgi_init.
  46. $CGI::PARAM_UTF8++;
  47. # NPH, or "no-parsed-header", scripts bypass the server completely by
  48. # sending the complete HTTP header directly to the browser.
  49. $q->nph(1);
  50. DoWikiRequest();
  51. }
  52. }
  53. die <<'EOT' unless -f $wiki;
  54. Usage: perl server.pl [WIKI [PORT [DIR]]]
  55. Example: perl server.pl ./wiki.pl 8080 ~/src/oddmuse/test-data
  56. You may provide the Oddmuse wiki script on the command line. If you do not
  57. provide it, WIKI will default to './wiki.pl'.
  58. You may provide a port number on the command line. If you do not provide it,
  59. PORT will default to 8080.
  60. You may provide a data directory on the command line. It will be used to set the
  61. environment variable 'WikiDataDir'. If it is not not set, Oddmuse will default
  62. to '/tmp/oddmuse'.
  63. A simple test setup would be the following shell script:
  64. #/bin/sh
  65. if test -z "$WikiDataDir"; then
  66. export WikiDataDir=/tmp/oddmuse
  67. fi
  68. mkdir -p "$WikiDataDir"
  69. echo <<EOF > "$WikiDataDir/config"
  70. $AdminPass = 'foo';
  71. $ScriptName = 'http://localhost/';
  72. EOF
  73. perl stuff/server.pl ./wiki.pl &
  74. SERVER=$!
  75. sleep 1
  76. w3m http://localhost:8080/
  77. kill $!
  78. This will run the server exactly once, allow you to browse the site using w3m,
  79. and when you're done, it'll kill the server for you.
  80. EOT
  81. my $server = Oddmuse::Server->new($port);
  82. # $server->background();
  83. $server->run();