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

9
COPYING Normal file
View file

@ -0,0 +1,9 @@
MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

83
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,11 +713,23 @@ 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) {
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>

View file

@ -522,3 +522,11 @@ Popular DBI wrapper with some API conveniences. I may have taken some
inspiration from it in the design of this module's API.
=back
=head1 COPYRIGHT
MIT.
=head1 AUTHOR
Yorhel <projects@yorhel.nl>

View file

@ -312,3 +312,11 @@ for more weirdness and edge cases.
See also L<IO::FDPass> for a more portable solution, although that one does not
support passing along regular data.
=head1 COPYRIGHT
MIT.
=head1 AUTHOR
Yorhel <projects@yorhel.nl>

View file

@ -289,3 +289,11 @@ L<DSL::HTML>, but its syntax isn't quite as nice.
And there's also L<HTML::FromArrayref>, L<HTML::Tiny>, L<HTML::Untidy> and many
more modules on CPAN. In fact I don't know why you should use this module
instead of whatever is available on CPAN.
=head1 COPYRIGHT
MIT.
=head1 AUTHOR
Yorhel <projects@yorhel.nl>

View file

@ -16,8 +16,12 @@ make install
Things that may or may not happen:
- FU - The website framework, taking inspiration from TUWF.
- FU::JSON - JSON::{XS,PP,etc}-compatible wrapper around FU::Util's JSON functions? I prolly won't need this myself, but could be handy.
- FU::DBI - DBI wrapper with a FU::Pg-like API, for easier integration into FU.
- FU::Log - Basic logger.
- FU::Validate - TUWF::Validate & normalization with some improvements.
- FU::Mailer - Simple sendmail wrapper
# License
MIT.