This repository of code examples provides reference implementations for building, extending, and operating systems within the Hanzo ecosystem.
Category: Hanzo Ecosystem & Reference Examples Languages: Go, Rust, C++ (Metal/CUDA) Related Skills: hanzo/hanzo-migrate-backend-to-go.md, hanzo/hanzo-coursework-fullstack-go-cloud.md, hanzo/hanzo-tutorial-realtime-sse-backend.md
This repository of code examples provides reference implementations for building, extending, and operating systems within the Hanzo ecosystem. We enforce a strict native code only policy:
hanzoai/reth), tokenizers, and payment switches.A single-file Go service running an embedded database, REST API, and native Server-Sent Events:
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
_ "modernc.org/sqlite"
)
type Item struct {
ID int64 `json:"id"`
Title string `json:"title"`
CreatedAt string `json:"created_at"`
}
func setupDB() *sql.DB {
db, err := sql.Open("sqlite", "file:hanzo_base.db?cache=shared&mode=rwc")
if err != nil {
log.Fatalf("Failed to open SQLite: %v", err)
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`)
if err != nil {
log.Fatalf("Failed to migrate table: %v", err)
}
return db
}
func main() {
db := setupDB()
mux := http.NewServeMux()
// Items REST API
mux.HandleFunc("POST /v1/items", func(w http.ResponseWriter, r *http.Request) {
var req struct {
Title string `json:"title"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
res, err := db.Exec("INSERT INTO items (title) VALUES (?)", req.Title)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id, _ := res.LastInsertId()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(Item{ID: id, Title: req.Title, CreatedAt: time.Now().Format(time.RFC3339)})
})
// Realtime SSE Stream
mux.HandleFunc("GET /v1/items/stream", func(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "SSE not supported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
for {
select {
case <-r.Context().Done():
return
case t := <-ticker.C:
fmt.Fprintf(w, "data: {\"heartbeat\": \"%s\"}\n\n", t.Format(time.RFC3339))
flusher.Flush()
}
}
})
log.Println("⚡ Hanzo Base service listening on http://127.0.0.1:8080")
http.ListenAndServe(":8080", mux)
}
A native Rust service leveraging Tokio and Rayon for zero-copy batch processing:
use axum::{
extract::State,
routing::post,
Json, Router,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::net::TcpListener;
#[derive(Deserialize)]
struct InferenceRequest {
prompt: String,
max_tokens: usize,
}
#[derive(Serialize)]
struct InferenceResponse {
model: &'static str,
output: String,
tokens_generated: usize,
latency_ms: f64,
}
struct EngineContext {
model_name: &'static str,
}
async fn handle_inference(
State(ctx): State<Arc<EngineContext>>,
Json(payload): Json<InferenceRequest>,
) -> Json<InferenceResponse> {
let start = std::time::Instant::now();
// High performance token processing
let generated = format!("Completed prompt: '{}'", payload.prompt);
let latency = start.elapsed().as_secs_f64() * 1000.0;
Json(InferenceResponse {
model: ctx.model_name,
output: generated,
tokens_generated: payload.max_tokens,
latency_ms: latency,
})
}
#[tokio::main]
async fn main() {
let state = Arc::new(EngineContext {
model_name: "zen-3.5-flash",
});
let app = Router::new()
.route("/v1/models/generate", post(handle_inference))
.with_state(state);
let listener = TcpListener::bind("127.0.0.1:9095").await.unwrap();
println!("⚡ Rust Engine listening on http://127.0.0.1:9095");
axum::serve(listener, app).await.unwrap();
}
Low-level tensor kernel hook for local inference on macOS Apple Silicon:
#include <iostream>
#include <vector>
#import <Metal/Metal.h>
const char* kernelSource = R"(
#include <metal_stdlib>
using namespace metal;
kernel void vector_add(
device const float* inA [[buffer(0)]],
device const float* inB [[buffer(1)]],
device float* result [[buffer(2)]],
uint id [[thread_position_in_grid]])
{
result[id] = inA[id] + inB[id];
}
)";
int main() {
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
if (!device) {
std::cerr << "Metal is not supported on this device.\n";
return 1;
}
NSError* error = nil;
id<MTLLibrary> library = [device newLibraryWithSource:[NSString stringWithUTF8String:kernelSource]
options:nil
error:&error];
if (!library) {
std::cerr << "Failed to compile Metal shader: " << [error.localizedDescription UTF8String] << "\n";
return 1;
}
id<MTLFunction> function = [library newFunctionWithName:@"vector_add"];
id<MTLComputePipelineState> pipeline = [device newComputePipelineStateWithFunction:function error:&error];
const unsigned int count = 1048576; // 1M floats
const size_t bytes = count * sizeof(float);
id<MTLBuffer> bufferA = [device newBufferWithLength:bytes options:MTLResourceStorageModeShared];
id<MTLBuffer> bufferB = [device newBufferWithLength:bytes options:MTLResourceStorageModeShared];
id<MTLBuffer> bufferResult = [device newBufferWithLength:bytes options:MTLResourceStorageModeShared];
id<MTLCommandQueue> commandQueue = [device newCommandQueue];
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
id<MTLComputeCommandEncoder> encoder = [commandBuffer computeCommandEncoder];
[encoder setComputePipelineState:pipeline];
[encoder setBuffer:bufferA offset:0 atIndex:0];
[encoder setBuffer:bufferB offset:0 atIndex:1];
[encoder setBuffer:bufferResult offset:0 atIndex:2];
MTLSize gridSize = MTLSizeMake(count, 1, 1);
NSUInteger threadGroupSize = pipeline.maxTotalThreadsPerThreadgroup;
if (threadGroupSize > count) threadGroupSize = count;
MTLSize threadgroupSize = MTLSizeMake(threadGroupSize, 1, 1);
[encoder dispatchThreads:gridSize threadsPerThreadgroup:threadgroupSize];
[encoder endEncoding];
[commandBuffer commit];
[commandBuffer waitUntilCompleted];
std::cout << "⚡ Metal tensor dispatch completed for " << count << " elements on "
<< [[device name] UTF8String] << std::endl;
return 0;
}
compose.yml)The standard multi-service native stack composition:
# compose.yml
services:
cloud:
image: ghcr.io/hanzoai/cloud:latest
ports:
- "8080:8080"
environment:
- HANZO_ENV=local
- HANZO_STORAGE=sqlite
- HANZO_SQLITE_PATH=/data/cloud.db
volumes:
- cloud-data:/data
base:
image: ghcr.io/hanzoai/base:latest
ports:
- "8090:8090"
environment:
- BASE_PORT=8090
- BASE_DB=/data/base.db
- BASE_REALTIME_SSE=true
volumes:
- base-data:/data
volumes:
cloud-data:
base-data:
Zero-dependency native client streaming:
// client.ts
export function subscribeToHanzoStream(url: string, onData: (data: any) => void) {
const source = new EventSource(url);
source.onmessage = (event) => {
try {
const payload = JSON.parse(event.data);
onData(payload);
} catch (e) {
console.error("Failed to parse event JSON:", e);
}
};
source.onerror = (err) => {
console.warn("SSE connection dropped, retrying...", err);
};
return () => source.close();
}