예제 #1
0
def run_panini():

    app = panini_app.App(
        service_name="test_web_server",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        web_server=True,
        web_port=8084,
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.listen("test_web_server.foo")
    async def foo(msg):
        msg.data["data"] += 1
        return msg.data

    @app.http.post("/test_web_server/bar")
    async def post_listener(request):
        data = await request.json()
        data["data"] += 2
        return web.json_response(data)

    app.start()
def run_panini():

    app = panini_app.App(
        service_name="test_web_server_separately",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        web_server=True,
        web_port=8083,
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.http.get("/test_web_server_separately/get")
    async def get_listener(request):
        return web.Response(text="get response")

    @app.http.post("/test_web_server_separately/post")
    async def post_listener(request):
        data = await request.json()
        data["data"] += 1
        return web.json_response(data)

    @app.http.view("/test_web_server_separately/rest/endpoint")
    class RESTView(web.View):
        async def get(self):
            return await get_listener(self.request)

        async def post(self):
            return await post_listener(self.request)

    app.start()
예제 #3
0
def run_panini():
    app = panini_app.App(
        service_name="test_validator",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    class DataValidator(Validator):
        data = Field(type=int)

    @app.listen("test_validator.foo", validator=DataValidator)
    async def publish(msg):
        await app.publish(subject="test_validator.bar", message={"data": 1})

    @app.listen("test_validator.check")
    async def check(msg):
        try:
            DataValidator.validated_message(msg.data)
        except ValidationError:
            return {"success": False}

        return {"success": True}

    app.start()
예제 #4
0
def run_panini():
    app = panini_app.App(
        service_name="test_middleware",
        host="127.0.0.1",
        port=4222,
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.listen("test_middleware.publish")
    async def publish(msg):
        try:
            await app.publish(
                subject="test_middleware.publish.response",
                message={"data": msg.data["data"] + 1},
            )
        except Exception:
            app.logger.exception("test_middleware.publish")

    @app.listen("test_middleware.request")
    async def request(msg):
        try:
            response = await app.request(
                subject="test_middleware.request.helper",
                message={"data": msg.data["data"]},
            )
            return {"data": response["data"] + 2}
        except Exception:
            app.logger.exception("test_middleware.request")

    @app.listen("test_middleware.request.helper")
    async def helper(msg):
        try:
            return {"data": msg.data["data"] + 2}
        except Exception:
            app.logger.exception("test_middleware.request.helper")

    @app.listen("test_middleware.listen.publish")
    async def request(msg):
        try:
            await app.publish(
                subject="test_middleware.listen.publish.response",
                message={"data": msg.data["data"] + 3},
            )
        except Exception:
            app.logger.exception("test_middleware.listen.publish")

    @app.listen("test_middleware.listen.request")
    async def request(msg):
        try:
            return {"data": msg.data["data"] + 4}
        except Exception:
            app.logger.exception("test_middleware.listen.request")

    app.add_middleware(AddMiddleware)
    app.start()
예제 #5
0
def run_panini():
    app = panini_app.App(
        service_name="test_task",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.task()
    async def publish():
        await app.publish(subject="test_task.foo", message={"data": 1})

    app.start()
예제 #6
0
def run_panini():
    app = panini_app.App(
        service_name="test_request",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.listen("test_request.start")
    async def publish_request(msg):
        response = await app.request(subject="test_request.foo",
                                     message={"data": 1})
        return response

    app.start()
예제 #7
0
def run_panini():
    app = panini_app.App(
        service_name="test_listen",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.listen("test_listen.foo")
    async def subject_for_requests(msg):
        return {"data": msg.data["data"] + 1}

    @app.listen("test_listen.foo.*.bar")
    async def composite_subject_for_requests(msg):
        return {"data": msg.subject + str(msg.data["data"])}

    app.start()
예제 #8
0
def run_panini():
    app = ant_app.App(
        service_name="test_wss",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        web_server=True,
        web_port=1111,
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    manager = WSSManager(app)

    @app.listen("test_wss.start")
    async def publish(msg):
        await app.publish(subject="test_wss.foo.bar", message={"data": 1})

    @app.http.get("/test_wss/stream")
    async def web_endpoint_listener(request):
        ws = web.WebSocketResponse()
        await ws.prepare(request)
        connection_id = str(uuid.uuid4())[:10]
        await ws.send_str(
            json.dumps({
                "success": True,
                "data": "Successfully connected"
            }))
        await manager.client_listener(ws, connection_id)
        await ws.close()
        return ws

    async def incoming_messages_callback(subscriber, msg):
        await subscriber.send_str(
            json.dumps({
                "subject": msg.subject,
                "data": msg.data
            }))

    manager.callback = incoming_messages_callback
    app.http_server.web_app["subscribers"] = {}
    app.start()
예제 #9
0
def run_panini():
    app = panini_app.App(
        service_name="test_encoding",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.listen("test_encoding.foo")
    async def foo(msg):
        return {"len": len(msg.data["data"])}

    @app.listen("test_encoding.helper.correct")
    async def helper(msg):
        return {"data": "data"}

    @app.listen("test_encoding.helper.incorrect")
    async def helper(msg):
        return "message not dict"

    @app.listen("test_encoding.message.incorrect")
    async def bar(msg):
        await app.request(subject="test_encoding.helper.correct",
                          message="message not dict")
        return {"success": True}

    @app.listen("test_encoding.message.correct")
    async def bar(msg):
        await app.request(subject="test_encoding.helper.incorrect",
                          message={"data": "some data"})
        return {"success": True}

    @app.listen("test_encoding.correct")
    async def bar(msg):
        await app.request(subject="test_encoding.helper.correct",
                          message={"data": "some data"})
        return {"success": True}

    app.start()
예제 #10
0
def run_panini():
    app = panini_app.App(
        service_name="test_reply_to",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.listen("test_reply_to.start")
    async def reply_to(msg):
        await app.publish(
            message={"data": 1},
            subject="test_reply_to.foo",
            reply_to="test_reply_to.bar",
        )

    @app.listen("test_reply_to.foo")
    async def helper(msg):
        msg.data["data"] += 2
        return msg.data

    app.start()
예제 #11
0
def run_panini():
    app = panini_app.App(
        service_name="test_listen_subject_only_if_include",
        host="127.0.0.1",
        port=4222,
        listen_subject_only_if_include=["foo", "bar"],
        app_strategy="asyncio",
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.listen("test_parameter_listen_subject_only_if_include.start")
    async def start(msg):
        return {"data": 1}

    @app.listen("test_parameter_listen_subject_only_if_include.foo")
    async def foo(msg):
        return {"data": 2}

    @app.listen("test_parameter_listen_subject_only_if_include.bar")
    async def start(msg):
        return {"data": 3}

    app.start()
예제 #12
0
import os
import json

import pytest

from panini.test_client import TestClient, get_logger_files_path
from panini import app as panini_app

testing_logs_directory_path = get_logger_files_path("test_logger_logs",
                                                    remove_if_exist=True)


def run_panini():
    app = panini_app.App(
        service_name="test_logs",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        logger_required=True,
        logger_files_path=testing_logs_directory_path,
        logger_in_separate_process=False,
    )

    log = app.logger

    @app.listen("test_logs.foo")
    async def subject_for_requests(msg):
        log.info(f"Got subject: {msg.subject}", message=msg.data)
        return {"success": True}

    @app.listen("test_logs.foo.*.bar")
예제 #13
0
def run_panini():
    app = panini_app.App(
        service_name="test_sync",
        host="127.0.0.1",
        port=4222,
        app_strategy="sync",
        logger_files_path=get_logger_files_path(),
        logger_in_separate_process=False,
    )

    @app.listen("test_sync.foo")
    def subject_for_requests(msg):
        return {"test": msg.data["test"] + 1}

    @app.listen("test_sync.foo.*.bar")
    def composite_subject_for_requests(msg):
        return {"test": msg.subject + str(msg.data["test"])}

    @app.listen("test_sync.publish")
    def publish(msg):
        app.publish_sync(subject="test_sync.publish.listener",
                         message={"test": msg.data["test"] + 1})

    @app.listen("test_sync.publish.request")
    def publish_request(msg):
        response = app.request_sync(
            subject="test_sync.publish.request.helper",
            message={"test": msg.data["test"] + 1},
        )
        app.publish_sync(
            subject="test_sync.publish.request.listener",
            message={"test": response["test"] + 3},
        )

    @app.listen("test_sync.publish.request.helper")
    def publish_request_helper(msg):
        return {"test": msg.data["test"] + 2}

    @app.listen("test_sync.publish.request.reply")
    def publish_request(msg):
        app.publish_sync(
            subject="test_sync.publish.request.reply.helper",
            message={"test": msg.data["test"] + 1},
            reply_to="test_sync.publish.request.reply.replier",
        )

    @app.listen("test_sync.publish.request.reply.helper")
    def publish_request_helper(msg):
        res = {"test": msg.data["test"] + 1}
        print("Send: ", res)
        return res

    @app.listen("test_sync.publish.request.reply.replier")
    def publish_request_helper(msg):
        app.publish_sync(
            subject="test_sync.publish.request.reply.listener",
            message={"test": msg.data["test"] + 1},
        )

    @app.listen("test_sync.finish")
    def kill(msg):
        if hasattr(app.connector, "nats_listener_process"):
            app.connector.nats_listener_process.terminate()
        if hasattr(app.connector, "nats_sender_process"):
            app.connector.nats_sender_process.terminate()

    app.start()
예제 #14
0
def run_panini():
    app = panini_app.App(
        service_name="test_diff_datatypes",
        host="127.0.0.1",
        port=4222,
        app_strategy="asyncio",
        logger_in_separate_process=False,
        logger_files_path=get_logger_files_path(),
    )

    @app.listen("test_diff_datatypes.listen.dict")
    async def listen_dict(msg):
        return {"type": type(msg.data).__name__}

    @app.listen("test_diff_datatypes.listen.str", data_type=str)
    async def listen_dict(msg):
        return {"type": type(msg.data).__name__}

    @app.listen("test_diff_datatypes.listen.bytes.helper", data_type=bytes)
    async def listen_dict(msg):
        return {"type": type(msg.data).__name__}

    @app.listen("test_diff_datatypes.listen.bytes")
    async def listen_dict(msg):
        try:
            response = await app.request(
                "test_diff_datatypes.listen.bytes.helper",
                b"",
                data_type=bytes)
            return {"type": type(response).__name__}
        except Exception:
            app.logger.exception("test_diff_datatypes.listen.bytes")

    @app.listen("test_diff_datatypes.publish.dict.helper")
    async def listen_dict(msg):
        return {"type": type(msg.data).__name__}

    @app.listen("test_diff_datatypes.publish.dict")
    async def publish_dict(msg):
        await app.publish(
            "test_diff_datatypes.publish.dict.helper",
            {},
            reply_to="test_diff_datatypes.publish.listener",
        )

    @app.listen("test_diff_datatypes.publish.str.helper", data_type=str)
    async def listen_str(msg):
        return {"type": type(msg.data).__name__}

    @app.listen("test_diff_datatypes.publish.str")
    async def publish_str(msg):
        await app.publish(
            "test_diff_datatypes.publish.str.helper",
            "",
            data_type=str,
            reply_to="test_diff_datatypes.publish.listener",
        )

    @app.listen("test_diff_datatypes.publish.bytes.helper", data_type=bytes)
    async def listen_bytes(msg):
        return {"type": type(msg.data).__name__}

    @app.listen("test_diff_datatypes.publish.bytes")
    async def publish_bytes(msg):
        await app.publish(
            "test_diff_datatypes.publish.bytes.helper",
            b"",
            data_type=bytes,
            reply_to="test_diff_datatypes.publish.listener",
        )

    @app.listen("test_diff_datatypes.request.dict")
    async def request_dict(msg):
        response = await app.request("test_diff_datatypes.publish.dict.helper",
                                     {})
        return response

    @app.listen("test_diff_datatypes.request.str")
    async def request_str(msg):
        response = await app.request("test_diff_datatypes.publish.str.helper",
                                     "",
                                     data_type=str)
        return {"type": type(response).__name__}

    @app.listen("test_diff_datatypes.request.bytes")
    async def request_bytes(msg):
        response = await app.request(
            "test_diff_datatypes.publish.bytes.helper", b"", data_type=bytes)
        return {"type": type(response).__name__}

    app.start()