======================================================================
 HTTP::Handy PSGI Cheat Sheet                        [UZ] Ozbek
======================================================================

[ 1. Serverni ishga tushirish ]

  use HTTP::Handy;

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

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

  Togridam-togri skript sifatida ishga tushirish: perl lib/HTTP/Handy.pm [port]

[ 2. Sov muhit ozgaruvchilari ($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 tanasini oqish (psgi.inpu)
  psgi.errors       \*STDERR
  psgi.url_scheme   "http"

[ 3. Javob formati ]

  [$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 tanasini oqish (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. Yordamchi usullar ]

  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. Javob yaratuvchi usullar ]

  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. Statik fayllarni berish ]

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

[ 8. Marshrutlash namunasi ]

  $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. Xatolarni boshqarish ]

  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() tomonidan yaratilgan log fayllar ]

  logs/access/YYYYMMDDHHm0.log.ltsv  kirish logi (10 daqiqalik aylantirish, LTSV formati)
  logs/error/error.log                xato/ishga tushirish logi

  LTSV: time  method  path  status  size  ua  referer

[ 11. Rasmiy manbalar havolalari ]

  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

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