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

Allow hosting under a subpath #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
44 changes: 32 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub struct BinArgs {
/// maximum paste size in bytes (default. 32kB)
#[argh(option, default = "32 * 1024")]
max_paste_size: usize,
/// web path prefix (default "/")
#[argh(option, default = "\"/\".to_string()")]
path_prefix: String,
}

#[actix_web::main]
Expand All @@ -55,30 +58,46 @@ async fn main() -> std::io::Result<()> {

let store = Data::new(PasteStore::default());

// if path_prefix arg doesn't end with "/", append it
let path_prefix = if args.path_prefix.ends_with('/') {
if args.path_prefix.starts_with('/') {
args.path_prefix.clone()
} else {
format!("/{}", args.path_prefix.clone())
}
} else if args.path_prefix.starts_with('/') {
args.path_prefix.clone()
} else {
format!("/{}/", args.path_prefix.clone())
};

let server = HttpServer::new({
let args = args.clone();
let path_prefix = path_prefix.clone();
let path_prefix_data = Data::new(path_prefix.clone());

move || {
App::new()
.app_data(store.clone())
.app_data(path_prefix_data.clone())
.app_data(PayloadConfig::default().limit(args.max_paste_size))
.app_data(FormConfig::default().limit(args.max_paste_size))
.wrap(actix_web::middleware::Compress::default())
.route("/", web::get().to(index))
.route("/", web::post().to(submit))
.route("/", web::put().to(submit_raw))
.route("/", web::head().to(HttpResponse::MethodNotAllowed))
.route("/highlight.css", web::get().to(highlight_css))
.route("/{paste}", web::get().to(show_paste))
.route("/{paste}", web::head().to(HttpResponse::MethodNotAllowed))
.route(&path_prefix, web::get().to(index))
.route(&path_prefix, web::post().to(submit))
.route(&path_prefix, web::put().to(submit_raw))
.route(&path_prefix, web::head().to(HttpResponse::MethodNotAllowed))
.route(&(path_prefix.clone() + "highlight.css"), web::get().to(highlight_css))
.route(&(path_prefix.clone() + "{paste}"), web::get().to(show_paste))
.route(&(path_prefix.clone() + "{paste}"), web::head().to(HttpResponse::MethodNotAllowed))
.default_service(web::to(|req: HttpRequest| async move {
error!("Couldn't find resource {}", req.uri());
HttpResponse::from_error(NotFound)
}))
}
});

info!("Listening on http://{}", args.bind_addr);
info!("Listening on http://{}{}", args.bind_addr, path_prefix);

server.bind(args.bind_addr)?.run().await
}
Expand All @@ -96,9 +115,9 @@ struct IndexForm {
val: Bytes,
}

async fn submit(input: web::Form<IndexForm>, store: Data<PasteStore>) -> impl Responder {
async fn submit(input: web::Form<IndexForm>, store: Data<PasteStore>, path_prefix: Data<String>) -> impl Responder {
let id = generate_id();
let uri = format!("/{id}");
let uri = format!("{}{id}", *path_prefix);
store_paste(&store, id, input.into_inner().val);
HttpResponse::Found()
.append_header((header::LOCATION, uri))
Expand All @@ -109,12 +128,13 @@ async fn submit_raw(
data: Bytes,
host: HostHeader,
store: Data<PasteStore>,
path_prefix: Data<String>
) -> Result<String, Error> {
let id = generate_id();
let uri = if let Some(Ok(host)) = host.0.as_ref().map(|v| std::str::from_utf8(v.as_bytes())) {
format!("https://{host}/{id}\n")
format!("https://{host}{}{id}\n", *path_prefix)
} else {
format!("/{id}\n")
format!("{}{id}\n", *path_prefix)
};

store_paste(&store, id, data);
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{% endblock styles %}

{% block content %}
<form action="/" method="post">
<form action="" method="post">
<textarea name="val" placeholder="bin something" autofocus autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"></textarea>

<button type="submit" title="&#x2318;+&#x23ce;">&#x270e;</button>
Expand Down