Skip to content

Commit 4ded864

Browse files
committed
adding database support - static builds are tricky
1 parent 5bfc5d3 commit 4ded864

15 files changed

Lines changed: 266 additions & 32 deletions

File tree

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL=var/directory.sqlite

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.cargo/
22
/target/
33
/TODO.md
4+
/var/geoip-country.mmdb

Cargo.lock

Lines changed: 144 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,29 @@ edition = "2018"
66

77
[dependencies]
88
dns-lookup = "1.0.1"
9+
dotenv = "0.15.0"
910
hyper = "0.10.16"
1011
maxminddb = "0.13.0"
1112
regex = "1.3.6"
1213
rocket = "0.4.4"
1314

15+
[dependencies.diesel]
16+
version = "1.4.4"
17+
features = ["sqlite"]
18+
1419
[dependencies.hyper-sync-rustls]
1520
version = "0.3.0-rc.4"
1621
default-features = false
1722
features = ["client"]
1823

24+
[dependencies.libsqlite3-sys]
25+
version = "0.17.1"
26+
features = ["bundled"]
27+
1928
[dependencies.rocket_contrib]
2029
version = "0.4.4"
2130
default-features = false
22-
features = ["serve", "tera_templates"]
31+
features = ["diesel_sqlite_pool", "serve", "tera_templates"]
2332

2433
[dependencies.serde]
2534
version = "1.0.105"

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ LABEL maintainer="support@privatebin.org"
33

44
ARG GEOIP_MMDB
55
ENV GEOIP_MMDB $GEOIP_MMDB
6+
ARG ROCKET_DATABASES
7+
ENV ROCKET_DATABASES $ROCKET_DATABASES
68
ARG PORT
79
EXPOSE $PORT
810
USER 1000:1000
@@ -12,5 +14,6 @@ CMD ["directory"]
1214

1315
COPY css /css
1416
COPY img /img
17+
COPY migrations /bin/migrations
1518
COPY target/x86_64-unknown-linux-musl/release/directory /bin/
1619
COPY templates /bin/templates

Makefile

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,44 @@ NAME = directory
44
IMAGE = privatebin/$(NAME)
55
PORT = 8000
66
GEOIP_MMDB=var/geoip-country.mmdb
7+
DATABASE=var/directory.sqlite
8+
ROCKET_DATABASES="{directory={url=\"$(DATABASE)\"}}"
79

810
all: test build image run check clean ## Equivalent to "make test build image run check clean" (default).
911

1012
release: test build pack image run check clean ## Equivalent to "make test build pack image run check clean".
1113

1214
test: .cargo/registry ## Build and run the unit tests.
13-
docker run --rm -t --init -e GEOIP_MMDB="/home/rust/src/$(GEOIP_MMDB)" \
15+
docker run --rm -t --init \
16+
-e GEOIP_MMDB="$(GEOIP_MMDB)" \
17+
-e ROCKET_DATABASES="$(ROCKET_DATABASES)" \
1418
-v "$(CURDIR)":/home/rust/src \
1519
-v "$(CURDIR)"/.cargo/registry:/home/rust/.cargo/registry \
16-
ekidd/rust-musl-builder:nightly-2020-03-12 \
20+
ekidd/rust-musl-builder:nightly-2020-03-12-sqlite \
1721
cargo test --release # -- --nocapture
1822

1923
build: .cargo/registry ## Build the binary for release.
20-
docker run --rm -t --init -e GEOIP_MMDB="/home/rust/src/$(GEOIP_MMDB)" \
24+
docker run --rm -t --init \
25+
-e GEOIP_MMDB="$(GEOIP_MMDB)" \
26+
-e ROCKET_DATABASES="$(ROCKET_DATABASES)" \
2127
-v "$(CURDIR)":/home/rust/src \
2228
-v "$(CURDIR)"/.cargo/registry:/home/rust/.cargo/registry \
23-
ekidd/rust-musl-builder:nightly-2020-03-12 \
29+
ekidd/rust-musl-builder:nightly-2020-03-12-sqlite \
2430
cargo build --release
2531

2632
pack: ## Strips and compresses the binary to reduce it's size, only intended for the release.
2733
strip target/x86_64-unknown-linux-musl/release/directory
2834
upx --ultra-brute target/x86_64-unknown-linux-musl/release/directory
2935

3036
image: ## Build the container image.
31-
docker build --build-arg PORT=$(PORT) --build-arg GEOIP_MMDB="/$(GEOIP_MMDB)" -t $(IMAGE) .
37+
docker build --build-arg PORT=$(PORT) \
38+
--build-arg GEOIP_MMDB="/$(GEOIP_MMDB)" \
39+
--build-arg ROCKET_DATABASES='{directory={url="/'$(DATABASE)'"}}' \
40+
-t $(IMAGE) .
3241

3342
run: ## Run a container from the image.
3443
docker run -d --init --name $(NAME) -p=$(PORT):$(PORT) \
35-
--read-only -v "$(CURDIR)":/var:ro --restart=always $(IMAGE)
44+
--read-only -v "$(CURDIR)/var":/var --restart=always $(IMAGE)
3645

3746
check: ## Launch tests to verify that the service works as expected, requires a running container.
3847
@sleep 1
@@ -46,6 +55,7 @@ check: ## Launch tests to verify that the service works as expected, requires a
4655
clean: ## Stops and removes the running container.
4756
docker stop $(NAME)
4857
docker rm $(NAME)
58+
git checkout $(DATABASE)
4959

5060
help: ## Displays these usage instructions.
5161
@echo "Usage: make <target(s)>"

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
# Directory
22

33
Rust based directory application to collect lists of federated instances of a
4-
Software
4+
software.
5+
6+
## Configuration
7+
8+
The image supports the use of the following environment variables:
9+
10+
- `GEOIP_MMDB`: path to the GeoIP database, in MaxMind format
11+
- `ROCKET_DATABASES`: [database dict](https://api.rocket.rs/v0.4/rocket_contrib/databases/index.html#environment-variables)
12+
for Diesel SQLite library integration into Rocket
513

614
## Volumes
715

8-
- `/data`: Data
16+
- `/var/directory.sqlite`: Database file, needs to be writeable
917
- `/var/geoip-country.mmdb`: GeoIP database, country level is sufficient
1018

1119
## Network ports

0 commit comments

Comments
 (0)