Counter API

A stateful HTTP API with in-memory counters. Demonstrates state management across requests.

Features Used

  • http - Enables the serve() function

Endpoints

MethodPathDescription
GET/API info and available endpoints
GET/healthHealth check endpoint
GET/countersList all counters and their values
GET/counter/:nameGet a specific counter's value
POST/counter/:name/incrementIncrement a counter by 1
POST/counter/:name/decrementDecrement a counter by 1
POST/counter/:name/resetReset a counter to 0

Example Usage

# Get counter (auto-creates with value 0)
curl http://localhost:3000/counter/visitors
# {"name":"visitors","value":0}
 
# Increment
curl -X POST http://localhost:3000/counter/visitors/increment
# {"name":"visitors","value":1}
 
curl -X POST http://localhost:3000/counter/visitors/increment
# {"name":"visitors","value":2}
 
# Decrement
curl -X POST http://localhost:3000/counter/visitors/decrement
# {"name":"visitors","value":1}
 
# List all counters
curl http://localhost:3000/counters
# {"visitors":1}
 
# Reset
curl -X POST http://localhost:3000/counter/visitors/reset
# {"name":"visitors","value":0}

Local Development

Run without installing tish (from this directory; tish repo is ../..):

# Run with interpreter
cargo run -p tishlang--manifest-path ../../Cargo.toml --release --features http -- run src/main.tish --features http
 
# Test incrementing
curl -X POST http://localhost:3000/counter/test/increment

Or with tish installed: tish run src/main.tish --features http

Deploy

Deploy with Zectre: zectre deploy --wait from this directory. See Deploy Overview for details.

Note on State

This example uses in-memory state. Counter values are lost when the process restarts. For production use, consider persisting state to a database or file system.

Improve this documentation