Skip to content

Commit

Permalink
refactor!: add prefix to database_url env
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Feb 17, 2024
1 parent 2873966 commit 5137295
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DATABASE_URL = "sqlite://hatsu.sqlite3"
HATSU_DATABASE_URL = "sqlite://hatsu.sqlite3"
HATSU_DOMAIN = "hatsu.local"
HATSU_LISTEN_HOST = "0.0.0.0"
HATSU_LISTEN_PORT = "3939"
4 changes: 2 additions & 2 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ db *args='migration up':

# apply migrations to database.
db_migration *args='up':
just _cargo_ sea-orm-cli migrate {{args}} -d crates/db_migration
just _cargo_ sea-orm-cli migrate {{args}} -d crates/db_migration -u $HATSU_DATABASE_URL

# generate entities from database.
db_schema: (db_migration 'fresh')
just _cargo_ sea-orm-cli generate entity -l -o crates/db_schema/src
just _cargo_ sea-orm-cli generate entity -l -o crates/db_schema/src -u $HATSU_DATABASE_URL
2 changes: 1 addition & 1 deletion crates/utils/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub struct AppData {

#[derive(Clone, Debug)]
pub struct AppEnv {
pub database_url: String,
pub hatsu_access_token: Option<String>,
pub hatsu_database_url: String,
pub hatsu_domain: String,
pub hatsu_listen_host: String,
pub hatsu_listen_port: String,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/admins/environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ It is required unless it has a suffix (optional).

However, it may exist as a built-in preset (in the source code) or an example preset (in [`.env.example`](https://github.com/importantimport/hatsu/blob/main/.env.example))

## DATABASE_URL
## HATSU_DATABASE_URL

- default: `sqlite::memory:`
- example: `sqlite://hatsu.sqlite3`
Expand Down
2 changes: 1 addition & 1 deletion docs/src/admins/install-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ docker run -d \
--restart unless-stopped \
-p 3939:3939 \
-v /opt/hatsu/hatsu.sqlite3:/app/hatsu.sqlite3 \
-e DATABASE_URL=sqlite://hatsu.sqlite3 \
-e HATSU_DATABASE_URL=sqlite://hatsu.sqlite3 \
-e HATSU_DOMAIN={{hatsu-instance-domain}} \
-e HATSU_LISTEN_HOST=0.0.0.0 \
-e HATSU_PRIMARY_ACCOUNT={{your-static-site}} \
Expand Down
4 changes: 2 additions & 2 deletions examples/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ services:
# env_file:
# - .env
environment:
- DATABASE_URL=sqlite://hatsu.sqlite3
- HATSU_LISTEN_HOST=0.0.0.0
- HATSU_DATABASE_URL=sqlite://hatsu.sqlite3
- HATSU_DOMAIN=hatsu.example.com
- HATSU_LISTEN_HOST=0.0.0.0
- HATSU_PRIMARY_ACCOUNT=blog.example.com
volumes:
# - ./.env:/app/.env
Expand Down
4 changes: 2 additions & 2 deletions examples/docker-compose/litestream.docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ services:
# env_file:
# - .env
environment:
- DATABASE_URL=sqlite://hatsu.sqlite3
- HATSU_LISTEN_HOST=0.0.0.0
- HATSU_DATABASE_URL=sqlite://hatsu.sqlite3
- HATSU_DOMAIN=hatsu.example.com
- HATSU_LISTEN_HOST=0.0.0.0
- HATSU_PRIMARY_ACCOUNT=blog.example.com
volumes:
# - ./.env:/app/.env
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ async fn main() -> Result<(), AppError> {
// 环境变量
// Environments
let env = AppEnv {
database_url: env::var("DATABASE_URL").unwrap_or_else(|_| String::from("sqlite::memory:")),
hatsu_access_token: env::var_os("HATSU_ACCESS_TOKEN").map(|env| env.into_string().unwrap()),
hatsu_database_url: env::var("HATSU_DATABASE_URL")
.unwrap_or_else(|_| String::from("sqlite::memory:")),
hatsu_domain: env::var("HATSU_DOMAIN").expect("env HATSU_DOMAIN must be set"),
hatsu_listen_host: env::var("HATSU_LISTEN_HOST")
.unwrap_or_else(|_| String::from("127.0.0.1")),
Expand All @@ -44,7 +45,7 @@ async fn main() -> Result<(), AppError> {

// 连接数据库
// Connecting to database
let conn = Database::connect(&env.database_url)
let conn = Database::connect(&env.hatsu_database_url)
.await
.expect("Database connection failed");

Expand Down

0 comments on commit 5137295

Please sign in to comment.