0

我有一个带有美居服务器和 API 平台包的 Symfony 应用程序。我的美居服务器使用 docker 运行:

我的服务美居到我的 docker-compose.yml

     mercure:
        restart: unless-stopped
        image: dunglas/mercure:v0.10.4
        environment:
            - PUBLISHER_JWT_KEY=mercure_publisher
            - SUBSCRIBER_JWT_KEY=mercure_subscriber
            - CORS_ALLOWED_ORIGINS=*
            - PUBLISH_ALLOWED_ORIGINS=*
            - ALLOW_ANONYMOUS=1
            - MERCURE_EXTRA_DIRECTIVES=cors_allowed_origins *
            - SERVER_NAME=":80"
        ports:
            - 8001:80

我的美居服务器工作http://127.0.0.1:8001/

Welcome to Mercure!

在网址http://127.0.0.1:8001/.well-known/mercure我有消息:

Missing "topic" parameter.

我的树枝模板中有一个简单的监听器。

const url = new URL('http://127.0.0.1:8001/.well-known/mercure', window.origin);
url.searchParams.append('topic', 'https://127.0.0.1:8000/api/recipes/{id}');
eventSource.onmessage = e => {
            console.log("Real Time debug : ", event.data);
        } // do something with the payload

我已经尝试过 Postman 和那项工作。

登录我的容器美居:

time="2021-08-16T10:12:18Z" level=info msg="Update published" event_id="urn:uuid:d8bb10df-9500-4de2-931b-2d05e904bcd3" event_retry=0 event_type= remote_addr="172.20.0.1:62402" update_private=false update_topics="[https://127.0.0.1:8000/api/recipes/1]"

172.20.0.1 - - [16/Aug/2021:10:12:18 +0000] "POST /.well-known/mercure HTTP/1.1" 200 45 "" "PostmanRuntime/7.28.3"

time="2021-08-16T10:12:18Z" level=info msg="Event sent" event_id="urn:uuid:d8bb10df-9500-4de2-931b-2d05e904bcd3" event_retry=0 event_type= last_event_id= remote_addr="172.20.0.1:62394" subscriber_id="urn:uuid:a6640fd4-49f5-4b73-97c2-0ba438a274d3" subscriber_topics="[https://127.0.0.1:8000/api/recipes/{id} http://127.0.0.1:8000/api/recipes/{id} http://127.0.0.1:8000/api/recipes/34 https://127.0.0.1:8000/api/recipes/34]" update_private=false update_topics="[https://127.0.0.1:8000/api/recipes/1]"

我已经安装了 ApiPlateform 和 Mercure 包

composer require api
composer require symfony/mercure-bundle

我有编辑 .env

MERCURE_URL=http://127.0.0.1:8001/.well-known/mercure
MERCURE_PUBLIC_URL=http://127.0.0.1:8001/.well-known/mercure
MERCURE_JWT_SECRET=mercure_publisher

我已经创建了一个带有参数标题的实体配方,并且我已经使用 mercure 选项添加了 ApiRessource 注释:

#[ApiResource(
    mercure:true
)]

我创建了一个简单的表单来添加新食谱

#[Route('/recipe/new', name: 'recipe.new')]
    #[Route('/recipe/{id}', name: 'recipe.create')]
    public function index(Recipe $recipe = null, Request $request, RecipeRepository $repo): Response
    {
        if(!$recipe){
            $recipe = new Recipe();
        }
        
        $formBuilder = $this->createFormBuilder($recipe);
        $formBuilder
            ->add('title')
            ->add('submit',SubmitType::class)
        ;
        $form = $formBuilder->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();

            $manager = $this->getDoctrine()->getManager();
            $manager->persist($data);
            $manager->flush();

            return $this->redirectToRoute("recipe.new");
        }

        $recipeList = $repo->findAll();

        return $this->render('app/index.html.twig', [
            'form' => $form->createView(),
            'recipeList' => $recipeList
        ]);
    }

当我提交时,使用 Doctrine 插入我的数据,但我收到一个错误:发送更新失败。

Symfony\Component\HttpClient\Exception\
ClientException
HTTP/1.1 401 Unauthorized returned for "http://127.0.0.1:8001/.well-known/mercure".

我有未经授权的错误,但我将 secret_jwt 配置到我的 .env 中

MERCURE_JWT_SECRET=mercure_publisher

进入配置>包>mercure.yaml

mercure:
    hubs:
        default:
            url: '%env(MERCURE_URL)%'
            public_url: '%env(MERCURE_PUBLIC_URL)%'
            jwt:
                secret: '%env(MERCURE_JWT_SECRET)%'
                publish: '*'

我试过邮递员,我得到同样的错误

Method POST
http://127.0.0.1:8000/api/recipes
{
  "title": "test"
}

登录到我的容器美居

time="2021-08-16T09:32:03Z" level=info msg="json: cannot unmarshal number 1629109923.229465 into Go struct field claims.exp of type int64" remote_addr="172.20.0.1:62266"

172.20.0.1 - - [16/Aug/2021:09:32:03 +0000] "POST /.well-known/mercure HTTP/1.1" 401 13 "" "Symfony HttpClient/Curl"

当 Symfony 想要更新中心美居时,我的问题出现了。当我在http://127.0.0.1:8000/api/recipes Doctrine 创建实体但 Mercure 无法更新集线器时 POST 。

我不明白为什么那行不通:/

感谢您的帮助。

编辑:问题解决了。如果您希望我创建了一个存储库:github.com/ThomasMouchelet/docker-apiplateform-mercure

4

0 回答 0