~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
439 lines · 17.4 KB Raw History
const std = @import("std");

pub const Options = struct {
    host: []const u8 = "127.0.0.1",
    port: u16 = 2583,
    db_path: []const u8 = "dev/zds.sqlite3",
    public_url: ?[]const u8 = null,
    server_did: ?[]const u8 = null,
    plc_directory: ?[]const u8 = null,
    plc_rotation_key: ?[]const u8 = null,
    recovery_did_key: ?[]const u8 = null,
    mail_provider: ?[]const u8 = null,
    email_from: ?[]const u8 = null,
    resend_api_key: ?[]const u8 = null,
    comail_api_key: ?[]const u8 = null,
    comail_did: ?[]const u8 = null,
    blob_upload_limit: ?usize = null,
    blobstore_path: ?[]const u8 = null,
    handle_domains: ?[]const u8 = null,
    crawlers: ?[]const u8 = null,
    operator_handle: ?[]const u8 = null,
    max_concurrent_repo_exports: ?usize = null,
    db_readers: ?usize = null,
    repo_export_cache_bytes: ?usize = null,
    seq_floor: ?usize = null,
    proxy_service_did: ?[]const u8 = null,
    proxy_service_id: ?[]const u8 = null,
    proxy_service_url: ?[]const u8 = null,
    jwt_secret: ?[]const u8 = null,
    dpop_secret: ?[]const u8 = null,
    admin_token: ?[]const u8 = null,
    invite_required: bool = false,
    plc_tombstone_on_delete: bool = false,
    permissioned_data: bool = false,
    allow_private_fetch: bool = false,
    log_level: ?[]const u8 = null,
    /// Presentation only: which skin the homepage and OAuth cards wear.
    web_skin: ?[]const u8 = null,
    web_font: ?[]const u8 = null,
    /// environment only: the blob mirror bucket and its S3 settings
    blob_mirror: BlobMirror = .{},

    pub const BlobMirror = struct {
        bucket: ?[]const u8 = null,
        prefix: []const u8 = "blobs/",
        path_style: bool = false,
        endpoint: ?[]const u8 = null,
        region: []const u8 = "auto",
        access_key_id: ?[]const u8 = null,
        secret_access_key: ?[]const u8 = null,
        session_token: ?[]const u8 = null,
    };
};

pub const ParseError = error{
    Help,
    InvalidLogLevel,
    MissingBlobUploadLimit,
    MissingBlobstorePath,
    MissingCrawlers,
    MissingOperatorHandle,
    MissingMaxConcurrentRepoExports,
    MissingSeqFloor,
    MissingProxyServiceDid,
    MissingProxyServiceId,
    MissingProxyServiceUrl,
    MissingDatabasePath,
    MissingEmailFrom,
    MissingHandleDomains,
    MissingHost,
    MissingAdminToken,
    MissingJwtSecret,
    MissingDpopSecret,
    MissingLogLevel,
    MissingWebSkin,
    MissingWebFont,
    MissingPlcDirectory,
    MissingPlcRotationKey,
    MissingRecoveryDidKey,
    MissingMailProvider,
    MissingPort,
    MissingPublicUrl,
    MissingResendApiKey,
    MissingComailApiKey,
    MissingComailDid,
    MissingServerDid,
    UnknownArgument,
} || std.fmt.ParseIntError;

pub fn parse(init: std.process.Init.Minimal) ParseError!Options {
    var options = Options{
        .host = env("ZDS_HOST") orelse "127.0.0.1",
        .port = try envU16("ZDS_PORT") orelse 2583,
        .db_path = env("ZDS_DB") orelse env("ZDS_DB_PATH") orelse "dev/zds.sqlite3",
        .public_url = env("ZDS_PUBLIC_URL"),
        .server_did = env("ZDS_SERVER_DID"),
        .plc_directory = env("ZDS_PLC_DIRECTORY"),
        .plc_rotation_key = env("ZDS_PLC_ROTATION_KEY"),
        .recovery_did_key = env("ZDS_RECOVERY_DID_KEY"),
        .mail_provider = env("ZDS_MAIL_PROVIDER"),
        .email_from = env("ZDS_EMAIL_FROM"),
        .resend_api_key = env("ZDS_RESEND_API_KEY"),
        .comail_api_key = env("ZDS_COMAIL_API_KEY"),
        .comail_did = env("ZDS_COMAIL_DID"),
        .blob_upload_limit = try envUsize("ZDS_BLOB_UPLOAD_LIMIT"),
        .blobstore_path = env("ZDS_BLOBSTORE_PATH"),
        .handle_domains = env("ZDS_HANDLE_DOMAINS"),
        .crawlers = env("ZDS_CRAWLERS"),
        .operator_handle = env("ZDS_OPERATOR_HANDLE"),
        .max_concurrent_repo_exports = try envUsize("ZDS_MAX_CONCURRENT_REPO_EXPORTS"),
        .db_readers = try envUsize("ZDS_DB_READERS"),
        .repo_export_cache_bytes = try envUsize("ZDS_REPO_EXPORT_CACHE_BYTES"),
        .seq_floor = try envUsize("ZDS_SEQ_FLOOR"),
        .proxy_service_did = env("ZDS_PROXY_SERVICE_DID"),
        .proxy_service_id = env("ZDS_PROXY_SERVICE_ID"),
        .proxy_service_url = env("ZDS_PROXY_SERVICE_URL"),
        .jwt_secret = env("ZDS_JWT_SECRET"),
        .dpop_secret = env("ZDS_DPOP_SECRET"),
        .admin_token = env("ZDS_ADMIN_TOKEN"),
        .invite_required = envBool("ZDS_INVITE_REQUIRED"),
        .plc_tombstone_on_delete = envBool("ZDS_PLC_TOMBSTONE_ON_DELETE"),
        .permissioned_data = envBool("ZDS_PERMISSIONED_DATA"),
        .allow_private_fetch = envBool("ZDS_ALLOW_PRIVATE_FETCH"),
        .log_level = env("ZDS_LOG_LEVEL"),
        .web_skin = env("ZDS_WEB_SKIN"),
        .web_font = env("ZDS_WEB_FONT"),
        .blob_mirror = .{
            .bucket = nonEmpty(env("ZDS_BLOB_MIRROR_BUCKET")),
            .prefix = env("ZDS_BLOB_MIRROR_PREFIX") orelse "blobs/",
            .path_style = envBool("ZDS_BLOB_MIRROR_PATH_STYLE"),
            .endpoint = nonEmpty(env("AWS_ENDPOINT_URL_S3")),
            .region = nonEmpty(env("AWS_REGION")) orelse "auto",
            .access_key_id = nonEmpty(env("AWS_ACCESS_KEY_ID")),
            .secret_access_key = nonEmpty(env("AWS_SECRET_ACCESS_KEY")),
            .session_token = nonEmpty(env("AWS_SESSION_TOKEN")),
        },
    };
    if (envBool("ZDS_DEBUG")) options.log_level = "debug";

    var args = std.process.Args.Iterator.init(init.args);
    _ = args.next();
    while (args.next()) |arg| {
        if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) return error.Help;
        if (try parseSplitArg(&options, arg, &args)) continue;
        if (try parseJoinedArg(&options, arg)) continue;
        return error.UnknownArgument;
    }

    return options;
}

pub fn usage() void {
    std.debug.print(
        \\usage: zds [--host HOST] [--port PORT] [--db PATH] [--public-url URL] [--server-did DID]
        \\           [--plc-rotation-key KEY] [--recovery-did-key DIDKEY]
        \\           [--mail-provider comail|resend] [--email-from ADDRESS]
        \\           [--blob-upload-limit BYTES] [--blobstore-path PATH]
        \\           [--handle-domains DOMAINS] [--crawlers URLS]
        \\           [--max-concurrent-repo-exports N] [--seq-floor SEQ] [--jwt-secret SECRET]
        \\           [--dpop-secret SECRET]
        \\           [--proxy-service-did DID] [--proxy-service-id ID] [--proxy-service-url URL]
        \\           [--admin-token TOKEN] [--invite-required] [--plc-tombstone-on-delete]
        \\           [--log-level error|info|debug] [--debug] [--web-skin glass|paper|garden]
        \\           [--web-font nunito|inter|fraunces|system|<file.woff2>]
        \\
        \\Runs a local PDS-shaped HTTP server.
        \\Environment defaults use ZDS_HOST, ZDS_PORT, and ZDS_DB.
        \\DOMAINS is a comma-separated list of leading-dot suffixes such as ".pds.example.com".
        \\URLS is a comma-separated crawler list, defaulting to bsky.network and vsky.network.
        \\
    , .{});
}

fn parseSplitArg(options: *Options, arg: []const u8, args: *std.process.Args.Iterator) ParseError!bool {
    if (std.mem.eql(u8, arg, "--debug")) {
        options.log_level = "debug";
        return true;
    }
    if (std.mem.eql(u8, arg, "--invite-required")) {
        options.invite_required = true;
        return true;
    }
    if (std.mem.eql(u8, arg, "--plc-tombstone-on-delete")) {
        options.plc_tombstone_on_delete = true;
        return true;
    }
    if (std.mem.eql(u8, arg, "--port")) {
        options.port = try std.fmt.parseInt(u16, args.next() orelse return error.MissingPort, 10);
        return true;
    }
    if (std.mem.eql(u8, arg, "--host")) {
        options.host = args.next() orelse return error.MissingHost;
        return true;
    }
    if (std.mem.eql(u8, arg, "--db")) {
        options.db_path = args.next() orelse return error.MissingDatabasePath;
        return true;
    }
    if (std.mem.eql(u8, arg, "--public-url")) {
        options.public_url = args.next() orelse return error.MissingPublicUrl;
        return true;
    }
    if (std.mem.eql(u8, arg, "--server-did")) {
        options.server_did = args.next() orelse return error.MissingServerDid;
        return true;
    }
    if (std.mem.eql(u8, arg, "--plc-directory")) {
        options.plc_directory = args.next() orelse return error.MissingPlcDirectory;
        return true;
    }
    if (std.mem.eql(u8, arg, "--plc-rotation-key")) {
        options.plc_rotation_key = args.next() orelse return error.MissingPlcRotationKey;
        return true;
    }
    if (std.mem.eql(u8, arg, "--recovery-did-key")) {
        options.recovery_did_key = args.next() orelse return error.MissingRecoveryDidKey;
        return true;
    }
    if (std.mem.eql(u8, arg, "--mail-provider")) {
        options.mail_provider = args.next() orelse return error.MissingMailProvider;
        return true;
    }
    if (std.mem.eql(u8, arg, "--email-from")) {
        options.email_from = args.next() orelse return error.MissingEmailFrom;
        return true;
    }
    if (std.mem.eql(u8, arg, "--resend-api-key")) {
        options.resend_api_key = args.next() orelse return error.MissingResendApiKey;
        return true;
    }
    if (std.mem.eql(u8, arg, "--comail-api-key")) {
        options.comail_api_key = args.next() orelse return error.MissingComailApiKey;
        return true;
    }
    if (std.mem.eql(u8, arg, "--comail-did")) {
        options.comail_did = args.next() orelse return error.MissingComailDid;
        return true;
    }
    if (std.mem.eql(u8, arg, "--blob-upload-limit")) {
        options.blob_upload_limit = try std.fmt.parseInt(usize, args.next() orelse return error.MissingBlobUploadLimit, 10);
        return true;
    }
    if (std.mem.eql(u8, arg, "--blobstore-path")) {
        options.blobstore_path = args.next() orelse return error.MissingBlobstorePath;
        return true;
    }
    if (std.mem.eql(u8, arg, "--handle-domains")) {
        options.handle_domains = args.next() orelse return error.MissingHandleDomains;
        return true;
    }
    if (std.mem.eql(u8, arg, "--crawlers")) {
        options.crawlers = args.next() orelse return error.MissingCrawlers;
        return true;
    }
    if (std.mem.eql(u8, arg, "--operator-handle")) {
        options.operator_handle = args.next() orelse return error.MissingOperatorHandle;
        return true;
    }
    if (std.mem.eql(u8, arg, "--seq-floor")) {
        options.seq_floor = try std.fmt.parseInt(usize, args.next() orelse return error.MissingSeqFloor, 10);
        return true;
    }
    if (std.mem.eql(u8, arg, "--max-concurrent-repo-exports")) {
        options.max_concurrent_repo_exports = try std.fmt.parseInt(usize, args.next() orelse return error.MissingMaxConcurrentRepoExports, 10);
        return true;
    }
    if (std.mem.eql(u8, arg, "--proxy-service-did")) {
        options.proxy_service_did = args.next() orelse return error.MissingProxyServiceDid;
        return true;
    }
    if (std.mem.eql(u8, arg, "--proxy-service-id")) {
        options.proxy_service_id = args.next() orelse return error.MissingProxyServiceId;
        return true;
    }
    if (std.mem.eql(u8, arg, "--proxy-service-url")) {
        options.proxy_service_url = args.next() orelse return error.MissingProxyServiceUrl;
        return true;
    }
    if (std.mem.eql(u8, arg, "--jwt-secret")) {
        options.jwt_secret = args.next() orelse return error.MissingJwtSecret;
        return true;
    }
    if (std.mem.eql(u8, arg, "--dpop-secret")) {
        options.dpop_secret = args.next() orelse return error.MissingDpopSecret;
        return true;
    }
    if (std.mem.eql(u8, arg, "--admin-token")) {
        options.admin_token = args.next() orelse return error.MissingAdminToken;
        return true;
    }
    if (std.mem.eql(u8, arg, "--log-level")) {
        options.log_level = args.next() orelse return error.MissingLogLevel;
        return true;
    }
    if (std.mem.eql(u8, arg, "--web-skin")) {
        options.web_skin = args.next() orelse return error.MissingWebSkin;
        return true;
    }
    if (std.mem.eql(u8, arg, "--web-font")) {
        options.web_font = args.next() orelse return error.MissingWebFont;
        return true;
    }
    return false;
}

fn parseJoinedArg(options: *Options, arg: []const u8) ParseError!bool {
    inline for (joined_string_options) |option| {
        if (std.mem.startsWith(u8, arg, option.flag)) {
            @field(options, option.field) = arg[option.flag.len..];
            return true;
        }
    }
    if (std.mem.startsWith(u8, arg, "--port=")) {
        options.port = try std.fmt.parseInt(u16, arg["--port=".len..], 10);
        return true;
    }
    if (std.mem.startsWith(u8, arg, "--blob-upload-limit=")) {
        options.blob_upload_limit = try std.fmt.parseInt(usize, arg["--blob-upload-limit=".len..], 10);
        return true;
    }
    if (std.mem.startsWith(u8, arg, "--seq-floor=")) {
        options.seq_floor = try std.fmt.parseInt(usize, arg["--seq-floor=".len..], 10);
        return true;
    }
    if (std.mem.startsWith(u8, arg, "--max-concurrent-repo-exports=")) {
        options.max_concurrent_repo_exports = try std.fmt.parseInt(usize, arg["--max-concurrent-repo-exports=".len..], 10);
        return true;
    }
    return false;
}

const JoinedStringOption = struct {
    flag: []const u8,
    field: []const u8,
};

const joined_string_options = [_]JoinedStringOption{
    .{ .flag = "--host=", .field = "host" },
    .{ .flag = "--db=", .field = "db_path" },
    .{ .flag = "--public-url=", .field = "public_url" },
    .{ .flag = "--server-did=", .field = "server_did" },
    .{ .flag = "--plc-directory=", .field = "plc_directory" },
    .{ .flag = "--plc-rotation-key=", .field = "plc_rotation_key" },
    .{ .flag = "--recovery-did-key=", .field = "recovery_did_key" },
    .{ .flag = "--mail-provider=", .field = "mail_provider" },
    .{ .flag = "--email-from=", .field = "email_from" },
    .{ .flag = "--resend-api-key=", .field = "resend_api_key" },
    .{ .flag = "--comail-api-key=", .field = "comail_api_key" },
    .{ .flag = "--comail-did=", .field = "comail_did" },
    .{ .flag = "--blobstore-path=", .field = "blobstore_path" },
    .{ .flag = "--handle-domains=", .field = "handle_domains" },
    .{ .flag = "--crawlers=", .field = "crawlers" },
    .{ .flag = "--operator-handle=", .field = "operator_handle" },
    .{ .flag = "--proxy-service-did=", .field = "proxy_service_did" },
    .{ .flag = "--proxy-service-id=", .field = "proxy_service_id" },
    .{ .flag = "--proxy-service-url=", .field = "proxy_service_url" },
    .{ .flag = "--jwt-secret=", .field = "jwt_secret" },
    .{ .flag = "--dpop-secret=", .field = "dpop_secret" },
    .{ .flag = "--admin-token=", .field = "admin_token" },
    .{ .flag = "--log-level=", .field = "log_level" },
    .{ .flag = "--web-skin=", .field = "web_skin" },
    .{ .flag = "--web-font=", .field = "web_font" },
};

fn env(name: [*:0]const u8) ?[]const u8 {
    return if (std.c.getenv(name)) |value| std.mem.span(value) else null;
}

fn envUsize(name: [*:0]const u8) !?usize {
    const value = env(name) orelse return null;
    return try std.fmt.parseInt(usize, value, 10);
}

fn envU16(name: [*:0]const u8) !?u16 {
    const value = env(name) orelse return null;
    return try std.fmt.parseInt(u16, value, 10);
}

fn nonEmpty(value: ?[]const u8) ?[]const u8 {
    const v = value orelse return null;
    return if (v.len == 0) null else v;
}

fn envBool(name: [*:0]const u8) bool {
    const value = env(name) orelse return false;
    return std.ascii.eqlIgnoreCase(value, "1") or
        std.ascii.eqlIgnoreCase(value, "true") or
        std.ascii.eqlIgnoreCase(value, "yes") or
        std.ascii.eqlIgnoreCase(value, "on");
}

test "parse split and joined arguments" {
    var argv = [_][*:0]const u8{
        "zds",
        "--host",
        "0.0.0.0",
        "--port=8080",
        "--db",
        "dev/test.sqlite3",
        "--plc-rotation-key=00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
        "--recovery-did-key",
        "did:key:zRecovery",
        "--mail-provider=comail",
        "--comail-api-key",
        "atmos_test",
        "--comail-did=did:plc:mailer",
        "--email-from",
        "ZDS <noreply@example.com>",
        "--debug",
        "--web-skin=paper",
        "--web-font=inter",
    };
    const init = std.process.Init.Minimal{ .environ = .empty, .args = .{ .vector = &argv } };

    const options = try parse(init);

    try std.testing.expectEqualStrings("0.0.0.0", options.host);
    try std.testing.expectEqual(@as(u16, 8080), options.port);
    try std.testing.expectEqualStrings("dev/test.sqlite3", options.db_path);
    try std.testing.expectEqualStrings("00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff", options.plc_rotation_key.?);
    try std.testing.expectEqualStrings("did:key:zRecovery", options.recovery_did_key.?);
    try std.testing.expectEqualStrings("comail", options.mail_provider.?);
    try std.testing.expectEqualStrings("atmos_test", options.comail_api_key.?);
    try std.testing.expectEqualStrings("did:plc:mailer", options.comail_did.?);
    try std.testing.expectEqualStrings("ZDS <noreply@example.com>", options.email_from.?);
    try std.testing.expectEqualStrings("debug", options.log_level.?);
    try std.testing.expectEqualStrings("paper", options.web_skin.?);
    try std.testing.expectEqualStrings("inter", options.web_font.?);
}

test "split arguments require a value" {
    var argv = [_][*:0]const u8{
        "zds",
        "--db",
    };
    const init = std.process.Init.Minimal{ .environ = .empty, .args = .{ .vector = &argv } };

    try std.testing.expectError(error.MissingDatabasePath, parse(init));
}