summaryrefslogtreecommitdiff
path: root/examples/welcome3.py
blob: 6273ec89efea8705a93c7e3cd0bd6714d9d41311 (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
56
57
58
59
60
61
62
import uwsgi
import os

from os.path import abspath, dirname, join

logo_png = abspath(join(dirname(__file__), "../logo_uWSGI.png"))


def xsendfile(e, sr):
    sr(
        "200 OK", [("Content-Type", "image/png"), ("X-Sendfile", logo_png),],
    )
    return b""


def serve_logo(e, sr):
    sr("200 OK", [("Content-Type", "image/png")])
    return uwsgi.sendfile(logo_png)


def serve_config(e, sr):
    sr("200 OK", [("Content-Type", "text/html")])
    for opt in uwsgi.opt.keys():

        def decode_if_bytes(val):
            if isinstance(val, bytes):
                return val.decode("ascii")
            return val

        body = "{opt} = {optvalue}<br/>".format(
            opt=opt.decode("ascii"), optvalue=decode_if_bytes(uwsgi.opt[opt])
        )
        yield bytes(body.encode("ascii"))


routes = {}
routes["/xsendfile"] = xsendfile
routes["/logo"] = serve_logo
routes["/config"] = serve_config


def application(env, start_response):

    if env["PATH_INFO"] in routes:
        return routes[env["PATH_INFO"]](env, start_response)

    start_response("200 OK", [("Content-Type", "text/html")])

    body = """
<img src="/logo"/> version {version}<br/>
<hr size="1"/>

Configuration<br/>
<iframe src="/config"></iframe><br/>

<br/>

    """.format(
        version=uwsgi.version
    )

    return [bytes(body.encode("ascii"))]