summaryrefslogtreecommitdiff
path: root/examples/http-server.js
blob: 8f9e9172b5fe65e75b2471b0941a3fbc274aff37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2010 litl, LLC

// This is a simple example of a HTTP server in GJS using libsoup
// open http://localhost:1080 in your browser or use http-client.js

import Soup from 'gi://Soup?version=3.0';
import GLib from 'gi://GLib';

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

function handler(_server, msg, _path, _query) {
    msg.set_status(200, null);
    msg.get_response_headers().set_content_type('text/html', {charset: 'UTF-8'});
    msg.get_response_body().append(`
        <html>
        <body>
            Greetings, visitor from ${msg.get_remote_host()}<br>
            What is your name?
            <form action="/hello">
                <input name="myname">
            </form>
        </body>
        </html>
    `);
}

function helloHandler(_server, msg, path, query) {
    if (!query) {
        msg.set_redirect(302, '/');
        return;
    }

    msg.set_status(200, null);
    msg.get_response_headers().set_content_type('text/html', {charset: 'UTF-8'});
    msg.get_response_body().append(`
        <html>
        <body>
            Hello, ${query.myname}! ☺<br>
            <a href="/">Go back</a>
        </body>
        </html>
    `);
}

function main() {
    let server = new Soup.Server();
    server.add_handler('/', handler);
    server.add_handler('/hello', helloHandler);
    server.listen_local(1080, Soup.ServerListenOptions.IPV4_ONLY);
}

main();

loop.run();