alex-foleon/service-library-catalog-php

v0.1.0 2020-12-24 15:51 UTC

This package is auto-updated.

Last update: 2024-09-25 21:58:19 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score

service-library-catalog-php

任务

构建一个小的REST API,并使用它构建两个资源:书籍和作者。

GET: /book/:id 期望得到一个包含作者的书籍

GET: /author/:id 期望得到一个包含所有书籍的作者

POST: /author/ 创建一个作者

POST: /book 创建一本书并添加一个作者到它。

想法,开发

  • 存储库:实现了PDO(Mysql)存储和Redis缓存,可以通过envs进行配置(见:docker-compose.override.yml)。测试使用Sqlite和redis-mock。想法是App缓存书籍及其作者,以及作者及其书籍。当客户端添加新书时,作者的缓存将失效。
  • 目录:管理所有这些存储库的服务。
  • 认证:App具有Auth接口和Bearer认证的实现。也可以添加其他实现。
  • 授权:接口和简单的ACL实现:访客可以阅读,用户可以添加书籍(但不能添加作者),管理员可以添加书籍和作者。
  • API端点:HTTP端点的规范。
  • 转换器和注水器:App有自己的"序列化器",帮助编码/解码实体以存储在某些存储库(面向文档)中。基本转换器用于在HTTP响应中表示数据。
  • 控制器:V1实现了基本的json REST,我们可以添加更多版本以支持其他协议。

使用的库

  • php-di/php-di:DI容器实现;
  • predis/predis:Redis客户端;
  • rakit/validation:用于验证传入请求的库;
  • nikic/fast-route:PHP路由(thephpleague/route的基础);
  • nyholm/psr7laminas/laminas-httphandlerrunner:PSR7实现(HTTP请求和响应);
  • laminas/laminas-permissions-acl:经典ZF ACL实现。

安装

为了让docker化的网络在本地机器上工作,请将以下内容添加到您的 /etc/hosts

    127.0.0.1 host.docker.internal

然后

    ### Download
    composer create-project alex-foleon/service-library-catalog-php:^0.1 catalogue --prefer-dist --ignore-platform-reqs
    cd catalogue
    ### Prepare default dockerization
    cp docker-compose.override.yml.dist docker-compose.override.yml
    docker-compose up -d
    
    ### Create database
    docker exec -it rds-library bash -c "mysql -uroot -pmy-secret-pw -e \"create database catalogue CHARACTER SET utf8 COLLATE utf8_general_ci\""
    ### Start migrations
    docker exec -it service-catalogue-php bash -c "./scripts/migrate.sh"
    ### Check system works
    curl -i http://127.0.0.1:8080/api/v1/healthcheck

之后,应用程序应通过 http://127.0.0.1:8080 工作

测试

    docker exec -it service-catalogue-php bash -c "composer test"

测试覆盖率超过 90%。应该实现更多测试,但这需要时间。代码质量为 ~9.8

认证

App具有简单的Auth-service。客户端应在"Authorization" HTTP头部提供base64编码的令牌。令牌应具有以下json结构

{
  "secret": "some-secret",
  "payload": "roleId:clientId"
}

例如

{"secret": "some-secret", "payload": "user:184"}

App中有3个角色:guestuseradmin。无论使用什么ID,因为App没有用户存储库。

您可以使用以下编码令牌,默认环境有效

  • 访客:eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJndWVzdDozIn0=
  • 用户:eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJ1c2VyOjQifQ==
  • 管理员:eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJhZG1pbjo1In0=
  • 外星人:eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJhbGllbjo2In0= (应该被拒绝)

使用管理员示例

    curl -i -H "Authorization: Bearer eyJzZWNyZXQiOiJzdXBlcl9kdXBlcl9zZWN1cml0eSIsInBheWxvYWQiOiJhZG1pbjo1In0=" http://127.0.0.1:8080/api/v1/author/1

端点

  1. 创建作者。

POST /author

{
    "name": "Charles Dickens",
    "birthdate": "1812-02-07",
    "deathdate": "1870-06-09",
    "biography": "Born in Portsmouth, Dickens left school to work in a factory when his father was incarcerated in a debtors' prison. Despite his lack of formal education, he edited a weekly journal for 20 years, wrote 15 novels, five novellas, hundreds of short stories and non-fiction articles, lectured and performed readings extensively, was an indefatigable letter writer, and campaigned vigorously for children's rights, education, and other social reforms. ",
    "summary": "Charles John Huffam Dickens FRSA (/ˈdɪkɪnz/; 7 February 1812 – 9 June 1870) was an English writer and social critic. He created some of the world's best-known fictional characters and is regarded by many as the greatest novelist of the Victorian era.[1] His works enjoyed unprecedented popularity during his lifetime, and by the 20th century, critics and scholars had recognised him as a literary genius. His novels and short stories are still widely read today."
}

响应:[200]

{
    "id": "1",
    "name": "Charles Dickens",
    "birthdate": "1812-02-07",
    "deathdate": "1870-06-09",
    "biography": "Born in Portsmouth, Dickens left school to work in a factory when his father was incarcerated in a debtors' prison. Despite his lack of formal education, he edited a weekly journal for 20 years, wrote 15 novels, five novellas, hundreds of short stories and non-fiction articles, lectured and performed readings extensively, was an indefatigable letter writer, and campaigned vigorously for children's rights, education, and other social reforms. ",
    "summary": "Charles John Huffam Dickens FRSA (/ˈdɪkɪnz/; 7 February 1812 – 9 June 1870) was an English writer and social critic. He created some of the world's best-known fictional characters and is regarded by many as the greatest novelist of the Victorian era.[1] His works enjoyed unprecedented popularity during his lifetime, and by the 20th century, critics and scholars had recognised him as a literary genius. His novels and short stories are still widely read today."
}
  1. 创建书籍。

POST /book

{
    "title": "Dombey and Son",
    "summary": "Dombey and Son is a novel by English author Charles Dickens. It follows the fortunes of a shipping firm owner, who is frustrated at the lack of a son to follow him in his footsteps; he initially rejects his daughter’s love before eventually becoming reconciled with her before his death. ",
    "authorId": 1
}

响应:[200]

{
    "id": "1",
    "title": "Dombey and Son",
    "summary": "Dombey and Son is a novel by English author Charles Dickens. It follows the fortunes of a shipping firm owner, who is frustrated at the lack of a son to follow him in his footsteps; he initially rejects his daughter’s love before eventually becoming reconciled with her before his death. "
}
  1. 阅读作者信息。

GET /author/ID

响应:[200]

{
    "id": "1",
    "name": "Charles Dickens",
    "birthdate": "1812-02-07",
    "deathdate": "1870-06-09",
    "biography": "Born in Portsmouth, Dickens left school to work in a factory when his father was incarcerated in a debtors' prison. Despite his lack of formal education, he edited a weekly journal for 20 years, wrote 15 novels, five novellas, hundreds of short stories and non-fiction articles, lectured and performed readings extensively, was an indefatigable letter writer, and campaigned vigorously for children's rights, education, and other social reforms. ",
    "summary": "Charles John Huffam Dickens FRSA (/ˈdɪkɪnz/; 7 February 1812 – 9 June 1870) was an English writer and social critic. He created some of the world's best-known fictional characters and is regarded by many as the greatest novelist of the Victorian era.[1] His works enjoyed unprecedented popularity during his lifetime, and by the 20th century, critics and scholars had recognised him as a literary genius. His novels and short stories are still widely read today.",
    "books": [
        {
            "id": "1",
            "title": "Dombey and Son",
            "summary": "Dombey and Son is a novel by English author Charles Dickens. It follows the fortunes of a shipping firm owner, who is frustrated at the lack of a son to follow him in his footsteps; he initially rejects his daughter’s love before eventually becoming reconciled with her before his death. "
        }
    ]
}
  1. 阅读书籍信息。

GET /book/ID

响应:[200]

{
    "id": "1",
    "title": "Dombey and Son",
    "summary": "Dombey and Son is a novel by English author Charles Dickens. It follows the fortunes of a shipping firm owner, who is frustrated at the lack of a son to follow him in his footsteps; he initially rejects his daughter’s love before eventually becoming reconciled with her before his death. ",
    "author": {
        "id": "1",
        "name": "Charles Dickens",
        "birthdate": "1812-02-07",
        "deathdate": "1870-06-09",
        "biography": "Born in Portsmouth, Dickens left school to work in a factory when his father was incarcerated in a debtors' prison. Despite his lack of formal education, he edited a weekly journal for 20 years, wrote 15 novels, five novellas, hundreds of short stories and non-fiction articles, lectured and performed readings extensively, was an indefatigable letter writer, and campaigned vigorously for children's rights, education, and other social reforms. ",
        "summary": "Charles John Huffam Dickens FRSA (/ˈdɪkɪnz/; 7 February 1812 – 9 June 1870) was an English writer and social critic. He created some of the world's best-known fictional characters and is regarded by many as the greatest novelist of the Victorian era.[1] His works enjoyed unprecedented popularity during his lifetime, and by the 20th century, critics and scholars had recognised him as a literary genius. His novels and short stories are still widely read today."
    }
}