mirror of
https://github.com/jamesroberts/fastwsgi.git
synced 2025-12-30 10:49:46 -06:00
Use python function to init server
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
15
fast_wsgi.py
Normal file
15
fast_wsgi.py
Normal file
@@ -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")
|
||||
6
setup.py
6
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]
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user