Tussle.Routes (Tussle v0.4.0)

Copy Markdown View Source

Provides a macro for defining TUS upload routes in a Phoenix router.

This macro sets up all the routes needed for TUS resumable uploads, including the CloudFlare-compatible GET route for HEAD-to-GET conversion.

Usage

defmodule MyAppWeb.Router do
  use MyAppWeb, :router
  import Tussle.Routes

  scope "/api/xfer", MyAppWeb do
    pipe_through :api
    add_tus_routes UploadController
  end
end

Routes Defined

MethodPathActionPurpose
OPTIONS/:optionsServer capabilities
POST/:postCreate new upload
HEAD/:uid:headGet upload metadata
GET/:uid:getCloudFlare HEAD-to-GET compatibility
PATCH/:uid:patchUpload chunk
DELETE/:uid:deleteCancel upload

CloudFlare Compatibility

⚠️ Important Note

CloudFlare's caching layer converts HEAD requests to GET requests. This unexpectedly violates the expectations of the TUS protocol, which specifies HEAD for metadata retrieval. The conversion can cause requests to not match HEAD routes, resulting in 404 errors.

The add_tus_routes/1 macro includes a GET route that mirrors HEAD behavior, ensuring resumable uploads work correctly when behind CloudFlare or similar CDNs. See Tussle.get/2 for details.

Alternative: Manual Route Definition

If you need custom routing, you can define routes manually:

scope "/files", MyAppWeb do
  pipe_through :api

  options "/",          UploadController, :options
  post "/",             UploadController, :post
  match :head, "/:uid",  UploadController, :head
  get "/:uid",           UploadController, :get   # CloudFlare compatibility
  patch "/:uid",         UploadController, :patch
  delete "/:uid",        UploadController, :delete
end

Summary

Functions

add_tus_routes(controller)

(macro)