magic-api Documentation

repository·master·Indexed 23 days ago

https://github.com/ssssssss-team/magic-api

A rapid interface development framework for Java that enables building and managing HTTP APIs via a web UI, eliminating the need for traditional boilerplate like Controllers and Mappers. It supports JDBC-compliant databases, NoSQL (Redis, MongoDB), and features a dynamic compilation engine via magic-script for real-time changes. The framework includes support for Spring Boot integration, cluster deployment, Swagger generation, and a Nebula graph plugin for executing NGSL queries.

Tokens
2.1K
Snippets
7
Records
10
Agent score
84%

What's inside magic-api

  1. Introduction to magic-api

    master
    magic-api is a Java-based rapid interface development framework. It allows developers to create HTTP APIs through a web-based UI, automatically mapping them to HTTP endpoints. This eliminates the need to manually define traditional Java boilerplate such as Controllers, Services, Daos, Mappers, XML files, or VO objects.
  2. Key features of magic-api

    master

    magic-api provides a wide range of capabilities for rapid API development:

    • Database Support: Supports JDBC-compliant databases (MySQL, MariaDB, Oracle, DB2, PostgreSQL, SQLServer) and NoSQL (Redis, MongoDB).
    • Dynamic Development: Uses the magic-script engine for dynamic compilation. Changes are applied in real-time without requiring a server restart.
    • Advanced Querying: Supports Linq-style queries, database transactions, SQL concatenation, placeholders, and conditional syntax.
    • Management & DevOps: Supports cluster deployment with automatic interface synchronization, script version history comparison/recovery, and Swagger documentation generation.
    • Extensibility: Allows importing Spring Beans and Java classes, and supports custom utility classes, modules, type extensions, dialects, and column name conversions.
    • Developer Experience: Features include code completion, parameter hints, error highlighting, online debugging, and file upload/download capabilities.
  3. Add magic-api to a Spring Boot project via Maven

    master

    To use magic-api in a Spring Boot application, include the magic-api-spring-boot-starter dependency in your pom.xml file.

    <!-- 以spring-boot-starter的方式引用 -->
    <dependency>
    	<groupId>org.ssssssss</groupId>
        <artifactId>magic-api-spring-boot-starter</artifactId>
        <version>2.2.2</version>
    </dependency>
  4. Configure database tables for magic-api v0.7.x+

    master

    For versions 0.7.x and later, magic-api requires two specific tables to manage files and backup records. You must execute the following SQL statements to set up the schema in your database.

    -- File storage table
    CREATE TABLE `magic_api_file_v2` (
      `file_path` varchar(512) NOT NULL,
      `file_content` mediumtext,
      PRIMARY KEY (`file_path`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
    
    -- Backup record table
    CREATE TABLE `magic_backup_record_v2` (
      `id` varchar(32) NOT NULL COMMENT '原对象ID',
      `create_date` bigint(13) NOT NULL COMMENT '备份时间',
      `tag` varchar(32) DEFAULT NULL COMMENT '标签',
      `type` varchar(32) DEFAULT NULL COMMENT '类型',
      `name` varchar(64) DEFAULT NULL COMMENT '原名称',
      `content` blob COMMENT '备份内容',
      `create_by` varchar(64) DEFAULT NULL COMMENT '操作人',
      PRIMARY KEY (`id`,`create_date`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  5. Install the Nebula plugin

    master

    To use Nebula graph capabilities within magic-api, add the magic-api-plugin-nebula dependency to your Maven pom.xml file. Replace magic-api-lastest-version with the actual latest version of magic-api.

    <dependency>
        <groupId>org.ssssssss</groupId>
        <artifactId>magic-api-plugin-nebula</artifactId>
        <version>magic-api-lastest-version</version>
    </dependency>
  6. Access the magic-api online editor

    master
    Once the application is running with the configured magic-api.web path, you can access the online editor to develop and manage your APIs by navigating to the configured URL in your browser (e.g., http://localhost:9999/magic/web).
  7. Configure the Nebula plugin

    master

    Configure the connection details for your Nebula graph instance in your application's YAML configuration file using the nebula prefix. You can use environment variables for sensitive information.

    Available keys:

    • hostAddress: The host and port of the Nebula server (defaults to localhost:9669).
    • userName: The username for authentication (defaults to root).
    • password: The password for authentication (defaults to nebula).
    nebula:
      hostAddress: ${NEBULA_HOSTADDRESS:localhost:9669}
      userName: ${NEBULA_USERNAME:root}
      password: ${NEBULA_PASSWORD:nebula}
  8. Configure magic-api in application.properties

    master

    After adding the dependency, configure the following properties in your application.properties file:

    • magic-api.web: Defines the URL path for the web-based management interface.
    • magic-api.resource.location: Defines where the configuration files are stored. Note: If the path starts with classpath:, the mode is read-only.
    server.port=9999
    #配置web页面入口
    magic-api.web=/magic/web
    #配置文件存储位置。当以classpath开头时,为只读模式
    magic-api.resource.location=/data/magic-api
  9. Execute Nebula queries in magic-api

    master

    You can interact with Nebula using the nebula module imported in your magic-api scripts.

    Key methods:

    • nebula.executeJson(ngsl): Executes a Nebula Graph Language (NGSL) query and returns the result as a JSON object. Use nebula.convert(resultJson) to format the output.
    • nebula.executeNebulaModel(ngsl): Executes a query specifically designed to return a graph model structure.

    For a full list of available methods, refer to the source class org.ssssssss.magicapi.nebula.NebulaModule.

    import nebula;
    var ngsl = 
        """
            USE db_name;MATCH p_=(p:`assignee`)-[*3]-(p2:`transferor`) where  id(p2) == "阿里巴巴"  or id(p)== "阿里巴巴" RETURN p_ limit 1000'
        """
    var resultJson = nebula.executeJson(ngsl)
    nebula.convert(resultJson)
    
    nebula.executeNebulaModel(ngsl)
  10. Nebula query response data format

    master

    The Nebula plugin returns data in a structured format containing nodes and edges, which is compatible with many frontend graph visualization libraries like AntV G6.

    {
        "code": 0,
        "message": "success",
        "data": {
            "nodes": [
                {
                    "edgeSize": 1,
                    "assignee.name": "中航纽赫融资租赁(上海)有限公司",
                    "type": "vertex",
                    "assignee.addr": "上海市中国(上海)自由贸易试验区正定路530号A5库区集中辅助区三层318室",
                    "assignee.legal_person": "周勇",
                    "assignee.type": "企业",
                    "id": "中航纽赫融资租赁(上海)有限公司"
                }
            ],
            "edges": [
                {
                    "dst": "陕西海富融资租赁有限公司",
                    "src": "中航纽赫融资租赁(上海)有限公司",
                    "label": "trans_with",
                    "type": "edge",
                    "target": "陕西海富融资租赁有限公司"
                }
            ]
        },
        "timestamp": 1692149280167,
        "requestTime": 1692149280143,
        "executeTime": 24
    }