| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | ## | ||
| 4 | ## simple openframe server | ||
| 5 | ## | ||
| 6 | |||
| 7 | use lib './lib'; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | use Pipeline; | ||
| 12 | use HTTP::Daemon; | ||
| 13 | use Data::Dumper; | ||
| 14 | use OpenFrame::Segment::HTTP::Request; | ||
| 15 | use OpenFrame::Segment::ContentLoader; | ||
| 16 | |||
| 17 | my $d = HTTP::Daemon->new( LocalPort => '8080', Reuse => 1); | ||
| 18 | die $! unless $d; | ||
| 19 | |||
| 20 | print "server running at http://localhost:8080/\n"; | ||
| 21 | |||
| 22 | ## wait for connections | ||
| 23 | while(my $c = $d->accept()) { | ||
| 24 | ## get requests of the connection | ||
| 25 | while(my $r = $c->get_request) { | ||
| 26 | |||
| 27 | ## create a new Pipeline object | ||
| 28 | my $pipeline = Pipeline->new(); | ||
| 29 | |||
| 30 | ## | ||
| 31 | ## create the segments | ||
| 32 | ## | ||
| 33 | |||
| 34 | ## the http request segment turns an HTTP request into something we | ||
| 35 | ## can deal with... | ||
| 36 | my $hr = OpenFrame::Segment::HTTP::Request->new(); | ||
| 37 | |||
| 38 | ## the content loader simply loads the file that we are | ||
| 39 | ## looking for based on the URI | ||
| 40 | my $cl = OpenFrame::Segment::ContentLoader->new() | ||
| 41 | ->directory("./webpages"); | ||
| 42 | |||
| 43 | ## add the two segments to the pipeline | ||
| 44 | $pipeline->add_segment( $hr, $cl ); | ||
| 45 | |||
| 46 | ## create a new store | ||
| 47 | my $store = Pipeline::Store::Simple->new(); | ||
| 48 | |||
| 49 | ## add the request into the store and then add the store to the pipeline | ||
| 50 | $pipeline->store( $store->set( $r ) ); | ||
| 51 | |||
| 52 | ## dispatch the pipeline | ||
| 53 | $pipeline->dispatch(); | ||
| 54 | |||
| 55 | ## get the response out | ||
| 56 | my $response = $pipeline->store->get('HTTP::Response'); | ||
| 57 | |||
| 58 | ## send it to the client. | ||
| 59 | $c->send_response( $response ); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 |