« blib/lib/OpenFrame/AppKit/Segment/TT2.pm
↑ Summary
blib/lib/OpenFrame/AppKit/Examples/NameForm.pm »
| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | ## | ||
| 4 | ## simple openframe server | ||
| 5 | ## | ||
| 6 | |||
| 7 | use lib './lib', '../openframe3/lib'; | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | use Pipeline; | ||
| 12 | use HTTP::Daemon; | ||
| 13 | use OpenFrame::AppKit; | ||
| 14 | use OpenFrame::Segment::HTTP::Request; | ||
| 15 | use OpenFrame::AppKit::Examples::Hangman; | ||
| 16 | use OpenFrame::AppKit::Examples::NameForm; | ||
| 17 | |||
| 18 | my $d = HTTP::Daemon->new( LocalPort => '8080', Reuse => 1); | ||
| 19 | die $! unless $d; | ||
| 20 | |||
| 21 | print "server running at http://localhost:8080/\n"; | ||
| 22 | |||
| 23 | ## wait for connections | ||
| 24 | while(my $c = $d->accept()) { | ||
| 25 | ## get requests of the connection | ||
| 26 | while(my $r = $c->get_request) { | ||
| 27 | ## create a new Pipeline object | ||
| 28 | my $pipeline = Pipeline->new()->debug(1); | ||
| 29 | |||
| 30 | ## | ||
| 31 | ## create the segments | ||
| 32 | ## | ||
| 33 | my $http_request = OpenFrame::Segment::HTTP::Request->new(); | ||
| 34 | my $image_loader = OpenFrame::AppKit::Segment::Images->new() | ||
| 35 | ->directory("./templates"); | ||
| 36 | my $session_loader = OpenFrame::AppKit::Segment::SessionLoader->new(); | ||
| 37 | my $name_form = OpenFrame::AppKit::Examples::NameForm->new() | ||
| 38 | ->uri( qr:/(index\.html|)$: ) | ||
| 39 | ->namespace( 'nameform' ); | ||
| 40 | my $hangman = OpenFrame::AppKit::Examples::Hangman->new() | ||
| 41 | ->uri( '/hangman' ) | ||
| 42 | ->namespace( 'hangman' ) | ||
| 43 | ->wordlist( './words.txt' ); | ||
| 44 | my $content_loader = OpenFrame::AppKit::Segment::TT2->new() | ||
| 45 | ->directory("./templates"); | ||
| 46 | my $logger = OpenFrame::AppKit::Segment::LogFile->new(); | ||
| 47 | |||
| 48 | |||
| 49 | ## order is important here. | ||
| 50 | $pipeline->add_segment( | ||
| 51 | $http_request, | ||
| 52 | $image_loader, | ||
| 53 | $session_loader, | ||
| 54 | $name_form, | ||
| 55 | $hangman, | ||
| 56 | $content_loader, | ||
| 57 | ); | ||
| 58 | |||
| 59 | $pipeline->cleanups->add_segment( $logger ); | ||
| 60 | |||
| 61 | ## create a new store | ||
| 62 | my $store = Pipeline::Store::Simple->new(); | ||
| 63 | |||
| 64 | ## add the request into the store and then add the store to the pipeline | ||
| 65 | $pipeline->store( $store->set( $r ) ); | ||
| 66 | |||
| 67 | ## dispatch the pipeline | ||
| 68 | $pipeline->dispatch(); | ||
| 69 | |||
| 70 | ## get the response out | ||
| 71 | my $response = $pipeline->store->get('HTTP::Response'); | ||
| 72 | |||
| 73 | ## send it to the client. | ||
| 74 | $c->send_response( $response ); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 |