Skip to content
GitLab
  • Menu
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • L libsoup
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 161
    • Issues 161
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 11
    • Merge requests 11
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • GNOME
  • libsoup
  • Merge requests
  • !111

Routed server

  • Review changes

  • Download
  • Email patches
  • Plain diff
Open Philip Chimento requested to merge ptomato/libsoup:routed-server into master Nov 19, 2019
  • Overview 15
  • Commits 1
  • Pipelines 10
  • Changes 7

This is a subclass of SoupServer that can be used to set up a REST-style service very quickly and conveniently. Its API is based on that of Express.js and is especially powerful when used in a language such as JavaScript where you can define callbacks inline. For example:

const {GLib, Soup} = imports.gi;

const documents = {
    'hello': '<h1>Hello, world!</h1>',
    'deleteme': '<p>Try deleting this</p>',
}

const server = new Soup.RoutedServer();
server.get('/document/:id', (server, {id}, query, msg) => {
    if (!(id in documents))
        return false;

    msg.status_code = Soup.Status.OK;
    msg.response_headers.set_content_type('text/html', {charset: 'UTF-8'});
    msg.response_body.append(`
        <html>
        <body>
            ${documents[id]}
        </body>
        </html>
    `);
    return true;
});
server.delete('/document/:id', (server, {id}, query, msg) => {
    if (!(id in documents))
        return false;

    msg.status_code = Soup.Status.OK;
    delete documents[id];
    return true;
});

server.listen_local(10001, 0);

const loop = GLib.MainLoop.new(null, false);
loop.run();

Will Greenberg and Cosimo Cecchi wrote most of this a few years ago for some internal Endless code, and I've been meaning to extract it and contribute it to libsoup for quite some time, as I think it can be more generally useful for libsoup users. I've cleaned up the API and written some documentation and expanded the test coverage.

Assignee
Assign to
Reviewer
Request review from
Time tracking
Source branch: routed-server