======================================================================
 HTTP::Handy PSGI Hızlı Başvuru                       [TR] Türkçe
======================================================================

[ 1. Sunucuyu başlatma ]

  use HTTP::Handy;

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

  HTTP::Handy->run(
      app           => $app,       # zorunlu: PSGI app code reference
      host          => '127.0.0.1',# isteğe bağlı: bind address (default: 0.0.0.0)
      port          => 8080,       # isteğe bağlı: port (default: 8080)
      log           => 1,          # isteğe bağlı: access log (default: 1)
      max_post_size => 10485760,   # isteğe bağlı: max POST bytes (default: 10MB)
  );

  perl lib/HTTP/Handy.pm [port]

[ 2. İstek ortam değişkenleri ($env) ]

  REQUEST_METHOD    dize  "GET" / "POST"
  PATH_INFO         dize  /index.html
  QUERY_STRING      dize  key=val
  SERVER_NAME       dize  hostname
  SERVER_PORT       tam sayı  8080
  CONTENT_TYPE      dize  content-type
  CONTENT_LENGTH    tam sayı  (bytes)
  HTTP_*            dize  HTTP_USER_AGENT ...
  psgi.input        nesne  (POST body)
  psgi.errors       glob  \*STDERR
  psgi.url_scheme   dize  "http"

[ 3. Yanıt biçimi ]

  [$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 gövdesini okuma (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. Yardımcı metodlar ]

  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. Yanıt oluşturucu metodlar ]

  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 dosya sunumu ]

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

[ 8. Yönlendirme kalıbı ]

  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. Hata yönetimi ]

  die "msg"  ->  500 otomatik 500 yanıtı
  [500, ['Content-Type','text/plain'], ['error']]
  [403, ['Content-Type','text/plain'], ['forbidden']]
  [400, ['Content-Type','text/plain'], ['bad request']]

[ 10. run() tarafından oluşturulan günlük dosyaları ]

  logs/access/YYYYMMDDHHm0.log.ltsv  erişim günlüğü (10 dak. döngüsü, LTSV)
  logs/error/error.log                hata/başlangıç günlüğü

  LTSV: time  method  path  status  size  ua  referer

[ 11. Resmi kaynaklar ]

  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

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