Skip to content

Instantly share code, notes, and snippets.

@lambda-mike
Created December 27, 2025 00:24
Show Gist options
  • Select an option

  • Save lambda-mike/ce82559d1bb53b2344d13e88ff636e4b to your computer and use it in GitHub Desktop.

Select an option

Save lambda-mike/ce82559d1bb53b2344d13e88ff636e4b to your computer and use it in GitHub Desktop.
Mock web server in Perl
#!/usr/bin/env nix-shell
#! nix-shell -i perl -p perl perl540Packages.Mojolicious
# chmod u+x ./mserver.pl # in order to run it directly:
# ./mserver.pl
use strict;
use warnings;
use Mojolicious::Lite;
my $BASE = app->home->child('pages'); # Mojo::Path object; stringifies automatically
sub serve_html {
my ($c, $file) = @_;
my $full_path = "$BASE/$file";
# If the file does not exist, fall back to a tiny plain‑text message
unless (-e $full_path) {
$c->render(text => "File $file not found", status => 500);
return;
}
$c->reply->file($full_path);
$c->res->headers->content_type('text/html; charset=utf-8');
}
get '/' => sub {
my $c = shift;
serve_html($c, 'home.html');
};
get '/login' => sub {
my $c = shift;
serve_html($c, 'login.html');
};
# /ifconfigme – 302 redirect to /login
get '/ifconfigme' => sub {
my $c = shift;
$c->redirect_to('/login'); # automatically sends 302 + Location header
};
app->start('daemon', '-l', 'http://localhost:7890'); # listen on all interfaces, port 8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment