diff --git a/fast-wsgi/fast-wsgimodule.c b/fast-wsgi/fast-wsgimodule.c index d9d501b..cd1ffc5 100644 --- a/fast-wsgi/fast-wsgimodule.c +++ b/fast-wsgi/fast-wsgimodule.c @@ -14,6 +14,6 @@ static struct PyModuleDef module = { FastWsgiFunctions, }; -PyMODINIT_FUNC PyInit_fast_wsgi(void) { +PyMODINIT_FUNC PyInit__fast_wsgi(void) { return PyModule_Create(&module); } \ No newline at end of file diff --git a/fast-wsgi/server.c b/fast-wsgi/server.c index 5522577..a2e4ffd 100644 --- a/fast-wsgi/server.c +++ b/fast-wsgi/server.c @@ -14,17 +14,6 @@ "\r\n" \ "Hello, World!\n" -static const char* HOST = "0.0.0.0"; -static const int PORT = 5000; -static const int BACKLOG = 256; - -static uv_tcp_t server; - -static uv_buf_t response_buf; - -uv_loop_t* loop; - -struct sockaddr_in addr; void close_cb(uv_handle_t* handle) { printf("disconnected\n"); @@ -95,7 +84,7 @@ int main() { response_buf.base = SIMPLE_RESPONSE; response_buf.len = sizeof(SIMPLE_RESPONSE); - uv_ip4_addr(HOST, PORT, &addr); + uv_ip4_addr(host, port, &addr); int r = uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0); if (r) { @@ -103,7 +92,7 @@ int main() { return 1; } - r = uv_listen((uv_stream_t*)&server, BACKLOG, connection_cb); + r = uv_listen((uv_stream_t*)&server, backlog, connection_cb); if (r) { fprintf(stderr, "Listen error %s\n", uv_strerror(r)); return 1; @@ -112,7 +101,7 @@ int main() { } PyObject* run_server(PyObject* self, PyObject* args) { - PyArg_UnpackTuple(args, "ref", 1, 1, &wsgi_app); + PyArg_ParseTuple(args, "Osii", &wsgi_app, &host, &port, &backlog); main(); return Py_BuildValue("s", "'run_server' function executed"); } \ No newline at end of file diff --git a/fast-wsgi/server.h b/fast-wsgi/server.h index 225b96c..5700912 100644 --- a/fast-wsgi/server.h +++ b/fast-wsgi/server.h @@ -3,11 +3,15 @@ #include "llhttp.h" PyObject* wsgi_app; +char* host; +int port; +int backlog; -typedef struct { - PyObject* host; - PyObject* port; -} ServerArgs; +uv_tcp_t server; +uv_buf_t response_buf; +uv_loop_t* loop; + +struct sockaddr_in addr; typedef struct { uv_write_t req; diff --git a/fast_wsgi.py b/fast_wsgi.py new file mode 100644 index 0000000..4f74d42 --- /dev/null +++ b/fast_wsgi.py @@ -0,0 +1,15 @@ +import _fast_wsgi + + +def run(wsgi_app, host, port, backlog=256): + try: + print(f"Running server on {host} and port {port}") + _fast_wsgi.run_server(wsgi_app, host, port, backlog) + finally: + print("Closing...") + + +print("Running...") +run({}, "0.0.0.0", 5000) +# TODO: Shutdown on Ctrl-C somehow +print("Done") diff --git a/setup.py b/setup.py index 4e79581..2c125b6 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from distutils.core import Extension, setup SOURCE_FILES = glob.glob("fast-wsgi/*.c") + glob.glob("llhttp/src/*.c") module = Extension( - "fast_wsgi", + "_fast_wsgi", sources=SOURCE_FILES, libraries=['uv'], include_dirs=["llhttp/include", "/usr/include"], @@ -13,7 +13,7 @@ module = Extension( setup( name="fast_wsgi", - version="1.0", - description="This is a package for fast_wsgi", + version="0.1", + description="An ultra fast WSGI server for Python 3", ext_modules=[module] )