Skip to content

Commit

Permalink
Add feature to embed Lemmy for production use (#73)
Browse files Browse the repository at this point in the history
* Add feature to embed Lemmy for production use

* update readme

* fix drone
  • Loading branch information
Nutomic authored Dec 23, 2022
1 parent 39a9d1f commit 1e38568
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ steps:
commands:
- /root/.cargo/bin/cargo fmt -- --check

- name: cargo check
image: rust:1.66-buster
environment:
CARGO_HOME: .cargo
commands:
- cargo check
- cargo check --features embed-lemmy

- name: cargo clippy
image: rust:1.66-buster
environment:
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ lto = "thin"
strip = "symbols"
debug = 0

[features]
embed-lemmy = ["lemmy_server", "send_wrapper"]

[dependencies]
log = "0.4.17"
env_logger = { version = "0.9.1", features = ["termcolor", "humantime", "atty"], default-features = false }
lemmy_api_common = { git = "https://github.com/LemmyNet/lemmy.git" }
lemmy_db_schema = { git = "https://github.com/LemmyNet/lemmy.git" }
lemmy_db_views = { git = "https://github.com/LemmyNet/lemmy.git" }
lemmy_db_views_actor = { git = "https://github.com/LemmyNet/lemmy.git" }
lemmy_server = { git = "https://github.com/LemmyNet/lemmy.git", optional = true }
once_cell = "1.15.0"
anyhow = "1.0.66"
rocket = { version = "0.5.0-rc.2", default-features = false }
Expand All @@ -35,6 +39,7 @@ rand = "0.8.5"
deser-hjson = "1.0.2"
json-gettext = { version = "4.0.3", default-features = false }
tokio = "1.23.0"
send_wrapper = { version = "0.6.0", features = ["futures"], optional = true }

[dev-dependencies]
serial_test = "0.9.0"
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,26 @@ Note, you must subscribe manually to remote communities, so that new activities

## Development

Execute the following command, with a Lemmy instance of your choice:
First install dependencies and setup the database.
```
sudo apt install git cargo postgresql libssl-dev pkg-config
sudo systemctl start postgresql
sudo -u postgres psql -c "create user lemmy with password 'password' superuser;" -U postgres
sudo -u postgres psql -c 'create database lemmy with owner lemmy;' -U postgres
```

Then start LemmyBB with embedded Lemmy instance.
```bash
git clone https://github.com/LemmyNet/lemmyBB.git --recursive
cd lemmyBB
LEMMYBB_BACKEND=https://lemmy.ml cargo run
cargo run --features embed-lemmy
```

You can also run a local development instance of Lemmy, either [native](https://join-lemmy.org/docs/en/contributing/local_development.html) or in [Docker](https://join-lemmy.org/docs/en/contributing/docker_development.html), and connect to it with:
Then open http://127.0.0.1:1244 in your browser.

It can also be useful to use a production instance as backend, for example if you notice a bug on a specific instance but don't know what causes it. To do this, run the following command with an instance of your choice.
```
LEMMYBB_BACKEND=http://localhost:8536 cargo run
LEMMYBB_BACKEND=https://lemmy.ml cargo run
```

## License
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ async fn main() -> Result<(), Error> {
Env::default().default_filter_or("warn,lemmybb=debug,rocket=info,handlebars=info"),
)
.init();
let _ = init_rocket()?.launch().await?;
let rocket = init_rocket()?.launch();
#[cfg(not(feature = "embed-lemmy"))]
let _ = rocket.await?;
#[cfg(feature = "embed-lemmy")]
{
let lemmy = send_wrapper::SendWrapper::new(lemmy_server::start_lemmy_server());
let (frontend, backend) = tokio::join!(rocket, lemmy);
let _ = frontend.unwrap();
if let Err(e) = backend {
log::error!("{}", e);
}
}
Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ fn random_string() -> String {
async fn run_test<Fut: Future<Output = ()>>(
test: impl Fn(asynchronous::Client, Sensitive<String>) -> Fut,
) {
let local = LocalSet::new();
local
LocalSet::new()
.run_until(async move {
let backend = spawn_local(lemmy_server::start_lemmy_server());
let rocket = init_rocket().unwrap();
Expand Down

0 comments on commit 1e38568

Please sign in to comment.