FU: Revert integration with XMLWriter & import options; copyright stuff

Realized that, since html_() now returns a string, it's just as easy to
just pass that to fu->set_body(); no need for integration complexity.

Combined import options don't save much typing, not worth the overhead
either.
This commit is contained in:
Yorhel 2025-02-20 08:46:01 +01:00
parent d0665ef7c2
commit 48d3fb86a5
6 changed files with 78 additions and 46 deletions

85
FU.pm
View file

@ -4,22 +4,14 @@ use Carp 'confess', 'croak';
use IO::Socket;
use POSIX;
use FU::Util;
use FU::XMLWriter;
my %export_funcs = do { no strict 'refs'; +(
(map +($_, \*{'FU::Util::'.$_}), @FU::Util::EXPORT_OK),
(map +($_, \*{'FU::XMLWriter::'.$_}), @FU::XMLWriter::EXPORT_OK),
) };
my %export_tags = %FU::XMLWriter::EXPORT_TAGS;
sub import($pkg, @opt) {
my $c = caller;
no strict 'refs';
*{$c.'::fu'} = \&fu;
for (map /^:(.+)$/ && $export_tags{$1} ? $export_tags{$1}->@* : ($_), @opt) {
for (@opt) {
if ($_ eq '-spawn') { _spawn() }
elsif ($export_funcs{$_}) { *{$c.'::'.$_} = $export_funcs{$_} }
else { croak "Unknown import option: '$_'" }
}
}
@ -276,10 +268,7 @@ sub _log_err($e) {
}
sub _do_req($c) {
local $REQ = {
hdr => {},
xml => FU::XMLWriter::_new(),
};
local $REQ = { hdr => {} };
local $fu = bless {}, 'FU::obj';
$REQ->{ip} = $c->{client_sock} isa 'IO::Socket::INET' ? $c->{client_sock}->peerhost : '127.0.0.1';
@ -588,10 +577,7 @@ sub done { die bless [200,'Done'], 'FU::err' }
sub error($,$code,$msg=$code) { die bless [$code,$msg], 'FU::err' }
sub status($, $code) { $FU::REQ->{status} = $code }
sub set_body($, $data) {
$FU::REQ->{xml}->_done;
$FU::REQ->{resbody} = $data;
}
sub set_body($, $data) { $FU::REQ->{resbody} = $data }
sub reset {
fu->status(200);
@ -650,16 +636,14 @@ sub _finalize {
state $haszstd = eval { require Compress::Zstd; 1 };
my $r = $FU::REQ;
my $xml = $r->{xml}->_done;
$r->{resbody} = $xml if length $xml;
if ($r->{status} == 204) {
delete $r->{reshdr}{'content-length'};
delete $r->{reshdr}{'content-encoding'};
$r->{resbody} = '';
} else {
if (($haszlib || $haszstd) && !defined $r->{reshdr}{'content-encoding'} && FU::compress_mimes->{$r->{reshdr}{'content-type'}}) {
if (($haszlib || $haszstd) && length($r->{resbody}) > 256
&& !defined $r->{reshdr}{'content-encoding'} && FU::compress_mimes->{$r->{reshdr}{'content-type'}}) {
$r->{reshdr}{'vary'} = ($r->{reshdr}{'vary'} ? $r->{reshdr}{'vary'}.', ' : '').'accept-encoding'
if ($r->{reshdr}{'vary'}||'') !~ /accept-encoding/i;
@ -729,10 +713,22 @@ FU - Framework Ultimatum: A Lean and Efficient Zero-Dependency Web Framework.
=head1 SYNOPSIS
use v5.36;
use FU -spawn, ':html5_';
use FU -spawn;
use FU::XMLWriter ':html5_';
sub myhtml_($title, $body) {
fu->set_body(html_ sub {
head_ sub {
title_ $title;
};
body_ $body;
});
}
FU::get qr{/hello/(.+)}, sub($who) {
h1_ "Hello, $who!";
my_html_ "Website title", sub {
h1_ "Hello, $who!";
};
};
FU::run;
@ -758,7 +754,17 @@ standalone and can be used independently of the framework:
Note that everything in this distribution requires a moderately recent version
of Perl (5.36+), a C compiler and a 64-bit POSIXy system (not Windows, that
is).
is). There are a few additional optional dependencies:
=over
=item * C<libpq.so> - required for L<FU::Pg>, dynamically loaded through
C<dlopen()>.
=item * L<Compress::Zstd> - to support transparent HTTP compression through
Zstandard.
=back
=head2 Framework Overview
@ -794,27 +800,8 @@ certainly not great if you plan to transfer large files.
=back
=head2 Importing FU
You'll usually want to add the following statement somewhere near the top of
your script:
use FU -spawn;
The C<-spawn> option tells C<FU> to read running configuration from environment
variables and command-line arguments during early startup, see L</"Running the
Site"> below.
C<FU> additionally re-exports all tags and functions from L<FU::Util> and
L<FU::XMLWriter>, so you can shorten your C<use> statements:
use FU -spawn, 'json_format', ':html5_';
Is equivalent to:
use FU -spawn;
use FU::Util 'json_format';
use FU::XMLWriter ':html5_';
The rest of this document is reference documentation; there's no easy
introductionary cookbook-style docs yet, sorry about that.
=head2 Framework Configuration
@ -983,3 +970,11 @@ their code. In both modes, C<SIGTERM> or C<SIGINT> can be used to trigger a
clean shutdown.
I<TODO:> Alternate FastCGI spawning options & server config examples.
=head1 COPYRIGHT
MIT.
=head1 AUTHOR
Yorhel <projects@yorhel.nl>