From 293b2a97c2e661d21c180245408a438489adebb2 Mon Sep 17 00:00:00 2001 From: leunga1000 <24807638+leunga1000@users.noreply.github.com> Date: Fri, 3 May 2024 16:32:02 +0100 Subject: [PATCH] Update getting-started.md (#175) Add __name__ == "__main__" clauses and a make_app function so that the app can be run with the CLI too. --- docs/getting-started.md | 58 +++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/docs/getting-started.md b/docs/getting-started.md index 4d653d2..7be7f18 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -8,10 +8,14 @@ Hello world app ```python from socketify import App -app = App() -app.get("/", lambda res, req: res.end("Hello World socketify from Python!")) -app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port)) -app.run() +def make_app(app: App): + app.get("/", lambda res, req: res.end("Hello World socketify from Python!")) + +if __name__ == "__main__": + app = App() + make_app(app) + app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port)) + app.run() ``` > This example just show how intuitive is to start an simple hello world app. @@ -19,10 +23,13 @@ SSL version sample ``` python from socketify import App, AppOptions -app = App(AppOptions(key_file_name="./misc/key.pem", cert_file_name="./misc/cert.pem", passphrase="1234")) -app.get("/", lambda res, req: res.end("Hello World socketify from Python!")) -app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port)) -app.run() +def make_app(app): + app.get("/", lambda res, req: res.end("Hello World socketify from Python!")) + +if __name__ == "__main__": + app = App(AppOptions(key_file_name="./misc/key.pem", cert_file_name="./misc/cert.pem", passphrase="1234")) + app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port)) + app.run() ``` > We have a lot of SSL options, but this is the most common you can see all the options in the [API Reference](api.md) @@ -38,20 +45,25 @@ def ws_open(ws): def ws_message(ws, message, opcode): #Ok is false if backpressure was built up, wait for drain ok = ws.send(message, opcode) - -app = App() -app.ws("/*", { - 'compression': CompressOptions.SHARED_COMPRESSOR, - 'max_payload_length': 16 * 1024 * 1024, - 'idle_timeout': 12, - 'open': ws_open, - 'message': ws_message, - 'drain': lambda ws: print('WebSocket backpressure: %i' % ws.get_buffered_amount()), - 'close': lambda ws, code, message: print('WebSocket closed') -}) -app.any("/", lambda res,req: res.end("Nothing to see here!'")) -app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port))) -app.run() + +def make_app(app): + app.ws("/*", { + 'compression': CompressOptions.SHARED_COMPRESSOR, + 'max_payload_length': 16 * 1024 * 1024, + 'idle_timeout': 12, + 'open': ws_open, + 'message': ws_message, + 'drain': lambda ws: print('WebSocket backpressure: %i' % ws.get_buffered_amount()), + 'close': lambda ws, code, message: print('WebSocket closed') + }) + app.any("/", lambda res,req: res.end("Nothing to see here!'")) + + +if __name__ == "__main__": + app = App() + make_app(app) + app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port))) + app.run() ``` > We can have multiple routes for WebSockets, but in this example we just get one for anything we need, adding an option of compression using SHARED_COMPRESSOR, max_payload_length of 1mb and an idle timeout of 12s just to show some most commonly used features you can see all these options in the [API Reference](api.md) @@ -59,4 +71,4 @@ app.run() If you just wanna to see some more examples you can go to our [examples folder](https://github.com/cirospaciari/socketify.py/tree/main/examples) for more than 25 quick examples. -### Next [Corking Concept](corking.md) \ No newline at end of file +### Next [Corking Concept](corking.md)