zat.dev/zds
atproto pds · mirror of https://tangled.org/zat.dev/zds
zds 0.25.1 Latest 2 packages, all images
0
0
const std = @import("std");
const zds = @import("zds");
const Io = std.Io;
var app_threaded_io: Io.Threaded = undefined;
pub const std_options_debug_threaded_io: ?*Io.Threaded = &app_threaded_io;
const io_worker_limit = 16;
/// Names the request a thread was serving before the default panic report,
/// whose stack trace can stop inside the HTTP library.
pub const panic = std.debug.FullPanic(panicWithRequest);
fn panicWithRequest(message: []const u8, return_address: ?usize) noreturn {
var buf: [768]u8 = undefined;
var w = std.Io.Writer.fixed(&buf);
zds.internal.request_context.describeForPanic(&w);
std.debug.print("{s}", .{w.buffered()});
std.debug.defaultPanic(message, return_address);
}
pub fn main(init: std.process.Init) !void {
const allocator = std.heap.smp_allocator;
app_threaded_io = Io.Threaded.init(allocator, .{
.async_limit = .limited(io_worker_limit),
.concurrent_limit = .limited(io_worker_limit),
});
const io = app_threaded_io.io();
const options = zds.internal.cli.parse(init.minimal) catch |err| switch (err) {
error.Help => {
zds.internal.cli.usage();
return;
},
else => return err,
};
if (options.public_url) |value| zds.core.config.setPublicUrl(value);
if (options.server_did) |value| {
zds.core.config.setServerDid(value);
} else if (options.public_url != null) {
try zds.core.config.deriveServerDidFromPublicUrl();
}
if (options.plc_directory) |value| zds.core.config.setPlcDirectory(value);
zds.core.config.setPlcRotationKey(options.plc_rotation_key);
zds.core.config.setRecoveryDidKey(options.recovery_did_key);
if (options.mail_provider) |value| {
const parsed = zds.core.config.parseMailProvider(value) orelse return error.InvalidMailProvider;
zds.core.config.setMailProvider(parsed);
}
zds.core.config.setEmailFrom(options.email_from);
zds.core.config.setResendApiKey(options.resend_api_key);
zds.core.config.setComailApiKey(options.comail_api_key);
zds.core.config.setComailDid(options.comail_did);
if (options.blob_upload_limit) |value| zds.core.config.setBlobUploadLimit(value);
if (options.blobstore_path) |value| zds.core.config.setBlobstorePath(value);
if (options.handle_domains) |value| {
try zds.internal.handles.validateDomains(value);
zds.core.config.setHandleDomains(value);
}
if (options.crawlers) |value| zds.core.config.setCrawlers(value);
if (options.operator_handle) |value| zds.core.config.setOperatorHandle(value);
if (options.max_concurrent_repo_exports) |value| zds.core.config.setMaxConcurrentRepoExports(value);
if (options.db_readers) |value| zds.core.config.setDbReaders(value);
if (options.repo_export_cache_bytes) |value| zds.core.config.setRepoExportCacheBytes(value);
if (options.seq_floor) |value| zds.core.config.setSeqFloor(value);
if (options.proxy_service_did) |value| zds.core.config.setProxyServiceDid(value);
if (options.proxy_service_id) |value| zds.core.config.setProxyServiceId(value);
if (options.proxy_service_url) |value| zds.core.config.setProxyServiceUrl(value);
if (options.jwt_secret) |value| zds.core.config.setJwtSecret(value);
zds.core.config.setDpopSecret(options.dpop_secret);
zds.core.config.setAdminToken(options.admin_token);
zds.core.config.setInviteRequired(options.invite_required);
zds.core.config.setPlcTombstoneOnDelete(options.plc_tombstone_on_delete);
zds.core.config.setPermissionedData(options.permissioned_data);
zds.core.config.setAllowPrivateFetch(options.allow_private_fetch);
if (options.log_level) |value| {
const parsed = zds.core.config.parseLogLevel(value) orelse return error.InvalidLogLevel;
zds.core.config.setLogLevel(parsed);
}
if (options.web_skin) |value| {
const parsed = zds.internal.web_skin.Skin.parse(value) orelse return error.InvalidWebSkin;
if (parsed == .garden and !zds.internal.web_skin.garden_built) {
std.log.err("ZDS_WEB_SKIN=garden needs a build with -Dweb-garden=true (or the -garden image)", .{});
return error.WebSkinNotBuilt;
}
zds.internal.web_skin.select(parsed);
}
if (options.web_font) |value| {
const font = zds.internal.web_skin.Font.parse(value) orelse font: {
if (!std.mem.endsWith(u8, value, ".woff2")) return error.InvalidWebFont;
break :font zds.internal.web_skin.Font{ .file = try Io.Dir.cwd().readFileAlloc(io, value, allocator, .limited(4 * 1024 * 1024)) };
};
zds.internal.web_skin.selectFont(font);
}
zds.core.clock.init(io);
const startup_started = zds.core.clock.monotonicNs();
zds.storage.blobstore.init(io, zds.core.config.blobstorePath());
if (options.blob_mirror.bucket) |bucket| {
const mirror = options.blob_mirror;
const config: zds.storage.s3.Config = .{
.endpoint = mirror.endpoint orelse return error.MissingBlobMirrorEndpoint,
.bucket = bucket,
.region = mirror.region,
.access_key_id = mirror.access_key_id orelse return error.MissingBlobMirrorCredentials,
.secret_access_key = mirror.secret_access_key orelse return error.MissingBlobMirrorCredentials,
.session_token = mirror.session_token,
.path_style = mirror.path_style,
};
try zds.storage.s3.validate(config);
zds.storage.blobstore.initMirror(config, mirror.prefix);
zds.core.log.info("blob mirror bucket={s} prefix={s} endpoint={s}\n", .{ bucket, mirror.prefix, config.endpoint });
}
zds.storage.eventlog.init(io);
try zds.storage.store.init(io, options.db_path);
zds.core.mail.setOutboxSink(zds.storage.store.recordOutboxMail);
if (zds.core.config.mailProvider() == .outbox) {
zds.core.log.info("mail provider is outbox: mail is kept for dev.zat.admin.listMail and never sent\n", .{});
}
const storage_ready = zds.core.clock.monotonicNs();
zds.core.log.info("startup storage_ms={d}\n", .{(storage_ready - startup_started) / std.time.ns_per_ms});
zds.storage.store.startBlobGarbageCollector();
zds.storage.blob_mirror.start();
if (zds.core.config.permissionedData()) {
try zds.atproto.space.startNotificationWorker();
try zds.atproto.identity.startKemKeyBackfill();
}
if (zds.core.config.inviteRequired()) {
if (try zds.storage.store.ensureBootstrapInviteCode(allocator, zds.core.config.publicUrl())) |code| {
zds.core.log.info("bootstrap invite code: {s}\n", .{code});
}
}
zds.storage.store.sequenceUnannouncedAccounts(allocator) catch |err| {
zds.core.log.err("startup sequence_unannounced_accounts failed err={s}\n", .{@errorName(err)});
};
zds.core.log.info("startup account_setup_ms={d}\n", .{(zds.core.clock.monotonicNs() - storage_ready) / std.time.ns_per_ms});
zds.atproto.sync.notifyCrawlers(true);
try zds.http.server.listen(io, .{ .host = options.host, .port = options.port });
}