Skip to content

supamii/QttpServer

 
 

Repository files navigation

Build Status

QttpServer 1.0.0

QttpServer focuses on developing a lean and mean C++ based API server. In addition to leveraging modern C++, QttpServer also employs Qt to promote productivity and reinforce best practices. Use it for systems that are resource constrained, general prototyping, integrate into existing Qt Applications, or if you simply wish to save a few bucks on your cloud servers.

Getting started is EASY! Just load qttpserver.pro into QtCreator!

Check out the examples and samples to get started with your RESTful API server!

QttpServer

Features

Example 1:

Using a raw std::function based callback

#include <httpserver.h>

int main(int argc, char** argv)
{
  QCoreApplication app(argc, argv);

  // Always initialize in the main thread.
  qttp::HttpServer* httpSvr = qttp::HttpServer::getInstance();
  httpSvr->initialize();

  // Create an action, named "sayHello", that will handle all requests
  auto action = httpSvr->createAction([](qttp::HttpData& data) {
    // Form the JSON content and let the framework handle the rest.
    QJsonObject& json = data.getResponse().getJson();
    json["hello"] = "world";
  });

  // Register the action to handle http GET for the path "/".
  action->registerRoute(qttp::HttpMethod::GET, "/");

  // Register the action to handle http GET for the path "/hello".
  action->registerRoute(qttp::HttpMethod::GET, "/hello");

  // Libuv runs in its own thread.
  httpSvr->startServer();

  // Qt takes the main thread per the usual.
  return app.exec();
}

Example 2:

Using the action interface

#include <httpserver.h>

using namespace qttp;

class SayGoodbye : public Action {
  public:
    void onAction(HttpData& data) {
      QJsonObject& json = data.getResponse().getJson();
      json["response"] = "adios";
    }
    const const char* getName() const { return "sayGoodbye"; }
};

int main(int argc, char** argv)
{
  QCoreApplication app(argc, argv);
  HttpServer* httpSvr = HttpServer::getInstance();
  httpSvr->initialize();
  
  // Adds the action interface via template method.
  auto action = httpSvr->createAction<SayGoodbye>();
  
  action->registerRoute({
    {qttp::HttpMethod::GET, "/bye"},
    {qttp::HttpMethod::POST, "/aloha"}
  });

  httpSvr->startServer();
  return app.exec();
}

SwaggerUI Integrated

SwaggerUIQttpServer Operations

Get Started NOW

Prerequisites

  1. git

  2. qt installer or build from source. Below I've successfully used this tarball.

    # Building from source is a breeze for Ubuntu SERVER 12 & 14 LTS
    sudo apt-get install libssl-dev
    wget http://download.qt.io/official_releases/qt/5.5/5.5.1/single/qt-everywhere-opensource-src-5.5.1.tar.gz
    tar -xvf qt-everywhere-opensource-src-5.5.1.tar.tz
    cd qt-everywhere-opensource-src-5.5.1
    ./configure -nomake examples -opensource -skip qtwebkit -skip qtwebchannel -skip qtquick1 -skip qtdeclarative -openssl-linked
    make
    sudo make install

Setup

git clone https://github.com/supamii/QttpServer.git
cd QttpServer

Git submodules/dependencies automatically pulls in mongodb-drivers, boost, libuv, http-parser

git submodule update --init

Using QtCreator, open qttpserver.pro and enjoy!

QttpServer project loaded in QtCreator

Using QMake:

qmake CONFIG+=debug qttpserver.pro
make

By default, qttpserver will compile as a static library for ease of use. Check out the examples here for a quick-start. If you run into any blockers, feel free to open bug(s) and ask questions!

More Resources

Building Redis and MongoDB

Checkout the guide.

Examples and Tutorials

Riiiight over... Here

Alternate Build Scripts

node.native was previously built using gyp - you can learn more about that here

Note: It's likely that these will get phased out.

Credits

QttpServer is a fork from node.native with some additional contributions from tojocky.

License

The MIT License (MIT)

Copyright (c) 2016 Son-Huy Pham

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Build your API with C++ - Powered by Qt and Node's libuv

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 50.0%
  • JavaScript 34.3%
  • Python 6.5%
  • CSS 5.7%
  • C 1.5%
  • CMake 0.4%
  • Other 1.6%