Skip to content

Commit

Permalink
chore: fix some path issues, allow longer id's
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
  • Loading branch information
explodingcamera committed Aug 27, 2024
1 parent 86ba783 commit d49e3ce
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 53 deletions.
30 changes: 5 additions & 25 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ base_url="http://localhost:9042"
port=9042

# # Folder to store the database in (Will be created if it doesn't exist)
# # defaults to ./.local/share/liwan/data (or $XDG_DATA_HOME/liwan/data) on linux/macos
# # defaults to $HOME/.local/share/liwan/data on linux/macos
# # defaults to ./liwan-data on other platforms
# data_dir="./liwan-data"

Expand Down
2 changes: 1 addition & 1 deletion data/licenses-npm.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/app/core/geoip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ impl LiwanGeoIP {
return Ok(None);
};

if geoip.maxmind_account_id.is_none() && geoip.maxmind_license_key.is_none() && geoip.maxmind_db_path.is_none()
{
tracing::trace!("GeoIP support disabled, skipping...");
return Ok(None);
}

let edition = geoip.maxmind_edition.as_deref().unwrap_or("GeoLite2-City");
let default_path = PathBuf::from(config.data_dir.clone()).join(format!("./geoip/{}.mmdb", edition));
let path = geoip.maxmind_db_path.as_ref().map(PathBuf::from).unwrap_or(default_path);
Expand Down
5 changes: 4 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ pub struct AddUser {

pub fn handle_command(mut config: Config, cmd: Command) -> Result<()> {
config.geoip = None; // disable GeoIP support in CLI
let app = Liwan::try_new(config)?;

match cmd {
Command::UpdatePassword(update) => {
let app = Liwan::try_new(config)?;
app.users.update_password(&update.username, &update.password)?;
println!("Password updated for user {}", update.username);
}
Command::Users(_) => {
let app = Liwan::try_new(config)?;
let users = app.users.all()?;
if users.is_empty() {
println!("{}", "No users found".bold());
Expand All @@ -109,6 +110,7 @@ pub fn handle_command(mut config: Config, cmd: Command) -> Result<()> {
}
}
Command::AddUser(add) => {
let app = Liwan::try_new(config)?;
app.users.create(
&add.username,
&add.password,
Expand All @@ -130,6 +132,7 @@ pub fn handle_command(mut config: Config, cmd: Command) -> Result<()> {
}
#[cfg(debug_assertions)]
Command::SeedDatabase(_) => {
let app = Liwan::try_new(config)?;
app.seed_database()?;
println!("Database seeded with test data");
}
Expand Down
35 changes: 17 additions & 18 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ fn default_port() -> u16 {
fn default_data_dir() -> String {
#[cfg(target_family = "unix")]
{
if std::path::Path::new("~/.local/share").exists() {
return "~/.local/share/liwan/data".to_string();
}
let home = std::env::var("HOME").ok().unwrap_or_else(|| "/root".to_string());
std::env::var("XDG_DATA_HOME")
.map(|home| format!("{}/liwan/data", home))
.unwrap_or_else(|_| "./liwan-data".to_string())
.map(|data_home| format!("{data_home}/liwan/data"))
.unwrap_or_else(|_| format!("{home}/.local/share/liwan/data"))
}

#[cfg(not(target_family = "unix"))]
Expand Down Expand Up @@ -64,21 +62,22 @@ impl Config {

let path = path.or_else(|| std::env::var("LIWAN_CONFIG").ok());

let mut config = Figment::new()
.merge(Toml::file("liwan.config.toml"))
.merge(Toml::file(path.unwrap_or("liwan.config.toml".to_string())));

#[cfg(target_family = "unix")]
let path = path.or_else(|| match std::env::var("XDG_CONFIG_HOME") {
Ok(home) => Some(format!("{}/liwan/config.toml", home)),
Err(_) => {
if std::path::Path::new("~/.config").exists() {
Some("~/.config/liwan/config.toml".to_string())
} else {
None
}
}
});
{
let home = std::env::var("HOME").unwrap_or_else(|_| "/root".to_string());
let config_dir = std::env::var("XDG_CONFIG_HOME").unwrap_or_else(|_| format!("{home}/.config"));

config = config
.join(Toml::file(format!("{config_dir}/liwan/config.toml")))
.join(Toml::file(format!("{config_dir}/liwan/liwan.config.toml")))
.join(Toml::file(format!("{config_dir}/liwan.config.toml")))
}

let config: Config = Figment::new()
.merge(Toml::file("liwan.config.toml"))
.merge(Toml::file(path.unwrap_or("liwan.config.toml".to_string())))
let config: Config = config
.merge(Env::raw().filter_map(|key| match key {
k if !k.starts_with("LIWAN_") => None,
k if k.starts_with("LIWAN_MAXMIND_") => Some(format!("geoip.maxmind_{}", &k[14..]).into()),
Expand Down
6 changes: 4 additions & 2 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use colored::Colorize;
use crossbeam::channel::Sender;
use eyre::{Context, Result};
use rust_embed::RustEmbed;
use std::path::Path;

use poem::endpoint::EmbeddedFileEndpoint;
use poem::listener::TcpListener;
Expand All @@ -29,7 +28,10 @@ struct Files;
#[folder = "./tracker"]
struct Script;

#[cfg(debug_assertions)]
fn save_spec() -> Result<()> {
use std::path::Path;

let path = Path::new("./web/src/api/dashboard.ts");
if path.exists() {
let spec = serde_json::to_string(&serde_json::from_str::<serde_json::Value>(&dashboard_service().spec())?)?
Expand Down Expand Up @@ -88,7 +90,7 @@ pub async fn start_webserver(app: Liwan, events: Sender<Event>) -> Result<()> {
let listener = TcpListener::bind(("0.0.0.0", app.config.port));

if let Some(onboarding) = app.onboarding.token()? {
let get_started = format!("http://localhost:{}/setup?t={}", app.config.port, onboarding).underline().bold();
let get_started = format!("{}/setup?t={}", app.config.base_url, onboarding).underline().bold();
let command = "liwan --help".bold();
tracing::info!("{}", "It looks like you're running Liwan for the first time!".white());
tracing::info!("{}", format!("You can get started by visiting: {get_started}").white());
Expand Down
Binary file modified web/bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-tabs": "^1.1.0",
"@scaleway/use-query-params": "^5.0.5",
"@tanstack/react-query": "^5.52.1",
"@tanstack/react-query": "^5.52.2",
"@uidotdev/usehooks": "^2.4.1",
"date-fns": "^3.6.0",
"fets": "^0.8.3",
Expand Down
6 changes: 3 additions & 3 deletions web/src/components/settings/dialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export const CreateProject = () => {
<form onSubmit={handleSubmit}>
<label>
Project ID <small>(This cannot be changed later)</small>
<input required pattern="^[A-Za-z0-9_\-]{1,15}$" name="id" type="text" placeholder="my-project" />
<input required pattern="^[A-Za-z0-9_\-.]{1,40}$" name="id" type="text" placeholder="my-project" />
</label>
<label>
Project Name <small>(Used in the dashboard)</small>
Expand Down Expand Up @@ -462,7 +462,7 @@ export const CreateEntity = () => {
<form onSubmit={handleSubmit}>
<label>
Entity ID <small>(This cannot be changed later)</small>
<input required pattern="^[A-Za-z0-9_\-]{1,15}$" name="id" type="text" placeholder="my-website" />
<input required pattern="^[A-Za-z0-9_\-.]{1,40}$" name="id" type="text" placeholder="my-website" />
</label>
<label>
Entity Name <small>(Used in the dashboard)</small>
Expand Down Expand Up @@ -669,7 +669,7 @@ export const CreateUser = () => {
Username <small>(This cannot be changed later)</small>
<input
required
pattern="^[A-Za-z0-9_\-]{1,15}$"
pattern="^[A-Za-z0-9_\-]{1,20}$"
name="username"
type="text"
placeholder="MyUsername"
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/settings/tables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const EntityDropdown = ({ entity }: { entity: EntityResponse }) => {
onClick={() => {
navigator.clipboard
.writeText(
`<script type="module" data-entity=${entity.id} src="${window.location.origin}/script.js"></script>`,
`<script type="module" data-entity="${entity.id}" src="${window.location.origin}/script.js"></script>`,
)
.then(() => createToast("Snippet copied to clipboard", "info"))
.catch(() => createToast("Failed to copy snippet to clipboard", "error"));
Expand Down

0 comments on commit d49e3ce

Please sign in to comment.