Skip to content

Commit 41d2ec5

Browse files
committed
use env instead of cmd args
1 parent 8c4e307 commit 41d2ec5

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SimpleLink
22

3-
A very performant and light (2MB in memory) link shortener and tracker. Written in Rust and React and uses Postgres.
3+
A very performant and light (2MB in memory) link shortener and tracker. Written in Rust and React and uses Postgres or SQLite.
44

55
![MainView](readme_img/mainview.jpg)
66

@@ -10,38 +10,44 @@ A very performant and light (2MB in memory) link shortener and tracker. Written
1010

1111
### From Docker:
1212

13-
```Bash
13+
```bash
1414
docker run -p 8080:8080 \
1515
-e JWT_SECRET=change-me-in-production \
16+
17+
-e SIMPLELINK_PASS=your-secure-password \
1618
-v simplelink_data:/data \
1719
ghcr.io/waveringana/simplelink:v2
1820
```
1921

20-
Find the admin-setup-token pasted into the terminal output, or in admin-setup-token.txt in the container's root.
22+
### Environment Variables
23+
24+
- `JWT_SECRET`: Required. Used for JWT token generation
25+
- `SIMPLELINK_USER`: Optional. If set along with SIMPLELINK_PASS, creates an admin user on first run
26+
- `SIMPLELINK_PASS`: Optional. Admin user password
27+
- `DATABASE_URL`: Optional. Postgres connection string. If not set, uses SQLite
28+
- `INITIAL_LINKS`: Optional. Semicolon-separated list of initial links in format "url,code;url2,code2"
29+
- `SERVER_HOST`: Optional. Default: "127.0.0.1"
30+
- `SERVER_PORT`: Optional. Default: "8080"
2131

22-
This is needed to register with the frontend. (TODO, register admin account with ENV)
32+
If `SIMPLELINK_USER` and `SIMPLELINK_PASS` are not passed, an admin-setup-token is pasted to the console and as a text file in the project root.
2333

2434
### From Docker Compose:
2535

26-
Edit the docker-compose.yml file. It comes included with a postgressql db for use
36+
Edit the docker-compose.yml file. It comes included with a PostgreSQL db configuration.
2737

2838
## Build
2939

3040
### From Source
3141

3242
First configure .env.example and save it to .env
3343

34-
If DATABASE_URL is set, it will connect to a Postgres DB. If blank, it will use an sqlite db in /data
35-
3644
```bash
3745
git clone https://github.com/waveringana/simplelink && cd simplelink
3846
./build.sh
3947
cargo run
4048
```
4149

42-
On an empty database, an admin-setup-token.txt is created as well as pasted into the terminal output. This is needed to make the admin account.
43-
44-
Alternatively if you want a binary form
50+
Alternatively for a binary build:
4551

4652
```bash
4753
./build.sh --binary
@@ -55,10 +61,20 @@ then check /target/release for the binary named `SimpleGit`
5561
docker build -t simplelink .
5662
docker run -p 8080:8080 \
5763
-e JWT_SECRET=change-me-in-production \
64+
65+
-e SIMPLELINK_PASS=your-secure-password \
5866
-v simplelink_data:/data \
5967
simplelink
6068
```
6169

6270
### From Docker Compose
6371

6472
Adjust the included docker-compose.yml to your liking; it includes a postgres config as well.
73+
74+
## Features
75+
76+
- Support for both PostgreSQL and SQLite databases
77+
- Initial links can be configured via environment variables
78+
- Admin user can be created on first run via environment variables
79+
- Link click tracking and statistics
80+
- Lightweight and performant

src/main.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ use rust_embed::RustEmbed;
55
use simplelink::check_and_generate_admin_token;
66
use simplelink::{create_db_pool, run_migrations};
77
use simplelink::{handlers, AppState};
8-
use tracing::info;
8+
use sqlx::{Postgres, Sqlite};
9+
use tracing::{error, info};
910

11+
#[derive(Parser, Debug)]
12+
#[command(author, version, about, long_about = None)]
1013
#[derive(RustEmbed)]
1114
#[folder = "static/"]
1215
struct Asset;
@@ -35,6 +38,58 @@ async fn main() -> Result<()> {
3538
let pool = create_db_pool().await?;
3639
run_migrations(&pool).await?;
3740

41+
// First check if admin credentials are provided in environment variables
42+
let admin_credentials = match (
43+
std::env::var("SIMPLELINK_USER"),
44+
std::env::var("SIMPLELINK_PASS"),
45+
) {
46+
(Ok(user), Ok(pass)) => Some((user, pass)),
47+
_ => None,
48+
};
49+
50+
if let Some((email, password)) = admin_credentials {
51+
// Now check for existing users
52+
let user_count = match &pool {
53+
DatabasePool::Postgres(pool) => {
54+
let mut tx = pool.begin().await?;
55+
let count =
56+
sqlx::query_as::<Postgres, (i64,)>("SELECT COUNT(*)::bigint FROM users")
57+
.fetch_one(&mut *tx)
58+
.await?
59+
.0;
60+
tx.commit().await?;
61+
count
62+
}
63+
DatabasePool::Sqlite(pool) => {
64+
let mut tx = pool.begin().await?;
65+
let count = sqlx::query_as::<Sqlite, (i64,)>("SELECT COUNT(*) FROM users")
66+
.fetch_one(&mut *tx)
67+
.await?
68+
.0;
69+
tx.commit().await?;
70+
count
71+
}
72+
};
73+
74+
if user_count == 0 {
75+
info!("No users found, creating admin user: {}", email);
76+
match create_admin_user(&pool, &email, &password).await {
77+
Ok(_) => info!("Successfully created admin user"),
78+
Err(e) => {
79+
error!("Failed to create admin user: {}", e);
80+
return Err(anyhow::anyhow!("Failed to create admin user: {}", e));
81+
}
82+
}
83+
}
84+
} else {
85+
info!(
86+
"No admin credentials provided in environment variables, skipping admin user creation"
87+
);
88+
}
89+
90+
// Create initial links from environment variables
91+
create_initial_links(&pool).await?;
92+
3893
let admin_token = check_and_generate_admin_token(&pool).await?;
3994

4095
let state = AppState {

0 commit comments

Comments
 (0)