JUCE Mongoose Module - Embedded Web Server Library


#1

I recently needed my app to run a lightweight local web server. I found This Project that wraps the Mongoose library in C++. I decided to make a basic JUCE module to wrap it! It’s very useful — especially when combined with a WebBrowserComponent. Your app can be both a web client and a web server, as well as interface with other web APIs as you would in a traditional web app.

Here is the link: https://github.com/cpenny42/juce_mongoose.

I just threw it together — didn’t bother to rewrite documentation or JUCE-ify it more. I’m planning on updating it as I go to make it better fit with the JUCE style, but it’s very powerful as it is.

Here’s a basic example:
After running this app, you could go to http://localhost:8080/hello?name=Jules to see it working.

#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
using namespace Mongoose;

class MainComponent   : public Component, public WebController
{
public:
    //==============================================================================
    MainComponent()
    : server (8080)
    {
        setupRoutes();
        server.registerController(this);
        server.start();
        
        setSize (600, 400);
    }
    
    void hello (Request &request, StreamResponse &response)
    {
        response << "Hello " << htmlEntities(request.get("name", "... what's your name ?")) << "\n";
    }
    
    void setupRoutes()
    {
        addRoute("GET", "/hello", MainComponent, hello);
    }
    
    void paint (Graphics& g) override
    {
        g.fillAll (Colours::black);
        g.setColour (Colours::white);
        g.drawText("Visit http://localhost:8080/hello to see the demo! Click to launch...",
                   0, 0, getWidth(), getHeight(), Justification::centred);
    }
    
    void mouseDown (const MouseEvent& e) override
    {
        URL url ("http://localhost:8080/hello?name=Jules");
        url.launchInDefaultBrowser();
    }
    
private:
    
    Server server;
    
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

To add it to your project, simply drag the “juce_mongoose” module into the Projucer next to the other modules (or put it in your custom modules location).


#2

would it work with civetweb? mongoose is GPL2 licenced, civetweb is a liberally licensed fork before it went GPL


#3

We’ve used civetweb - it was good!


#4

Cool! I think I’ll switch to that.


#5

Wow, this sounds nuts… Im curious if the switch to the liberal licensed variant is underway?


#6

I’ve used cpp-netlib before:

http://cpp-netlib.org/

It has a liberal license and uses very modern C++. The downside is that it relies heavily on boost asio (and boost in general). However, if your project is already using boost this might be an option.