Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public Url for Paths #78

Merged
merged 4 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,64 @@ Displays your MicroBin's version information.

Changes the maximum width of the UI from 720 pixels to 1080 pixels.

### --public-path [PUBLIC_PATH]

Add the given public path prefix to all urls.

This allows you to host MicroBin behind a reverse proxy on a subpath.
Note that MicroBin itself still expects all routes to be as without this option, and thus is unsuited
if you are running MicroBin directly.

#### Example Usage (caddy)

An example of running MicroBin behind the reverse proxy `caddy` on the path `/paste` (using systemd)

**microbin.service**

```unit file (systemd)
[Unit]
Description=Micobin Paste

[Service]
Type=simple
User=microbin
# Path to your binary
ExecStart=/home/microbin/microbin/target/release/microbin
# Set your desired working directory, eg where microbin places its data
WorkingDirectory=/home/pi/bin/micro

# bind to localhost, as its exposed via Caddy
Environment=MICROBIN_BIND=127.0.0.1
# bind to a unused port
Environment=MICROBIN_PORT=31333
# All your other config (change and add as needed)
Environment=MICROBIN_EDITABLE=true
Environment=MICROBIN_HIGHLIGHTSYNTAX=true
Environment=MICROBIN_THREADS=2
Environment=MICROBIN_FOOTER_TEXT="Bin the bytes"
# Set your **public** url. Eg the one you will later use to access microbin
# Ensure it either starts with http(s):// or with a /
Environment=MICROBIN_PUBLIC_PATH="http://100.127.233.32/paste"

[Install]
WantedBy=multi-user.target
```

**Caddyfile**

```caddy
example.com {
# Your normal http root
root * /var/www/html
file_server

# Route all requests to past
handle_path /paste/* {
reverse_proxy http://127.0.0.1:31333
}
}
```
szabodanika marked this conversation as resolved.
Show resolved Hide resolved

### --no-file-upload

Disables and hides the file upload option in the UI.
Disables and hides the file upload option in the UI.
27 changes: 25 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::convert::Infallible;
use std::fmt;
use std::net::IpAddr;
use std::str::FromStr;
use clap::Parser;
use lazy_static::lazy_static;

Expand Down Expand Up @@ -48,6 +51,9 @@ pub struct Args {
#[clap(long, env="MICROBIN_PURE_HTML")]
pub pure_html: bool,

#[clap(long, env="MICROBIN_PUBLIC_PATH", default_value_t = PublicUrl(String::from("")))]
pub public_path: PublicUrl,

#[clap(long, env="MICROBIN_READONLY")]
pub readonly: bool,

Expand All @@ -59,7 +65,24 @@ pub struct Args {

#[clap(long, env="MICROBIN_WIDE")]
pub wide: bool,

#[clap(short, long, env="MICROBIN_NO_FILE_UPLOAD")]
pub no_file_upload: bool,
}
}

#[derive(Debug, Clone)]
pub struct PublicUrl(pub String);

impl fmt::Display for PublicUrl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl FromStr for PublicUrl {

Check failure

Code scanning / clippy

this file contains an unclosed delimiter

this file contains an unclosed delimiter
type Err = Infallible;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let uri = s.strip_suffix('/').unwrap_or(s).to_owned();
Ok(PublicUrl(uri))
}

Check failure

Code scanning / clippy

this file contains an unclosed delimiter

this file contains an unclosed delimiter
4 changes: 2 additions & 2 deletions src/endpoints/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub async fn create(
) -> Result<HttpResponse, Error> {
if ARGS.readonly {
return Ok(HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish());
}

Expand Down Expand Up @@ -158,6 +158,6 @@ pub async fn create(
save_to_file(&pastas);

Ok(HttpResponse::Found()
.append_header(("Location", format!("/pasta/{}", to_animal_names(id))))
.append_header(("Location", format!("{}/pasta/{}", ARGS.public_path, to_animal_names(id))))
.finish())
}
6 changes: 3 additions & 3 deletions src/endpoints/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn get_edit(data: web::Data<AppState>, id: web::Path<String>) -> HttpR
if pasta.id == id {
if !pasta.editable {
return HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish();
}
return HttpResponse::Ok().content_type("text/html").body(
Expand All @@ -55,7 +55,7 @@ pub async fn post_edit(
) -> Result<HttpResponse, Error> {
if ARGS.readonly {
return Ok(HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish());
}

Expand Down Expand Up @@ -85,7 +85,7 @@ pub async fn post_edit(
save_to_file(&pastas);

return Ok(HttpResponse::Found()
.append_header(("Location", format!("/pasta/{}", pastas[i].id_as_animals())))
.append_header(("Location", format!("{}/pasta/{}", ARGS.public_path, pastas[i].id_as_animals())))
.finish());
} else {
break;
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/pastalist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct PastaListTemplate<'a> {
pub async fn list(data: web::Data<AppState>) -> HttpResponse {
if ARGS.no_listing {
return HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish();
}

Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::fs;
pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
if ARGS.readonly {
return HttpResponse::Found()
.append_header(("Location", "/"))
.append_header(("Location", format!("{}/", ARGS.public_path)))
.finish();
}

Expand All @@ -39,7 +39,7 @@ pub async fn remove(data: web::Data<AppState>, id: web::Path<String>) -> HttpRes
// remove it from in-memory pasta list
pastas.remove(i);
return HttpResponse::Found()
.append_header(("Location", "/pastalist"))
.append_header(("Location", format!("{}/pastalist", ARGS.public_path)))
.finish();
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ <h2>404</h2>
<b>Not Found</b>
<br>
<br>
<a href="/" > Go Home</a>
<a href="{{ args.public_path }}/" > Go Home</a>
<br>
<br>
{% include "footer.html" %}
8 changes: 4 additions & 4 deletions templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/svg+xml" href="/static/favicon.svg">
{% if !args.pure_html %}
<link rel="stylesheet" href="/static/water.css">
<link rel="stylesheet" href="{{ args.public_path }}/static/water.css">
{%- endif %}
</head>
{% if args.wide %}
Expand Down Expand Up @@ -50,11 +50,11 @@
{%- endif %}
</b>

<a href="/" style="margin-right: 0.5rem; margin-left: 0.5rem">New Pasta</a>
<a href="{{ args.public_path }}/" style="margin-right: 0.5rem; margin-left: 0.5rem">New Pasta</a>

<a href="/pastalist" style="margin-right: 0.5rem; margin-left: 0.5rem">Pasta List</a>
<a href="{{ args.public_path }}/pastalist" style="margin-right: 0.5rem; margin-left: 0.5rem">Pasta List</a>

<a href="/help" style="margin-right: 0.5rem; margin-left: 0.5rem">Help</a>
<a href="{{ args.public_path }}/help" style="margin-right: 0.5rem; margin-left: 0.5rem">Help</a>

<hr>

Expand Down
10 changes: 5 additions & 5 deletions templates/pasta.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{% include "header.html" %}
<div style="float: left">
<a style="margin-right: 0.5rem" href="/raw/{{pasta.id_as_animals()}}">Raw Text Content</a>
<a style="margin-right: 0.5rem" href="{{ args.public_path }}/raw/{{pasta.id_as_animals()}}">Raw Text Content</a>
{% if pasta.file.is_some() %}
<a style="margin-right: 0.5rem; margin-left: 0.5rem"
href="/file/{{pasta.id_as_animals()}}/{{pasta.file.as_ref().unwrap().name()}}">
href="{{ args.public_path }}/file/{{pasta.id_as_animals()}}/{{pasta.file.as_ref().unwrap().name()}}">
Attached file'{{pasta.file.as_ref().unwrap().name()}}' [{{pasta.file.as_ref().unwrap().size}}]
</a>
{%- endif %}
{% if pasta.editable %}
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="/edit/{{pasta.id_as_animals()}}">Edit</a>
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="{{ args.public_path }}/edit/{{pasta.id_as_animals()}}">Edit</a>
{%- endif %}
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="/remove/{{pasta.id_as_animals()}}">Remove</a>
<a style="margin-right: 0.5rem; margin-left: 0.5rem" href="{{ args.public_path }}/remove/{{pasta.id_as_animals()}}">Remove</a>
</div>
<div style="float: right">
<a href="/pasta/{{pasta.id_as_animals()}}"><i>{{pasta.id_as_animals()}}</i></a>
<a href="{{ args.public_path }}/pasta/{{pasta.id_as_animals()}}"><i>{{pasta.id_as_animals()}}</i></a>
</div>
<br>
<div class="code-container">
Expand Down
20 changes: 10 additions & 10 deletions templates/pastalist.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% if pastas.is_empty() %}
<br>
<p>
No pastas yet. 😔 Create one <a href="/">here</a>.
No pastas yet. 😔 Create one <a href="{{ args.public_path }}/">here</a>.
</p>
<br>
{%- else %}
Expand Down Expand Up @@ -38,7 +38,7 @@
{% if pasta.pasta_type == "text" && !pasta.private %}
<tr>
<td>
<a href="/pasta/{{pasta.id_as_animals()}}">{{pasta.id_as_animals()}}</a>
<a href="{{ args.public_path }}/pasta/{{pasta.id_as_animals()}}">{{pasta.id_as_animals()}}</a>
</td>
<td>
{{pasta.created_as_string()}}
Expand All @@ -47,14 +47,14 @@
{{pasta.expiration_as_string()}}
</td>
<td>
<a style="margin-right:1rem" href="/raw/{{pasta.id_as_animals()}}">Raw</a>
<a style="margin-right:1rem" href="{{ args.public_path }}/raw/{{pasta.id_as_animals()}}">Raw</a>
{% if pasta.file.is_some() %}
<a style="margin-right:1rem" href="/file/{{pasta.id_as_animals()}}/{{pasta.file.as_ref().unwrap().name()}}">File</a>
<a style="margin-right:1rem" href="{{ args.public_path }}/file/{{pasta.id_as_animals()}}/{{pasta.file.as_ref().unwrap().name()}}">File</a>
{%- endif %}
{% if pasta.editable %}
<a style="margin-right:1rem" href="/edit/{{pasta.id_as_animals()}}">Edit</a>
<a style="margin-right:1rem" href="{{ args.public_path }}/edit/{{pasta.id_as_animals()}}">Edit</a>
{%- endif %}
<a href="/remove/{{pasta.id_as_animals()}}">Remove</a>
<a href="{{ args.public_path }}/remove/{{pasta.id_as_animals()}}">Remove</a>
</td>
</tr>
{%- endif %}
Expand Down Expand Up @@ -90,7 +90,7 @@
{% if pasta.pasta_type == "url" && !pasta.private %}
<tr>
<td>
<a href="/url/{{pasta.id_as_animals()}}">{{pasta.id_as_animals()}}</a>
<a href="{{ args.public_path }}/url/{{pasta.id_as_animals()}}">{{pasta.id_as_animals()}}</a>
</td>
<td>
{{pasta.created_as_string()}}
Expand All @@ -99,11 +99,11 @@
{{pasta.expiration_as_string()}}
</td>
<td>
<a style="margin-right:1rem" href="/raw/{{pasta.id_as_animals()}}">Raw</a>
<a style="margin-right:1rem" href="{{ args.public_path }}raw/{{pasta.id_as_animals()}}">Raw</a>
{% if pasta.editable %}
<a style="margin-right:1rem" href="/edit/{{pasta.id_as_animals()}}">Edit</a>
<a style="margin-right:1rem" href="{{ args.public_path }}/edit/{{pasta.id_as_animals()}}">Edit</a>
{%- endif %}
<a href="/remove/{{pasta.id_as_animals()}}">Remove</a>
<a href="{{ args.public_path }}/remove/{{pasta.id_as_animals()}}">Remove</a>
</td>
</tr>
{%- endif %}
Expand Down