httpserver

command
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2025 License: MIT Imports: 12 Imported by: 0

README

HTTP Server Example

Demonstrates using graft's node-level caching for HTTP servers where global resources (config, DB) should initialize once while request-scoped nodes run fresh per request.

Dependency Graph

        config (cached)
           │
    ┌──────┴──────┐
    ▼             ▼
   db          admin
 (cached)    (per-request)
    │             │
    ▼             │
  user ◀──────────┤
(per-request)     │
    │             │
    └─────┬───────┘
          ▼
   request_logger
    (per-request)

How It Works

Nodes declare Cacheable: true to be cached after first execution:

graft.Register(graft.Node[Output]{
    ID:        "config",
    DependsOn: []graft.ID{},
    Run:       run,
    Cacheable: true, // executed once, then served from cache
})

Request-scoped nodes omit Cacheable and run fresh every time.

Running

go run .

Make requests:

curl http://localhost:8080/user/123
curl http://localhost:8080/user/456
curl http://localhost:8080/stats

Expected Behavior

=== Request req-1: GET /user/123 ===
[config] Executing (execution #1)
[db] Connecting (execution #1)
[request_logger] Logging req-1
[user] Fetching user 123

=== Request req-2: GET /user/456 ===
[request_logger] Logging req-2      ← config/db served from cache
[user] Fetching user 456

=== Stats after 5 requests ===
{
  "total_requests": 5,
  "config_executions": 1,    ← cached!
  "db_executions": 1         ← cached!
}

Config and DB execute once; request-scoped nodes run every request.

Documentation

Overview

Package main demonstrates using graft with node-level caching for HTTP servers.

Nodes declare themselves as cacheable via Cacheable: true. Only cacheable nodes are stored in the cache - request-scoped nodes run fresh every time.

Run this example and make a few requests:

curl http://localhost:8080/user/123
curl http://localhost:8080/user/456
curl http://localhost:8080/admin
curl http://localhost:8080/stats

You'll see that config and db (Cacheable: true) execute only ONCE, while request-scoped nodes execute on every request.

Directories

Path Synopsis
nodes
db

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL