1

How to enable CORS in vespa? I want to deploy my API's to swagger UI but due to CORS disbled in vespa, I am unable to do this. Is there any way to enable CORS in vespa or How should I use API's in swagger to use it? Please help.

Mohammad Sunny
  • 371
  • 1
  • 3
  • 15

1 Answers1

3

Vespa does not come with built-in CORS support. You'll have to implement the CORS logic yourself as JDisc security filters (http-server-and-filters.html, services-http.html#filter).

The Vespa source code contains a set of CORS filters that can be used as inspiration. You can technically use them, but they are not public API and can be changed/removed in the future.

You will need two filters:

  • A request filter handling CORS preflight HTTP requests
  • A response filter that appends CORS headers to HTTP responses

Note that the response filters are not invoked if a request filter returns a response. Any other JDisc filters in your application must therefore include the CORS headers when they return a response.

See http-server-and-filters.html#set-up-filter-chains on how to configure request/response filter chains.

bjorncs
  • 1,250
  • 11
  • 20
  • I am creating my custom RequestFilter and ResponseFilter. Where should I place these classes?Where should I create directory components? – Mohammad Sunny Dec 18 '18 at 06:05
  • 1
    See https://docs.vespa.ai/documentation/jdisc/http-server-and-filters.html – Jo Kristian Bergum Dec 18 '18 at 14:28
  • I am not able to find where should I create components directory using above link. I have created components directory in application package where we keeps search and searchDefitions directories. Now I components directory, Should I create src/main/java/com/yahoo/demo/CustomRequestFilter or direct keep CustomRequestFilter class? And according to the doc, where should I create artifact id filter-bundle? – Mohammad Sunny Dec 19 '18 at 07:51
  • The Java components can be placed in any Java package inside a bundle. You'll find examples on how to create a bundle with custom components in the vespa-engine/sample-apps repository (e.g https://github.com/vespa-engine/sample-apps/tree/master/http-api-using-request-handlers-and-processors). – bjorncs Jan 03 '19 at 13:37