Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active December 29, 2023 09:47
Show Gist options
  • Save x-yuri/e4ed2a18d5af356eaf895e9726e06b5c to your computer and use it in GitHub Desktop.
Save x-yuri/e4ed2a18d5af356eaf895e9726e06b5c to your computer and use it in GitHub Desktop.
simple http server (perl, go)

simple http server (perl, go)

Dockerfile:

FROM alpine:3.18
RUN apk add perl perl-http-daemon
COPY server.pl .
CMD ["perl", "server.pl"]

server.pl:

use strict;
use warnings;
use HTTP::Daemon;
my $d = HTTP::Daemon->new(LocalPort => 8080) or die;
while (my $c = $d->accept) {
    while (my $r = $c->get_request) {
        $c->send_response(200, undef, [
            'Content-Type' => 'text/html; charset=utf-8',
        ], "hello world (perl)\n");
    }
    $c->close;
}

go/Dockerfile:

FROM alpine:3.18 AS build
COPY server.go .
RUN apk add go && go build server.go

FROM alpine:3.18
COPY --from=build /server .
ENTRYPOINT ["/server"]

go/server.go:

package main

import (
    "fmt"
    "net/http"
)

func root(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "text/html; charset=utf-8")
    fmt.Fprintf(w, "hello world (go)\n")
}

func main() {
    http.HandleFunc("/", root)
    http.ListenAndServe(":8080", nil)
}

server.pl:

use strict;
use warnings;

{package MyServer;
use HTTP::Response;
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
sub handle_request {
    my ($self, $cgi) = @_;
    my $resp = HTTP::Response->new(200, undef, undef, 'hello world');
    $resp->protocol('HTTP/1.0');
    print $resp->as_string;
}
}

MyServer->new(80)->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment