从Quarkus提供的图像(静态文件)在服务器上失败。

huangapple go评论66阅读模式
英文:

serving images (static files) from quarkus is failing on server

问题

我正在使用Next.js将带有图像的表单提交到我的后端(quarkus; 3.1.2; Kotlin)。到目前为止,一切都很顺利。在我的机器上,它可以正常工作,并显示上传的图像,但在部署版本上无法正常工作。

我正在使用Docker和Treafik进行部署。这些图像保存在附加卷下的src/main/resources/META-INF/resources目录中(正如在以下帖子中提到的https://stackoverflow.com/a/73294744/354164)

在本地主机上,http://localhost:8080/api/image-123.png可以正常工作,但在我的部署版本https://domain.de/api/image-123.png上无法正常工作。

在application.properties文件中配置如下:

quarkus.http.root-path=/api
quarkus.http.non-application-root-path=${quarkus.http.root-path}

Treafik中的Docker标签:

backend:
...
  treafik: 
      "traefik.http.routers.server.entrypoints": "websecure"
      "traefik.http.routers.server.rule": "Host(`domain`) && PathPrefix(`/api`)"

向后端发出的所有其他调用,如获取数据或提交表单(post),都可以正常工作,没有问题。

英文:

I'm using Next.js to submit a form with images to my backend (quarkus; 3.1.2; Kotlin). So far so good. IT WORKS on my machine with with showing the upload images, but on the deployed version it does not work.

I'm using docker and treafik for the deployment. The images are save to attached volume under src/main/resources/META-INF/resources (as mentioned in the following post https://stackoverflow.com/a/73294744/354164)

on localhost http://localhost:8080/api/image-123.png works but on the my deployed version https://domain.de/api/image-123.png does not work.

configured following on in application.properties file:

quarkus.http.root-path=/api
quarkus.http.non-application-root-path=${quarkus.http.root-path}

docker labels in treafik:

backend:
...
  treafik: 
      "traefik.http.routers.server.entrypoints": "websecure"
      "traefik.http.routers.server.rule": "Host(`domain`) && PathPrefix(`/api`)"

all other calls to the backend like getting the data or submitting the form (post) works without an issue.

答案1

得分: 1

META-INF/resources 用于提供来自jar包的静态资源。您可能不会在那里添加新资源。

为了提供静态资源,您应该添加一个特定的Vert.x Web路由来提供给定目录中的文件。在上面的示例中,我从static/目录的static/路径中提供资源,例如static/image3.png将在http://localhost:8080/static/image3.png上提供。

请注意,在示例中,我们从启动应用程序的相对目录中提供文件,这是推荐的做法。通过调整StaticHandler.create()的选项,您可以从绝对路径提供文件,但这绝对不是我们推荐的做法,因为它可能会导致安全问题。

如果您希望路径可配置,可以在那里注入@ConfigProperty属性。

英文:

META-INF/resources is used to serve static resources coming from the jar. You may not add new resources there.

To serve static resources, you should add a specific Vert.x Web route to serve the files from a given directory. In the example above, I serve resources from the static/ directory on the static/ path, e.g. static/image3.png will be served on http://localhost:8080/static/image3.png.

Note that in the example, we are serving files from a directory relative to where you started the app, this is recommended. By adjusting the option of StaticHandler.create(), you can serve files from an absolute path but that's definitely not something we recommend as it could cause security issues.

You can inject @ConfigProperty properties there if you want the paths to be configurable.

package org.acme;

import io.quarkus.runtime.StartupEvent;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import jakarta.enterprise.event.Observes;


public class StaticResources {

    void installRoute(@Observes StartupEvent startupEvent, Router router) {
        router.route()
                .method(HttpMethod.GET).method(HttpMethod.HEAD)
                .path("/static/*")
                .handler(StaticHandler.create("static/"));
    }
}

Thanks for asking this question, I will improve our documentation.

huangapple
  • 本文由 发表于 2023年6月22日 18:07:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定