Skip to content
Draft
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
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"workspace": [
"./packages/core",
"./packages/server",
"./packages/web",
"./packages/web/adapter/lib",
"./packages/cli"
Expand Down
6 changes: 6 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const cli = new cliffy.Command()
asset_folder: forager_helpers.config.web.asset_folder,
port: forager_helpers.config.web.port,
logger: forager_helpers.config.web.logger,
forager,
config: forager_helpers.config,
kit: {
env: {
FORAGER_INSTANCE: forager as any,
Expand Down Expand Up @@ -111,6 +113,8 @@ const cli = new cliffy.Command()
asset_folder: forager_helpers.config.web.asset_folder,
port: forager_helpers.config.web.port,
logger: forager_helpers.config.web.logger,
forager,
config: forager_helpers.config,
kit: {
env: {
FORAGER_INSTANCE: forager as any,
Expand Down
21 changes: 21 additions & 0 deletions packages/server/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Andrew Kaiser

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions packages/server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @forager/server

Framework-agnostic HTTP server for [Forager](https://github.com/andykais/forager). Owns the RPC endpoint and the `/files/media_file` / `/files/thumbnail` streaming routes, and is intended to be embedded by `@forager/web` (SvelteKit + browser frontend) and the upcoming `@forager/desktop` Tauri shell.

The server is implemented as a request handler over plain `Request` / `Response`, with no dependency on SvelteKit, Oak, or any other framework.

## Usage

```ts
import { Forager } from '@forager/core'
import { ForagerServer } from '@forager/server'
import { load_config } from '@forager/server/config'

const config = await load_config('./forager.yml')
const forager = new Forager(config.core)
forager.init()

const server = new ForagerServer({ forager, config })
await server.start({ hostname: '127.0.0.1', port: 8000 })
```

## Exports

- `@forager/server` — `ForagerServer`, `Api`, `ApiSpec`, request handlers, types.
- `@forager/server/api` — RPC `Api` class (used by SvelteKit and Tauri client type-only imports).
- `@forager/server/config` — `PackagesConfig` Zod schema and `load_config` helper.
- `@forager/server/handlers` — Pure `(request, ctx) => Response` handler functions for `/rpc` and `/files/*`.
25 changes: 25 additions & 0 deletions packages/server/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@forager/server",
"version": "0.0.1",
"license": "MIT",

"imports": {
"@andykais/ts-rpc": "jsr:@andykais/ts-rpc@^0.2.2",
"@opentelemetry/api": "npm:@opentelemetry/api@1.9.0"
},

"exports": {
".": "./src/mod.ts",
"./api": "./src/api.ts",
"./config": "./src/config.ts",
"./handlers": "./src/handlers/mod.ts"
},

"publish": {
"include": [
"README.md",
"LICENSE",
"src/**/*.ts"
]
}
}
40 changes: 40 additions & 0 deletions packages/server/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { type Forager } from '@forager/core'
import * as rpc from '@andykais/ts-rpc/adapters/sveltekit.ts'
import type { Config } from './config.ts'


export interface ApiContext {
forager: Forager
config: Config
}

class ForagerTagApi extends rpc.ApiController<ApiContext> {
search = this.context.forager.tag.search
get = this.context.forager.tag.get
update = this.context.forager.tag.update
alias_create = this.context.forager.tag.alias_create
alias_delete = this.context.forager.tag.alias_delete
parent_create = this.context.forager.tag.parent_create
parent_delete = this.context.forager.tag.parent_delete
}

class ForagerApi extends rpc.ApiController<ApiContext> {
media = this.context.forager.media
series = this.context.forager.series
tag = this.module(ForagerTagApi)
views = this.context.forager.views
}

export class Api extends rpc.ApiController<ApiContext> {
forager = this.module(ForagerApi)

config(): Config {
return this.context.config
}

server_time(): Date {
return new Date()
}
}

export type ApiSpec = rpc.InferSpec<typeof Api>
106 changes: 106 additions & 0 deletions packages/server/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import * as yaml from '@std/yaml'
import z from 'zod'
import * as forager from '@forager/core'


const LogLevel = z.enum(['SILENT', 'ERROR', 'WARN', 'INFO', 'DEBUG'])

const Keybind = (default_keybind: string) => z.union([z.string(), z.string().array()]).default(default_keybind).transform(keybind => {
if (Array.isArray(keybind)) return keybind
else return [keybind]
})

export const PackagesConfig = z.object({
core: forager.parsers.ForagerConfig,

web: z.object({

// TODO reuse core editing config parser
editing: z.object({
editor: z.string(),
overwrite: z.boolean().default(true),
}).strict().optional(),

port: z.number().default(8000),
asset_folder: z.string(),

logger: z.object({
level: LogLevel.default('INFO'),
}).strict().prefault({}),

ui_defaults: z.object({

search: z.object({
advanced_filters: z.object({
hide: z.boolean().default(true)
}).strict().prefault({})
}).strict().prefault({}),

media_list: z.object({
thumbnail_size: z.number().default(110),
thumbnail_shape: z.enum(['square', 'original']).prefault('original')
}).strict().prefault({}),

sidebar: z.object({
hide: z.boolean().default(true),
size: z.number().default(200),
tags: z.object({
order: z.object({
group: z.string(),
}).array().prefault([])
}).strict().prefault({}),
}).strict().prefault({}),

media_view: z.object({
filmstrip: z.object({
enabled: z.boolean().default(false),
thumbnail_size: z.number().default(100),
}).strict().optional().transform(f => f ?? {enabled: false})
}).strict().prefault({}),
}).strict().prefault({}),

shortcuts: z.object({
OpenMedia: Keybind('Enter'),
Escape: Keybind('Escape'),

NextMedia: Keybind('ArrowRight'),
PrevMedia: Keybind('ArrowLeft'),

NextTagSuggestion: Keybind('ArrowDown'),
PrevTagSuggestion: Keybind('ArrowUp'),

CopyMedia: Keybind('Ctrl-C'),

ToggleSidebar: Keybind('Ctrl-Backslash'),

// DownMedia: Keybind('ArrowDown'),
// UpMedia: Keybind('ArrowUp'),
ToggleFitMedia: Keybind('Ctrl-Space'),
ToggleFullScreen: Keybind('KeyF'),
PlayPauseMedia: Keybind('Space'),
ToggleVideoMute: Keybind('KeyM'),
ToggleMediaControls: Keybind('Ctrl-C'),

Search: Keybind('Slash'),
AddTag: Keybind('Ctrl-M'),

// ToggleVideoPreviewVsThumbails: Keybind('KeyT'),

Star0: Keybind('Digit0'),
Star1: Keybind('Digit1'),
Star2: Keybind('Digit2'),
Star3: Keybind('Digit3'),
Star4: Keybind('Digit4'),
Star5: Keybind('Digit5'),
}).strict().prefault({}),
}).strict(),
}).strict()

export type Config = z.infer<typeof PackagesConfig>


export async function load_config(config_filepath: string): Promise<Config> {
const file_contents_string = await Deno.readTextFile(config_filepath)
const file_contents = yaml.parse(file_contents_string)
return PackagesConfig.parse(file_contents)
}
Loading
Loading