Skip to main content

Crate any2nix_api

Crate any2nix_api 

Source
Expand description

This crate provides an HTTP server to be used as an OpenAPI-compatible API for translating different formats to Nix.

It exposes its own router for integration with other axum-based programs.

§Usage

This crate is on crates.io and can be added as a dependency of your project:

cargo add any2nix-api

§Supported Formats

See any2nix::Format for an enumeration of supported formats.

Each format is gated behind its own feature.

The default feature enables all formats.

§Crate Features

Besides the features for each format, this crate also exposes the following features:

  • trace: Enables tracing of incoming HTTP requests through [tower-http] and [tracing].
  • utoipa: Enables OpenAPI schema generation through [utoipa].
  • utoipa-swagger-ui: Enables the Swagger UI for the API through [utoipa-swagger-ui].

§Examples: Adding it as an API to another program

  1. Add it as dependency of your program:
cargo add any2nix-api
  1. Add the router to your axum program through nesting:
use axum::{routing::get, Router};
use tokio::net::TcpListener;

let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
let api_router = any2nix_api::app();
let app = Router::new().route("/", get(|| async { "Hello, World!"})).nest("/api", api_router);

axum::serve(listener, app).await.unwrap();

§Examples: Sending TOML to the API

curl \
-d '{"input": "[package]\nname = \"any2nix\""}' \
-X POST \
-H "Content-Type: application/json" \
http://localhost:3000/v1/toml
# Output:
#
# {"nix":"{\n  package = {\n    name = \"any2nix\";\n  };\n}"}

Modules§

requests
responses

Structs§

Error
Error type of the API.

Functions§

app
Generator of the program’s router. Exposed publicly for integration with other axum-based programs.
get_root
Root endpoint of the API. Returns crate information.
post_format
Converts the supported formats to Nix.