bindFileApi registers the file api endpoints and the corresponding handlers.
(app core.App, rg *router.RouterGroup[*core.RequestEvent])
| 24 | |
| 25 | // bindFileApi registers the file api endpoints and the corresponding handlers. |
| 26 | func bindFileApi(app core.App, rg *router.RouterGroup[*core.RequestEvent]) { |
| 27 | maxWorkers := cast.ToInt64(os.Getenv("PB_THUMBS_MAX_WORKERS")) |
| 28 | if maxWorkers <= 0 { |
| 29 | maxWorkers = int64(runtime.NumCPU() + 2) // the value is arbitrary chosen and may change in the future |
| 30 | } |
| 31 | |
| 32 | maxWait := cast.ToInt64(os.Getenv("PB_THUMBS_MAX_WAIT")) |
| 33 | if maxWait <= 0 { |
| 34 | maxWait = 60 |
| 35 | } |
| 36 | |
| 37 | api := fileApi{ |
| 38 | thumbGenPending: new(singleflight.Group), |
| 39 | thumbGenSem: semaphore.NewWeighted(maxWorkers), |
| 40 | thumbGenMaxWait: time.Duration(maxWait) * time.Second, |
| 41 | } |
| 42 | |
| 43 | sub := rg.Group("/files") |
| 44 | sub.POST("/token", api.fileToken).Bind(RequireAuth()) |
| 45 | sub.GET("/{collection}/{recordId}/{filename}", api.download).Bind(collectionPathRateLimit("", "file")) |
| 46 | } |
| 47 | |
| 48 | type fileApi struct { |
| 49 | // thumbGenSem is a semaphore to prevent too much concurrent |
no test coverage detected
searching dependent graphs…