Просмотр исходного кода

Make sure everything ends up in statics, and support username logging

George Baugh 5 месяцев назад
Родитель
Сommit
bfa4464cd3
2 измененных файлов с 27 добавлено и 1 удалено
  1. 8 0
      .perltidyrc
  2. 19 1
      lib/TPSGI.pm

+ 8 - 0
.perltidyrc

@@ -0,0 +1,8 @@
+-l=400
+-i=4
+-dt=4
+-it=4
+-bar
+-nsfs
+-nolq
+--break-at-old-comma-breakpoint

+ 19 - 1
lib/TPSGI.pm

@@ -33,6 +33,7 @@ use DateTime::Format::HTTP();
 use URL::Encode();
 use File::Touch;
 use File::Path;
+use File::Copy;
 use Cwd            qw{abs_path};
 use File::Basename qw{dirname basename};
 use Log::Dispatch;
@@ -151,7 +152,9 @@ sub _log {
     my $tstamp = POSIX::strftime "%Y-%m-%dT%H:%M:%SZ", gmtime;
     my $uuid   = $self->request_id();
 
-    return "[Worker $$] {Request $uuid} $tstamp : $self->{ip} $msg\n";
+	my $udata = $self->{user} ? "[$self->{user}]" : "[nobody]";
+
+    return "[Worker $$] {Request $uuid} $udata $tstamp : $self->{ip} $msg\n";
 }
 
 # Logger short cuts
@@ -359,6 +362,21 @@ sub serve {
         print $IO::Compress::Gzip::GzipError if $IO::Compress::Gzip::GzipError;
         push( @headers, "Content-Length" => length($dfh) );
 
+		# Copy to the statics if it's not already there
+		die "incorrect path $path, this is a bug" if $path =~ m/^http/;
+		my $static_path = $path;
+		$static_path =~ s|^[/]*www/||;
+		$static_path = "www/static/$static_path";
+		if (!-f $static_path) {
+			my $target_dir = dirname($static_path);
+			$self->INFO("Copying $path to $static_path");
+			if (! -d $target_dir) {
+				File::Path::make_path($target_dir) or die "Could not make dir $target_dir";
+			}
+			File::Copy::copy($path, $static_path) or die "Could not copy $path to $static_path";
+			# TODO figure out cache invalidation, I guess check mtime/hash
+		}
+
         $self->INFO("GET 200 $fullpath");
 
         # Append server-timing headers