ipfs: make API URL configurable, reorganize config

This commit is contained in:
Alex Gleason 2023-09-07 19:22:28 -05:00
parent c4af44d582
commit c40f10539d
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 36 additions and 20 deletions

View File

@ -7,16 +7,6 @@ await dotenv.load({
examplePath: null,
});
const optionalBooleanSchema = z
.enum(['true', 'false'])
.optional()
.transform((value) => value !== undefined ? value === 'true' : undefined);
const optionalNumberSchema = z
.string()
.optional()
.transform((value) => value !== undefined ? Number(value) : undefined);
/** Application-wide configuration. */
const Conf = {
/** Ditto admin secret key in nip19 format. This is the way it's configured by an admin. */
@ -98,13 +88,37 @@ const Conf = {
return optionalBooleanSchema.parse(Deno.env.get('S3_USE_SSL'));
},
},
ipfs: {
/** Base URL for private IPFS API calls. */
get apiUrl() {
return Deno.env.get('IPFS_API_URL') || 'http://localhost:5001';
},
},
/** Domain of the Ditto server as a `URL` object, for easily grabbing the `hostname`, etc. */
get url() {
return new URL(Conf.localDomain);
},
/** Merges the path with the localDomain. */
local(path: string): string {
const url = new URL(path.startsWith('/') ? path : new URL(path).pathname, Conf.localDomain);
return mergePaths(Conf.localDomain, path);
},
};
const optionalBooleanSchema = z
.enum(['true', 'false'])
.optional()
.transform((value) => value !== undefined ? value === 'true' : undefined);
const optionalNumberSchema = z
.string()
.optional()
.transform((value) => value !== undefined ? Number(value) : undefined);
function mergePaths(base: string, path: string) {
const url = new URL(
path.startsWith('/') ? path : new URL(path).pathname,
base,
);
if (!path.startsWith('/')) {
// Copy query parameters from the original URL to the new URL
@ -113,7 +127,6 @@ const Conf = {
}
return url.toString();
},
};
}
export { Conf };

View File

@ -1,3 +1,4 @@
import { Conf } from '@/config.ts';
import { z } from '@/deps.ts';
import type { Uploader } from './types.ts';
@ -9,10 +10,12 @@ const ipfsAddResultSchema = z.object({
});
const ipfsUploader: Uploader = async (file) => {
const url = new URL('/api/v0/add', Conf.ipfs.apiUrl);
const formData = new FormData();
formData.append('file', file);
const response = await fetch('http://localhost:5001/api/v0/add', {
const response = await fetch(url, {
method: 'POST',
body: formData,
});