Code Monkey home page Code Monkey logo

served's Introduction

Served

Served Logo

Build Status

Overview

Served is a C++ library for building high performance RESTful web servers.

Served builds upon Boost.ASIO to provide a simple API for developers to create HTTP services in C++.

Features:

  • HTTP 1.1 compatible request parser
  • Middleware / plug-ins
  • Flexible handler API
  • Cross-platform compatible

Installation

Requirements

Building

$ git clone [email protected]:meltwater/served.git
$ mkdir served.build && cd served.build
$ cmake ../served && make

Or, using bazel:

$ git clone [email protected]:meltwater/served.git
$ cd served
$ bazel build :served
$ bazel test :served-test

Getting Started

The most basic example of creating a server and handling a HTTP GET for the path /hello:

#include <served/served.hpp>

int main(int argc, char const* argv[]) {
	// Create a multiplexer for handling requests
	served::multiplexer mux;

	// GET /hello
	mux.handle("/hello")
		.get([](served::response & res, const served::request & req) {
			res << "Hello world!";
		});

	// Create the server and run with 10 handler threads.
	served::net::server server("127.0.0.1", "8080", mux);
	server.run(10);

	return (EXIT_SUCCESS);
}

To test the above example, you could run the following command from a terminal:

$ curl http://localhost:8080/hello -ivh

You can also use named path variables for REST parameters:

mux.handle("/users/{id}")
	.get([](served::response & res, const served::request & req) {
		res << "User: " << req.params["id"];
	});

To test the above example, you could run the following command from a terminal:

$ curl http://localhost:8080/users/dave -ivh

If you need to be more specific, you can specify a pattern to use to validate the parameter:

mux.handle("/users/{id:\\d+}")
	.get([](served::response & res, const served::request & req) {
		res << "id: " << req.params["id"];
	});

To test the above example, you could run the following command from a terminal:

$ curl http://localhost:8080/users/1 -ivh

Method handlers can have arbitrary complexity:

mux.handle("/users/{id:\\d+}/{property}/{value:[a-zA-Z]+")
	.get([](served::response & res, const served::request & req) {
		// handler logic
	});

If you want to automatically log requests, you could use a plugin (or make your own):

#include <served/plugins.hpp>
// ...
mux.use_after(served::plugin::access_log);

You can also access the other elements of the request, including headers and components of the URI:

mux.handle("/posts/{id:\\d+}")
	.post([](served::response & res, const served::request & req) {
		if (req.header("Content-Type") != "application/json") {
			served::response::stock_reply(400, res);
			return;
		}
		res << req.url().fragment();
	});

Compile Options

Option Purpose
SERVED_BUILD_SHARED Build shared library
SERVED_BUILD_STATIC Build static library
SERVED_BUILD_TESTS Build unit test suite
SERVED_BUILD_EXAMPLES Build bundled examples
SERVED_BUILD_DEB Build DEB package (note: you must also have dpkg installed)
SERVED_BUILD_RPM Build RPM package (note: you must also have rpmbuild installed)

System Compatibility

OS Compiler Status
Linux GCC 4.8 Working
OSX Clang 3.5 Working

TODO

  • Chunked encoding support

Contributing

Pull requests are welcome.

Authors

Copyright

See LICENSE.md document

served's People

Contributors

jeffail avatar cjgdev avatar benjamg avatar jpihl avatar adriandc avatar luismartingil avatar rhubbarb avatar adevress avatar krzysztofwos avatar manashmandal avatar plule-ansys avatar bryant1410 avatar claudiouzelac avatar visual-rock avatar stevensona avatar r3mus-n0x avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.