Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/backend/drizzle/0006_gigantic_otto_octavius.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE `api_keys` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`key_hash` text NOT NULL,
`name` text,
`description` text,
`expires_at` text,
`created_at` text NOT NULL,
`updated_at` text NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `api_keys_key_hash_unique` ON `api_keys` (`key_hash`);
276 changes: 276 additions & 0 deletions apps/backend/drizzle/meta/0006_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
{
"version": "6",
"dialect": "sqlite",
"id": "777a608b-5340-48bf-af83-f1ebed3bef83",
"prevId": "5270772c-dde8-4413-983d-94850d0683be",
"tables": {
"api_keys": {
"name": "api_keys",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"key_hash": {
"name": "key_hash",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"description": {
"name": "description",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expires_at": {
"name": "expires_at",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"updated_at": {
"name": "updated_at",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"api_keys_key_hash_unique": {
"name": "api_keys_key_hash_unique",
"columns": [
"key_hash"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"downloads": {
"name": "downloads",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"torrent_filename": {
"name": "torrent_filename",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"peer_id": {
"name": "peer_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"peer_name": {
"name": "peer_name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"item_id": {
"name": "item_id",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"filename": {
"name": "filename",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"dest_path": {
"name": "dest_path",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"part_path": {
"name": "part_path",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"release_size": {
"name": "release_size",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"release_json": {
"name": "release_json",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"expected_bytes": {
"name": "expected_bytes",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expected_bytes_source": {
"name": "expected_bytes_source",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expected_bytes_mismatch": {
"name": "expected_bytes_mismatch",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
},
"downloaded_bytes": {
"name": "downloaded_bytes",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"attempts": {
"name": "attempts",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
},
"status": {
"name": "status",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"started_at": {
"name": "started_at",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"updated_at": {
"name": "updated_at",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"completed_at": {
"name": "completed_at",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"error": {
"name": "error",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"qb_category": {
"name": "qb_category",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"qb_source_server": {
"name": "qb_source_server",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {
"downloads_status_idx": {
"name": "downloads_status_idx",
"columns": [
"status"
],
"isUnique": false
},
"downloads_updated_at_idx": {
"name": "downloads_updated_at_idx",
"columns": [
"updated_at"
],
"isUnique": false
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {
"downloads_status_check": {
"name": "downloads_status_check",
"value": "\"downloads\".\"status\" in ('downloading', 'import_queued', 'imported', 'failed')"
},
"downloads_expected_bytes_source_check": {
"name": "downloads_expected_bytes_source_check",
"value": "\"downloads\".\"expected_bytes_source\" is null or \"downloads\".\"expected_bytes_source\" in ('content_length', 'content_range', 'release_size')"
}
}
}
},
"views": {},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
9 changes: 8 additions & 1 deletion apps/backend/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
"when": 1782255959947,
"tag": "0005_young_sabretooth",
"breakpoints": true
},
{
"idx": 6,
"version": "6",
"when": 1782296886480,
"tag": "0006_gigantic_otto_octavius",
"breakpoints": true
}
]
}
}
4 changes: 3 additions & 1 deletion apps/backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AppConfig } from './lib/config'
import type { Envs } from './lib/envs'
import type { ConnectorManager } from './lib/servers'
import type { ApiKeysRepository } from './modules/api-keys/api-keys.repository'
import type { DownloadsRepository } from './modules/downloads/downloads.repository'
import type { DownloadsService } from './modules/downloads/downloads.service'
import { httpInstrumentationMiddleware } from '@hono/otel'
Expand Down Expand Up @@ -28,6 +29,7 @@ import { getTorznabRouter } from './modules/torznab/torznab.router'
interface AppServices {
downloadsRepository?: DownloadsRepository
downloadsService?: DownloadsService
apiKeysRepository?: ApiKeysRepository
}

// Only the live `servers`/`peers` getters are used here, so accept the structural
Expand Down Expand Up @@ -94,7 +96,7 @@ export function getApp(envs: Envs, config: AppConfig, connManager: { servers: Co
app.route('/api/v2', getQbittorrentRouter(qbController))
}

app.use('*', requireApiKey(config.jack?.apiKey ?? ''))
app.use('*', requireApiKey(config.jack?.apiKey ?? '', services.apiKeysRepository))

app.route('/servers', serversRouter)
app.route('/items', itemsRouter)
Expand Down
15 changes: 15 additions & 0 deletions apps/backend/src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ export const downloads = sqliteTable('downloads', {

export type DownloadRow = typeof downloads.$inferSelect
export type NewDownloadRow = typeof downloads.$inferInsert

export const apiKeys = sqliteTable('api_keys', {
id: integer('id').primaryKey({ autoIncrement: true }),
// .unique() already creates a unique index on key_hash, which the planner uses
// for every findByHash lookup — no separate index needed.
keyHash: text('key_hash').notNull().unique(),
name: text('name'),
description: text('description'),
expiresAt: text('expires_at'),
createdAt: text('created_at').notNull(),
updatedAt: text('updated_at').notNull(),
})

export type ApiKeyRow = typeof apiKeys.$inferSelect
export type NewApiKeyRow = typeof apiKeys.$inferInsert
5 changes: 4 additions & 1 deletion apps/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ConnectorManager } from './lib/servers'
import { PROTOCOL_VERSION } from './lib/version'
import { logger } from './logger'
import { getManagementApp } from './management-app'
import { ApiKeysRepository } from './modules/api-keys/api-keys.repository'
import { ConfigService } from './modules/config/config.service'
import { DownloadsRepository } from './modules/downloads/downloads.repository'
import { DownloadsService } from './modules/downloads/downloads.service'
Expand All @@ -35,6 +36,7 @@ await connectorManager.initAll()

const database = await openDatabase({ appConfigPath: envs.APP_CONFIG_PATH })
const downloadsRepository = new DownloadsRepository(database.db)
const apiKeysRepository = new ApiKeysRepository(database.db)

// Seed the management service from the shared raw object returned by getAppConfig
// so the service's persisted state can never diverge from the loaded runtime config.
Expand All @@ -46,7 +48,7 @@ const downloadsService = config.downloads
? new DownloadsService(config.downloads, connectorManager, downloadsRepository)
: undefined

const app = getApp(envs, config, connectorManager, { downloadsRepository, downloadsService })
const app = getApp(envs, config, connectorManager, { downloadsRepository, downloadsService, apiKeysRepository })
const server = Bun.serve({
fetch: app.fetch,
})
Expand Down Expand Up @@ -76,6 +78,7 @@ function startManagementServer() {
connectors: connectorManager,
configService,
downloadsRepository,
apiKeysRepository,
})
const instance = Bun.serve({ port: envs.MANAGEMENT_PORT, fetch: managementApp.fetch })
logger.info({ port: instance.port }, 'Management API listening')
Expand Down
Loading