Workspace IndexKnowledge Notes › Web stack layers

#199PoC

Web stack layers

A five-layer map of the stack, with this project overlaid on it.

Reference — docs/knowledge/web-stack-layers.html.

Why

An orientation map rather than a study: which layer each piece of this project actually lives at, and where the gaps are. Useful mostly for noticing that several cards which sound like different problems turn out to sit at the same layer — and that one or two layers have nothing on them at all.

How it works

A static five-layer diagram with the project's routes and demos placed on it. No code.

Related code

"""Web stack layers PoC -- a request flowing through a chain of middleware layers.
Illustrates the core mechanism: each layer wraps the next, adding something on the way
in and/or the way out, and the order in which layers run is visible in the output.
"""

from typing import Callable

Handler = Callable[[dict], dict]


def logging_layer(next_layer: Handler) -> Handler:
    def handle(request: dict) -> dict:
        print(f"  [logging]  in:  {request['path']}")
        response = next_layer(request)
        print(f"  [logging]  out: status={response['status']}")
        return response
    return handle


def auth_layer(next_layer: Handler) -> Handler:
    def handle(request: dict) -> dict:
        print(f"  [auth]     checking token for {request['path']}")
        request["user"] = "jay"
        return next_layer(request)
    return handle


def cache_layer(cache: dict) -> Callable[[Handler], Handler]:
    def wrap(next_layer: Handler) -> Handler:
        def handle(request: dict) -> dict:
            if request["path"] in cache:
                print(f"  [cache]    hit for {request['path']}")
                return cache[request["path"]]
            print(f"  [cache]    miss for {request['path']}")
            response = next_layer(request)
            cache[request["path"]] = response
            return response
        return handle
    return wrap


def app_layer(request: dict) -> dict:
    print(f"  [app]      handling {request['path']} for user={request.get('user')}")
    return {"status": 200, "body": f"hello, {request.get('user')}"}


if __name__ == "__main__":
    cache: dict = {}
    # Layers compose from the outside in: logging -> auth -> cache -> app.
    stack = logging_layer(auth_layer(cache_layer(cache)(app_layer)))

    print("request 1 (/profile):")
    stack({"path": "/profile"})

    print("\nrequest 2 (/profile again -- cache should hit):")
    stack({"path": "/profile"})

Where it lands in Jayverse

  • gitboard: build the same five-layer map for Jayverse's own services. Placing Rabbit, Verex, Wallet, Bridge, Number, and Devnet on one stack diagram the way this page does for its own project would surface which layers have nothing built on them yet.
  • Devnet: use the map to spot the layer with the fewest services depending on it. An orientation diagram is cheap to build, and it would show whether Devnet is actually load-bearing for every service or just some of them.

Key expressions

Words and phrases from this page worth keeping, with the Korean meaning and the sentence they come from.

Expression뜻 · 쓰이는 자리
overlaid on~ 위에 겹쳐 놓인 · 프로젝트 구성요소를 기존 지도 위에 포개어 표시할 때. "with this project overlaid on it"
orientation map방향을 잡아주는 지도, 개요도 · 깊이 있는 학습 자료가 아니라 전체 구조를 훑어보는 자료임을 밝힐 때. "An orientation map rather than a study"
sit at the same layer같은 계층에 위치하다 · 겉으로 다른 문제처럼 보이는 것들이 실은 같은 층에 속할 때. "turn out to sit at the same layer"
have nothing on~에 아무것도 없다, 해당 항목이 비어 있다 · 지도의 특정 칸이 채워지지 않은 것을 가리킬 때. "one or two layers have nothing on them at all"

← All Knowledge Notes · Workspace Index · Top ↑

웹 스택 계층

스택의 5계층 지도 위에 이 프로젝트를 얹어 본 것.

참조 — docs/knowledge/web-stack-layers.html.

스터디라기보다 방향 지도입니다: 이 프로젝트의 각 조각이 실제로 어느 계층에 사는지, 그리고 빈 곳은 어디인지. 서로 다른 문제처럼 들리던 카드 여럿이 사실 같은 계층에 앉아 있다는 것, 그리고 어떤 계층은 아예 비어 있다는 것을 알아차리는 데 주로 쓸모가 있습니다.

동작 방식

프로젝트의 라우트와 데모를 얹은 정적 5계층 다이어그램. 코드는 없습니다.

관련 코드

"""Web stack layers PoC -- a request flowing through a chain of middleware layers.
Illustrates the core mechanism: each layer wraps the next, adding something on the way
in and/or the way out, and the order in which layers run is visible in the output.
"""

from typing import Callable

Handler = Callable[[dict], dict]


def logging_layer(next_layer: Handler) -> Handler:
    def handle(request: dict) -> dict:
        print(f"  [logging]  in:  {request['path']}")
        response = next_layer(request)
        print(f"  [logging]  out: status={response['status']}")
        return response
    return handle


def auth_layer(next_layer: Handler) -> Handler:
    def handle(request: dict) -> dict:
        print(f"  [auth]     checking token for {request['path']}")
        request["user"] = "jay"
        return next_layer(request)
    return handle


def cache_layer(cache: dict) -> Callable[[Handler], Handler]:
    def wrap(next_layer: Handler) -> Handler:
        def handle(request: dict) -> dict:
            if request["path"] in cache:
                print(f"  [cache]    hit for {request['path']}")
                return cache[request["path"]]
            print(f"  [cache]    miss for {request['path']}")
            response = next_layer(request)
            cache[request["path"]] = response
            return response
        return handle
    return wrap


def app_layer(request: dict) -> dict:
    print(f"  [app]      handling {request['path']} for user={request.get('user')}")
    return {"status": 200, "body": f"hello, {request.get('user')}"}


if __name__ == "__main__":
    cache: dict = {}
    # Layers compose from the outside in: logging -> auth -> cache -> app.
    stack = logging_layer(auth_layer(cache_layer(cache)(app_layer)))

    print("request 1 (/profile):")
    stack({"path": "/profile"})

    print("\nrequest 2 (/profile again -- cache should hit):")
    stack({"path": "/profile"})

Jayverse에서의 위치

  • gitboard: Jayverse 자체 서비스로 같은 5계층 지도를 만든다. Rabbit, Verex, Wallet, Bridge, Number, Devnet을 이 글이 자기 프로젝트에 하듯 하나의 스택 다이어그램에 배치하면 아직 아무것도 없는 계층이 드러난다.
  • Devnet: 이 지도로 의존하는 서비스가 가장 적은 계층을 찾는다. 오리엔테이션 다이어그램은 만들기 싸고, Devnet이 모든 서비스에 실제로 짐을 지는지 일부에만 그런지 보여준다.

핵심 표현

이 페이지의 영어 본문에서 배울 만한 단어와 표현, 뜻과 나온 자리.

Expression뜻 · 쓰이는 자리
overlaid on~ 위에 겹쳐 놓인 · 프로젝트 구성요소를 기존 지도 위에 포개어 표시할 때. "with this project overlaid on it"
orientation map방향을 잡아주는 지도, 개요도 · 깊이 있는 학습 자료가 아니라 전체 구조를 훑어보는 자료임을 밝힐 때. "An orientation map rather than a study"
sit at the same layer같은 계층에 위치하다 · 겉으로 다른 문제처럼 보이는 것들이 실은 같은 층에 속할 때. "turn out to sit at the same layer"
have nothing on~에 아무것도 없다, 해당 항목이 비어 있다 · 지도의 특정 칸이 채워지지 않은 것을 가리킬 때. "one or two layers have nothing on them at all"

← 전체 기술 노트 · 워크스페이스 인덱스 · 맨 위 ↑