5.0.1 update
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
// HTTP клиент для тестирования FoxLang API
|
||||
include("../src/net.fox");
|
||||
|
||||
void test_api() {
|
||||
print("🧪 Testing FoxLang FastAPI Server");
|
||||
print("=================================");
|
||||
|
||||
string base_url = "http://localhost:8080";
|
||||
|
||||
// Тест GET запросов
|
||||
print("📥 Testing GET endpoints...");
|
||||
|
||||
string home = api_get(base_url, "/");
|
||||
print("GET /: " + home);
|
||||
|
||||
string health = api_get(base_url, "/health");
|
||||
print("GET /health: " + health);
|
||||
|
||||
string users = api_get(base_url, "/users");
|
||||
print("GET /users: " + users);
|
||||
|
||||
string docs = api_get(base_url, "/docs");
|
||||
print("GET /docs: " + docs);
|
||||
|
||||
// Тест POST запроса
|
||||
print("");
|
||||
print("📤 Testing POST endpoint...");
|
||||
|
||||
string user_data = "{\"name\":\"David\",\"role\":\"user\"}";
|
||||
string create_response = api_post(base_url, "/users", user_data);
|
||||
print("POST /users: " + create_response);
|
||||
|
||||
print("");
|
||||
print("✅ API testing completed!");
|
||||
}
|
||||
|
||||
void main() {
|
||||
test_api();
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -0,0 +1,103 @@
|
||||
// Полная демонстрация FastAPI возможностей FoxLang
|
||||
include("src/net.fox");
|
||||
|
||||
// === ОБРАБОТЧИКИ МАРШРУТОВ ===
|
||||
|
||||
void home() {
|
||||
json_response("{\"message\":\"🦊 Welcome to FoxLang FastAPI!\",\"version\":\"5.0.1\",\"features\":[\"HTTP Server\",\"REST API\",\"JSON Support\"]}");
|
||||
}
|
||||
|
||||
void health() {
|
||||
json_response("{\"status\":\"healthy\",\"uptime\":\"running\",\"memory\":\"ok\"}");
|
||||
}
|
||||
|
||||
void users_list() {
|
||||
json_response("{\"users\":[{\"id\":1,\"name\":\"Alice\",\"role\":\"admin\"},{\"id\":2,\"name\":\"Bob\",\"role\":\"user\"},{\"id\":3,\"name\":\"Charlie\",\"role\":\"guest\"}],\"total\":3}");
|
||||
}
|
||||
|
||||
void create_user() {
|
||||
json_response("{\"message\":\"User created successfully\",\"id\":4,\"status\":\"created\"}");
|
||||
}
|
||||
|
||||
void api_docs() {
|
||||
json_response("{\"title\":\"FoxLang API Documentation\",\"version\":\"1.0\",\"endpoints\":[\"/\",\"/health\",\"/users\",\"POST /users\",\"/docs\"]}");
|
||||
}
|
||||
|
||||
// === ГЛАВНАЯ ФУНКЦИЯ ===
|
||||
|
||||
void main() {
|
||||
print("🦊 FoxLang FastAPI Server Demo");
|
||||
print("==============================");
|
||||
print("");
|
||||
|
||||
// Запуск сервера
|
||||
print("🚀 Starting server...");
|
||||
start_server(8080);
|
||||
|
||||
// Регистрация маршрутов
|
||||
print("📋 Registering routes...");
|
||||
register_get("/", "home");
|
||||
register_get("/health", "health");
|
||||
register_get("/users", "users_list");
|
||||
register_get("/docs", "api_docs");
|
||||
register_post("/users", "create_user");
|
||||
|
||||
print("");
|
||||
print("✅ Server is ready!");
|
||||
print("🌐 Base URL: http://localhost:8080");
|
||||
print("");
|
||||
print("📋 Available endpoints:");
|
||||
print(" GET / - Welcome & API info");
|
||||
print(" GET /health - Health check");
|
||||
print(" GET /users - List all users");
|
||||
print(" GET /docs - API documentation");
|
||||
print(" POST /users - Create new user");
|
||||
print("");
|
||||
print("🧪 Testing endpoints in 3 seconds...");
|
||||
|
||||
// Ждем запуска сервера
|
||||
wait(3000);
|
||||
|
||||
// Тестирование API
|
||||
print("🔍 Testing API endpoints:");
|
||||
print("");
|
||||
|
||||
string base = "http://localhost:8080";
|
||||
|
||||
print("📥 GET /");
|
||||
string home_resp = api_get(base, "/");
|
||||
print("Response: " + home_resp);
|
||||
print("");
|
||||
|
||||
print("📥 GET /health");
|
||||
string health_resp = api_get(base, "/health");
|
||||
print("Response: " + health_resp);
|
||||
print("");
|
||||
|
||||
print("📥 GET /users");
|
||||
string users_resp = api_get(base, "/users");
|
||||
print("Response: " + users_resp);
|
||||
print("");
|
||||
|
||||
print("📤 POST /users");
|
||||
string create_resp = api_post(base, "/users", "{\"name\":\"David\",\"role\":\"user\"}");
|
||||
print("Response: " + create_resp);
|
||||
print("");
|
||||
|
||||
print("📥 GET /docs");
|
||||
string docs_resp = api_get(base, "/docs");
|
||||
print("Response: " + docs_resp);
|
||||
print("");
|
||||
|
||||
print("✅ All tests completed!");
|
||||
print("");
|
||||
print("🎯 Try these curl commands:");
|
||||
print(" curl http://localhost:8080/");
|
||||
print(" curl http://localhost:8080/users");
|
||||
print(" curl -X POST http://localhost:8080/users -d '{\"name\":\"Eve\"}'");
|
||||
print("");
|
||||
print("⏹️ To stop server: stop_server() or Ctrl+C");
|
||||
}
|
||||
|
||||
// Запуск демо
|
||||
main();
|
||||
@@ -0,0 +1,68 @@
|
||||
// Пример FastAPI-подобного сервера на FoxLang
|
||||
include("../src/net.fox");
|
||||
|
||||
// Обработчики маршрутов
|
||||
void api_home() {
|
||||
json_response("{\"message\":\"🦊 Welcome to FoxLang FastAPI!\",\"version\":\"5.0.1\",\"docs\":\"/docs\"}");
|
||||
}
|
||||
|
||||
void api_health() {
|
||||
json_response("{\"status\":\"healthy\",\"timestamp\":\"2026-01-22\",\"uptime\":\"running\"}");
|
||||
}
|
||||
|
||||
void api_users() {
|
||||
json_response("{\"users\":[{\"id\":1,\"name\":\"Alice\",\"role\":\"admin\"},{\"id\":2,\"name\":\"Bob\",\"role\":\"user\"},{\"id\":3,\"name\":\"Charlie\",\"role\":\"guest\"}]}");
|
||||
}
|
||||
|
||||
void api_create_user() {
|
||||
text_response("User created successfully with ID: 4");
|
||||
}
|
||||
|
||||
void api_docs() {
|
||||
json_response("{\"endpoints\":[\"/\",\"/health\",\"/users\",\"POST /users\",\"/docs\"]}");
|
||||
}
|
||||
|
||||
// Главная функция
|
||||
void main() {
|
||||
print("🦊 FoxLang FastAPI Server Example");
|
||||
print("==================================");
|
||||
|
||||
// Запуск сервера на порту 8080
|
||||
start_server(8080);
|
||||
|
||||
// Регистрация маршрутов
|
||||
get("/", "api_home");
|
||||
get("/health", "api_health");
|
||||
get("/users", "api_users");
|
||||
get("/docs", "api_docs");
|
||||
post("/users", "api_create_user");
|
||||
|
||||
print("");
|
||||
print("✅ Server is running with the following endpoints:");
|
||||
print(" GET / - Welcome message");
|
||||
print(" GET /health - Health check");
|
||||
print(" GET /users - List all users");
|
||||
print(" GET /docs - API documentation");
|
||||
print(" POST /users - Create new user");
|
||||
print("");
|
||||
print("🌐 Open in browser: http://localhost:8080");
|
||||
print("🧪 Test with curl:");
|
||||
print(" curl http://localhost:8080/");
|
||||
print(" curl http://localhost:8080/users");
|
||||
print(" curl -X POST http://localhost:8080/users");
|
||||
print("");
|
||||
print("⏹️ Press Ctrl+C to stop the server");
|
||||
|
||||
// Демонстрация клиентских запросов
|
||||
print("🧪 Testing endpoints...");
|
||||
wait(3000); // Ждем запуска сервера
|
||||
|
||||
string home_response = http_get_simple("http://localhost:8080/");
|
||||
print("Home endpoint: " + home_response);
|
||||
|
||||
string users_response = http_get_simple("http://localhost:8080/users");
|
||||
print("Users endpoint: " + users_response);
|
||||
}
|
||||
|
||||
// Запуск сервера
|
||||
main();
|
||||
@@ -0,0 +1,42 @@
|
||||
// Примеры HTTP запросов в FoxLang
|
||||
|
||||
void example_api_calls() {
|
||||
print("🚀 FoxLang HTTP Client Examples");
|
||||
print("===============================");
|
||||
|
||||
// 1. Простой GET запрос
|
||||
print("1️⃣ GET запрос к JSONPlaceholder API:");
|
||||
string user = httpget("https://jsonplaceholder.typicode.com/users/1");
|
||||
print("User data: " + user);
|
||||
print("");
|
||||
|
||||
// 2. POST запрос с JSON данными
|
||||
print("2️⃣ POST запрос (создание поста):");
|
||||
string post_data = "{\"title\":\"FoxLang Post\",\"body\":\"Created with FoxLang!\",\"userId\":1}";
|
||||
string new_post = httppost("https://jsonplaceholder.typicode.com/posts", post_data, "application/json");
|
||||
print("Created post: " + new_post);
|
||||
print("");
|
||||
|
||||
// 3. PUT запрос (обновление)
|
||||
print("3️⃣ PUT запрос (обновление поста):");
|
||||
string update_data = "{\"id\":1,\"title\":\"Updated by FoxLang\",\"body\":\"Modified content\",\"userId\":1}";
|
||||
string updated = httpput("https://jsonplaceholder.typicode.com/posts/1", update_data, "application/json");
|
||||
print("Updated post: " + updated);
|
||||
print("");
|
||||
|
||||
// 4. DELETE запрос
|
||||
print("4️⃣ DELETE запрос (удаление поста):");
|
||||
string deleted = httpdelete("https://jsonplaceholder.typicode.com/posts/1");
|
||||
print("Delete response: " + deleted);
|
||||
print("");
|
||||
|
||||
// 5. Работа с GitHub API
|
||||
print("5️⃣ GitHub API запрос:");
|
||||
string github_user = httpget("https://api.github.com/users/octocat");
|
||||
print("GitHub user: " + github_user);
|
||||
print("");
|
||||
|
||||
print("✅ Все примеры выполнены!");
|
||||
}
|
||||
|
||||
example_api_calls();
|
||||
@@ -0,0 +1,36 @@
|
||||
// Демонстрация всех HTTP возможностей FoxLang
|
||||
|
||||
void demo_http_client() {
|
||||
print("🌐 HTTP Client Demo");
|
||||
print("==================");
|
||||
|
||||
// GET запрос
|
||||
print("📥 GET request to JSONPlaceholder:");
|
||||
string user = httpget("https://jsonplaceholder.typicode.com/users/1");
|
||||
print("User: " + user);
|
||||
print("");
|
||||
|
||||
// POST запрос
|
||||
print("📤 POST request (create post):");
|
||||
string post_data = "{\"title\":\"FoxLang Post\",\"body\":\"Hello from FoxLang!\",\"userId\":1}";
|
||||
string new_post = httppost("https://jsonplaceholder.typicode.com/posts", post_data);
|
||||
print("Created: " + new_post);
|
||||
print("");
|
||||
|
||||
// PUT запрос
|
||||
print("🔄 PUT request (update post):");
|
||||
string update_data = "{\"id\":1,\"title\":\"Updated by FoxLang\",\"body\":\"Modified content\",\"userId\":1}";
|
||||
string updated = httpput("https://jsonplaceholder.typicode.com/posts/1", update_data);
|
||||
print("Updated: " + updated);
|
||||
print("");
|
||||
|
||||
// DELETE запрос
|
||||
print("🗑️ DELETE request:");
|
||||
string deleted = httpdelete("https://jsonplaceholder.typicode.com/posts/1");
|
||||
print("Deleted: " + deleted);
|
||||
print("");
|
||||
|
||||
print("✅ HTTP Client demo completed!");
|
||||
}
|
||||
|
||||
demo_http_client();
|
||||
@@ -0,0 +1,60 @@
|
||||
// Простые HTTP функции для удобства
|
||||
|
||||
// GET запрос с обработкой ошибок
|
||||
string http_get(string url) {
|
||||
string response = httpget(url);
|
||||
if (response == "") {
|
||||
print("❌ GET failed: " + url);
|
||||
return "{}";
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
// POST запрос с JSON
|
||||
string http_post(string url, string json_data) {
|
||||
string response = httppost(url, json_data, "application/json");
|
||||
if (response == "") {
|
||||
print("❌ POST failed: " + url);
|
||||
return "{}";
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
// PUT запрос с JSON
|
||||
string http_put(string url, string json_data) {
|
||||
string response = httpput(url, json_data, "application/json");
|
||||
if (response == "") {
|
||||
print("❌ PUT failed: " + url);
|
||||
return "{}";
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
// DELETE запрос
|
||||
string http_delete(string url) {
|
||||
string response = httpdelete(url);
|
||||
if (response == "") {
|
||||
print("❌ DELETE failed: " + url);
|
||||
return "{}";
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
// Тест простых функций
|
||||
void test_simple_http() {
|
||||
print("🧪 Testing simple HTTP functions:");
|
||||
|
||||
string user = http_get("https://jsonplaceholder.typicode.com/users/1");
|
||||
print("GET: " + user);
|
||||
|
||||
string new_post = http_post("https://jsonplaceholder.typicode.com/posts", "{\"title\":\"Test\"}");
|
||||
print("POST: " + new_post);
|
||||
|
||||
string updated = http_put("https://jsonplaceholder.typicode.com/posts/1", "{\"title\":\"Updated\"}");
|
||||
print("PUT: " + updated);
|
||||
|
||||
string deleted = http_delete("https://jsonplaceholder.typicode.com/posts/1");
|
||||
print("DELETE: " + deleted);
|
||||
}
|
||||
|
||||
test_simple_http();
|
||||
Reference in New Issue
Block a user