| 128 | // ============================================================================ |
| 129 | |
| 130 | static bool startHttpServer() { |
| 131 | if (s_http_server.get()) { |
| 132 | return true; // Already running |
| 133 | } |
| 134 | |
| 135 | s_http_server = fl::make_unique<fl::asio::http::Server>(); |
| 136 | |
| 137 | // Register routes using the unified API |
| 138 | s_http_server->get("/ping", [](const fl::asio::http::Request&) { |
| 139 | return fl::asio::http::Response::ok("pong"); |
| 140 | }); |
| 141 | |
| 142 | s_http_server->get("/status", [](const fl::asio::http::Request&) { |
| 143 | fl::json json = fl::json::object(); |
| 144 | json.set("uptime_ms", static_cast<int64_t>(millis())); |
| 145 | json.set("free_heap", static_cast<int64_t>(ESP.getFreeHeap())); |
| 146 | #if defined(FL_IS_ESP_32S3) |
| 147 | json.set("chip", "esp32s3"); |
| 148 | #elif defined(FL_IS_ESP_32C6) |
| 149 | json.set("chip", "esp32c6"); |
| 150 | #elif defined(FL_IS_ESP_32C3) |
| 151 | json.set("chip", "esp32c3"); |
| 152 | #else |
| 153 | json.set("chip", "esp32"); |
| 154 | #endif |
| 155 | fl::asio::http::Response resp; |
| 156 | resp.json(json); |
| 157 | return resp; |
| 158 | }); |
| 159 | |
| 160 | s_http_server->post("/echo", [](const fl::asio::http::Request& req) { |
| 161 | if (!req.has_body()) { |
| 162 | return fl::asio::http::Response::bad_request("No body"); |
| 163 | } |
| 164 | return fl::asio::http::Response::ok(req.body()); |
| 165 | }); |
| 166 | |
| 167 | s_http_server->get("/leds", [](const fl::asio::http::Request&) { |
| 168 | fl::json json = fl::json::object(); |
| 169 | json.set("num_leds", static_cast<int64_t>(10)); |
| 170 | json.set("brightness", static_cast<int64_t>(64)); |
| 171 | fl::asio::http::Response resp; |
| 172 | resp.json(json); |
| 173 | return resp; |
| 174 | }); |
| 175 | |
| 176 | if (!s_http_server->start(s_net_state.server_port)) { |
| 177 | FL_WARN("[NET] HTTP server failed to start: " << s_http_server->last_error().c_str()); |
| 178 | s_http_server.reset(); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | s_net_state.http_server_active = true; |
| 183 | FL_WARN("[NET] HTTP server started on port " << s_net_state.server_port); |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | // ============================================================================ |