apigen

Crates.ioapigen
lib.rsapigen
version0.3.1
sourcesrc
created_at2024-10-17 23:09:40.78787
updated_at2024-11-13 23:28:41.063293
descriptionA tool to genearate REST API written in Rust
homepagehttps://github.com/Pulko/apigen
repositoryhttps://github.com/Pulko/apigen
max_upload_size
id1413687
size63,783
Fedor Tkachenko (Pulko)

documentation

README

apigen

Overview

This project is a Rust-based API generator that allows you to generate a complete API based on a given schema in JSON format. It is designed to support multiple backends and database configurations, currently supporting PostgreSQL templates.

Features

  • Generates API folders with schema, entities, and configuration files.
  • Supports different backends and databases through templates (PostgreSQL is supported out of the box).
  • Automatically creates folder structures and essential files (Cargo.toml, .gitignore, etc.).
  • Extensible with multiple filters like capitalize_first_letter, pluralize, and diesel_type for template rendering.

Prerequisites

  • Rust: Make sure Rust is installed. You can get it from rust-lang.org.
  • Cargo: Cargo should be installed as part of the Rust toolchain.

To check if Rust is installed, run:

rustc --version

Usage

  1. Clone the repository:

    git clone <your-repo-url>
    cd api_generator
    
  2. Prepare your API schema in JSON format. An example schema:

    {
      "entities": [
        {
          "name": "User",
          "fields": [
            { "name": "id", "field_type": "i32" },
            {
              "name": "username",
              "field_type": "String"
            }
          ]
        },
        {
          "name": "Post",
          "fields": [
            { "name": "id", "field_type": "i32" },
            {
              "name": "title",
              "field_type": "String"
            },
            {
              "name": "body",
              "field_type": "String"
            },
            {
              "name": "author_id",
              "field_type": "i32"
            }
          ]
        }
      ]
    }
    
  3. Run the apigen:

    apigen '<api_schema_json>'
    

    Replace <api_schema_json> with your actual JSON schema. For example:

    apigen '{"entities":[{"name":"User","fields":[{"name":"id","field_type":"i32"},{"name":"username","field_type":"String"}]}]}'
    
  4. The generated API will be saved in the output directory. The output folder will contain:

    • A generated Cargo.toml file
    • Source code for the API, including routes and schema
    • Other configuration files such as .gitignore and Dockerfile.

Usage with Docker

You can also run the API generator using Docker. You can run the API generator using the following command:

docker run --rm -v "$PWD:/usr/src/app" vzdbovich455/apigen:0.3.0 '<api_schema_json>'

Replace <api_schema_json> with your actual JSON schema.

Example:

docker run --rm -v "$PWD:/usr/src/app" vzdbovich455/apigen:0.3.0 '{"entities":[{"name":"User","fields":[{"name":"id","field_type":"i32"},{"name":"username","field_type":"String"}]}]}'

Configuration

The project uses a TemplateConfig to load template files depending on the selected database backend. By default, it uses PostgreSQL templates located in the templates/postgres/ directory.

If you want to extend the project to support more databases or backends, you can:

  1. Add new templates in the templates/<backend>/ directory.
  2. Adjust the TemplateConfig to point to your new templates.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Commit count: 23

cargo fmt