======================================================================
 HTTP::Handy PSGI चिट सिट                             [NE] नेपाली
======================================================================

[ 1. सर्भर सुरू गर्नु ]

  use HTTP::Handy;

  my $app = sub {
      my $env = shift;
      return [200, ['Content-Type', 'text/plain'], ['Hello']];
  };

  HTTP::Handy->run(
      app           => $app,       # अनिवार्य: PSGI app code reference
      host          => '127.0.0.1',# वैकल्पिक: bind address (default: 0.0.0.0)
      port          => 8080,       # वैकल्पिक: port (default: 8080)
      log           => 1,          # वैकल्पिक: access log (default: 1)
      max_post_size => 10485760,   # वैकल्पिक: max POST bytes (default: 10MB)
  );

  perl lib/HTTP/Handy.pm [port]

[ 2. अनुरोध वातावरण चर ($env) ]

  REQUEST_METHOD    स्ट्रिङ  "GET" / "POST"
  PATH_INFO         स्ट्रिङ  /index.html
  QUERY_STRING      स्ट्रिङ  key=val
  SERVER_NAME       स्ट्रिङ  hostname
  SERVER_PORT       पूर्णांक  8080
  CONTENT_TYPE      स्ट्रिङ  content-type
  CONTENT_LENGTH    पूर्णांक  (bytes)
  HTTP_*            स्ट्रिङ  HTTP_USER_AGENT ...
  psgi.input        वस्तु  (POST body)
  psgi.errors       glob  \*STDERR
  psgi.url_scheme   स्ट्रिङ  "http"

[ 3. प्रतिक्रिया ढाँचा ]

  [$status, \@headers, \@body]

  [200, ['Content-Type', 'text/plain'], ['OK']]
  [404, ['Content-Type', 'text/plain'], ['Not Found']]
  [302, ['Location', '/new'], ['']]
  [204, ['Content-Length', '0'], []]

[ 4. POST मुख्य भाग पढ्नु (psgi.input) ]

  my $body = '';
  my $len  = $env->{CONTENT_LENGTH} || 0;
  $env->{'psgi.input'}->read($body, $len) if $len > 0;

  read($buf,$len)   read($buf,$len,$off)
  seek($pos,$wh)    tell()
  getline()         getlines()

[ 5. उपयोगिता विधिहरू ]

  HTTP::Handy->parse_query($env->{QUERY_STRING})  ->  %hash
  HTTP::Handy->parse_query($post_body)             ->  %hash
  HTTP::Handy->url_decode('hello%20world')         ->  "hello world"
  HTTP::Handy->mime_type('css')                    ->  "text/css"
  HTTP::Handy->is_htmx($env)                       ->  1 / 0

[ 6. प्रतिक्रिया निर्माण विधिहरू ]

  response_html($html [,$code])    text/html; charset=utf-8
  response_text($text [,$code])    text/plain; charset=utf-8
  response_json($json [,$code])    application/json
  response_redirect($url [,$code]) Location: $url

[ 7. स्थिर फाइलहरू परोस्नु ]

  return HTTP::Handy->serve_static($env, './htdocs');
  return HTTP::Handy->serve_static($env, './htdocs',
      cache_max_age => 3600);

[ 8. राउटिङ ढाँचा ]

  my $method = $env->{REQUEST_METHOD};
  my $path   = $env->{PATH_INFO};

  if ($method eq 'GET'  && $path eq '/') { ... }
  if ($method eq 'POST' && $path eq '/x') { ... }
  if ($path =~ m{^/api/}) { ... }
  if ($path =~ m{^/user/(\d+)$}) { my $id=$1; ... }

[ 9. त्रुटि व्यवस्थापन ]

  die "msg"  ->  500 स्वचालित 500 प्रतिक्रिया
  [500, ['Content-Type','text/plain'], ['error']]
  [403, ['Content-Type','text/plain'], ['forbidden']]
  [400, ['Content-Type','text/plain'], ['bad request']]

[ 10. run() ले बनाएका लग फाइलहरू ]

  logs/access/YYYYMMDDHHm0.log.ltsv  पहुँच लग (10 मिनेट रोटेसन, LTSV)
  logs/error/error.log                त्रुटि/स्टार्टअप लग

  LTSV: time  method  path  status  size  ua  referer

[ 11. आधिकारिक स्रोतहरू ]

  PSGI specification:
    https://github.com/plack/psgi-specs/blob/master/PSGI.pod
  PSGI (MetaCPAN): https://metacpan.org/pod/PSGI
  Plack: https://plackperl.org/
  HTTP::Handy: https://metacpan.org/dist/HTTP-Handy

======================================================================
