|
| 1 | +mod proto; |
| 2 | + |
| 3 | +use axum::{ |
| 4 | + body::Bytes, |
| 5 | + extract::{Path, State}, |
| 6 | + http::{header, StatusCode}, |
| 7 | + response::IntoResponse, |
| 8 | + routing::{delete, get, post}, |
| 9 | + Router, |
| 10 | +}; |
| 11 | +use proto::{ProtoDecoder, ProtoEncoder}; |
| 12 | +use std::sync::{Arc, Mutex}; |
| 13 | +use std::time::{SystemTime, UNIX_EPOCH}; |
| 14 | + |
| 15 | +// ── Domain ─────────────────────────────────────────────────────────────────── |
| 16 | + |
| 17 | +struct Note { |
| 18 | + id: u32, |
| 19 | + title: String, |
| 20 | + done: bool, |
| 21 | + created_at: u64, |
| 22 | +} |
| 23 | + |
| 24 | +/// Proto field layout (shared with WASM frontend): |
| 25 | +/// Note { 1: uint32 id, 2: string title, 3: bool done, 4: uint64 created_at } |
| 26 | +/// NoteList { 1: repeated Note (sub-msg), 2: uint32 total, 3: uint32 done_count } |
| 27 | +/// CreateReq { 1: string title } |
| 28 | +impl Note { |
| 29 | + fn to_proto(&self) -> ProtoEncoder { |
| 30 | + ProtoEncoder::new() |
| 31 | + .uint32(1, self.id) |
| 32 | + .string(2, &self.title) |
| 33 | + .bool(3, self.done) |
| 34 | + .uint64(4, self.created_at) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn now_ms() -> u64 { |
| 39 | + SystemTime::now() |
| 40 | + .duration_since(UNIX_EPOCH) |
| 41 | + .unwrap() |
| 42 | + .as_millis() as u64 |
| 43 | +} |
| 44 | + |
| 45 | +fn proto_response(status: StatusCode, body: Vec<u8>) -> impl IntoResponse { |
| 46 | + ( |
| 47 | + status, |
| 48 | + [(header::CONTENT_TYPE, "application/protobuf")], |
| 49 | + body, |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +// ── Shared State ───────────────────────────────────────────────────────────── |
| 54 | + |
| 55 | +struct AppState { |
| 56 | + notes: Mutex<Vec<Note>>, |
| 57 | + next_id: Mutex<u32>, |
| 58 | +} |
| 59 | + |
| 60 | +type SharedState = Arc<AppState>; |
| 61 | + |
| 62 | +// ── Handlers ───────────────────────────────────────────────────────────────── |
| 63 | + |
| 64 | +async fn list_notes(State(state): State<SharedState>) -> impl IntoResponse { |
| 65 | + let notes = state.notes.lock().unwrap(); |
| 66 | + let total = notes.len() as u32; |
| 67 | + let done_count = notes.iter().filter(|n| n.done).count() as u32; |
| 68 | + |
| 69 | + let mut enc = ProtoEncoder::new(); |
| 70 | + for note in notes.iter() { |
| 71 | + enc = enc.message(1, ¬e.to_proto()); |
| 72 | + } |
| 73 | + enc = enc.uint32(2, total).uint32(3, done_count); |
| 74 | + |
| 75 | + proto_response(StatusCode::OK, enc.finish()) |
| 76 | +} |
| 77 | + |
| 78 | +async fn create_note( |
| 79 | + State(state): State<SharedState>, |
| 80 | + body: Bytes, |
| 81 | +) -> impl IntoResponse { |
| 82 | + let mut title = String::new(); |
| 83 | + let mut decoder = ProtoDecoder::new(&body); |
| 84 | + while let Some(field) = decoder.next() { |
| 85 | + if field.number == 1 { |
| 86 | + title = field.as_str().to_string(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if title.is_empty() { |
| 91 | + return proto_response( |
| 92 | + StatusCode::BAD_REQUEST, |
| 93 | + ProtoEncoder::new().string(1, "title is required").finish(), |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + let mut next_id = state.next_id.lock().unwrap(); |
| 98 | + let id = *next_id; |
| 99 | + *next_id += 1; |
| 100 | + drop(next_id); |
| 101 | + |
| 102 | + let note = Note { |
| 103 | + id, |
| 104 | + title, |
| 105 | + done: false, |
| 106 | + created_at: now_ms(), |
| 107 | + }; |
| 108 | + let resp = note.to_proto().finish(); |
| 109 | + state.notes.lock().unwrap().push(note); |
| 110 | + |
| 111 | + proto_response(StatusCode::CREATED, resp) |
| 112 | +} |
| 113 | + |
| 114 | +async fn toggle_note( |
| 115 | + State(state): State<SharedState>, |
| 116 | + Path(id): Path<u32>, |
| 117 | +) -> impl IntoResponse { |
| 118 | + let mut notes = state.notes.lock().unwrap(); |
| 119 | + if let Some(note) = notes.iter_mut().find(|n| n.id == id) { |
| 120 | + note.done = !note.done; |
| 121 | + let resp = note.to_proto().finish(); |
| 122 | + proto_response(StatusCode::OK, resp) |
| 123 | + } else { |
| 124 | + proto_response(StatusCode::NOT_FOUND, Vec::new()) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +async fn delete_note( |
| 129 | + State(state): State<SharedState>, |
| 130 | + Path(id): Path<u32>, |
| 131 | +) -> impl IntoResponse { |
| 132 | + let mut notes = state.notes.lock().unwrap(); |
| 133 | + if let Some(pos) = notes.iter().position(|n| n.id == id) { |
| 134 | + let removed = notes.remove(pos); |
| 135 | + let resp = removed.to_proto().finish(); |
| 136 | + proto_response(StatusCode::OK, resp) |
| 137 | + } else { |
| 138 | + proto_response(StatusCode::NOT_FOUND, Vec::new()) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// ── Main ───────────────────────────────────────────────────────────────────── |
| 143 | + |
| 144 | +#[tokio::main] |
| 145 | +async fn main() { |
| 146 | + let state = Arc::new(AppState { |
| 147 | + notes: Mutex::new(vec![ |
| 148 | + Note { |
| 149 | + id: 1, |
| 150 | + title: "Learn the Oxide browser".into(), |
| 151 | + done: true, |
| 152 | + created_at: 1710000000000, |
| 153 | + }, |
| 154 | + Note { |
| 155 | + id: 2, |
| 156 | + title: "Build a WASM guest app".into(), |
| 157 | + done: false, |
| 158 | + created_at: 1710000060000, |
| 159 | + }, |
| 160 | + Note { |
| 161 | + id: 3, |
| 162 | + title: "Deploy to production".into(), |
| 163 | + done: false, |
| 164 | + created_at: 1710000120000, |
| 165 | + }, |
| 166 | + ]), |
| 167 | + next_id: Mutex::new(4), |
| 168 | + }); |
| 169 | + |
| 170 | + let app = Router::new() |
| 171 | + .route("/api/notes", get(list_notes).post(create_note)) |
| 172 | + .route("/api/notes/{id}/toggle", post(toggle_note)) |
| 173 | + .route("/api/notes/{id}", delete(delete_note)) |
| 174 | + .with_state(state); |
| 175 | + |
| 176 | + let addr = "0.0.0.0:3333"; |
| 177 | + println!("notes-server listening on http://{addr}"); |
| 178 | + |
| 179 | + let listener = tokio::net::TcpListener::bind(addr).await.unwrap(); |
| 180 | + axum::serve(listener, app).await.unwrap(); |
| 181 | +} |
0 commit comments