======================================================================
 HTTP::Handy PSGI Cheat Sheet                        [NE] Nepali
======================================================================

[ 1. server suru garne ]

  use HTTP::Handy;

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

  HTTP::Handy->run(
      app           => $app,       # aavashyak: PSGI app code ref
      host          => '127.0.0.1',# vaikalpik: bind address (0.0.0.0)
      port          => 8080,       # vaikalpik: port (purvanirdharit: 8080)
      log           => 1,          # vaikalpik: access log (purvanirdharit: 1)
      max_post_size => 10485760,   # vaikalpik: max POST bytes (purvanirdharit: 10MB)
  );

  script ko rupama siddha chalaunus: perl lib/HTTP/Handy.pm [port]

[ 2. anurodh vatavaran char ($env) ]

  REQUEST_METHOD    "GET" / "POST"
  PATH_INFO         /index.html
  QUERY_STRING      key=val&key2=val2
  SERVER_NAME       hostname
  SERVER_PORT       8080
  CONTENT_TYPE      application/x-www-form-urlencoded
  CONTENT_LENGTH    (bytes)
  HTTP_USER_AGENT   Mozilla/5.0 ...
  psgi.input        (POST body padhne (psgi.input))
  psgi.errors       \*STDERR
  psgi.url_scheme   "http"

[ 3. pratikriya dhanchhaa ]

  [$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 body padhne (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. upayogita vidhiharu ]

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

[ 6. pratikriya nirmata vidhiharu ]

  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. sthir failharu seva garne ]

  serve_static($env, './htdocs')
  serve_static($env, './htdocs', cache_max_age => 3600)

[ 8. routing dhanchhaa ]

  $method = $env->{REQUEST_METHOD};
  $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. truti vyavasthapan ]

  die "msg"  ->  500 response (auto)
  [500, ['Content-Type','text/plain'], ['error']]
  [403, ['Content-Type','text/plain'], ['forbidden']]
  [400, ['Content-Type','text/plain'], ['bad request']]

[ 10. run() le sirchhya gareka log failharu ]

  logs/access/YYYYMMDDHHm0.log.ltsv  pahunach log (10 mineta rotation, LTSV dhanchhaa)
  logs/error/error.log                truti/startup 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 (MetaCPAN):
    https://metacpan.org/dist/HTTP-Handy

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