Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Mojolicious to serve more openAPIs

by Anonymous Monk
on Feb 08, 2019 at 09:47 UTC ( [id://1229605]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to set up a webserver that provides two or more APIs.

The APIs would be described according to OpenAPI 3.0 specification (https://swagger.io/).

I understand that Mojolicious would allow to add routes and input/output validation starting from the JSON API specification.

The webserver is running Apache and hosts various virtualhosts.

I wonder if this is compatible with Mojolicious and I'd like to have some suggestion on how to implement the architecture so to provide the two APIs (that are completely unrelated) as:

http://api.example.com/api01/
http://api.example.com/api02/

Also, is Mojolicious the best choice? I only have experience with CGI and Apache Web Server.

Thank you

Replies are listed 'Best First'.
Re: Mojolicious to serve more openAPIs
by Anonymous Monk on Feb 08, 2019 at 16:44 UTC
    I have modified existing Apache virtual servers so that they listen to port 8080 instead of 80.

    Then I have installed NGINX to receive connections to port 80 and defined one server block for each Apache virtual server with proxy_pass set to the correponding Apache virtual server.

    This allows me to use all the existing CGI applications.

    I have then created a server block for the new api.example.com with proxy_pass set to localhost:3000:

    server { listen 80; listen [::]:80; server_name api.example.com; location / { proxy_pass http://localhost:3000; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #try_files $uri $uri/ =404; } }
    Then I have created two Mojolicious apps, to be mounted via Toadfarm:

    #!/usr/bin/perl use Toadfarm -init; mount "/path/to/api01.pl" => { "X-Request-Base" => "http://api.example.com/api01" }; mount "/path/to/api02.pl" => { "X-Request-Base" => "http://api.example.com/api02" }; start ["http://*:3000"], workers => 8;
    And I've created the files /path/to/api01.pl:

    use Mojolicious::Lite; get '/' => {text => 'Hello! api01 here'}; app->start;
    and /path/to/api02.pl:

    use Mojolicious::Lite; get '/' => {text => 'Hello! api02 here'}; app->start;
    I can then start the script:

    $ ./api_start.pl start [2019-02-08 17:17:49.47784] [63828] [info] Mounting api01 with conditi +ons [2019-02-08 17:17:49.48110] [63828] [info] Mounting api02 with conditi +ons * Starting the process api_start_pl ...done. (already running with pid=63819)
    But from browser api.example.com/api01 and api.example.com/api02 give me a "Raptor not found Mojolicios message".

    Any suggestion? Thanks.

    2019-02-10 Athanasius changed <pre> to <code> tags

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1229605]
Approved by dorko
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-16 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found