JUCE Mongoose Module - Embedded Web Server Library

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).

6 Likes

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

1 Like

We’ve used civetweb - it was good!

Cool! I think I’ll switch to that.

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

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.

How did you integrate civetweb in your Juce application?

I think it is easier to try GitHub - yhirose/cpp-httplib: A C++ header-only HTTP/HTTPS server and client library. It is only a header include file for client + server. I have used this in Windows and Linux without any problems. A basic server look like this:


#include "juceHeader.h"
#include "httplib.h"

using namespace httplib;

class HttpServer : public Thread
{
public:
	HttpServer() : Thread("HttpServer")
	{
	}
	
	 void start(int portNumber)
	 {
	    mPortNumber = portNumber;
		
	    mServer.Post("/login", [this](const Request& req, Response& res)
	    {
               // ...
  	   });
		
	  // ...
		
	   mServer.Get("/logout", [this](const Request& req, Response& res)
           {
	    // ...	                        	
	   });
		
	   startThread();	 
	 }
	 	 
	void stop() 
	{    
		mServer.stop();
	}

	bool isRunning() const noexcept
	{
	  return isThreadRunning();
	}
	
protected:
 void run() override
 {
       mServer.listen("0.0.0.0", mPortNumber);    
  }
	
protected:
  Server                   mServer;
  int                      mPortNumber;	
};


elli

1 Like