Update getting-started.md

Add __name__ == "__main__" clauses and a make_app function so that the app can be run with the CLI too.
pull/175/head
leunga1000 2024-05-03 12:57:15 +01:00 zatwierdzone przez GitHub
rodzic 2aba41ff7b
commit 3d2e1f5ce2
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 35 dodań i 23 usunięć

Wyświetl plik

@ -8,8 +8,12 @@ Hello world app
```python ```python
from socketify import App from socketify import App
app = App() def make_app(app: App):
app.get("/", lambda res, req: res.end("Hello World socketify from Python!")) 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.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
app.run() app.run()
``` ```
@ -19,8 +23,11 @@ SSL version sample
``` python ``` python
from socketify import App, AppOptions from socketify import App, AppOptions
app = App(AppOptions(key_file_name="./misc/key.pem", cert_file_name="./misc/cert.pem", passphrase="1234")) def make_app(app):
app.get("/", lambda res, req: res.end("Hello World socketify from Python!")) 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.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
app.run() app.run()
``` ```
@ -39,7 +46,7 @@ def ws_message(ws, message, opcode):
#Ok is false if backpressure was built up, wait for drain #Ok is false if backpressure was built up, wait for drain
ok = ws.send(message, opcode) ok = ws.send(message, opcode)
app = App() def make_app(app):
app.ws("/*", { app.ws("/*", {
'compression': CompressOptions.SHARED_COMPRESSOR, 'compression': CompressOptions.SHARED_COMPRESSOR,
'max_payload_length': 16 * 1024 * 1024, 'max_payload_length': 16 * 1024 * 1024,
@ -50,6 +57,11 @@ app.ws("/*", {
'close': lambda ws, code, message: print('WebSocket closed') 'close': lambda ws, code, message: print('WebSocket closed')
}) })
app.any("/", lambda res,req: res.end("Nothing to see here!'")) 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.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
app.run() app.run()
``` ```