zeppaman/api-farm

ApiFarm 核心包。使用 zeppaman/api-farm-skeleton 创建项目

dev-main 2021-03-18 17:48 UTC

This package is auto-updated.

Last update: 2024-09-19 01:27:11 UTC


README

Api Farm 是一个基于 Symfony 和 MongoDB 的低代码平台。它支持以下功能

  • 数据管理
  • Rest API
  • 用户管理
  • Oaut2 身份验证(作为独立服务器或 JWT 资源服务器)
  • 支持 OpenID connect
  • 命令行自动化
  • 使用查询和变异的 GraphQL
  • 动态端点生成

安装选项

建议的部署方法是使用骨架项目,顺便说一句,您可以将此包添加到您的应用程序中或直接运行此包。

使用骨架

# create a project
COMPOSER_MEMORY_LIMIT=-1 composer create-project zeppaman/api-farm my-project

cd my-project

docker-compose up 

# configure the application
docker-compose exec app ./vendor/bin/apifarm-install apifarm:config [--db-host DB-HOST] [--db-port DB-PORT] [--db-password DB-PASSWORD] [--db-user DB-USER]

# install the database
docker-compose exec app  bin/console apifarm:install [--adminuser ADMINUSER] [--adminpassword ADMINPASSWORD] [--db-host DB-HOST] [--db-port DB-PORT] [--db-password DB-PASSWORD] [--db-user DB-USER]

手动安装

composer require zeppaman/api-farm
  1. 在 bundles.php 中启用核心包

    ApiFarm\CoreBundle\CoreBundle::class => ['all' => true],
    ApiFarm\UiBundle\UiBundle::class => ['all' => true],
    
  2. 通过运行配置 yaml 文件

    ./vendor/bin/apifarm-install apifarm:config [--db-host DB-HOST] [--db-port DB-PORT] [--db-password DB-PASSWORD] [--db-user DB-USER]

    此命令将创建 api-farm 设置文件,并将从 lexik_jwt_authenticationtrikoder_oauth2 生成的文件替换。如果文件存在,则将其重命名为 .old,以便您可以手动合并它们。该命令创建连接字符串。现在 ApiFarm 准备就绪!

  3. 运行 bin/console asset:install --symlink --relative public 安装资源

  4. 运行安装过程

     bin/console apifarm:install [--adminuser ADMINUSER] [--adminpassword ADMINPASSWORD] 

    此命令将

    • 创建一个管理员用户名
    • 生成用于 JWT 的密钥对
    • 创建包含 UI 模块注册的 config.json 文件
    • 使用示例集合模式填充数据
  5. 配置服务器以提供应用程序。/bundles/core/ 路由必须从 /bundles/core/index.html 提供服务。以下 .htaccess 需要放置在您的 public 文件夹中(如果您使用 Apache!)

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^bundles/core/(.*)$ /bundles/core/index.html [NC,L,QSA]

运行此包。

此包是独立用于开发和测试目的的。

只需克隆并运行即可

git clone https://github.com/zeppaman/API-Farm.git .
docker-compose up

然后您可以使用 apifarm-install 跟随安装步骤

# configure the application
docker-compose exec app ./bundles/InstallBundle/bin/apifarm-install apifarm:config [--db-host DB-HOST] [--db-port DB-PORT] [--db-password DB-PASSWORD] [--db-user DB-USER]

# install the database
docker-compose exec app  bin/console apifarm:install [--adminuser ADMINUSER] [--adminpassword ADMINPASSWORD] [--db-host DB-HOST] [--db-port DB-PORT] [--db-password DB-PASSWORD] [--db-user DB-USER]

API 使用

在本节中,我们将了解如何使用 API Farm。

控制台命令

bin/console app:crud:upsert db collection {\"filed\":\"value\"}
bin/console app:crud:find db collection {\"filed\":\"value\"}
bin/console app:crud:delete db collection {\"filed\":\"value\"}

curl --location --request POST 'https:///api/data/test/_schemas'
--header 'Content-Type: text/plain'
--data-raw

Api REST

它使用资源 URI 模式。

集合方法

  • GET https:///api/{db}/{collection} + GET 列表
  • POST https:///api/{db}/{collection} 列表

项目方法

  • GET https:///api/{db}/{collection}/{id} 替换对象
  • PUT https:///api/{db}/{collection}/{id} 替换对象
  • PATCH https:///api/{db}/{collection}/{id} 修补对象
  • DELETE https:///api/{db}/{collection}/{id} 删除对象

查找

https:///api/data/test/prova?skip=10&limit=20&query={%22title%22:%22value%22}

{"request":{"database":"test","collection":"test","skip":"10","limit":"20","query":"{}"},"data":[{"_id":"604335dcddfd8c484b4da692","title":"mytitle","body":"mybody"},{"_id":"604335ea05dcae70fe1ed752","title":"mytitle","body":"mybody"},{"_id":"604335f88288e669541ccb72","title":"mytitle","body":"mybody"},{"_id":"6043363cf9265b45a810f262","title":"mytitle","body":"mybody"},{"_id":"60433646825e954b41534232","title":"mytitle","body":"mybody"},{"_id":"6043368e22619b6cd151d852","title":"mytitle","body":"mybody"},{"_id":"6043369ca48c2a285d4682f2","title":"mytitle","body":"mybody"},{"_id":"604336f26e205328b82ddd32","title":"mytitle","body":"mybody"},{"_id":"6043370630658259257fd982","title":"mytitle","body":"mybody"},{"_id":"60433722f915fc6ba00f5292","title":"mytitle","body":"mybody"},{"_id":"6043387ef42a7a265a743c02","filed":"value"}],"metadata":[]}

GET

https:///api/data/test/test/60428383bdb90439ec593df2

{"request":{"database":"test","collection":"test","id":"60428383bdb90439ec593df2"},"data":{"_id":"60428383bdb90439ec593df2","ddd":"ddd"},"metadata":[]}

OAUTH2

生成客户端

 bin/console trikoder:oauth2:create-client

 -------- 
  Identifier                         Secret                                                                                                                            
 ---------------------------------- ---------------------------------------------------------------------------------------------------------------------------------- 
  c0a71bf0379c66c46da3ed41a4f4aab2   e8f9855c30bb9915e61bc093656e75c7e8e3bc3b221eca2be796790af96e6f347b738a28c84ffb9db61617e812b21c51c8b29d9d8d1c92d2df9b386a2404c394  
 ---------------------------------- ---------------------------------------------------------------------------------------------------------------------------------- 

更新客户端

bin/console trikoder:oauth2:update-client --grant-type client_credentials --grant-type password c0a71bf0379c66c46da3ed41a4f4aab2

列出客户端

bin/console trikoder:oauth2:list-clients

获取令牌

使用密码协议使用令牌

curl --location --request POST 'localhost/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=bob' \
--data-urlencode 'password=xyz' \
--data-urlencode 'client_id=c0a71bf0379c66c46da3ed41a4f4aab2' \
--data-urlencode 'client_secret=e8f9855c30bb9915e61bc093656e75c7e8e3bc3b221eca2be796790af96e6f347b738a28c84ffb9db61617e812b21c51c8b29d9d8d1c92d2df9b386a2404c394'

获取用户信息

用户信息可在 localhost/userinfo 上获取。

curl --location --request GET 'localhost/userinfo' --header 'Authorization: Bearer {token}'


{
    
    "_id": "6043d6703e01b17faf118b53",
    "username": "bob",
    "nome": "Bobby" 
     /* any data here*/
}

GraphQL

端点位于 localhost/api/graph/{db}

query { 
  entity1{
      _id,
      title,
      amount
  }
}
{
    "data": {
        "entity1": [
            {
                "_id": "6045038ae0c6cde18c3c93bf",
                "title": "test data",
                "amount": 1000
            }
        ]
    }
}

代码 lambda

端点位于 localhost/api/do/{db}/{action}/

curl --location --request GET 'localhost/api/do/{db}/{action}/' 



{
    "_id" : ObjectId("60452b54200a9b44267324e8"),
    "name" : "test2",
    "code" : "return $container['crud']->find('test','_mutations',[],1,10);"
}

设置 & 测试

#create the client oauht2
docker-compose exec app bin/console trikoder:oauth2:create-client --grant-type password c0a71bf0379c66c46da3ed41a4f4aab2

docker-compose exec app bin/console  app:crud:upsert test "_users" '{"username":"admin","newpassword":"password"}'
#create an entity with fields title and amount
docker-compose exec app bin/console  app:crud:upsert test '_schemas' '{ "name" : "entity1","db" :"test","fields":{"title":{"type": "text",            "name" : "title","label" : "Title"},"amount" : {"type" : "int", "name" : "amount","label" : "Amount"}}}'

#get token
curl --location --request POST 'localhost/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=admin' \
--data-urlencode 'password=password' \
--data-urlencode 'client_id={replace}' \
--data-urlencode 'client_secret={replace}'

# enter data
docker-compose exec app bin/console  app:crud:upsert test "entity1" '{"title":"title 1","amount":100}'
docker-compose exec app bin/console  app:crud:upsert test "entity1" '{"title":"title 2","amount":101}'
docker-compose exec app bin/console  app:crud:upsert test "entity1" '{"title":"title 3","amount":102}'
docker-compose exec app bin/console  app:crud:upsert test "entity1" '{"title":"title 4","amount":103}'

#get data GraphQL
curl --location --request GET 'localhost/api/graph/test' --header 'Content-Type: application/json' --data-raw '{"query":"query { \r\n  entity1{\r\n      _id,\r\n      title,\r\n      amount\r\n  }\r\n}","variables":{}}'

curl --location --request GET 'https:///api/data/test/entity1' \
--header 'Authorization: Bearer {token}'