earthstar-project / earthstar-pub

Example of a cloud server hosting Earthstar workspaces
GNU Affero General Public License v3.0
11 stars 4 forks source link

Persist to disk (switch to StorageSqlite) #1

Closed cinnamon-bun closed 4 years ago

cinnamon-bun commented 4 years ago

Right now we're using the in-memory Earthstar storage, which forgets everything when the pub is restarted.

Add an option to store each workspace in its own sqlite file in a configurable directory.

Places to make changes:

Changes:

How to validate a workspace address and use it as a filename

A workspace address looks like +gardenclub.jaoidf23jdoq9dj. Remove the leading + and append .sqlite to make a filename. Make sure to validate the string as a proper workspace address using ValidatorES4._checkWorkspaceIsValid from the earthstar package. (Valid workspace addresses are safe as filenames.)

import { ValidatorES4, isErr, notErr } from 'earthstar';
let addr = '+foo.xxxxxxx';
let err = ValidatorES4._checkWorkspaceIsValid(addr);
if (isErr(err)) {
    console.error('bad workspace address: ' + err.message);
} else {
    // addr is now a valid workspace address
    let filename = addr.slice(1) + '.sqlite';  // remove leading '+'
    // filename is now is guaranteed to be safe for use as a filename
}
cinnamon-bun commented 4 years ago

Done! Also updated the demo pub:

https://earthstar-demo-pub-v5-a.glitch.me/

https://glitch.com/~earthstar-demo-pub-v5-a

This is released as earthstar-pub 5.3.2