| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- using Astralis;
- using Invercargill;
- using Invercargill.DataStructures;
- /**
- * PathRouting Example
- *
- * Demonstrates path component parsing and routing in Astralis.
- * Uses the EndpointRouter's named segment feature for dynamic routing.
- */
- // Root handler - shows available endpoints
- class RootEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route) throws Error {
- return new HttpStringResult("""Welcome to the Path Routing Example!
- Available endpoints:
- GET / - This message
- GET /hello - Simple greeting
- GET /hello/{name} - Greeting with name
- GET /users - List all users
- GET /users/{id} - Get user by ID
- GET /users/{id}/posts - Get posts for a user
- GET /api/v1/status - API status
- GET /api/v1/items/{id} - Get item by ID
- GET /files/{category}/{name} - Get file by category and name
- GET /pathinfo - Path information demo
- """);
- }
- }
- // Simple hello handler
- class HelloEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route) throws Error {
- return new HttpStringResult("Hello, World!");
- }
- }
- // Hello with name handler - uses named segment
- class HelloNameEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route_info) throws Error {
- string? name = null;
- route_info.mapped_parameters.try_get("name", out name);
- return new HttpStringResult(@"Hello, $(name ?? "Unknown")!");
- }
- }
- // Users list handler
- class UsersEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route) throws Error {
- var users = new Series<User>();
- users.add(new User(1, "Alice", "alice@example.com"));
- users.add(new User(2, "Bob", "bob@example.com"));
- users.add(new User(3, "Charlie", "charlie@example.com"));
-
- var json_parts = new Series<string>();
- json_parts.add(@"{ \"users\": [");
-
- bool first = true;
- users.to_immutable_buffer().iterate((user) => {
- if (!first) json_parts.add(", ");
- json_parts.add(user.to_json());
- first = false;
- });
-
- json_parts.add("] }");
-
- var json_string = json_parts.to_immutable_buffer()
- .aggregate<string>("", (acc, s) => acc + s);
-
- return new HttpStringResult(json_string)
- .set_header("Content-Type", "application/json");
- }
- }
- // User by ID handler - uses named segment
- class UserByIdEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route_info) throws Error {
- string? id_str = null;
- route_info.mapped_parameters.try_get("id", out id_str);
- var id = int.parse(id_str ?? "0");
-
- // Simulated user lookup
- var users = new Series<User>();
- users.add(new User(1, "Alice", "alice@example.com"));
- users.add(new User(2, "Bob", "bob@example.com"));
- users.add(new User(3, "Charlie", "charlie@example.com"));
-
- var user = users.to_immutable_buffer()
- .first_or_default(u => u.id == id);
-
- if (user == null) {
- return new HttpStringResult(@"{ \"error\": \"User not found\" }")
- .set_header("Content-Type", "application/json");
- }
-
- return new HttpStringResult(user.to_json())
- .set_header("Content-Type", "application/json");
- }
- }
- // User posts handler - uses named segment
- class UserPostsEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route_info) throws Error {
- string? id_str = null;
- route_info.mapped_parameters.try_get("id", out id_str);
- var id = int.parse(id_str ?? "0");
-
- // Simulated posts for user
- var posts = new Series<Post>();
- posts.add(new Post(101, id, "First Post", "This is my first post"));
- posts.add(new Post(102, id, "Second Post", "This is my second post"));
-
- var json_parts = new Series<string>();
- json_parts.add(@"{ \"user_id\": $id, \"posts\": [");
-
- bool first = true;
- posts.to_immutable_buffer().iterate((post) => {
- if (!first) json_parts.add(", ");
- json_parts.add(post.to_json());
- first = false;
- });
-
- json_parts.add("] }");
-
- var json_string = json_parts.to_immutable_buffer()
- .aggregate<string>("", (acc, s) => acc + s);
-
- return new HttpStringResult(json_string)
- .set_header("Content-Type", "application/json");
- }
- }
- // API status handler
- class ApiStatusEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route) throws Error {
- var status = new Dictionary<string, string>();
- status.set("status", "operational");
- status.set("version", "1.0.0");
- status.set("timestamp", new DateTime.now_local().format_iso8601());
-
- var json_parts = new Series<string>();
- json_parts.add("{ ");
-
- bool first = true;
- status.to_immutable_buffer().iterate((kv) => {
- if (!first) json_parts.add(", ");
- json_parts.add(@"\"$(kv.key)\": \"$(kv.value)\"");
- first = false;
- });
-
- json_parts.add(" }");
-
- var json_string = json_parts.to_immutable_buffer()
- .aggregate<string>("", (acc, s) => acc + s);
-
- return new HttpStringResult(json_string)
- .set_header("Content-Type", "application/json");
- }
- }
- // API item handler - uses named segment
- class ApiItemEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route_info) throws Error {
- string? id_str = null;
- route_info.mapped_parameters.try_get("id", out id_str);
- var id = int.parse(id_str ?? "0");
-
- var items = new Dictionary<int, Item>();
- items.set(1, new Item(1, "Widget", "A useful widget", 9.99));
- items.set(2, new Item(2, "Gadget", "A fancy gadget", 19.99));
- items.set(3, new Item(3, "Doohickey", "A mysterious doohickey", 29.99));
-
- Item? item = null;
- if (items.try_get(id, out item)) {
- return new HttpStringResult(item.to_json())
- .set_header("Content-Type", "application/json");
- }
-
- return new HttpStringResult(@"{ \"error\": \"Item not found\" }")
- .set_header("Content-Type", "application/json");
- }
- }
- // Files handler - uses two named segments
- class FilesEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route_info) throws Error {
- string? category = null;
- string? name = null;
- route_info.mapped_parameters.try_get("category", out category);
- route_info.mapped_parameters.try_get("name", out name);
-
- // Simulated file lookup
- var files = new Dictionary<string, Dictionary<string, ExampleFile>>();
- var docs = new Dictionary<string, ExampleFile>();
- docs.set("readme.txt", new ExampleFile("readme.txt", "docs", "This is the readme file"));
- docs.set("guide.pdf", new ExampleFile("guide.pdf", "docs", "User guide"));
-
- var images = new Dictionary<string, ExampleFile>();
- images.set("logo.png", new ExampleFile("logo.png", "images", "Company logo"));
- images.set("banner.jpg", new ExampleFile("banner.jpg", "images", "Website banner"));
-
- files.set("docs", docs);
- files.set("images", images);
-
- Dictionary<string, ExampleFile>? category_files = null;
- if (files.try_get(category ?? "", out category_files)) {
- ExampleFile? file = null;
- if (category_files.try_get(name ?? "", out file)) {
- return new HttpStringResult(file.to_json())
- .set_header("Content-Type", "application/json");
- }
- }
-
- return new HttpStringResult(@"{ \"error\": \"File not found in category '$category'\" }")
- .set_header("Content-Type", "application/json");
- }
- }
- // Path info handler
- class PathInfoEndpoint : Object, Endpoint {
- public async HttpResult handle_request(HttpContext http_context, RouteContext route_info) throws Error {
- var parts = new Series<string>();
- parts.add("Path Information:\n");
- parts.add(@" Raw path: $(http_context.request.raw_path)\n");
- parts.add(@" Path components: $(http_context.request.path_components.count())\n");
- parts.add("\n Components:\n");
-
- http_context.request.path_components
- .with_positions()
- .iterate((pair) => {
- parts.add(@" [$((int)pair.position)]: $(pair.item)\n");
- });
-
- parts.add(@"\n Query string: $(http_context.request.query_string)\n");
-
- // Show named segments from route information
- parts.add("\n Named segments:\n");
- route_info.mapped_parameters.to_immutable_buffer()
- .iterate((kv) => {
- parts.add(@" $(kv.key): $(kv.value)\n");
- });
-
- var result = parts.to_immutable_buffer()
- .aggregate<string>("", (acc, s) => acc + s);
-
- return new HttpStringResult(result);
- }
- }
- void main() {
- var application = new WebApplication(8082);
-
- application.container.register_scoped<Endpoint>(() => new RootEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/"));
-
- application.container.register_scoped<Endpoint>(() => new HelloEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/hello"));
-
- application.container.register_scoped<Endpoint>(() => new HelloNameEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/hello/{name}"));
-
- application.container.register_scoped<Endpoint>(() => new UsersEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/users"));
-
- application.container.register_scoped<Endpoint>(() => new UserByIdEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/users/{id}"));
-
- application.container.register_scoped<Endpoint>(() => new UserPostsEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/users/{id}/posts"));
-
- application.container.register_scoped<Endpoint>(() => new ApiStatusEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/api/v1/status"));
-
- application.container.register_scoped<Endpoint>(() => new ApiItemEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/api/v1/items/{id}"));
-
- application.container.register_scoped<Endpoint>(() => new FilesEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/files/{category}/{name}"));
-
- application.container.register_scoped<Endpoint>(() => new PathInfoEndpoint())
- .with_metadata<EndpointRoute>(new EndpointRoute("/pathinfo"));
-
- print("Path Routing Example Server running on port 8082\n");
- print("Try these endpoints:\n");
- print(" - http://localhost:8082/\n");
- print(" - http://localhost:8082/hello\n");
- print(" - http://localhost:8082/hello/Alice\n");
- print(" - http://localhost:8082/users\n");
- print(" - http://localhost:8082/users/1\n");
- print(" - http://localhost:8082/users/1/posts\n");
- print(" - http://localhost:8082/api/v1/status\n");
- print(" - http://localhost:8082/api/v1/items/2\n");
- print(" - http://localhost:8082/files/docs/readme.txt\n");
- print(" - http://localhost:8082/pathinfo?test=1\n");
-
- application.run();
- }
- // Helper classes for the example
- class User {
- public int id { get; private set; }
- public string name { get; private set; }
- public string email { get; private set; }
-
- public User(int id, string name, string email) {
- this.id = id;
- this.name = name;
- this.email = email;
- }
-
- public string to_json() {
- return @"{ \"id\": $id, \"name\": \"$name\", \"email\": \"$email\" }";
- }
- }
- class Post {
- public int id { get; private set; }
- public int user_id { get; private set; }
- public string title { get; private set; }
- public string content { get; private set; }
-
- public Post(int id, int user_id, string title, string content) {
- this.id = id;
- this.user_id = user_id;
- this.title = title;
- this.content = content;
- }
-
- public string to_json() {
- return @"{ \"id\": $id, \"user_id\": $user_id, \"title\": \"$title\", \"content\": \"$content\" }";
- }
- }
- class Item {
- public int id { get; private set; }
- public string name { get; private set; }
- public string description { get; private set; }
- public double price { get; private set; }
-
- public Item(int id, string name, string description, double price) {
- this.id = id;
- this.name = name;
- this.description = description;
- this.price = price;
- }
-
- public string to_json() {
- return @"{ \"id\": $id, \"name\": \"$name\", \"description\": \"$description\", \"price\": $price }";
- }
- }
- class ExampleFile {
- public string name { get; private set; }
- public string category { get; private set; }
- public string description { get; private set; }
-
- public ExampleFile(string name, string category, string description) {
- this.name = name;
- this.category = category;
- this.description = description;
- }
-
- public string to_json() {
- return @"{ \"name\": \"$name\", \"category\": \"$category\", \"description\": \"$description\" }";
- }
- }
|