======================================================================
 HTTP::Handy PSGI Cheat Sheet                         [VI] Tiếng Việt
======================================================================

[ 1. Khởi động máy chủ ]

  use HTTP::Handy;

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

  HTTP::Handy->run(
      app           => $app,       # bắt buộc: PSGI app code reference
      host          => '127.0.0.1',# tuỳ chọn: bind address (default: 0.0.0.0)
      port          => 8080,       # tuỳ chọn: port (default: 8080)
      log           => 1,          # tuỳ chọn: access log (default: 1)
      max_post_size => 10485760,   # tuỳ chọn: max POST bytes (default: 10MB)
  );

  perl lib/HTTP/Handy.pm [port]

[ 2. Biến môi trường yêu cầu ($env) ]

  REQUEST_METHOD    chuỗi  "GET" / "POST"
  PATH_INFO         chuỗi  /index.html
  QUERY_STRING      chuỗi  key=val
  SERVER_NAME       chuỗi  hostname
  SERVER_PORT       số nguyên  8080
  CONTENT_TYPE      chuỗi  content-type
  CONTENT_LENGTH    số nguyên  (bytes)
  HTTP_*            chuỗi  HTTP_USER_AGENT ...
  psgi.input        đối tượng  (POST body)
  psgi.errors       glob  \*STDERR
  psgi.url_scheme   chuỗi  "http"

[ 3. Định dạng phản hồi ]

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

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

[ 4. Đọc thân 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. Các phương thức tiện ích ]

  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. Các phương thức tạo phản hồi ]

  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. Phục vụ tệp tĩnh ]

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

[ 8. Mẫu định tuyến ]

  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. Xử lý lỗi ]

  die "msg"  ->  500 tự động phản hồi 500
  [500, ['Content-Type','text/plain'], ['error']]
  [403, ['Content-Type','text/plain'], ['forbidden']]
  [400, ['Content-Type','text/plain'], ['bad request']]

[ 10. Tệp nhật ký được tạo bởi run() ]

  logs/access/YYYYMMDDHHm0.log.ltsv  nhật ký truy cập (xoay vòng 10 phút, LTSV)
  logs/error/error.log                nhật ký lỗi/khởi động

  LTSV: time  method  path  status  size  ua  referer

[ 11. Tài nguyên chính thức ]

  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

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