mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-04-05 10:52:23 +02:00
inbox: add health endpoint & docs (#64)
* inbox: add health endpoint && docs * inbox: update post endpoint uri * inbox: update docs & dockerfile --------- Co-authored-by: Abdullah Atta <abdullahatta@streetwriters.co>
This commit is contained in:
@@ -33,9 +33,15 @@ interface EncryptedInboxItem {
|
||||
alg: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypts raw data using OpenPGP with the recipient's public key
|
||||
*
|
||||
* @param {string} rawData - The plaintext data to encrypt
|
||||
* @param {string} rawPublicKey - The recipient's OpenPGP public key
|
||||
*/
|
||||
async function encrypt(
|
||||
rawData: string,
|
||||
rawPublicKey: string
|
||||
rawPublicKey: string,
|
||||
): Promise<EncryptedInboxItem> {
|
||||
const publicKey = await openpgp.readKey({ armoredKey: rawPublicKey });
|
||||
const message = await openpgp.createMessage({ text: rawData });
|
||||
@@ -57,11 +63,11 @@ async function getInboxPublicEncryptionKey(apiKey: string) {
|
||||
headers: {
|
||||
Authorization: apiKey,
|
||||
},
|
||||
}
|
||||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(
|
||||
`failed to fetch inbox public encryption key: ${await response.text()}`
|
||||
`failed to fetch inbox public encryption key: ${await response.text()}`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -71,7 +77,7 @@ async function getInboxPublicEncryptionKey(apiKey: string) {
|
||||
|
||||
async function postEncryptedInboxItem(
|
||||
apiKey: string,
|
||||
item: EncryptedInboxItem
|
||||
item: EncryptedInboxItem,
|
||||
) {
|
||||
const response = await fetch(`${NOTESNOOK_API_SERVER_URL}/inbox/items`, {
|
||||
method: "POST",
|
||||
@@ -92,9 +98,12 @@ app.use(
|
||||
rateLimit({
|
||||
windowMs: 1 * 60 * 1000, // 1 minute
|
||||
limit: 60,
|
||||
})
|
||||
}),
|
||||
);
|
||||
app.post("/inbox", async (req, res) => {
|
||||
app.get("/health", (_, res) => {
|
||||
return res.status(200).json({ status: "ok" });
|
||||
});
|
||||
app.post("/", async (req, res) => {
|
||||
try {
|
||||
const apiKey = req.headers["authorization"];
|
||||
if (!apiKey) {
|
||||
@@ -117,7 +126,7 @@ app.post("/inbox", async (req, res) => {
|
||||
|
||||
const encryptedItem = await encrypt(
|
||||
JSON.stringify(validationResult.data),
|
||||
inboxPublicKey
|
||||
inboxPublicKey,
|
||||
);
|
||||
console.log("[info] encrypted item");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user