Skip to content

Commit

Permalink
feat: add domain and storage settings in manage page
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitea committed Mar 17, 2024
1 parent f27d686 commit 45b511a
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 21 deletions.
36 changes: 18 additions & 18 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ default-members = ["land-cli"]
resolver = "2"

[workspace.package]
version = "0.4.0"
version = "0.2.0"
edition = "2021"
authors = ["fuxiaohei <fudong0797@gmail.com>"]

Expand Down
2 changes: 1 addition & 1 deletion deploy/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
proxy:
platform: linux/amd64
image: traefik:v3.0
command: --api.insecure=true --providers.docker=true --providers.docker.exposedByDefault=false --accesslog=true --providers.file.filename=/data/runtime-land/traefik.yaml
command: --api.insecure=true --providers.docker=true --providers.docker.exposedByDefault=false --accesslog=true --providers.file.filename=/data/runtime-land/traefik.yaml --providers.file.watch=true
restart: always
ports:
- "80:80"
Expand Down
2 changes: 2 additions & 0 deletions land-server/src/server/dash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub fn router(assets_dir: &str) -> Result<Router> {
.route("/settings", get(settings::index))
.route("/settings/create-token", post(settings::create_token))
.route("/settings/manage", get(settings::manage))
.route("/settings/update-domain", post(settings::update_domain))
.route("/settings/update-storage", post(settings::update_storage))
.nest_service("/static", ServeDir::new(static_assets_dir))
.layer(CsrfLayer::new(config))
.with_state(Engine::from(hbs))
Expand Down
58 changes: 58 additions & 0 deletions land-server/src/server/dash/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ pub async fn manage(
tokens: Vec<TokenVar>,
token_usage: String,
workers: Vec<WorkerVar>,
domain: String,
protocol: String,
storage: String,
}
let csrf_token = csrf.authenticity_token()?;
let token_values = user_token::list_by_user(user.id, Some(user_token::Usage::Worker)).await?;
Expand Down Expand Up @@ -144,6 +147,15 @@ pub async fn manage(
});
}

// domain, protocol
let (domain, protocol) = land_dao::settings::get_domain_settings().await?;
let storage_setting = land_dao::settings::get("storage").await?;
let storage_content = if let Some(m) = storage_setting {
m.value
} else {
"unknown".to_string()
};

Ok((
csrf,
RenderHtml(
Expand All @@ -156,8 +168,54 @@ pub async fn manage(
tokens,
token_usage: Usage::Worker.to_string(),
workers,
domain,
protocol,
storage: storage_content,
},
),
)
.into_response())
}

#[derive(Deserialize)]
pub struct UpdateDomainForm {
protocol: String,
domain: String,
csrf: String,
}

/// update_domain is a handler for POST /settings/update-domain
pub async fn update_domain(
csrf: CsrfToken,
Extension(user): Extension<SessionUser>,
Form(form): Form<UpdateDomainForm>,
) -> Result<impl IntoResponse, ServerError> {
if !user.is_admin {
return Err(ServerError::forbidden("Permission denied"));
}
csrf.verify(&form.csrf)?;
info!("Update domain settings: {},{}", form.protocol, form.domain);
land_dao::settings::set_domain_settings(form.domain, form.protocol).await?;
Ok(redirect_response("/settings/manage"))
}

#[derive(Deserialize)]
pub struct UpdateStorageForm {
storage: String,
csrf: String,
}

/// update_storage is a handler for POST /settings/update-storage
pub async fn update_storage(
csrf: CsrfToken,
Extension(user): Extension<SessionUser>,
Form(form): Form<UpdateStorageForm>,
) -> Result<impl IntoResponse, ServerError> {
if !user.is_admin {
return Err(ServerError::forbidden("Permission denied"));
}
csrf.verify(&form.csrf)?;
info!("Update storage: {}", form.storage);
land_dao::settings::set("storage", &form.storage).await?;
Ok(redirect_response("/settings/manage"))
}
3 changes: 3 additions & 0 deletions land-server/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ impl ServerError {
anyhow::anyhow!(msg.to_string()),
)
}
pub fn forbidden(msg: &str) -> Self {
Self(StatusCode::FORBIDDEN, anyhow::anyhow!(msg.to_string()))
}
}

// Tell axum how to convert `AppError` into a response.
Expand Down
45 changes: 45 additions & 0 deletions land-server/tpls/manage.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,57 @@
</div>
</div>
</div>
<div class="py-4">
<div class="card border-secondary-subtle">
<div class="card-header">
<h6 class="mb-1 mt-2">Domains</h6>
<p class="text-body-tertiary mb-0 small">Project domain suffix and protocol.</p>
</div>
<div class="card-body">
<form action="/settings/update-domain" method="post">
<div class="input-group mb-3">
<select class="form-select" id="protocol-select" data-x-value="{{protocol}}"
name="protocol">
<option value="http">http</option>
<option value="https">https</option>
</select>
<label class="input-group-text ps-2 pe-0">://</label>
<span class="input-group-text ps-0 pe-2">{project.domain}.</span>
<input required name="domain" type="text" class="form-control" value="{{domain}}" />
<input type="hidden" name="csrf" value="{{csrf_token}}">
</div>
<div class="input-group mb-1 text-end justify-content-end">
<button type="submit" class="btn btn-primary">Save Domains</button>
</div>
</form>
</div>
</div>
</div>
<div class="py-4">
<div class="card border-secondary-subtle">
<div class="card-header">
<h6 class="mb-1 mt-2">Storage</h6>
<p class="text-body-tertiary mb-0 small">Storage settings for WebAssembly modules</p>
</div>
<div class="card-body">
<form action="/settings/update-storage" method="post">
<textarea class="form-control mb-3" name="storage" rows="6">{{storage}}</textarea>
<input type="hidden" name="csrf" value="{{csrf_token}}">
<div class="input-group mb-1 text-end justify-content-end">
<button type="submit" class="btn btn-primary">Save Storage</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>

</div>
{{> partials/footer.hbs}}
<script type="text/javascript">
let s = document.getElementById("protocol-select");
s.value = s.getAttribute("data-x-value");
document.querySelectorAll(".token-value-copy-btn").forEach((btn) => {
btn.addEventListener("click", (e) => {
let uuid = btn.getAttribute("data-x-id");
Expand Down
2 changes: 1 addition & 1 deletion land-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ land-worker-server = { workspace = true }
once_cell = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_yaml = "0.9.32"
serde_yaml = "0.9.33"
tokio = { workspace = true }
tracing = { workspace = true }
url = { workspace = true }

0 comments on commit 45b511a

Please sign in to comment.